Skip to content

Commit 66aaa38

Browse files
authored
Merge pull request #72 from sommersoft/cpython_strict_modules
Add Additional CPython Strict Module Name checks.
2 parents 94667a3 + dd7281f commit 66aaa38

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

adabot/circuitpython_libraries.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
ERROR_PYFILE_MISSING_STRUCT = ".py file contains reference to import ustruct" \
5959
" without reference to import struct. See issue " \
6060
"https://github.com/adafruit/circuitpython/issues/782"
61+
ERROR_PYFILE_MISSING_RE = ".py file contains reference to import ure" \
62+
" without reference to import re. See issue " \
63+
"https://github.com/adafruit/circuitpython/issues/1582"
64+
ERROR_PYFILE_MISSING_JSON = ".py file contains reference to import ujson" \
65+
" without reference to import json. See issue " \
66+
"https://github.com/adafruit/circuitpython/issues/1582"
67+
ERROR_PYFILE_MISSING_ERRNO = ".py file contains reference to import uerrno" \
68+
" without reference to import errno. See issue " \
69+
"https://github.com/adafruit/circuitpython/issues/1582"
6170
ERROR_MISMATCHED_READTHEDOCS = "Mismatched readthedocs.yml"
6271
ERROR_MISSING_EXAMPLE_FILES = "Missing .py files in examples folder"
6372
ERROR_MISSING_EXAMPLE_FOLDER = "Missing examples folder"
@@ -421,10 +430,10 @@ def validate_readme(repo, download_url):
421430

422431
return errors
423432

424-
def validate_py_for_ustruct(repo, download_url):
425-
""" For a .py file, look for usage of "import ustruct" and
426-
look for "import struct". If the "import ustruct" is
427-
used with NO "import struct" generate an error.
433+
def validate_py_for_u_modules(repo, download_url):
434+
""" For a .py file, look for usage of "import u___" and
435+
look for "import ___". If the "import u___" is
436+
used with NO "import ____" generate an error.
428437
"""
429438
# We use requests because file contents are hosted by githubusercontent.com, not the API domain.
430439
contents = requests.get(download_url, timeout=30)
@@ -439,6 +448,21 @@ def validate_py_for_ustruct(repo, download_url):
439448
if ustruct_lines and not struct_lines:
440449
errors.append(ERROR_PYFILE_MISSING_STRUCT)
441450

451+
ure_lines = [l for l in lines if re.match(r"[\s]*import[\s][\s]*ure", l)]
452+
re_lines = [l for l in lines if re.match(r"[\s]*import[\s][\s]*re", l)]
453+
if ure_lines and not re_lines:
454+
errors.append(ERROR_PYFILE_MISSING_RE)
455+
456+
ujson_lines = [l for l in lines if re.match(r"[\s]*import[\s][\s]*ujson", l)]
457+
json_lines = [l for l in lines if re.match(r"[\s]*import[\s][\s]*json", l)]
458+
if ujson_lines and not json_lines:
459+
errors.append(ERROR_PYFILE_MISSING_JSON)
460+
461+
uerrno_lines = [l for l in lines if re.match(r"[\s]*import[\s][\s]*uerrno", l)]
462+
errno_lines = [l for l in lines if re.match(r"[\s]*import[\s][\s]*errno", l)]
463+
if uerrno_lines and not errno_lines:
464+
errors.append(ERROR_PYFILE_MISSING_ERRNO)
465+
442466
return errors
443467

444468
def validate_travis_yml(repo, travis_yml_file_info):
@@ -611,8 +635,8 @@ def validate_contents(repo):
611635
re_str = re.compile('adafruit\_[\w]*\.py')
612636
pyfiles = [x["download_url"] for x in content_list if re_str.fullmatch(x["name"])]
613637
for pyfile in pyfiles:
614-
# adafruit_xxx.py file; check if for proper usage of ustruct
615-
errors.extend(validate_py_for_ustruct(repo, pyfile))
638+
# adafruit_xxx.py file; check if for proper usage of u___ versions of modules
639+
errors.extend(validate_py_for_u_modules(repo, pyfile))
616640

617641
# now location any directories whose names begin with "adafruit_"
618642
re_str = re.compile('adafruit\_[\w]*')
@@ -626,8 +650,8 @@ def validate_contents(repo):
626650
# search for .py files in that directory
627651
dir_files = [x["download_url"] for x in dir_file_list if x["type"] == "file" and x["name"].endswith(".py")]
628652
for dir_file in dir_files:
629-
# .py files in subdirectory adafruit_xxx; check if for proper usage of ustruct
630-
errors.extend(validate_py_for_ustruct(repo, dir_file))
653+
# .py files in subdirectory adafruit_xxx; check if for proper usage of u___ versions of modules
654+
errors.extend(validate_py_for_u_modules(repo, dir_file))
631655

632656
return errors
633657

0 commit comments

Comments
 (0)