Source code for pydmt.utils.python
"""
python.y
"""
import os
import glob
import pprint
import importlib
[docs]
def hlp_source_under(folder):
"""
this function finds all the python packages under a folder and
write the 'packages' and 'package_dir' entries for a python setup.py
script
"""
# walk the folder and find the __init__.py entries for packages.
packages = []
package_dir = {}
for root, _, files in os.walk(folder):
for file in files:
if file != '__init__.py':
continue
full = os.path.dirname(os.path.join(root, file))
relative = os.path.relpath(full, folder)
packages.append(relative)
package_dir[relative] = full
# we use pprint because we want the order to always remain the same
return f"packages={sorted(packages)},\npackage_dir={pprint.pformat(package_dir)}"
[docs]
def hlp_files_under(destination_folder, pat):
file_list = [x for x in glob.glob(pat) if os.path.isfile(x)]
return f"('{destination_folder}', {file_list})"
[docs]
def make_hlp_project_keywords(d):
def hlp_project_keywords():
return f"{d.project_keywords.split()}"
return hlp_project_keywords
[docs]
def make_hlp_project_classifiers(d):
def hlp_project_classifiers():
classifiers = d.project_classifiers.split('\n')
classifiers = [x.strip()[1:-1] for x in classifiers]
return f"{classifiers}"
return hlp_project_classifiers
[docs]
def make_hlp_wrap(level):
def hlp_wrap(t):
return t.replace('\n', '\n' + '\t' * level)
return hlp_wrap
[docs]
def collect_reqs(add_dev=False) -> list[str]:
try:
reqs = []
mod = importlib.import_module("config.python")
if hasattr(mod, "requires"):
reqs += getattr(mod, "requires")
if add_dev and hasattr(mod, "dev_requires"):
reqs += getattr(mod, "dev_requires")
return reqs
except ModuleNotFoundError:
return []
[docs]
def collect_bootstrap_reqs() -> list[str]:
try:
mod = importlib.import_module("config.bootstrap")
if hasattr(mod, "requires"):
return getattr(mod, "requires")
return []
except ModuleNotFoundError:
return []
[docs]
def get_install_args():
return [
"python",
"-m",
"pip",
"install",
]