From c27b4b80f8430c90f5a9558753793f496ba6fad6 Mon Sep 17 00:00:00 2001 From: Matt Katz Date: Tue, 4 Aug 2020 17:57:42 -0400 Subject: [PATCH] default to adding __init__.py files to directories Python unittest discover still relies on __init__.py files and doesn't support namespace packages. By default we can create these but allow for their suppression if using other test frameworks. --- mkcodes.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) mode change 100644 => 100755 mkcodes.py diff --git a/mkcodes.py b/mkcodes.py old mode 100644 new mode 100755 index db6d02c..adf2b76 --- a/mkcodes.py +++ b/mkcodes.py @@ -79,6 +79,15 @@ def get_files(inputs): elif path.suffix in markdown_extensions: yield path, path.parent +def add_inits_to_dir(path): + """Recursively add __init__.py files to a directory + This compensates for https://bugs.python.org/issue23882 and https://bugs.python.org/issue35617 + """ + for child in path.rglob('*'): + if child.is_dir(): + (child / '__init__.py').touch() + + @click.command() @click.argument( @@ -88,7 +97,9 @@ def get_files(inputs): help='Github-flavored fence blocks or pure markdown.') @click.option('--safe/--unsafe', default=True, help='Allow code blocks without language hints.') -def main(inputs, output, github, safe): +@click.option('--package-python', default=True, + help='Add __init__.py files to python output to aid in test discovery') +def main(inputs, output, github, safe, package_python): collect_codeblocks = github_codeblocks if github else markdown_codeblocks outputbasedir = Path(output).parent outputbasename = Path(output).name @@ -104,4 +115,7 @@ def main(inputs, output, github, safe): outputfilename.parent.mkdir(parents=True, exist_ok=True) outputfilename.write_text('\n\n'.join(codeblocks)) + if package_python: + add_inits_to_dir(outputbasedir) +