-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaunisync
executable file
·1039 lines (902 loc) · 40 KB
/
aunisync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# Automated Unison Profile Synchronizer
# Copyright (C) 2014 Andrew Chadwick <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
## Imports
import os
import sys
import os.path
from random import random
import weakref
import logging
import traceback
from functools import reduce
logger = logging.getLogger(__name__)
import signal
from gettext import gettext as _
import gi
gi.require_version("Notify", "0.7")
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gio
from gi.repository import Notify
## Constants
#: Program name for the about box.
PROGRAM_NAME = 'aUniSync'
#: Program version for the about box.
PROGRAM_VERSION = '0.2.2'
#: Command to use for batch-mode synchronization (suffixed with profile name)
UNISON_BATCH_CMD = ['unison', '-batch', '-silent', '-retry', '5']
#: Command to use for manual intervention mode (suffixed with profile name for
#: individual profiles, or not for the general configuration command.
UNISON_MANUAL_CMD = ['unison-gtk']
## Functions
def normalize_path(root):
"""Normalizes a local path to a canonical, absolute representation."""
root = os.path.normpath(root)
root = os.path.abspath(root)
return root
## Class definitions
class ProfileState:
"""Profile state codes."""
UNKNOWN = 0 #: Status not known
IDLE = 1 #: Profile is in sync, and has not changed
CHANGED = 2 #: Files have changed, profile is waiting for auto-sync
DUE = 3 #: Timer elapsed, sync is due
SYNCING = 4 #: Automatic batch sync is running
MANUAL = 5 #: Manual intervention is happening, auto-sync is paused
OFFLINE = 6 #: Network is offline, auto-sync is paused
PAUSED = 7 #: Profile is manually paused
ERROR = 8 #: Error was detected: auto-sync is stopped
class StatusIcon (Gtk.StatusIcon):
"""App status icon, with a basic menu.
The icon shows the highest state code of all profiles, i.e. the one which
is hopefully most relevant to the user. The menu shows per-profile states,
and allows Unison to be invoked for manual intervention unless manual
intervention is already happening.
"""
## Class constants
ICON_NAME = { ProfileState.UNKNOWN: 'aunisync-idle-symbolic',
ProfileState.IDLE: 'aunisync-idle-symbolic',
ProfileState.CHANGED: 'aunisync-changed-symbolic',
ProfileState.DUE: 'aunisync-changed-symbolic',
ProfileState.SYNCING: 'aunisync-active-symbolic',
ProfileState.MANUAL: 'aunisync-properties-symbolic',
ProfileState.OFFLINE: 'aunisync-offline-symbolic',
ProfileState.PAUSED: 'aunisync-paused-symbolic',
ProfileState.ERROR: 'aunisync-error-symbolic', }
STATUS_TEXT = { ProfileState.UNKNOWN: _('Unknown State'),
ProfileState.IDLE: _('Idle'),
ProfileState.CHANGED: _('Updates Detected'),
ProfileState.DUE: _('Synchronization Due'),
ProfileState.SYNCING: _('Synchronizing'),
ProfileState.MANUAL: _('Running Unison Manually'),
ProfileState.OFFLINE: _("Offline"),
ProfileState.PAUSED: _("Paused"),
ProfileState.ERROR: _('Errors Encountered'), }
ICON_SIZES = [16, 22, 24, 32, 48, 64]
def __init__(self, app):
"""Initialise, connected to the main app."""
Gtk.StatusIcon.__init__(self)
theme = Gtk.IconTheme.get_default()
logger.debug("icon theme search path: %r", theme.get_search_path())
self._app = app
self._menu = None
self.set_from_icon_name(self.ICON_NAME[ProfileState.UNKNOWN])
self.connect("popup-menu", self._popup_menu_cb)
self._profile_items = {}
self._update_menu()
self._size = None
self.connect("size-changed", self._size_changed_cb)
self.connect("activate", self._activate_cb)
def _update_menu(self):
"""Updates the menu. Called when profile states change."""
if self._menu is None:
self._menu = Gtk.Menu()
item = Gtk.SeparatorMenuItem()
self._menu.append(item)
item = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_PREFERENCES,None)
item.connect('activate', lambda *a: self._app.configure())
self._menu.append(item)
item = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_ABOUT, None)
item.connect('activate', lambda *a: self._app.about())
self._menu.append(item)
item = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT, None)
item.connect('activate', lambda *a: self._app.quit())
self._menu.append(item)
if self._app.profiles != set(self._profile_items.keys()):
for profile, item in self._profile_items.items():
self._menu.remove(item)
self._profile_items = {}
for profile in self._app.profiles:
item = Gtk.ImageMenuItem(str(profile))
item.connect('activate', self._profile_menu_item_activate_cb,
profile)
self._menu.insert(item, 0)
self._profile_items[profile] = item
for profile, item in self._profile_items.items():
state = profile.state
icon_name = self.ICON_NAME[state]
image = Gtk.Image()
image.set_from_icon_name(icon_name, Gtk.IconSize.MENU)
item.set_image(image)
if state in [ProfileState.SYNCING, ProfileState.MANUAL]:
item.set_sensitive(False)
else:
item.set_sensitive(True)
def _profile_menu_item_activate_cb(self, item, profile):
"""Menu command to launch a manual sync (Unison GUI) for a profile"""
profile.spawn_manual_sync()
def update_icon(self):
"""Update the icon to show the app's overall state"""
state = self._app.state
icon_name = self.ICON_NAME[state]
status_text = self.STATUS_TEXT[state]
# Symbolic icons fakery.
# https://mail.gnome.org/archives/gtk-list/2013-May/msg00016.html
# GtkStatusIcon may eventually do this itself.
loaded = False
screen = self.get_screen()
if icon_name.endswith("-symbolic") and self._size and screen:
# Not too happy with this size-setting fudge.
# Looks OK in Xfce4 when using a borderless tray
size = max(min(self.ICON_SIZES), self._size)
for sanesize in self.ICON_SIZES:
if sanesize > self._size:
break
size = sanesize
# Simulate the GtkImage path of GtkStatusIcon's internal one
fakepath = Gtk.WidgetPath()
fakepath.append_type(Gtk.Image)
fakestyle = Gtk.StyleContext()
fakestyle.set_path(fakepath)
# Can also add classes: "warning" and "error" work here
#fakestyle.add_class("warning")
theme = Gtk.IconTheme.get_for_screen(self.get_screen())
iconflags = ( Gtk.IconLookupFlags.FORCE_SVG |
Gtk.IconLookupFlags.FORCE_SIZE )
iconinfo = theme.lookup_icon(icon_name, size, iconflags)
pixbuf, was_symb = iconinfo.load_symbolic_for_context(fakestyle)
if was_symb:
self.set_from_pixbuf(pixbuf)
loaded = True
# FIXME: should cache these.
if not loaded:
self.set_from_icon_name(icon_name)
tooltip = _("Unison: %s") % (status_text,)
self.set_tooltip_text(tooltip)
self._update_menu()
def _size_changed_cb(self, statusicon, size):
screen = self.get_screen()
if not screen:
return
self._size = size
self.update_icon()
def _popup_menu_cb(self, status_icon, button, activate_time):
"""Menu showing, with the appropriate position function"""
self._menu.show_all()
self._menu.popup(parent_menu_shell=None, parent_menu_item=None,
func=Gtk.StatusIcon.position_menu, data=self,
button=button, activate_time=activate_time)
def _activate_cb(self, status_icon):
self._menu.show_all()
self._menu.popup(parent_menu_shell=None, parent_menu_item=None,
func=Gtk.StatusIcon.position_menu, data=self,
button=0, activate_time=Gdk.CURRENT_TIME)
class RecursiveDirMonitor (object):
"""Monitor directories via GIO (typically inotify on Linux)."""
## Class constants
_QUERY_FLAGS = Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS
_MONITOR_FLAGS = Gio.FileMonitorFlags.NONE
def __init__(self, profile):
"""Construct, ready to report back to its profile."""
super(RecursiveDirMonitor, self).__init__()
self._profile = weakref.proxy(profile)
self._monitors_by_uri = {}
def _add_monitor(self, directory, root, parent=None):
"""Adds monitoring for a subtree
Always returns False, so it's suitable for use as an idle callback:
this is used internally for building the tree of monitors for
subdirectories. To test for success, check `self._monitors_by_uri`
for the root directory's URI after calling.
"""
uri = directory.get_uri()
directory_type = directory.query_file_type(self._QUERY_FLAGS, None)
if directory_type != Gio.FileType.DIRECTORY:
logger.debug("Not watching %r: directories only", uri)
return False
try:
flags = self._MONITOR_FLAGS
mon = Gio.File.monitor_directory(directory, flags, None)
except GLib.GError as err:
logger.exception("Failed to monitor %r", uri)
mon = None
if not mon:
return False
mon.__root = root
mon.__uri = uri
# Monitor child directories too
try:
attrs = ",".join([Gio.FILE_ATTRIBUTE_STANDARD_NAME,
Gio.FILE_ATTRIBUTE_STANDARD_TYPE])
flags = self._QUERY_FLAGS
children = directory.enumerate_children(attrs, flags, None)
except GLib.GError as err:
logger.exception("Failed to add child monitors for %r", uri)
return False
mon.__parent = parent
if parent is not None:
if parent:
parent.__child_monitors.add(mon)
mon.__connid = mon.connect("changed", self._changed_cb)
mon.__child_monitors = set()
if uri in self._monitors_by_uri:
self._delete_monitor(self._monitors_by_uri[uri])
self._monitors_by_uri[uri] = mon
for child_info in children:
if child_info.get_file_type() == Gio.FileType.DIRECTORY:
child_name = child_info.get_name()
child_dir = directory.get_child(child_name)
GObject.idle_add(self._add_monitor, child_dir, root, mon)
logger.debug("Added monitor for %r", uri)
return False
def _delete_monitor(self, monitor):
"""Removes monitoring for a subtree."""
uri = monitor.__uri
logger.debug("Removing monitor for %r", uri)
self._monitors_by_uri.pop(uri, None)
if monitor.__parent:
if monitor in monitor.__parent.__child_monitors:
monitor.__parent.__child_monitors.remove(monitor)
monitor.__parent = None
if monitor.__connid:
monitor.disconnect(monitor.__connid)
monitor.__connid = None
monitor.cancel()
for childmon in list(monitor.__child_monitors):
self._delete_monitor(childmon)
monitor.__child_monitors = set()
def _changed_cb(self, monitor, file1, file2, event):
"""Handles changes: adds or removes monitoring, & notifies the app."""
root = monitor.__root
root_uri = Gio.File.new_for_path(root).get_uri()
if root_uri not in self._monitors_by_uri:
logger.debug("Not reporting change in %r: was removed", root)
return
logger.debug("Changes detected in %r (%r)", root, event)
self._profile.monitored_path_changed(root)
if event == Gio.FileMonitorEvent.CREATED:
file1_type = file1.query_file_type(self._QUERY_FLAGS, None)
if file1_type == Gio.FileType.DIRECTORY:
self._add_monitor(file1, root, monitor)
elif event == Gio.FileMonitorEvent.DELETED:
uri = file1.get_uri()
deadmon = self._monitors_by_uri.get(uri, None)
if deadmon:
self._delete_monitor(deadmon)
def add_path(self, root):
"""Recursively adds a dirctory to the monitor.
All subdirectories of the path will be monitored for changes, which are
reported back to the main app via its ``monitored_path_changed()``
method.
"""
if not os.path.exists(root):
return False
directory = Gio.File.new_for_path(root)
self._add_monitor(directory, root)
uri = directory.get_uri()
if self._monitors_by_uri.get(uri, None):
return True
return False
def remove_path(self, root):
"""Removes a directory and cancels all monitoring on it."""
uri = Gio.File.new_for_path(root).get_uri()
root_mon = self._monitors_by_uri.get(uri, None)
if root_mon:
self._delete_monitor(root_mon)
class Profile (object):
"""Unison profile and state."""
PROFILE_DIR = ".unison"
PROFILE_SUFFIX = ".prf"
SETTLE_INTERVAL = 5
## Construction
@classmethod
def load_profiles(cls, app):
"""Returns a set of all valid user `Profile`s, freshly read from disk
Only profiles with at least one local root are considered valid.
"""
profiles = set()
profile_dir = os.path.join(os.environ["HOME"], cls.PROFILE_DIR)
for ent in os.listdir(profile_dir):
if not ent.endswith(cls.PROFILE_SUFFIX):
continue
profile = Profile(app, os.path.join(profile_dir, ent))
nroots = len(profile._roots)
if nroots < 1:
logger.warning("%s: must have at least 1 local root: ignoring",
profile)
elif nroots > 2:
logger.warning("%s: more than 2 local roots: ignoring",
profile)
else:
profiles.add(profile)
return profiles
def __init__(self, app, filename):
"""Initialize from a profile filename."""
super(Profile, self).__init__()
self._app = weakref.proxy(app)
# Defaults
assert filename.endswith(self.PROFILE_SUFFIX)
filename = normalize_path(filename)
name = os.path.basename(filename[:-len(self.PROFILE_SUFFIX)])
self._name = name #: Canonical name of the profile
self._label = str(self._name) #: Human-readable label
self._roots = set() #: Local roots
self._paths = set() #: Paths within roots, for monitoring
self._mountpoints = set() #: Check-mounted points
# Cached values
self.__monitored_paths = set()
self.__mountpoint_paths = set()
# Filesystem monitoring
self._monitor = RecursiveDirMonitor(self)
# State and synchronization management
self._state = ProfileState.UNKNOWN
self._settle_timeout = None
self.remove_on_exit = False
self.pause_on_exit = False
# Load config
loaded_configs = set()
fp = open(filename, 'r')
lines = fp.readlines()
fp.close()
loaded_configs.add(filename)
while len(lines) > 0:
line = lines.pop(0)
line = line.strip()
# Skip comments and blank lines
if line.startswith("#") or line == "":
continue
# Process directives
if line.split()[0].lower() == "include":
try:
inc_directive, inc_name = line.split(None, 1)
except ValueError:
logger.error("%r: failed to process include directive %r",
filename, line)
continue
loaded_inc_file = False
potential_inc_files = [inc_name, inc_name+self.PROFILE_SUFFIX]
for inc_file in potential_inc_files:
prf_dir = os.path.join(os.environ["HOME"], self.PROFILE_DIR)
inc_file = os.path.join(prf_dir, inc_file)
inc_file = normalize_path(inc_file)
if inc_file in loaded_configs:
logger.warning("%r: skipping circular include of %r",
filename, inc_file)
elif os.path.isfile(inc_file):
logger.debug("including %r", inc_file)
inc_fp = open(inc_file, "r")
lines = inc_fp.readlines() + lines
inc_fp.close()
loaded_configs.add(inc_file)
loaded_inc_file = True
break
if not loaded_inc_file:
logger.error("%r: include file %r not found",
filename, inc_name)
continue
# Process known key=value directives
try:
key, value = line.split('=', 1)
except ValueError:
continue
key = key.lower().strip()
value = value.strip()
if key == "root":
if not ("://" in value or value.startswith("//")):
localroot = normalize_path(value)
self._roots.add(localroot)
elif key == "path":
self._paths.add(value)
elif key == "mountpoint":
self._mountpoints.add(value)
elif key == "label":
self._label = str(value)
# Error notifications
self._notification = Notify.Notification.new("", "", "unison")
self._notification_shown = False
self._notification.add_action("open-gui", "Open GUI",
self._notification_action_cb, None)
self._notification.add_action("retry", "Retry",
self._notification_action_cb, None)
self._notification.set_timeout(Notify.EXPIRES_NEVER)
## Public read-only properties
@property
def name(self):
return self._name
@property
def monitored_paths(self):
"""Local paths to be monitored for this profile."""
if not self.__monitored_paths:
monpaths = set()
if self._paths:
for r in self._roots:
for p in self._paths:
p = normalize_path(os.path.join(r, p))
monpaths.add(p)
else:
for r in self._roots:
r = normalize_path(r)
monpaths.add(r)
self.__monitored_paths = monpaths
return self.__monitored_paths.copy()
## Standard overrides
def __repr__(self):
return "<Profile %r>" % (self.name,)
def __str__(self):
"""Display name for the profile"""
result = str(self._label)
if self.remove_on_exit:
result = "%s (obsolete)" % (result,)
return result
def __eq__(self, other):
"""Profiles are equal if their names and dirs are the same."""
# State is deliberately ignored
return other.name == self.name \
and other._roots == self._roots \
and other._paths == self._paths \
and other._mountpoints == self._mountpoints
def __hash__(self):
n = hash(self.name)
f = lambda a, b: a^b
n = reduce(f, [hash(a) for a in sorted(self._roots)], n)
n = reduce(f, [hash(a) for a in sorted(self._paths)], n)
n = reduce(f, [hash(a) for a in sorted(self._mountpoints)], n)
return n
## State flag
@property
def state(self):
"""The profile's current state; modification updates the UI."""
return self._state
@state.setter
def state(self, state):
"""Set the profile's current state, and notify if it changed."""
oldstate = self._state
self._state = state
if state != oldstate:
self._app.state_changed()
self._update_notification()
## Filesystem monitoring
# It's a little inefficient to have one RecursiveDirMonitor per profile
# instead of one big one in the app itself, but it avoids having to
# maintain refcounts when profiles overlap. Locally overlapping profiles
# should be possible (and scheduled one at a time via a queue)
def start_monitoring(self):
"""Starts filesystem monitoring for this profile"""
for path in self.monitored_paths:
self._monitor.add_path(path)
def stop_monitoring(self):
"""Stops filesystem monitoring for this profile"""
for path in self.monitored_paths:
self._monitor.remove_path(path)
def monitored_path_changed(self, path):
"""Starts the profile settle timer."""
self.restart_settle_timeout()
## Post-change settle timer
def restart_settle_timeout(self, timeout=None):
"""Starts (or restarts) the post-change settle timer for a profile.
The post-change settle timeout callback synchronizes the profile, and
updates its state depending on whether the batch-mode sync failed or
succeeded. Calling this method repeatedly when files change on the
local end has the effect of waiting for bursts of changes to settle and
(heuristically) be less likely to happen during the subsequent sync.
"""
if timeout is None:
timeout = self.SETTLE_INTERVAL
logger.debug("%s: restarting settle timer %ds", self, timeout)
self.stop_settle_timeout()
restart_states = [ProfileState.UNKNOWN, ProfileState.IDLE,
ProfileState.CHANGED]
if self.state not in restart_states:
logger.debug("%s: not idle: ignoring", self)
return
self.state = ProfileState.CHANGED
cb = self._settle_time_elapsed
cb_id = GLib.timeout_add_seconds(timeout, cb)
self._settle_timeout = cb_id
def stop_settle_timeout(self):
"""Stops the post-change settle timer for a profile."""
logger.debug("%s: stopping settle timer", self)
if self._settle_timeout:
GLib.source_remove(self._settle_timeout)
self._settle_timeout = None
if self.state in [ProfileState.CHANGED, ProfileState.DUE]:
self.state = ProfileState.IDLE
def _settle_time_elapsed(self):
"""Timeout callback: sync a single profile."""
logger.debug("%s: settle timer finished", self)
assert self.state == ProfileState.CHANGED
# Slight duplication (command spawning does this too), but need to
# avoid update tennis when there are overlaps.
self.stop_settle_timeout()
self.stop_monitoring()
# Try to defer after already-running overlapping profiles
defer_after_states = [ProfileState.DUE, ProfileState.SYNCING]
for profile in self._app.profiles:
if profile is self:
continue
if self.overlaps(profile):
logger.debug("%s (%r) overlaps %s (%r)", self, self.state,
profile, profile.state)
if profile.state in defer_after_states:
logger.debug("%s: deferring till after %s", self, profile)
self.state = ProfileState.DUE
return
# This profile is the first, or has unique monitored paths, so
# it can be processed now.
self.state = ProfileState.DUE
GObject.idle_add(self._spawn_batch_sync) # for some reason, idle needed
def overlaps(self, other):
disjoint = self.monitored_paths.isdisjoint(other.monitored_paths)
return not disjoint
## Command spawning (via the central app)
def _spawn_batch_sync(self):
"""Spawns a batch-mode sync."""
nogo = [ProfileState.SYNCING, ProfileState.MANUAL]
if self.state in nogo:
logger.warning("%s: already syncing: batch sync not possible",
self)
return
cmd = UNISON_BATCH_CMD + [self.name]
self.spawn_command(cmd, ProfileState.SYNCING)
def spawn_manual_sync(self):
"""Spawns a manual synchronization (launches the GUI)"""
logger.info("%s: manual sync requested", self)
nogo = [ProfileState.SYNCING, ProfileState.MANUAL]
if self.state in nogo:
logger.warning("%s: already syncing: manual sync not possible",
self)
return
cmd = UNISON_MANUAL_CMD + [self.name]
self.spawn_command(cmd, ProfileState.MANUAL)
def spawn_command(self, cmd, runstate):
"""Run a per-profile command."""
self.stop_settle_timeout()
self.stop_monitoring()
GObject.idle_add(self._spawn_command_idle_cb, cmd, runstate)
def _spawn_command_idle_cb(self, cmd, runstate):
if self._app.spawn_command(cmd, self._command_finished_cb):
self.state = runstate
else:
self.state = ProfileState.ERROR
return False
def _command_finished_cb(self, success, *_ignored):
"""Per-profile command finished callback."""
if self.remove_on_exit:
logger.info("%s: removing at command exit", self)
self._app.profiles.remove(self)
self.state = ProfileState.UNKNOWN
elif self.pause_on_exit:
logger.info("%s: pausing at command exit", self)
self.state = ProfileState.PAUSED
elif not success:
# Failure will already have been logged fully
self.state = ProfileState.ERROR
else:
GObject.idle_add(self.start_monitoring)
self.state = ProfileState.IDLE
# Try and run just one remaining due sync
for profile in self._app.profiles:
if profile is self:
continue
if profile.state == ProfileState.DUE:
logger.info("%s: was deferred: now running (after %s)",
profile, self)
profile._spawn_batch_sync()
break
## Error notifications
def _update_notification(self):
"""Updates the status notification for this Profile
This only shows when in the ERROR state currently.
"""
icon = StatusIcon.ICON_NAME[self._state]
summary = _("Synchronization Error")
body = (_("Unison profile \u201C%s\u201D failed to synchronize.") %
(self._label,))
self._notification.update(summary, body, icon)
try:
if self._state >= ProfileState.ERROR:
self._notification.show()
self._notification_shown = True
elif self._notification_shown:
self._notification.close()
self._notification_shown = False
except GLib.GError as err:
logger.exception("Notification update failed for %r", self)
# Typically random DBus failures
def _notification_action_cb(self, notification, action, data):
"""Handles notification popup action buttons"""
if action == "open-gui":
self.spawn_manual_sync()
elif action == "retry":
self._spawn_batch_sync()
class App (Gtk.Application):
"""Main application instance."""
REMOTE_CHECK_INTERVAL = 300
IDENTIFIER = "org.piffle.aunisync"
## Initialization and startup
def __init__(self):
"""Initialize, in an unconfigured state."""
super().__init__(
application_id=self.IDENTIFIER,
)
self._orig_excepthook = None
self.profiles = set()
self._status_icon = None
self._override_state = None
self._net_monitor = None
self._net_available = False
self._remote_check_timeout_id = None
self.connect("startup", self._startup_cb)
self.connect("activate", self._activate_cb)
def run(self, *args, **kwargs):
"""Enter the application (all instances)
This override reestablishes normal Ctrl-C behaviour, i.e.
unceremoniously killing the process.
"""
signal.signal(signal.SIGINT, signal.SIG_DFL)
os.chdir(GLib.get_home_dir())
return super().run(*args, **kwargs)
def _startup_cb(self, app):
"""Application startup (primary instance only)"""
# Init objects to be used in the primary instance
self._status_icon = StatusIcon(self)
self._net_monitor = Gio.NetworkMonitor.get_default()
self._net_monitor.connect("network-changed", self._network_changed)
# Load config
self.reload_config()
self._net_available = False
def _activate_cb(self, app):
available = self._net_monitor.get_property("network-available")
self._network_changed(self._net_monitor, available)
self.restart_remote_checking()
Notify.init(PROGRAM_NAME)
self.hold()
logger.info("Activated")
# While in the GTK main loop, handle exceptions ourself
if not self._orig_excepthook:
self._orig_excepthook = sys.excepthook
sys.excepthook = self._handle_exception
def _handle_exception(self, exctyp, value, tb):
"""Handles exceptions by ending the process without shutdown"""
logger.critical("Caught exception: logging traceback...")
blocks = traceback.format_exception(exctyp, value, tb, limit=None)
for block in blocks:
block = block.rstrip("\n")
for line in block.split("\n"):
logger.error(line)
# Terminate any GTK main loops
logger.debug("Terminating GTK main loop(s)")
while Gtk.main_level():
Gtk.main_quit()
# Invoke the original exception hook, whatever it was
logger.debug("Invoking original signal handler")
self._orig_excepthook(exctyp, value, tb)
sys.exit("Exception caught, bailing out!")
## Overall state (summarises all the profiles' states)
@property
def state(self):
"""Overall application state."""
if self._override_state:
return self._override_state
state = ProfileState.UNKNOWN
for profile in self.profiles:
if profile.state > state:
state = profile.state
return state
def state_changed(self):
"""Called when the state of a profile changes."""
self._status_icon.update_icon()
## Remote checking timer
def restart_remote_checking(self):
"""Starts (or restarts) the periodic remote-checking timer.
The remote-checking timeout callback causes a synchronization of all
idle profiles to catch updates on the remote end.
"""
self.stop_remote_checking()
cb = self._remote_checking_due
interval = self.REMOTE_CHECK_INTERVAL
cb_id = GLib.timeout_add_seconds(interval, cb)
self._remote_check_timeout_id = cb_id
def stop_remote_checking(self):
"""Stops the periodic remote-checking timer."""
if not self._remote_check_timeout_id:
return
GLib.source_remove(self._remote_check_timeout_id)
self._remote_check_timeout_id = None
def _remote_checking_due(self):
"""Timeout callback: sync idle profiles."""
if not self._remote_check_timeout_id:
return False
idle_states = [ProfileState.UNKNOWN, ProfileState.IDLE]
self._queue_profiles_in_states(idle_states)
return True
## Utility methods
def _queue_profiles_in_states(self, states):
"""Starts the post-change settle timer for profiles in given states"""
for profile in self.profiles:
if profile.state in states:
profile.state = ProfileState.CHANGED # for the look
t = Profile.SETTLE_INTERVAL
t += int(random() * 3 * t)
profile.restart_settle_timeout(t)
# Clear the pause flag too.
profile.pause_on_exit = False
def _pause_all(self):
"""Pauses all inactive profiles, and stops remote checking.
While paused, active profiles will be paused automatically when their
command terminates.
"""
active_states = [ProfileState.SYNCING, ProfileState.MANUAL]
for profile in self.profiles:
if profile.state in active_states:
profile.pause_on_exit = True
else:
profile.stop_monitoring()
profile.stop_settle_timeout()
profile.state = ProfileState.PAUSED
self.stop_remote_checking()
def _unpause_all(self):
"""Unpauses all paused profiles, and restart remote checking."""
self._queue_profiles_in_states([ProfileState.PAUSED])
self.restart_remote_checking()
## Network monitoring
def _network_changed(self, net_monitor, available):
if self._net_available and (not available):
logger.info("Network no longer available")
nostop_states = [ProfileState.SYNCING, ProfileState.MANUAL]
for profile in self.profiles:
if profile.state not in nostop_states:
profile.state = ProfileState.OFFLINE
profile.stop_settle_timeout()
profile.stop_monitoring()
self._net_available = False
elif (not self._net_available) and available:
logger.info("Network now available")
# Start idle and offline profiles, and try to clear errors
start_states = [ProfileState.UNKNOWN, ProfileState.IDLE,
ProfileState.OFFLINE, ProfileState.ERROR]
self._queue_profiles_in_states(start_states)
self._net_available = True
## Menu commands
def configure(self):
"""Stop all synchronising, and run the configuration GUI."""
logger.info("launching configuration GUI")
self._spawn_override_command(UNISON_MANUAL_CMD)
def reload_config(self):
"""Reloads Unison's config, updating the list of profiles."""
logger.info("(re)loading Unison profiles")
old_profiles = self.profiles
self.profiles = Profile.load_profiles(self)
# Keep unique old profiles that are still synchronizing or in manual
# mode. Mark them for removal when whatever's happening has been
# resolved.
old_profiles -= self.profiles
active_states = [ProfileState.CHANGED, ProfileState.SYNCING,
ProfileState.MANUAL]
for profile in old_profiles:
if profile.state in active_states:
logger.info("%s: profile still active: marking for removal",
profile)
profile.remove_on_exit = True
self.profiles.add(profile)
starting_states = [ProfileState.UNKNOWN]
self._queue_profiles_in_states(starting_states)
self.state_changed()
def about(self):
"""Show the about dialog"""
dia = Gtk.AboutDialog()
dia.set_program_name(PROGRAM_NAME)
dia.set_license_type(Gtk.License.GPL_3_0)
license_txt = _("""
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
""")
dia.set_license(license_txt)
dia.set_wrap_license(False)
dia.set_version(PROGRAM_VERSION)
dia.set_copyright(_("Copyright © 2014 Andrew Chadwick "
"<[email protected]>"))
dia.set_comments(_("Automatically synchronizes files and folders in\n"
"your Unison profiles after you modify them."))
dia.set_logo_icon_name("unison-gtk")
dia.connect("response", lambda *a: dia.destroy())
dia.run()
## Override commands
def _spawn_override_command(self, cmd, state=ProfileState.MANUAL):
"""Spawns an override command.
Running an override command pauses any idle, unknown, or changed
profiles, and arranges for profiles running a command to pause when
ther command finish. Override commands are intended for editing the
entirety of Unison's configuration.
"""
self._pause_all()
GObject.idle_add(self._override_command_idle_cb, cmd, state=state)
def _override_command_idle_cb(self, cmd, state=ProfileState.MANUAL):
"""Internal idle callback: run the override command"""
if self.spawn_command(cmd, self._override_command_finished_cb):
self._override_state = state
self.state_changed()
else:
GObject.idle_add(self._unpause_all)
def _override_command_finished_cb(self, success, data):
"""Override command finished callback."""
self._override_state = None
self.reload_config()
self._unpause_all()
self.state_changed()
## General command spawning
def spawn_command(self, cmd, finish_cb, finish_data=None):
"""Spawn a command, and arrange for a callback to be run when it exits.
When the command in the sequence `cmd` exits, the callable `finish_cb`
is called as ``finish_cb(SUCCESS, finish_data)`` where SUCCESS is a