def execute(self): MODE = 0770 # ensure filesystem root exists fs_root = self.location.filesystem_root if not os.path.isdir(fs_root): try: os.makedirs(fs_root, MODE) except error as e: raise ActionError( "Unable to create filesystem root directory: " + fs_root + \ "\n " + str(e) ) # remember the directories created below dir_lookup = {} # create standard directories ('projects', 'bash', 'config', etc.) for dir_name in ['bash', 'projects', 'config', '.logs', 'plugins']: dir_path = os.path.join(fs_root, dir_name) dir_lookup[dir_name] = dir_path if not os.path.isdir(dir_path): try: os.makedirs(dir_path, MODE) except error as e: raise ActionError( "Unable to create root subdirectory: " + dir_path + \ "\n " + str(e) ) # locate the install location to find the bash template install_pkg_dir = os.path.dirname(os.path.abspath(dpa.__file__)) # ---- bash template # the file to read from bash_template_file = os.path.join( install_pkg_dir, 'data', 'bash', BASH_ACTIVATE_TEMPLATE ) if not os.path.exists(bash_template_file): raise ActionError("Unable to locate LOCATION template bash script.") # the file to write to bash_activate_file = os.path.join( dir_lookup['bash'], BASH_ACTIVATE_TEMPLATE ) # ---- readme file # readme file bash_readme_template_file = os.path.join( install_pkg_dir, 'data', 'bash', BASH_README_TEMPLATE ) if not os.path.exists(bash_readme_template_file): raise ActionError("Unable to locate README template file.") # the file to write to bash_readme_file = os.path.join( dir_lookup['bash'], BASH_README_TEMPLATE ) # ---- format template files file_pairs = [ (bash_template_file, bash_activate_file), (bash_readme_template_file, bash_readme_file), ] replacements = ( ("__DPA_LOCATION_CODE__", self.location.code), ("__DPA_DATA_SERVER__", self.server), ("__DPA_FILESYSTEM_ROOT__", self.location.filesystem_root), ) # handle the file formatting and writing for in_file_path, out_file_path in file_pairs: with open(in_file_path) as in_file: with open(out_file_path, 'w') as out_file: text = in_file.read() for in_str, out_str in replacements: text = text.replace(in_str, out_str) # write new text to bash file in config dir out_file.write(text) # print info to user about bash file to source Output.text( "\nA bash script has been created to activate the pipeline in " + \ "this location. The path to the bash script is: \n\n" + \ " " + Style.bright + bash_activate_file + Style.reset + "\n\n" + \ "See the README in the same directory for instructions on how " + \ "to reference the script.\n", margin=4, )