-
Notifications
You must be signed in to change notification settings - Fork 0
/
revert.py
executable file
·71 lines (60 loc) · 2.09 KB
/
revert.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
#!/usr/bin/env python3
import shutil
from datetime import datetime
from ac_assign import (
ASSIGNED_ACS_FILE,
AVAILABLE_ACS_FILE,
BACKUP_DIR,
WORKING_PATH,
get_backup_file_counters,
get_backup_files,
get_filename_with_counter,
)
def get_number_lines(path):
with open(path) as f:
return len([line for line in f.readlines() if line])
def revert(version, working_path):
backup_path = working_path / BACKUP_DIR
for file in [
AVAILABLE_ACS_FILE,
ASSIGNED_ACS_FILE,
]:
src = backup_path / get_filename_with_counter(file, version)
dest = working_path / file
print(f"Replacing {dest} with {src}")
shutil.copy2(src, dest)
print(f"Finished reverting to version {version}. Backup files left untouched.")
def list_backups_and_ask_for_version(working_path):
backup_path = working_path / BACKUP_DIR
print(f"Listing versions in {backup_path} from newest to oldest:")
if not backup_path.exists():
print("Backup path does not exist")
return
files = get_backup_files(backup_path)
counters = sorted(get_backup_file_counters(files), reverse=True)
for counter in counters:
print(f"Version {counter}")
for filename in [AVAILABLE_ACS_FILE, ASSIGNED_ACS_FILE]:
filename_with_counter = get_filename_with_counter(filename, counter)
path = backup_path / filename_with_counter
stat = path.stat()
date = datetime.fromtimestamp(stat.st_mtime)
print(
" --",
filename_with_counter,
"\t|\tmodified",
date.strftime("%Y-%m-%d %H:%M:%S"),
"\t|\t",
get_number_lines(path),
"lines",
)
revert_version = int(input("Enter version number to revert to: "))
if revert_version not in counters:
print("Selected version not available.")
else:
revert(revert_version, working_path)
def main():
print(WORKING_PATH)
list_backups_and_ask_for_version(WORKING_PATH)
if __name__ == "__main__":
main()