-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaver.py
More file actions
40 lines (35 loc) · 1.12 KB
/
Copy pathsaver.py
File metadata and controls
40 lines (35 loc) · 1.12 KB
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
import os
import shutil
import sys
from datetime import datetime
class Saver:
def __init__(self, backup_dir="Saver_Branches"):
self.backup_dir = backup_dir
os.makedirs(self.backup_dir, exist_ok=True)
def backup(self, file_path):
if not os.path.isfile(file_path):
return
ts = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
name = os.path.basename(file_path)
dest = os.path.join(self.backup_dir, f"{name}_{ts}.saver.txt")
try:
shutil.copy2(file_path, dest)
print(f"Backed up {file_path} to {dest}")
except:
pass
def restore(self, backup_path, target_path="train.py"):
if not os.path.isfile(backup_path):
return
try:
shutil.copy2(backup_path, target_path)
print(f"Restored {backup_path} to {target_path}")
except:
pass
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit(1)
saver = Saver()
if sys.argv[1] == "--restore" and len(sys.argv) > 2:
saver.restore(sys.argv[2])
else:
saver.backup(sys.argv[1])