forked from vcmi/vcmi-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcmiutil.py
65 lines (58 loc) · 1.91 KB
/
vcmiutil.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
import os
import shutil
class ReplacementEntry:
def __init__(self, src, dst):
self.src = src
self.dst = dst
def rewriteFile(path, content):
replacementFile = open(path + "__tmp", "w")
replacementFile.write(content)
replacementFile.close()
os.replace(path + "__tmp", path)
def entryMatches(entry, line, fullMatch):
if fullMatch:
return entry == line
else:
return str.find(line, entry) == 0 # basically line.startsWith(entry)
def fixFile(path, replacements, fullMatch=True):
try:
inputFile = open(path, "r")
outputContent = ""
replacedCount = 0
for line in inputFile:
replaced = False
for rep in replacements:
if entryMatches(rep.src.strip(), line.strip(), fullMatch):
outputContent += rep.dst + "\n"
replacedCount = replacedCount + 1
replaced = True
break
if replaced == False:
outputContent += line
inputFile.close()
rewriteFile(path, outputContent)
if replacedCount > 0:
print("Fixed " + path + " (replaced " + str(replacedCount) + " lines)")
else:
print("Didn't fix anything in " + path + " (already fixed?)")
except FileNotFoundError:
print("File " + path + " not found; skipping")
def flatCopyWithExt(srcDir, dstDir, ext):
if not os.path.exists(dstDir):
os.makedirs(dstDir)
for basename in os.listdir(srcDir):
if basename.endswith(ext):
pathname = os.path.join(srcDir, basename)
if os.path.isfile(pathname):
shutil.copy2(pathname, dstDir)
def copytree(src, dst, symlinks=False, ignore=None):
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
shutil.copy2(s, d)