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

login
register
mail settings
Submitter timeless@mozdev.org
Date April 12, 2016, 9:53 p.m.
Message ID <df592defa539bf8a1f33.1460498039@waste.org>
Download mbox | patch
Permalink /patch/14561/
State Superseded
Headers show

Comments

timeless@mozdev.org - April 12, 2016, 9:53 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1460414044 0
#      Mon Apr 11 22:34:04 2016 +0000
# Node ID df592defa539bf8a1f338a81bfbbb73f6e7d9935
# Parent  16255662446d2ce08bd0a4210df30afe3d6377f6
import-checker: refactor source reading

This will allow .t files to generate multiple sources.
It will also allow .py doctests to generate additional sources.
Simon Farnsworth - April 13, 2016, 10:35 a.m.
On 12/04/2016 22:53, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1460414044 0
> #      Mon Apr 11 22:34:04 2016 +0000
> # Node ID df592defa539bf8a1f338a81bfbbb73f6e7d9935
> # Parent  16255662446d2ce08bd0a4210df30afe3d6377f6
> import-checker: refactor source reading
>
> This will allow .t files to generate multiple sources.
> It will also allow .py doctests to generate additional sources.
>
> 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] ...}')
> @@ -581,14 +586,13 @@
>           modname = dotted_name_of_path(source_path, trimpure=True)
>           localmods[modname] = source_path


I really dislike the use of modname2 below:

>       for modname, source_path in sorted(localmods.items()):

If you made "modname" here into "localmodname", then...

> -        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 src, modname2 in sources(source_path, modname):

you could remove the digit 2 from modname here.

> +            used_imports[modname2] = sorted(
> +                imported_modules(src, modname2, localmods, ignore_nested=True))
> +            for error, lineno in verify_import_convention(modname2, src,
> +                                                          localmods):
> +                any_errors = True
> +                print('%s:%d: %s' % (source_path, lineno, error))
>       cycles = find_cycles(used_imports)
>       if cycles:
>           firstmods = set()
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://urldefense.proofpoint.com/v2/url?u=https-3A__www.mercurial-2Dscm.org_mailman_listinfo_mercurial-2Ddevel&d=CwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=mEgSWILcY4c4W3zjApBQLA&m=pvGuifaQhTCM-DbZ9YFx6ITbkTOsLkXTgbVo2sN8MVo&s=SZABDGHssoBhSJ53F_ZcpwW1xKL94YLKC76VTbFc3vg&e=
>
timeless - April 13, 2016, 4:30 p.m.
Thanks, I didn't like it either, but I couldn't come up w/ a better
thing (and i did a bunch of refactors to make things happy, which is
why the linecont thing glitched).

On Wed, Apr 13, 2016 at 6:35 AM, Simon Farnsworth <simonfar@fb.com> wrote:
> On 12/04/2016 22:53, timeless wrote:
>>
>> # HG changeset patch
>> # User timeless <timeless@mozdev.org>
>> # Date 1460414044 0
>> #      Mon Apr 11 22:34:04 2016 +0000
>> # Node ID df592defa539bf8a1f338a81bfbbb73f6e7d9935
>> # Parent  16255662446d2ce08bd0a4210df30afe3d6377f6
>> import-checker: refactor source reading
>>
>> This will allow .t files to generate multiple sources.
>> It will also allow .py doctests to generate additional sources.
>>
>> 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] ...}')
>> @@ -581,14 +586,13 @@
>>           modname = dotted_name_of_path(source_path, trimpure=True)
>>           localmods[modname] = source_path
>
>
>
> I really dislike the use of modname2 below:
>
>>       for modname, source_path in sorted(localmods.items()):
>
>
> If you made "modname" here into "localmodname", then...
>
>> -        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 src, modname2 in sources(source_path, modname):
>
>
> you could remove the digit 2 from modname here.
>
>> +            used_imports[modname2] = sorted(
>> +                imported_modules(src, modname2, localmods,
>> ignore_nested=True))
>> +            for error, lineno in verify_import_convention(modname2, src,
>> +                                                          localmods):
>> +                any_errors = True
>> +                print('%s:%d: %s' % (source_path, lineno, error))
>>       cycles = find_cycles(used_imports)
>>       if cycles:
>>           firstmods = set()
>> _______________________________________________
>> Mercurial-devel mailing list
>> Mercurial-devel@mercurial-scm.org
>>
>> https://urldefense.proofpoint.com/v2/url?u=https-3A__www.mercurial-2Dscm.org_mailman_listinfo_mercurial-2Ddevel&d=CwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=mEgSWILcY4c4W3zjApBQLA&m=pvGuifaQhTCM-DbZ9YFx6ITbkTOsLkXTgbVo2sN8MVo&s=SZABDGHssoBhSJ53F_ZcpwW1xKL94YLKC76VTbFc3vg&e=
>>
>
> --
> Simon Farnsworth
>
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

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] ...}')
@@ -581,14 +586,13 @@ 
         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 src, modname2 in sources(source_path, modname):
+            used_imports[modname2] = sorted(
+                imported_modules(src, modname2, localmods, ignore_nested=True))
+            for error, lineno in verify_import_convention(modname2, src,
+                                                          localmods):
+                any_errors = True
+                print('%s:%d: %s' % (source_path, lineno, error))
     cycles = find_cycles(used_imports)
     if cycles:
         firstmods = set()