-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_package.py
87 lines (65 loc) · 3.12 KB
/
create_package.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
"""Script to generate a new Python package from a template directory."""
import os
import shutil
import subprocess
from datetime import date
from methods.methods_get_parameters_from_ini_file import get_parameters_from_ini_file
# %% Inputs
parameters = get_parameters_from_ini_file(ini_file="template_inputs.ini")
# %% New Directory
# Path to the template directory
template_dir = "package_folder"
new_package_dir = f'../{parameters["project_package_name"]}'
# Copy the template directory to a new location
shutil.copytree(template_dir, new_package_dir)
# Rename the package folder
old_folder = f'../{parameters["project_package_name"]}/folder'
new_folder = f'../{parameters["project_package_name"]}/' + parameters["package_name"]
os.rename(old_folder, new_folder)
# %% Updates the files
# Function to replace placeholders in a file
def replace_placeholders(_file_path: str, _placeholders: dict) -> None:
"""Replace placeholders in a file with the corresponding values."""
with open(_file_path, encoding="utf-8") as file:
content = file.read()
for placeholder, value in _placeholders.items():
content = content.replace(placeholder, value)
with open(_file_path, "w", encoding="utf-8") as file:
file.write(content)
# Dictionary of placeholders to replace
placeholders = {
"{{PACKAGE_NAME}}": parameters["package_name"],
"{{PROJECT_NAME}}": parameters["project_package_name"],
"{{LONG_DESCRIPTION}}": parameters["long_description"],
"{{SHORT_DESCRIPTION}}": parameters["short_description"],
"{{AUTHOR}}": parameters["author"],
"{{CONTACT}}": parameters["email"],
"{{VERSION}}": parameters["python_version"],
"{{DATE}}": date.today().strftime("%d/%m/%Y") + " (Initialization)",
"{{REQUIRED_PACKAGES}}": parameters["required_packages"],
}
# Recursively find and update all files in the new package directory
for root, _dirs, files in os.walk(new_package_dir):
for file_name in files:
file_path = os.path.join(root, file_name)
replace_placeholders(file_path, placeholders)
print(f"\nThe package '{parameters['package_name']}' has been successfully generated in {new_package_dir}.\n")
# %% Git Repository
# Initialize a new Git repository
os.chdir(new_package_dir) # Change directory to the new package directory
subprocess.run(["git", "init"], check=False) # Initialize a new git repository
subprocess.run(["git", "add", "."], check=False) # Add all files to staging
subprocess.run(["git", "commit", "-m", "Initial commit"], check=False) # Commit the changes
# Rename the default branch to 'master'
subprocess.run(["git", "branch", "-M", "master"], check=False)
if parameters["package_url"]:
# Set up the upstream repository and push
push_command = (
f"git push --set-upstream git@{parameters['package_url']}/"
f"$(git rev-parse --show-toplevel | xargs basename).git "
f"$(git rev-parse --abbrev-ref HEAD)"
)
subprocess.run(push_command, shell=True, check=False)
print(f"\nA new Git repository has been initialized in {new_package_dir}.")
else:
print(f"\nA new local Git repository has been initialized in {new_package_dir}.")