Sun, 28 Jan 2018 11:38:01 +0000
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
0 | 1 | #!/usr/local/bin/python3 |
2 | ||
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
3 | # Common modules |
41
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
4 | import os |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
43
diff
changeset
|
5 | import sys |
41
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
6 | import argparse |
43
8f3ac19f11b6
Use platform package to detect whether to:
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
7 | import platform |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
8 | import branding |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
9 | # Own modules needed at this stage |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
10 | import locations |
49 | 11 | |
12 | # | |
41
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
13 | # Argument processing |
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
14 | # |
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
15 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
16 | parser=argparse.ArgumentParser( |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
17 | description=branding.appname_stylised + ": BorgBackup scheduler, queue, and tray icon.", |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
18 | epilog='Configuration file location:\n\n %s\n ' % locations.cfgfile, |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
19 | formatter_class=argparse.RawDescriptionHelpFormatter) |
41
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
20 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
21 | parser.add_argument( |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
22 | '--no-tray', |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
23 | dest='notray', |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
24 | action='store_true', |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
25 | help='Do not show the tray icon') |
41
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
26 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
27 | parser.add_argument( |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
28 | '--debug', |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
29 | dest='debug', |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
30 | action='store_true', |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
31 | help='Set logging level to debug') |
73
4f0e9cf8f230
Added --debug switch to enable debug logging level
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
32 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
33 | args=parser.parse_args() |
41
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
34 | |
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
35 | # |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
36 | # Done parsing args, import our own modules, and launch everything |
41
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
37 | # |
e2641cb9ca6d
--no-tray command line option for running on non-MacOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
38 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
39 | import branding |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
40 | import config |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
41 | import dreamtime |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
42 | import loggers |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
43 | import logging |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
44 | from scheduler import Scheduler |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
45 | from repository import Repository |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
46 | from backup import Backup |
31 | 47 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
48 | logger=loggers.mainlogger |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
49 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
50 | if args.debug: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
51 | logger.setLevel(logging.DEBUG) |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
52 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
53 | tray = None |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
54 | repos=[] |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
55 | backups=[] |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
56 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
57 | try: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
58 | dreamtime.start_monitoring() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
59 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
60 | scheduler = Scheduler() |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
61 | scheduler.start() |
40
cfbeeec8cb82
Improved exception reporting etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
62 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
63 | repoconfigs=config.settings['repositories'] |
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
73
diff
changeset
|
64 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
65 | for i in range(len(repoconfigs)): |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
66 | logger.info('Setting up repository %d' % i) |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
67 | r=Repository(i, repoconfigs[i]) |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
68 | repos.append(r) |
74
4f56142e7497
Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents:
73
diff
changeset
|
69 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
70 | backupconfigs=config.settings['backups'] |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
71 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
72 | for i in range(len(backupconfigs)): |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
73 | logger.info('Setting up backup set %d' % i) |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
74 | b=Backup(i, backupconfigs[i], scheduler) |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
75 | backups.append(b) |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
76 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
77 | for r in repos: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
78 | r.start() |
50 | 79 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
80 | for b in backups: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
81 | b.start() |
50 | 82 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
83 | if args.notray or platform.system()!='Darwin': |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
84 | # Wait for scheduler to finish |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
85 | scheduler.join() |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
86 | else: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
87 | # Start UI, and let it handle exit control |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
88 | from ui import BorgendTray |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
89 | tray=BorgendTray(backups); |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
90 | tray.run() |
9
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
91 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
92 | except Exception as err: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
93 | # TODO: Should write errors here to stderr; |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
94 | # perhaps add an extra stderr logger for error level messages |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
95 | logger.exception("Exception fell through: exiting") |
9
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
96 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
97 | finally: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
98 | for b in backups: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
99 | b.terminate() |
9
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
100 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
101 | for r in repos: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
102 | r.terminate() |
9
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
103 | |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
104 | if tray: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
105 | tray.quit() |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
106 | else: |
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
107 | logging.shutdown() |
9
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
108 |