]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
feat(tools): check last modification times in async_to_sync.py 744/head
authorDenis Laxalde <denis.laxalde@dalibo.com>
Mon, 4 Mar 2024 07:37:58 +0000 (08:37 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 1 Apr 2024 23:02:29 +0000 (23:02 +0000)
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.

tools/async_to_sync.py

index 1ceefd234e788755564345e233caa36fb34cc2c2..2a6f8ab77c7d4afbbcb2e0ffbdde52f5be8bfbea 100755 (executable)
@@ -85,13 +85,30 @@ def main() -> int:
             PYVER,
         )
 
+    if not opt.convert_all:
+        inputs, outputs = [], []
+        for fpin in opt.inputs:
+            fpout = fpin.parent / fpin.name.replace("_async", "")
+            if fpout.stat().st_mtime >= fpin.stat().st_mtime:
+                logger.debug("not converting %s as %s is up to date", fpin, fpout)
+                continue
+            inputs.append(fpin)
+            outputs.append(fpout)
+        if not outputs:
+            logger.warning("all output files are up to date, nothing to do")
+            return 0
+
+    else:
+        inputs = opt.inputs
+        outputs = [fpin.parent / fpin.name.replace("_async", "") for fpin in inputs]
+
     if opt.jobs == 1:
         logger.debug("multi-processing disabled")
-        for fpin in opt.inputs:
-            convert(fpin)
+        for fpin, fpout in zip(inputs, outputs):
+            convert(fpin, fpout)
     else:
         with ProcessPoolExecutor(max_workers=opt.jobs) as executor:
-            outputs = executor.map(convert, opt.inputs)
+            executor.map(convert, inputs, outputs)
 
     if opt.check:
         return check([str(o) for o in outputs])
@@ -99,8 +116,7 @@ def main() -> int:
     return 0
 
 
-def convert(fpin: Path) -> Path:
-    fpout = fpin.parent / fpin.name.replace("_async", "")
+def convert(fpin: Path, fpout: Path) -> None:
     logger.info("converting %s", fpin)
     with fpin.open() as f:
         source = f.read()
@@ -114,8 +130,6 @@ def convert(fpin: Path) -> Path:
 
     sp.check_call(["black", "-q", str(fpout)])
 
-    return fpout
-
 
 def check(outputs: list[str]) -> int:
     try:
@@ -578,6 +592,12 @@ def parse_cmdline() -> Namespace:
     parser.add_argument(
         "--all", action="store_true", help="process all the files of the project"
     )
+    parser.add_argument(
+        "-B",
+        "--convert-all",
+        action="store_true",
+        help="process specified files without checking last modification times",
+    )
     parser.add_argument(
         "-j",
         "--jobs",