forked from AlabamaWaterInstitute/unithg_bmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_project.py
132 lines (119 loc) · 4.56 KB
/
setup_project.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import sys, os, json, re, time, datetime, logging, traceback, ast
from pathlib import Path
from typing import Any
project_name = "pyflo_bmi"
proj_folder = Path("pyflo_bmi")
this_folder = Path(".")
requirements = []
author_name = "Chad Perry"
# Month Day, Year
current_date = datetime.datetime.now().strftime("%B %d, %Y")
def harvest_requirements(src_dir:Path = proj_folder, requirements:list[str] = []):
for name in src_dir.iterdir():
if name.is_dir():
harvest_requirements(name, requirements)
elif name.suffix == ".py":
tree = ast.parse(name.read_text())
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
if alias.name == ".":
continue
requirements.append(alias.name)
elif isinstance(node, ast.ImportFrom):
if node.module == None:
continue
requirements.append(node.module)
return requirements
_requirements = harvest_requirements()
for req in _requirements:
_req = req
if req in sys.builtin_module_names or req in sys.stdlib_module_names:
continue
if req == project_name:
continue
if req.startswith("."):
continue
if "." in req:
req = req.split(".")[0]
if (proj_folder/req).exists() or (proj_folder/(req + ".py")).exists():
continue
print(f"Adding requirement: {_req}")
requirements.append(req)
requirements = list(set(requirements))
requirements.sort()
print(requirements)
def build_requirements_txt(requirements:list[str], path:Path = this_folder/"requirements.txt"):
path.write_text("\n".join(requirements))
build_requirements_txt(requirements)
def build_pyproject_toml(project_name:str, requirements:list[str], path:Path = this_folder/"pyproject.toml"):
toml = {}
toml["build-system"] = {
"requires": ["setuptools", "wheel"],
"build-backend": "setuptools.build_meta"
}
toml["project"] = {
"name": project_name,
"version": "0.1.0",
"description": "A Python-based Ngen-BMI model for the PyFlo module.",
"authors": [{"name": author_name}],
"maintainers": [{"name": author_name}],
"license": {"file": "LICENSE"},
"requires-python": ">=3.9",
}
dependencies = requirements
toml["project"]["dependencies"] = dependencies
# toml["project"]["dynamic"] = ["readme"]
toml["tool.setuptools.packages.find"] = {"where": ["."]}
def toml_str(toml:dict):
def toml_val_len(val:Any):
if isinstance(val, list):
return sum([toml_val_len(v) for v in val])
elif isinstance(val, dict):
return sum([1 + toml_val_len(v) for k, v in val.items()])
return 1
def toml_val_str(val:Any):
if isinstance(val, str):
return f'"{val}"'
elif isinstance(val, list):
len_val = toml_val_len(val)
if len_val == 0:
return "[]"
if len_val <= 2:
# Inline list
return f"[{', '.join([toml_val_str(v) for v in val])}]"
# Multiline list
result = "[\n"
indent = "\t"
for v in val:
result += f"{indent}{toml_val_str(v)},\n"
result += "]"
return result
elif isinstance(val, dict):
return f"{{{', '.join([f'{k} = {toml_val_str(v)}' for k, v in val.items()])}}}"
return str(val)
cats = []
for cat, val in toml.items():
cat_str = f"[{cat}]\n"
# val is always a dict
val:dict[str, Any]
for key, val in val.items():
cat_str += f"{key} = {toml_val_str(val)}\n"
cats.append(cat_str)
return "\n".join(cats)
path.write_text(toml_str(toml))
build_pyproject_toml(project_name, requirements)
def build_init_py(path:Path = proj_folder/"__init__.py"):
imports = []
for name in proj_folder.iterdir():
if name.is_dir():
prefix = name.name
for subname in name.iterdir():
if subname.suffix == ".py":
imports.append(f"from .{prefix}.{subname.stem} import *")
elif name.suffix == ".py":
if name.stem == "__init__":
continue
imports.append(f"from .{name.stem} import *")
path.write_text("\n".join(imports))
build_init_py()