aboutsummaryrefslogtreecommitdiffstats
path: root/generate_auto_job
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-01-25 16:03:01 +0100
committerJay Berkenbilt <ejb@ql.org>2022-01-30 19:11:03 +0100
commit965e473a01122291e9498bbec81af144bd33f075 (patch)
treee128dafa10911ae406ffbc54f9a6ef8485bb13f4 /generate_auto_job
parentf60526aff93ef1049242b215a9f247dca4c7f99e (diff)
downloadqpdf-965e473a01122291e9498bbec81af144bd33f075.tar.zst
generate_auto_job: don't replace files that are unchanged
Diffstat (limited to 'generate_auto_job')
-rwxr-xr-xgenerate_auto_job33
1 files changed, 23 insertions, 10 deletions
diff --git a/generate_auto_job b/generate_auto_job
index 731822c8..7a2b97b0 100755
--- a/generate_auto_job
+++ b/generate_auto_job
@@ -6,6 +6,8 @@ import hashlib
import re
import yaml
import json
+import filecmp
+from contextlib import contextmanager
whoami = os.path.basename(sys.argv[0])
BANNER = f'''//
@@ -19,6 +21,17 @@ def warn(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
+@contextmanager
+def write_file(filename):
+ tmpfile = filename + '.tmp'
+ with open(tmpfile, 'w') as f:
+ yield f
+ if os.path.exists(filename) and filecmp.cmp(filename, tmpfile, False):
+ os.unlink(tmpfile)
+ else:
+ os.rename(tmpfile, filename)
+
+
# QXXXQ
# These need manual handlers.
complex = set([
@@ -105,8 +118,10 @@ class Main:
return parser.parse_args(args)
def top(self, options):
- if options.check:
- self.check()
+ if self.check_hashes():
+ exit(0)
+ elif options.check:
+ exit(f'{whoami}: auto job inputs have changed')
elif options.generate:
self.generate()
else:
@@ -124,7 +139,7 @@ class Main:
pass
return hashes
- def check(self):
+ def check_hashes(self):
hashes = self.get_hashes()
match = False
try:
@@ -137,8 +152,7 @@ class Main:
match = old_hashes == hashes
except Exception:
pass
- if not match:
- exit(f'{whoami}: auto job inputs have changed')
+ return match
def update_hashes(self):
hashes = self.get_hashes()
@@ -279,7 +293,6 @@ class Main:
def generate(self):
warn(f'{whoami}: regenerating auto job files')
-
with open('job.yml', 'r') as f:
data = yaml.safe_load(f.read())
self.validate(data)
@@ -290,20 +303,20 @@ class Main:
)
self.options_without_help = set(self.help_options)
self.prepare(data)
- with open(self.DESTS['decl'], 'w') as f:
+ with write_file(self.DESTS['decl']) as f:
print(BANNER, file=f)
for i in self.decls:
print(i, file=f)
- with open(self.DESTS['init'], 'w') as f:
+ with write_file(self.DESTS['init']) as f:
print(BANNER, file=f)
for i in self.init:
print(i, file=f)
- with open(self.DESTS['help'], 'w') as f:
+ with write_file(self.DESTS['help']) as f:
with open('manual/cli.rst', 'r') as df:
print(BANNER, file=f)
self.generate_doc(df, f)
self.generate_schema(data)
- with open(self.DESTS['schema'], 'w') as f:
+ with write_file(self.DESTS['schema']) as f:
print('static constexpr char const* JOB_SCHEMA_DATA = R"(' +
json.dumps(self.schema, indent=2, separators=(',', ': ')) +
')";', file=f)