Skip to content

Commit 5a7f708

Browse files
dlaxdvarrazzo
authored andcommitted
feat(tools): check last modification times in async_to_sync.py
We now only process _async.py files with a modification time higher than their sync counterpart. A -B,--convert-all option is added to force conversion of all (specified) input files and skip last-modification time check.
1 parent 280720b commit 5a7f708

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

tools/async_to_sync.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,38 @@ def main() -> int:
8585
PYVER,
8686
)
8787

88+
if not opt.convert_all:
89+
inputs, outputs = [], []
90+
for fpin in opt.inputs:
91+
fpout = fpin.parent / fpin.name.replace("_async", "")
92+
if fpout.stat().st_mtime >= fpin.stat().st_mtime:
93+
logger.debug("not converting %s as %s is up to date", fpin, fpout)
94+
continue
95+
inputs.append(fpin)
96+
outputs.append(fpout)
97+
if not outputs:
98+
logger.warning("all output files are up to date, nothing to do")
99+
return 0
100+
101+
else:
102+
inputs = opt.inputs
103+
outputs = [fpin.parent / fpin.name.replace("_async", "") for fpin in inputs]
104+
88105
if opt.jobs == 1:
89106
logger.debug("multi-processing disabled")
90-
for fpin in opt.inputs:
91-
convert(fpin)
107+
for fpin, fpout in zip(inputs, outputs):
108+
convert(fpin, fpout)
92109
else:
93110
with ProcessPoolExecutor(max_workers=opt.jobs) as executor:
94-
outputs = executor.map(convert, opt.inputs)
111+
executor.map(convert, inputs, outputs)
95112

96113
if opt.check:
97114
return check([str(o) for o in outputs])
98115

99116
return 0
100117

101118

102-
def convert(fpin: Path) -> Path:
103-
fpout = fpin.parent / fpin.name.replace("_async", "")
119+
def convert(fpin: Path, fpout: Path) -> None:
104120
logger.info("converting %s", fpin)
105121
with fpin.open() as f:
106122
source = f.read()
@@ -114,8 +130,6 @@ def convert(fpin: Path) -> Path:
114130

115131
sp.check_call(["black", "-q", str(fpout)])
116132

117-
return fpout
118-
119133

120134
def check(outputs: list[str]) -> int:
121135
try:
@@ -578,6 +592,12 @@ def parse_cmdline() -> Namespace:
578592
parser.add_argument(
579593
"--all", action="store_true", help="process all the files of the project"
580594
)
595+
parser.add_argument(
596+
"-B",
597+
"--convert-all",
598+
action="store_true",
599+
help="process specified files without checking last modification times",
600+
)
581601
parser.add_argument(
582602
"-j",
583603
"--jobs",

0 commit comments

Comments
 (0)