89
89
# | Handling paths |
90
90
# |-----------------|
91
91
92
+
92
93
def sharePath ():
93
94
"""Get path where Back In Time is installed.
94
95
@@ -111,6 +112,7 @@ def sharePath():
111
112
112
113
return '/usr/share'
113
114
115
+
114
116
def backintimePath (* path ):
115
117
"""
116
118
Get path inside 'backintime' install folder.
@@ -349,9 +351,11 @@ def registerBackintimePath(*path):
349
351
would need this to actually import :py:mod:`tools`.
350
352
"""
351
353
path = backintimePath (* path )
352
- if not path in sys .path :
354
+
355
+ if path not in sys .path :
353
356
sys .path .insert (0 , path )
354
357
358
+
355
359
def runningFromSource ():
356
360
"""
357
361
Check if BackInTime is running from source (without installing).
@@ -361,14 +365,16 @@ def runningFromSource():
361
365
"""
362
366
return os .path .isfile (backintimePath ('common' , 'backintime' ))
363
367
368
+
364
369
def addSourceToPathEnviron ():
365
370
"""
366
371
Add 'backintime/common' path to 'PATH' environ variable.
367
372
"""
368
373
source = backintimePath ('common' )
369
374
path = os .getenv ('PATH' )
370
375
if path and source not in path .split (':' ):
371
- os .environ ['PATH' ] = '%s:%s' % (source , path )
376
+ os .environ ['PATH' ] = '%s:%s' % (source , path )
377
+
372
378
373
379
def get_git_repository_info (path = None , hash_length = None ):
374
380
"""Return the current branch and last commit hash.
@@ -456,6 +462,7 @@ def readFile(path, default=None):
456
462
457
463
return ret_val
458
464
465
+
459
466
def readFileLines (path , default = None ):
460
467
"""
461
468
Read the file in ``path`` or its '.gz' compressed variant and return its
@@ -484,6 +491,7 @@ def readFileLines(path, default = None):
484
491
485
492
return ret_val
486
493
494
+
487
495
def checkCommand (cmd ):
488
496
"""
489
497
Check if command ``cmd`` is a file in 'PATH' environ.
@@ -501,8 +509,10 @@ def checkCommand(cmd):
501
509
502
510
if os .path .isfile (cmd ):
503
511
return True
512
+
504
513
return not which (cmd ) is None
505
514
515
+
506
516
def which (cmd ):
507
517
"""
508
518
Get the fullpath of executable command ``cmd``. Works like
@@ -518,14 +528,19 @@ def which(cmd):
518
528
pathenv = os .getenv ('PATH' , '' )
519
529
path = pathenv .split (":" )
520
530
common = backintimePath ('common' )
531
+
521
532
if runningFromSource () and common not in path :
522
533
path .insert (0 , common )
534
+
523
535
for directory in path :
524
536
fullpath = os .path .join (directory , cmd )
537
+
525
538
if os .path .isfile (fullpath ) and os .access (fullpath , os .X_OK ):
526
539
return fullpath
540
+
527
541
return None
528
542
543
+
529
544
def makeDirs (path ):
530
545
"""
531
546
Create directories ``path`` recursive and return success.
@@ -542,15 +557,19 @@ def makeDirs(path):
542
557
543
558
if os .path .isdir (path ):
544
559
return True
560
+
545
561
else :
562
+
546
563
try :
547
564
os .makedirs (path )
548
565
except Exception as e :
549
566
logger .error ("Failed to make dirs '%s': %s"
550
- % (path , str (e )), traceDepth = 1 )
567
+ % (path , str (e )), traceDepth = 1 )
568
+
551
569
return os .path .isdir (path )
552
570
553
- def mkdir (path , mode = 0o755 , enforce_permissions = True ):
571
+
572
+ def mkdir (path , mode = 0o755 , enforce_permissions = True ):
554
573
"""
555
574
Create directory ``path``.
556
575
@@ -567,14 +586,18 @@ def mkdir(path, mode = 0o755, enforce_permissions = True):
567
586
os .chmod (path , mode )
568
587
except :
569
588
return False
589
+
570
590
return True
591
+
571
592
else :
572
593
os .mkdir (path , mode )
594
+
573
595
if mode & 0o002 == 0o002 :
574
- #make file world (other) writable was requested
575
- #debian and ubuntu won't set o+w with os.mkdir
576
- #this will fix it
596
+ # make file world (other) writable was requested
597
+ # debian and ubuntu won't set o+w with os.mkdir
598
+ # this will fix it
577
599
os .chmod (path , mode )
600
+
578
601
return os .path .isdir (path )
579
602
580
603
@@ -1105,6 +1128,7 @@ def checkCronPattern(s):
1105
1128
except ValueError :
1106
1129
return False
1107
1130
1131
+
1108
1132
#TODO: check if this is still necessary
1109
1133
def checkHomeEncrypt ():
1110
1134
"""
@@ -1131,6 +1155,7 @@ def checkHomeEncrypt():
1131
1155
return True
1132
1156
return False
1133
1157
1158
+
1134
1159
def envLoad (f ):
1135
1160
"""
1136
1161
Load environ variables from file ``f`` into current environ.
@@ -1150,6 +1175,7 @@ def envLoad(f):
1150
1175
os .environ [key ] = value
1151
1176
del (env_file )
1152
1177
1178
+
1153
1179
def envSave (f ):
1154
1180
"""
1155
1181
Save environ variables to file that are needed by cron
@@ -1169,6 +1195,7 @@ def envSave(f):
1169
1195
1170
1196
env_file .save (f )
1171
1197
1198
+
1172
1199
def keyringSupported ():
1173
1200
"""
1174
1201
Checks if a keyring (supported by BiT) is available
@@ -1275,12 +1302,14 @@ def password(*args):
1275
1302
return keyring .get_password (* args )
1276
1303
return None
1277
1304
1305
+
1278
1306
def setPassword (* args ):
1279
1307
1280
1308
if is_keyring_available :
1281
1309
return keyring .set_password (* args )
1282
1310
return False
1283
1311
1312
+
1284
1313
def mountpoint (path ):
1285
1314
"""
1286
1315
Get the mountpoint of ``path``. If your HOME is on a separate partition
@@ -1318,6 +1347,7 @@ def repl(m):
1318
1347
return chr (int (m .group (1 ), 8 ))
1319
1348
return re .sub (r'\\(\d{3})' , repl , s )
1320
1349
1350
+
1321
1351
def mountArgs (path ):
1322
1352
"""
1323
1353
Get all /etc/mtab args for the filesystem of ``path`` as a list.
@@ -1544,16 +1574,6 @@ def filesystemMountInfo():
1544
1574
if uuidFromDev (items [0 ]) != None }
1545
1575
1546
1576
1547
- def syncfs ():
1548
- """
1549
- Sync any data buffered in memory to disk.
1550
-
1551
- Returns:
1552
- bool: ``True`` if successful
1553
- """
1554
- if checkCommand ('sync' ):
1555
- return (Execute (['sync' ]).run () == 0 )
1556
-
1557
1577
def isRoot ():
1558
1578
"""
1559
1579
Check if we are root.
@@ -1887,6 +1907,7 @@ def fdDup(old, new_fd, mode = 'w'):
1887
1907
except OSError as e :
1888
1908
logger .debug ('Failed to redirect {}: {}' .format (old , str (e )))
1889
1909
1910
+
1890
1911
class UniquenessSet :
1891
1912
"""
1892
1913
Check for uniqueness or equality of files.
@@ -2003,6 +2024,7 @@ def checkEqual(self, path):
2003
2024
else :
2004
2025
return self .reference == (st .st_size , int (st .st_mtime ))
2005
2026
2027
+
2006
2028
class Alarm (object ):
2007
2029
"""
2008
2030
Establish a callback function that is called after a timeout.
@@ -2078,6 +2100,7 @@ def handler(self, signum, frame):
2078
2100
else :
2079
2101
self .callback ()
2080
2102
2103
+
2081
2104
class ShutDown (object ):
2082
2105
"""
2083
2106
Shutdown the system after the current snapshot has finished.
@@ -2237,7 +2260,6 @@ def shutdown(self):
2237
2260
return (False )
2238
2261
2239
2262
if self .is_root :
2240
- syncfs ()
2241
2263
self .started = True
2242
2264
proc = subprocess .Popen (['shutdown' , '-h' , 'now' ])
2243
2265
proc .communicate ()
@@ -2247,7 +2269,6 @@ def shutdown(self):
2247
2269
return (False )
2248
2270
2249
2271
else :
2250
- syncfs ()
2251
2272
self .started = True
2252
2273
2253
2274
return (self .proxy (* self .args ))
@@ -2343,6 +2364,7 @@ def clean(self):
2343
2364
return
2344
2365
self .iface .clean ()
2345
2366
2367
+
2346
2368
class PathHistory (object ):
2347
2369
def __init__ (self , path ):
2348
2370
self .history = [path ,]
@@ -2377,6 +2399,7 @@ def reset(self, path):
2377
2399
self .history = [path ,]
2378
2400
self .index = 0
2379
2401
2402
+
2380
2403
class OrderedSet (MutableSet ):
2381
2404
"""
2382
2405
OrderedSet from Python recipe
@@ -2438,6 +2461,7 @@ def __eq__(self, other):
2438
2461
return len (self ) == len (other ) and list (self ) == list (other )
2439
2462
return set (self ) == set (other )
2440
2463
2464
+
2441
2465
class Execute (object ):
2442
2466
"""
2443
2467
Execute external commands and handle its output.
@@ -2643,6 +2667,7 @@ def kill(self, signum, frame):
2643
2667
logger .info ('Kill process "%s"' % self .printable_cmd , self .parent , 2 )
2644
2668
return self .currentProc .kill ()
2645
2669
2670
+
2646
2671
class Daemon :
2647
2672
"""
2648
2673
A generic daemon class.
0 commit comments