From fa4bf4dc5cd15c8662c7862073b4bfe6a84294c8 Mon Sep 17 00:00:00 2001 From: chriskiehl Date: Tue, 20 Jan 2015 21:49:08 -0500 Subject: [PATCH] Discovered atexit module. registered cleanup function to remove generated files --- gooey/python_bindings/gooey_decorator.py | 20 +++++++++----------- gooey/python_bindings/modules.py | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/gooey/python_bindings/gooey_decorator.py b/gooey/python_bindings/gooey_decorator.py index 8f84c90..93a3d7c 100644 --- a/gooey/python_bindings/gooey_decorator.py +++ b/gooey/python_bindings/gooey_decorator.py @@ -46,8 +46,10 @@ done. ''' import os +import tempfile import wx import source_parser +import atexit from functools import partial from gooey.gui.lang import i18n from gooey.gui.windows import layouts @@ -77,15 +79,16 @@ def Gooey(f=None, advanced=True, _, filename = os.path.split(main_module_path) cleaned_source = clean_source(main_module_path) - filepath = os.path.join(create_local_storage(), 'gooey_' + filename) + descriptor, tmp_filepath = tempfile.mkstemp(suffix='.py') + atexit.register(cleanup, descriptor, tmp_filepath) - with open(filepath, 'w') as f: + with open(tmp_filepath, 'w') as f: f.write(cleaned_source) if not has_argparse(cleaned_source): show_config = False - run_cmd = 'python {}'.format(filepath) + run_cmd = 'python {}'.format(tmp_filepath) # Must be called before anything else app = wx.App(False) @@ -165,14 +168,9 @@ def has_argparse(source): bla = ['.parse_args()' in line.lower() for line in source.split('\n')] return any(bla) - -def create_local_storage(): - path = os.path.join(os.getcwd(), '_gooey_generated_files_') - try: - os.mkdir(path) - except OSError: - pass # already exists - return path +def cleanup(descriptor, filepath): + os.close(descriptor) + os.remove(filepath) if __name__ == '__main__': diff --git a/gooey/python_bindings/modules.py b/gooey/python_bindings/modules.py index c9c3644..c2bb0f5 100644 --- a/gooey/python_bindings/modules.py +++ b/gooey/python_bindings/modules.py @@ -10,10 +10,10 @@ sys.path.append(os.path.dirname(__file__)) def load(module_source): descriptor, tmpfilepath = tempfile.mkstemp(suffix='.py') - print descriptor, tmpfilepath + tmpfiledir = os.path.dirname(tmpfilepath) tmpfilename = os.path.splitext(os.path.split(tmpfilepath)[-1])[0] - print tmpfilename + sys.path.append(tmpfiledir) try: with open(tmpfilepath, 'w') as f: