Skip to content

Instantly share code, notes, and snippets.

@allenap
Last active March 6, 2018 08:47
Show Gist options
  • Select an option

  • Save allenap/7d59d96c5271226dc8640123738d9e8f to your computer and use it in GitHub Desktop.

Select an option

Save allenap/7d59d96c5271226dc8640123738d9e8f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
# To produce a report, run like so:
#
# $ ./this-script.py ui/src ui/tests | \
# > cut -f 1 | sort | uniq -c | sort -k 2
#
from pathlib import Path
import re
import sys
re_exposing_dotdot = re.compile(
'^((?:port\s+|effect\s+)?module|import)\s+.*?\s+exposing\s*[(][.][.][)]',
re.MULTILINE)
def report(classification, filepath):
print(f"{classification}\t{filepath}")
if __name__ == '__main__':
for directory in sys.argv[1:]:
dirpath = Path(directory)
for filepath in dirpath.rglob("*.elm"):
contents = filepath.read_text("utf-8")
module, imports = False, []
for match in re_exposing_dotdot.finditer(contents):
if match.group(1) == 'import':
imports.append(match.group(0))
else:
assert not module, "Multiple module decls: " + filepath
module = True
if module and len(imports) >= 1:
report("module-and-imports", filepath)
elif module:
report("module", filepath)
elif len(imports) >= 2:
report("imports-multiple", filepath)
elif len(imports) == 1:
report("imports-once", filepath)
else:
report("okay", filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment