-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_python_pkg.py
More file actions
executable file
·42 lines (34 loc) · 1.34 KB
/
build_python_pkg.py
File metadata and controls
executable file
·42 lines (34 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
import ast
import sys
from pathlib import Path
import argparse
def top_level_functions(body):
def cond(d):
f = isinstance(d, ast.FunctionDef) and (not d.name.startswith('_'))
c = isinstance(d, ast.ClassDef) and (not d.name.startswith('_'))
return f or c
return (f for f in body if cond(f))
def parse_ast(filename):
with open(filename, "rt") as file:
return ast.parse(file.read(), filename=filename)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--path", required=True, help="path to package")
args = parser.parse_args()
pgk=Path(args.path).resolve().name
files = []
for path in Path(args.path).rglob('*.py'):
files.append(path)
with open(f'{args.path}/__init__.py', 'w') as f:
for filename in files:
tree = parse_ast(filename)
for func in top_level_functions(tree.body):
hier = filename.relative_to(args.path)
hier = f'{hier.parent}'.replace('/','.')
if (hier == '.'):
hier = "" # In case there is no subfolders
else:
hier = hier + '.'
print(f'from {pgk}.{hier}{filename.stem} import {func.name}', file=f)
# from stats_math.src.tests.py.tests import white_test