Patchwork [1,of,7,v2] import-checker: refactor source reading

login
register
mail settings
Submitter timeless@mozdev.org
Date April 13, 2016, 5:21 p.m.
Message ID <20a703ce76f7cda378d2.1460568104@waste.org>
Download mbox | patch
Permalink /patch/14590/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

timeless@mozdev.org - April 13, 2016, 5:21 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1460414044 0
#      Mon Apr 11 22:34:04 2016 +0000
# Node ID 20a703ce76f7cda378d2ce950b7daa971b508d74
# Parent  02be5fc18c0c70c087a9d1ab5ffe5afce926f227
import-checker: refactor source reading

This will allow .t files to generate multiple sources.
It will also allow .py doctests to generate additional sources.
Yuya Nishihara - April 15, 2016, 2:38 p.m.
On Wed, 13 Apr 2016 12:21:44 -0500, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1460414044 0
> #      Mon Apr 11 22:34:04 2016 +0000
> # Node ID 20a703ce76f7cda378d2ce950b7daa971b508d74
> # Parent  02be5fc18c0c70c087a9d1ab5ffe5afce926f227
> import-checker: refactor source reading

Queued 1-5, thanks. Also, thanks for reviewing.

> +def sources(f, modname):
> +    if f.endswith('.py'):
> +        with open(f) as src:
> +            yield src.read(), modname

Can you add warnings for unprocessed files, as a follow-up patch?
timeless - April 15, 2016, 9:40 p.m.
Yuya Nishihara wrote:
> Can you add warnings for unprocessed files, as a follow-up patch?

Oh. I specifically did it this way so you could run import-checker on
** and have it not complain about random stuff.
Yuya Nishihara - April 16, 2016, 5:46 a.m.
On Fri, 15 Apr 2016 17:40:44 -0400, timeless wrote:
> Yuya Nishihara wrote:
> > Can you add warnings for unprocessed files, as a follow-up patch?  
> 
> Oh. I specifically did it this way so you could run import-checker on
> ** and have it not complain about random stuff.

Hmm, maybe I'm paranoid, but I prefer warnings so that I won't miss my
mistakes. e.g.

  $ ./contrib/import-checker.py hg hgweb.cgi

Patch

diff --git a/contrib/import-checker.py b/contrib/import-checker.py
--- a/contrib/import-checker.py
+++ b/contrib/import-checker.py
@@ -567,6 +567,11 @@ 
 def _cycle_sortkey(c):
     return len(c), c
 
+def sources(f, modname):
+    if f.endswith('.py'):
+        with open(f) as src:
+            yield src.read(), modname
+
 def main(argv):
     if len(argv) < 2 or (argv[1] == '-' and len(argv) > 2):
         print('Usage: %s {-|file [file] [file] ...}')
@@ -580,15 +585,14 @@ 
     for source_path in argv[1:]:
         modname = dotted_name_of_path(source_path, trimpure=True)
         localmods[modname] = source_path
-    for modname, source_path in sorted(localmods.items()):
-        f = open(source_path)
-        src = f.read()
-        used_imports[modname] = sorted(
-            imported_modules(src, modname, localmods, ignore_nested=True))
-        for error, lineno in verify_import_convention(modname, src, localmods):
-            any_errors = True
-            print('%s:%d: %s' % (source_path, lineno, error))
-        f.close()
+    for localmodname, source_path in sorted(localmods.items()):
+        for src, modname in sources(source_path, localmodname):
+            used_imports[modname] = sorted(
+                imported_modules(src, modname, localmods, ignore_nested=True))
+            for error, lineno in verify_import_convention(modname, src,
+                                                          localmods):
+                any_errors = True
+                print('%s:%d: %s' % (source_path, lineno, error))
     cycles = find_cycles(used_imports)
     if cycles:
         firstmods = set()