From 5f84f233de8b09b6d91b649d8fb46d06f86d22ea Mon Sep 17 00:00:00 2001 From: Eugene Fedorenko Date: Wed, 12 Feb 2020 14:33:58 -0800 Subject: [PATCH] bootstrap --- bootstrap/bootstrap.py | 83 ++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/bootstrap/bootstrap.py b/bootstrap/bootstrap.py index 0aa7c2e6..571d3443 100644 --- a/bootstrap/bootstrap.py +++ b/bootstrap/bootstrap.py @@ -1,6 +1,8 @@ import os import sys +import platform import argparse +# import shutil # from git import Repo @@ -32,30 +34,39 @@ def git_repo(self): def renamefiles(self): # Rename all files starting with diabetes_regression with project name strtoreplace = "diabetes_regression" - dirs = [".pipelines", r"ml_service\pipelines"] + dirs = [".pipelines", r"ml_service/pipelines"] for dir in dirs: - dirpath = os.path.join(self._project_directory, dir) + 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, dir, filename) + src = os.path.join(self._project_directory, normDir, filename) # NOQA: E501 dst = os.path.join(self._project_directory, - dir, filename.replace(strtoreplace, self._project_name, 1)) # NOQA: E501 + normDir, filename.replace(strtoreplace, self._project_name, 1)) # NOQA: E501 os.rename(src, dst) def renamedir(self): - # Rename any directory with diabetes_regression with project name - dirs = ["diabetes_regression"] - for dir in dirs: - src = os.path.join(self._project_directory, dir) - dst = os.path.join(self._project_directory, self._project_name) - os.rename(src, dst) + 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 deletedir(self): # Delete unwanted directories - dirs = ["docs", r"diabetes_regression\training\R"] + dirs = ["docs", r"diabetes_regression"] + if (platform.system() == "Windows"): + cmd = 'rmdir /S /Q "{}"' + else: + cmd = 'rm -r "{}"' for dir in dirs: os.system( - 'rmdir /S /Q "{}"'.format(os.path.join(self._project_directory, dir))) # NOQA: E501 + cmd.format(os.path.join(self._project_directory, os.path.normpath(dir)))) # NOQA: E501 def cleandir(self): # Clean up directories @@ -79,33 +90,33 @@ def validateargs(self): def replaceprojectname(project_dir, project_name, rename_name): # Replace instances of rename_name within files with project_name dirs = [r".env.example", - r".pipelines\azdo-base-pipeline.yml", - r".pipelines\azdo-pr-build-train.yml", - r".pipelines\diabetes_regression-ci-build-train.yml", - r".pipelines\diabetes_regression-ci-image.yml", - r".pipelines\diabetes_regression-template-get-model-version.yml", # NOQA: E501 - r".pipelines\diabetes_regression-variables.yml", - r"environment_setup\Dockerfile", - r"environment_setup\install_requirements.sh", - 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\pipelines\diabetes_regression_verify_train_pipeline.py", # NOQA: E501 - r"ml_service\util\create_scoring_image.py", - r"diabetes_regression\azureml_environment.json", - 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"] # NOQA: E501 - - for file in dirs: - fin = open(os.path.join(project_dir, file), + r".pipelines/azdo-base-pipeline.yml", + r".pipelines/azdo-pr-build-train.yml", + r".pipelines/diabetes_regression-ci-build-train.yml", + r".pipelines/diabetes_regression-ci-image.yml", + r".pipelines/diabetes_regression-template-get-model-version.yml", # NOQA: E501 + r".pipelines/diabetes_regression-variables.yml", + r"environment_setup/Dockerfile", + r"environment_setup/install_requirements.sh", + 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/pipelines/diabetes_regression_verify_train_pipeline.py", # NOQA: E501 + r"ml_service/util/create_scoring_image.py", + r"diabetes_regression/azureml_environment.json", + 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"] # NOQA: E501 + + for dir in dirs: + file = os.path.join(project_dir, os.path.normpath(dir)) + fin = open(file, "rt", encoding="utf8") data = fin.read() data = data.replace(rename_name, project_name) fin.close() - fin = open(os.path.join(project_dir, file), - "wt", encoding="utf8") + fin = open(os.path.join(project_dir, file), "wt", encoding="utf8") # NOQA: E501 fin.write(data) fin.close() @@ -127,9 +138,9 @@ def main(args): replaceprojectname(project_directory, project_name, "diabetes_regression") replaceprojectname(project_directory, project_name, "diabetes") - helper.deletedir() helper.renamefiles() helper.renamedir() + helper.deletedir() except Exception as e: print(e) return 0