-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupdate.py
executable file
·87 lines (64 loc) · 2.41 KB
/
update.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import json
import os
import re
import subprocess
import bikeshed
def main():
updateAndCommit()
def updateAndCommit():
#print(datetime.datetime.now(datetime.UTC))
print(subprocess.check_output("git pull", shell=True).decode(encoding="utf-8"))
scriptPath = os.path.dirname(os.path.realpath(__file__))
dataPath = os.path.join(scriptPath, "data")
try:
with open(os.path.join(dataPath, "manifest.txt"), "r", encoding="utf-8") as fh:
oldManifest = bikeshed.update.Manifest.fromString(fh.read())
except:
raise
updateDataFiles(path=dataPath)
try:
with open(os.path.join(dataPath, "manifest.txt"), "r", encoding="utf-8") as fh:
newManifest = bikeshed.update.Manifest.fromString(fh.read())
except:
raise
diffData = diffManifests(oldManifest, newManifest)
if diffData["total"] == 0:
print(f"Manifest is unchanged since {oldManifest.dt}, nothing to be committed")
return
os.chdir(scriptPath)
print(subprocess.check_output("git add data", shell=True))
command = f"git commit -m 'updated {diffData['total']} files: "
commandBits = []
for category in ["added", "removed", "changed"]:
if diffData[category]:
commandBits.append(category + " " + ", ".join(diffData[category]))
command += "; ".join(commandBits)
command += "'"
print(subprocess.check_output(command, shell=True))
print(subprocess.check_output("git push", shell=True))
def updateDataFiles(path):
bikeshed.constants.quiet = 0
mode = bikeshed.update.UpdateMode.MANUAL | bikeshed.update.UpdateMode.FORCE
with bikeshed.messages.messagesSilent() as _:
pass
bikeshed.update.update(path=path, updateMode=mode)
def diffManifests(old, new):
oldFiles = set(old.entries.items())
newFiles = set(new.entries.items())
oldPaths = set(old.entries.keys())
newPaths = set(new.entries.keys())
removedPaths = oldPaths - newPaths
addedPaths = newPaths - oldPaths
persistingPaths = oldPaths & newPaths
changedPaths = set(file[0] for file in oldFiles ^ newFiles) & persistingPaths
return {
"added": sorted(addedPaths),
"removed": sorted(removedPaths),
"changed": sorted(changedPaths),
"total": len(addedPaths) + len(removedPaths) + len(changedPaths),
}
if __name__ == "__main__":
main()