forked from robertknight/mandrawer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup-file-timestamps.py
executable file
·100 lines (90 loc) · 3.34 KB
/
backup-file-timestamps.py
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
#!/usr/bin/python
# Utility script for saving and restore the modification times for all files in a tree
from __future__ import print_function
#from timeit import default_timer as timer
import argparse
import json
import os
import sys
import time
file_attrs = {}
def collect_file_attrs(path):
dirs = []
scandir_it = os.scandir(path)
for entry in scandir_it:
file_attrs[entry.path] = {'mtime' : entry.stat().st_mtime}
if entry.is_dir():
dirs.append(entry.path)
for subdir in dirs:
collect_file_attrs(subdir)
# def collect_file_attrsOSWALK(path):
# dirs = os.walk(path)
# file_attrs = {}
# for (dirpath, dirnames, filenames) in dirs:
# files = dirnames + filenames
# for file in files:
# path = os.path.join(dirpath, file)
# file_attrs[path] = {
# 'mtime' : os.path.getmtime(path)
# }
# return file_attrs
def apply_file_attrs(attrs):
for path in sorted(attrs):
attr = attrs[path]
if os.path.lexists(path):
mtime = attr['mtime']
mtime_changed = os.path.getmtime(path) != mtime
if mtime_changed:
print('Updating mtime for %s' % path, file=sys.stderr)
os.utime(path, (mtime, mtime))
else:
print('Skipping non-existent file %s' % path, file=sys.stderr)
def dir_path(string):
if os.path.isdir(string):
return string
else:
print("error: - invalid path to folder provided")
sys.exit(1)
def main():
ATTR_FILE_NAME = '.saved-file-timestamps'
parser = argparse.ArgumentParser('Save / Restore timestamps for all files in a directory tree \nexamples:\n-save \"C:\\myfolder\"\n-restore \"C:\\myfolder\"')
parser.add_argument('path', nargs='?', type=dir_path, help='Path to the directory (uses current by default)')
parser.add_argument('-save', type=dir_path, help='Save the timestamps of files in the directory tree (used by default)')
parser.add_argument('-restore', type=dir_path, help='Restore saved file timestamps in the directory tree')
args = parser.parse_args()
if args.restore:
filepath = os.path.join(args.restore, ATTR_FILE_NAME)
if 'raw_input' in vars(__builtins__):
inp = raw_input
else:
inp = input
if not os.path.exists(filepath):
print('Timestamps file \'%s\' not found' % filepath, file=sys.stderr)
sys.exit(1)
if inp("- Are you sure you want to restore timestamps to previously saved ones? (Y/N)").lower() == 'y':
attr_file = open(filepath, 'r')
attrs = json.load(attr_file)
apply_file_attrs(attrs)
else:
print("- Aborted")
sys.exit(1)
else:
if args.save:
path = args.save
elif args.path:
path = args.path
else:
path = os.path.abspath(os.getcwd())
filepath = os.path.join(path, ATTR_FILE_NAME)
print('Saving timestamps to: \'%s\'' % filepath)
attr_file = open(filepath, 'w')
#start = timer()
collect_file_attrs(path)
#end = timer()
#print(end - start)
json.dump(file_attrs, attr_file)
print('Save complete !')
time.sleep(3)
#input("Press Enter to continue...")
if __name__ == '__main__':
main()