-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
bootstrap.py
155 lines (132 loc) · 6.4 KB
/
bootstrap.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
import sys
import platform
import argparse
import re
class Helper:
def __init__(self, project_directory, project_name):
self._project_directory = project_directory
self._project_name = project_name
self._git_repo = "https://github.com/microsoft/MLOpsPython.git"
@property
def project_directory(self):
return self._project_directory
@property
def project_name(self):
return self._project_name
@property
def git_repo(self):
return self._git_repo
def rename_files(self):
# Rename all files starting with diabetes_regression with project name
strtoreplace = "diabetes_regression"
dirs = [".pipelines", r"ml_service/pipelines"]
for dir in dirs:
normDir = os.path.normpath(dir)
dirpath = os.path.join(self._project_directory, normDir)
for filename in os.listdir(dirpath):
if(filename.find(strtoreplace) != -1):
src = os.path.join(self._project_directory, normDir, filename) # NOQA: E501
dst = os.path.join(self._project_directory,
normDir,
filename.replace(strtoreplace, self._project_name, 1)) # NOQA: E501
os.rename(src, dst)
def rename_dir(self):
dir = "diabetes_regression"
src = os.path.join(self._project_directory, dir)
for path, subdirs, files in os.walk(src):
for name in files:
newPath = path.replace(dir, self._project_name)
if (not (os.path.exists(newPath))):
os.mkdir(newPath)
file_path = os.path.join(path, name)
new_name = os.path.join(newPath, name)
os.rename(file_path, new_name)
def delete_dir(self):
# Delete unwanted directories
dirs = ["docs", r"diabetes_regression"]
if (platform.system() == "Windows"):
cmd = 'rmdir /S /Q "{}"'
else:
cmd = 'rm -r "{}"'
for dir in dirs:
os.system(cmd.format(os.path.join(self._project_directory, os.path.normpath(dir)))) # NOQA: E501
def clean_dir(self):
# Clean up directories
dirs = ["data", "experimentation"]
for dir in dirs:
for root, dirs, files in os.walk(os.path.join(self._project_directory, dir)): # NOQA: E501
for file in files:
os.remove(os.path.join(root, file))
def validate_args(self):
# Validate arguments
if (os.path.isdir(self._project_directory) is False):
raise Exception("Not a valid directory. Please provide an absolute directory path.") # NOQA: E501
if (len(self._project_name) < 3 or len(self._project_name) > 15):
raise Exception("Invalid project name length. Project name should be 3 to 15 chars long, letters and underscores only.") # NOQA: E501
if (not re.search("^[\\w_]+$", self._project_name)):
raise Exception("Invalid characters in project name. Project name should be 3 to 15 chars long, letters and underscores only.") # NOQA: E501
def replace_project_name(project_dir, project_name, rename_name):
# Replace instances of rename_name within files with project_name
files = [r".env.example",
r".pipelines/code-quality-template.yml",
r".pipelines/pr.yml",
r".pipelines/diabetes_regression-cd.yml",
r".pipelines/diabetes_regression-ci.yml",
r".pipelines/abtest.yml",
r".pipelines/diabetes_regression-ci-image.yml",
r".pipelines/diabetes_regression-publish-model-artifact-template.yml", # NOQA: E501
r".pipelines/diabetes_regression-get-model-id-artifact-template.yml", # NOQA: E501
r".pipelines/diabetes_regression-batchscoring-ci.yml",
r".pipelines/diabetes_regression-variables-template.yml",
r"environment_setup/Dockerfile",
r"environment_setup/install_requirements.sh",
r"ml_service/pipelines/diabetes_regression_build_parallel_batchscore_pipeline.py", # NOQA: E501
r"ml_service/pipelines/diabetes_regression_build_train_pipeline_with_r_on_dbricks.py", # NOQA: E501
r"ml_service/pipelines/diabetes_regression_build_train_pipeline_with_r.py", # NOQA: E501
r"ml_service/pipelines/diabetes_regression_build_train_pipeline.py", # NOQA: E501
r"ml_service/util/create_scoring_image.py",
r"diabetes_regression/conda_dependencies.yml",
r"diabetes_regression/evaluate/evaluate_model.py",
r"diabetes_regression/register/register_model.py",
r"diabetes_regression/training/test_train.py"]
for file in files:
path = os.path.join(project_dir, os.path.normpath(file))
try:
with open(path, "rt", encoding="utf8") as f_in:
data = f_in.read()
data = data.replace(rename_name, project_name)
with open(os.path.join(project_dir, file), "wt", encoding="utf8") as f_out: # NOQA: E501
f_out.write(data)
except IOError as e:
print("Could not modify \"%s\". Is the MLOpsPython repo already cloned at \"%s\"?" % (path, project_dir)) # NOQA: E501
raise e
def main(args):
parser = argparse.ArgumentParser(description='New Template')
parser.add_argument("-d",
"--directory",
type=str,
required=True,
help="Absolute path to new project direcory")
parser.add_argument("-n",
"--name",
type=str,
required=True,
help="Name of the project [3-15 chars, letters and underscores only]") # NOQA: E501
try:
args = parser.parse_args()
project_directory = args.directory
project_name = args.name
helper = Helper(project_directory, project_name)
helper.validate_args()
helper.clean_dir()
replace_project_name(project_directory, project_name, "diabetes_regression") # NOQA: E501
replace_project_name(project_directory, project_name, "diabetes")
helper.rename_files()
helper.rename_dir()
helper.delete_dir()
except Exception as e:
print(e)
return 0
if '__main__' == __name__:
sys.exit(main(sys.argv))