From cee7a870d203174e6c2e90b95651c3165aa44931 Mon Sep 17 00:00:00 2001 From: ryneeverett Date: Fri, 15 Jan 2016 10:33:52 -0500 Subject: [PATCH] Make one directory at a time. This way an __init__.py makes it into every directory. --- mkcodes.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/mkcodes.py b/mkcodes.py index 37afdcc..2307b61 100644 --- a/mkcodes.py +++ b/mkcodes.py @@ -88,6 +88,24 @@ def get_files(inputs): yield (i, 0) +def makedirs(directory): + to_make = [] + + while True: + try: + os.mkdir(directory) + except FileNotFoundError: + directory, tail = os.path.split(directory) + to_make.append(tail) + else: + with open(os.path.join(directory, '__init__.py'), 'w'): + pass + if to_make: + directory = os.path.join(directory, to_make.pop()) + else: + break + + @click.command() @click.argument( 'inputs', nargs=-1, required=True, type=click.Path(exists=True)) @@ -110,9 +128,7 @@ def main(inputs, output, github, safe): outputdir = os.path.dirname(outputfilename) if not os.path.exists(outputdir): - os.makedirs(outputdir) - with open(os.path.join(outputdir, '__init__.py'), 'w'): - pass + makedirs(outputdir) with open(outputfilename, 'w') as outputfile: outputfile.write('\n\n'.join(codeblocks))