Skip to content

Commit 73fa24b

Browse files
author
dingo35
committed
packfs.py: only compile packed_fs.c when files in data/ have changed
1 parent fdeffa3 commit 73fa24b

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

SmartEVSE-3/packfs.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,47 @@
55
if not os.path.isfile("data/update2.html"):
66
print("Missing file: data/update2.html")
77
sys.exit(1)
8+
89
if os.path.isdir("pack.tmp"):
910
shutil.rmtree('pack.tmp')
1011
try:
11-
filelist = []
12-
os.makedirs('pack.tmp/data')
13-
# now gzip the stuff except zones.csv since this file is not served by mongoose but directly accessed:
14-
for file in os.listdir("data"):
15-
filename = os.fsdecode(file)
16-
if filename == "cert.pem" or filename == "key.pem" or filename == "CH32V203.bin" or filename == "SmartEVSE.webp":
17-
shutil.copy('data/' + filename, 'pack.tmp/data/' + filename)
18-
filelist.append('data/' + filename)
19-
continue
20-
else:
21-
with open('data/' + filename, 'rb') as f_in, gzip.open('pack.tmp/data/' + filename + '.gz', 'wb') as f_out:
22-
f_out.writelines(f_in)
23-
filelist.append('data/' + filename + '.gz')
24-
continue
25-
os.chdir('pack.tmp')
26-
cmdstring = 'python ../pack.py ' + ' '.join(filelist)
27-
os.system(cmdstring + '>../src/packed_fs.c')
28-
os.chdir('..')
12+
# Skip generation if packed_fs.c is newer than everything in data/
13+
OUTPUT = "src/packed_fs.c"
14+
if os.path.isfile(OUTPUT):
15+
packed_mtime = os.path.getmtime(OUTPUT)
16+
latest = 0
17+
for root, _, files in os.walk("data"):
18+
for f in files:
19+
try:
20+
mtime = os.path.getmtime(os.path.join(root, f))
21+
if mtime > latest: latest = mtime
22+
except: pass
23+
if latest > packed_mtime:
24+
filelist = []
25+
os.makedirs('pack.tmp/data')
26+
# now gzip the stuff except zones.csv since this file is not served by mongoose but directly accessed:
27+
for file in os.listdir("data"):
28+
filename = os.fsdecode(file)
29+
if filename == "cert.pem" or filename == "key.pem" or filename == "CH32V203.bin" or filename == "SmartEVSE.webp":
30+
shutil.copy('data/' + filename, 'pack.tmp/data/' + filename)
31+
filelist.append('data/' + filename)
32+
continue
33+
else:
34+
with open('data/' + filename, 'rb') as f_in, gzip.open('pack.tmp/data/' + filename + '.gz', 'wb') as f_out:
35+
f_out.writelines(f_in)
36+
filelist.append('data/' + filename + '.gz')
37+
continue
38+
os.chdir('pack.tmp')
39+
cmdstring = 'python ../pack.py ' + ' '.join(filelist)
40+
os.system(cmdstring + '>../src/packed_fs.c')
41+
os.chdir('..')
2942
except Exception as e:
3043
print(f"An error occurred: {str(e)}")
3144
sys.exit(100)
32-
if shutil.rmtree("pack.tmp"):
33-
print("Failed to clean up temporary files")
34-
sys.exit(9)
45+
if os.path.isdir("pack.tmp"):
46+
if shutil.rmtree("pack.tmp"):
47+
print("Failed to clean up temporary files")
48+
sys.exit(9)
3549
# cleanup CH32 bin file if it was generated:
3650
if os.path.isfile("data/CH32V203.bin"):
3751
os.remove("data/CH32V203.bin")

0 commit comments

Comments
 (0)