Skip to content
Snippets Groups Projects
Commit f99bce96 authored by Mayr, Hannes's avatar Mayr, Hannes
Browse files

Publish required_imports.txt to subfolders.

parent fb0b398a
Branches
Tags
2 merge requests!65Release v0.3.0,!64New year developments
......@@ -150,6 +150,7 @@ class PublishOptions:
)
self.individual_data_storage(dst_path_invisible, plot)
self.export_imports(sys.argv[0], dst_path_invisible)
# If export was successful, make the directory visible
os.rename(dst_path_invisible, dst_path)
except FileExistsError as exc:
......@@ -240,14 +241,14 @@ class PublishOptions:
os.path.join(destination, final_file_path),
)
def export_imports(self, file: str) -> None:
def export_imports(self, file: str, destination: str) -> None:
"""Export all imported modules of a python script to file."""
with open(file, "r", encoding="utf-8") as source:
tree = ast.parse(source.read())
analyzer = Analyzer()
analyzer.visit(tree)
analyzer.report(self.dst_path)
analyzer.report(destination)
class Analyzer(ast.NodeVisitor):
......@@ -338,4 +339,3 @@ def publish(
publish_container = PublishOptions(figs_and_ids, src_datapath, dst_path, **kwargs)
publish_container.validate_input()
publish_container.export()
publish_container.export_imports(sys.argv[0])
......@@ -361,13 +361,15 @@ class TestPublish(unittest.TestCase):
]
folder = os.path.join("test_parent", "test_dst_folder")
os.mkdir(folder)
file_path = os.path.join(folder, "required_imports.txt")
publish_obj = PublishOptions(FIGS_AND_IDS, SRC_DIR, DST_PATH)
publish_obj.export_imports(os.path.join("tests", "test_publish.py"))
publish_obj.export_imports(os.path.join("tests", "test_publish.py"), folder)
file_path = os.path.join(folder, "required_imports.txt")
assert os.path.isfile(file_path)
with open(file_path, "r", encoding="utf-8") as file:
modules = file.readlines()
assert Counter(modules) == Counter(expected_modules)
assert os.path.isfile(file_path)
def tearDown(self) -> None:
"""Delete all files created in setUp."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment