-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilemanager.py
More file actions
139 lines (117 loc) · 4.56 KB
/
filemanager.py
File metadata and controls
139 lines (117 loc) · 4.56 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import glob
import os
import sys
import time
import threading
from easygui import msgbox
from format import Format
def fixPath(rootSearchPath:str):
for cur in rootSearchPath:
if cur == '\\':
cur='/'
def fileNameFromPath(path:str):
path = path[::-1]
ret = ""
for curChar in path:
if curChar == '\\':
break
else:
ret+=curChar
return ret[::-1]
def folderNameFromPath(path:str):
trunacate = '\\'+fileNameFromPath(path)
path = path.replace(trunacate,'')
return fileNameFromPath(path)
def makeAbsWindowsPath(path:str):
cwd = os.getcwd()
return cwd + "\\" + path.replace('/','\\')
class FileManager:
watchPeriodSeconds = 0.1
backupCount = 30
asmPathList = []
rootSearchPath = []
asmModifiedTime = []
def __init__(self,rootSearchPath):
self.rootSearchPath = rootSearchPath
self.getPaths()
self.asmModifiedTime = self.getModifiedTimeList()
os.makedirs('./MCS-Format Backup', exist_ok=True)
self.cout()
self.backupFiles()
self.formatFiles()
firstRunFlag =1
while 1:
newTimes = self.getModifiedTimeList()
if firstRunFlag == 1:
self.asmModifiedTime = newTimes
firstRunFlag = 0
self.getPaths()
#if mismatch detected (e.g user adds/deletes file) format all
if len(newTimes) != len(self.asmModifiedTime):
self.cout()
self.formatFiles()
else:
for i,(oldTime,newTime) in enumerate(zip(self.asmModifiedTime,newTimes)):
if(oldTime!=newTime):
self.backupFile(self.asmPathList[i],self.generateBackupPath(self.asmPathList[i]))
Format(self.asmPathList[i])
self.asmModifiedTime = self.getModifiedTimeList()
time.sleep(self.watchPeriodSeconds)
def cout(self):
msg = f"MCS-Format is watching \"{self.rootSearchPath}\"\nand it's subfolders for changes to .asm files.\n{len(self.asmPathList)} Files found."
print(msg)
print(f"Backups will be saved to ./MCS-Format Backup/**/*.asm")
def getPaths(self):
fixPath(self.rootSearchPath)
self.asmPathList = glob.glob(self.rootSearchPath + '/**/*.asm', recursive=True)
def getModifiedTimeList(self):
ret = []
for path in self.asmPathList:
try:
ret.append(os.stat(path).st_mtime)
except:
ret.append(0)
print(f"WARN: cannot update file time for {path}")
return ret
def formatFiles(self):
self.backupFiles()
for path in self.asmPathList:
Format(path)
def generateBackupPath(self,path):
backupTime = time.time()
folderName = folderNameFromPath(path)
fileName = fileNameFromPath(path)
os.makedirs(f'./MCS-Format Backup/{folderName}/{fileName.replace(".asm","")}',exist_ok=True)
bkupFileName = fileName.replace('.asm',f'{backupTime}.asm')
bkupFileName = f'MCS-Format Backup/{folderName}/{fileName.replace("asm","")}/{bkupFileName}'
bkupFileName = makeAbsWindowsPath(bkupFileName)
return bkupFileName
def generateBackupPaths(self):
backupTime = time.time()
backupPathList = []
for path in self.asmPathList:
bkupFileName = self.generateBackupPath(path)
backupPathList.append(bkupFileName)
return backupPathList
def backupFile(self,asmFilePath,backUpPath):
command = f'copy "{asmFilePath}" "{backUpPath}">nul'
os.system(command)
backUpPath = backUpPath[::-1]
for i,curChar in enumerate(backUpPath):
if curChar != '\\':
backUpPath = backUpPath[1:]
else:
backUpPath = backUpPath[1:]
backUpPath = backUpPath[::-1]
break
backUpFilesList = glob.glob(backUpPath+"/*.asm")
if len(backUpFilesList) > self.backupCount:
backUpFilesList.sort()
stopIndex = len(backUpFilesList) - self.backupCount
backUpFilesRemoveList = backUpFilesList[:stopIndex]
for path in backUpFilesRemoveList:
os.remove(path)
def backupFiles(self):
backupPathList = self.generateBackupPaths()
for path,backUpPath in zip(self.asmPathList,backupPathList):
self.backupFile(path,backUpPath)