Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Do not write to the file unless there are changes.
  • Loading branch information
ericsnowcurrently committed Mar 22, 2022
commit 62e329dfa130d0af1fb2a0cfce483fad6df93475
40 changes: 29 additions & 11 deletions Tools/scripts/generate_global_objects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import contextlib
import glob
import io
import os.path
import re
import sys
Expand Down Expand Up @@ -123,6 +124,7 @@ def iter_global_strings():
varname, string = m.groups()
yield varname, string, filename, lno, line


def iter_to_marker(lines, marker):
for line in lines:
if line.rstrip() == marker:
Expand Down Expand Up @@ -165,6 +167,18 @@ def block(self, prefix, suffix="", *, continuation=None):
self.write("}" + suffix)


@contextlib.contextmanager
def open_for_changes(filename, orig):
outfile = io.StringIO()
yield outfile
text = outfile.getvalue()
if text != orig:
with open(filename, 'w', encoding='utf-8') as outfile:
outfile.write(text)
else:
print(f'# not changed: {filename}')


#######################################
# the global objects

Expand All @@ -177,13 +191,15 @@ def generate_global_strings(identifiers, strings):

# Read the non-generated part of the file.
with open(filename) as infile:
before = ''.join(iter_to_marker(infile, START))[:-1]
for _ in iter_to_marker(infile, END):
pass
after = infile.read()[:-1]
orig = infile.read()
lines = iter(orig.rstrip().splitlines())
before = '\n'.join(iter_to_marker(lines, START))
for _ in iter_to_marker(lines, END):
pass
after = '\n'.join(lines)

# Generate the file.
with open(filename, 'w', encoding='utf-8') as outfile:
with open_for_changes(filename, orig) as outfile:
printer = Printer(outfile)
printer.write(before)
printer.write(START)
Expand All @@ -202,7 +218,6 @@ def generate_global_strings(identifiers, strings):
with printer.block('struct', ' latin1[128];'):
printer.write("PyCompactUnicodeObject _latin1;")
printer.write("uint8_t _data[2];")

printer.write(END)
printer.write(after)

Expand All @@ -227,13 +242,15 @@ def generate_runtime_init(identifiers, strings):

# Read the non-generated part of the file.
with open(filename) as infile:
before = ''.join(iter_to_marker(infile, START))[:-1]
for _ in iter_to_marker(infile, END):
pass
after = infile.read()[:-1]
orig = infile.read()
lines = iter(orig.rstrip().splitlines())
before = '\n'.join(iter_to_marker(lines, START))
for _ in iter_to_marker(lines, END):
pass
after = '\n'.join(lines)

# Generate the file.
with open(filename, 'w', encoding='utf-8') as outfile:
with open_for_changes(filename, orig) as outfile:
printer = Printer(outfile)
printer.write(before)
printer.write(START)
Expand Down Expand Up @@ -286,6 +303,7 @@ def get_identifiers_and_strings() -> 'tuple[set[str], dict[str, str]]':
raise ValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}')
return identifiers, strings


#######################################
# the script

Expand Down