def create_base_folder(path): """ Create base directory if needed """ basePath = pathlib.Path(path).parent # Check if the base folder is a file if basePath.exists(): # Check if the parent is a file or if its a symlink if basePath.is_file() or basePath.is_symlink(): logging.print_error( "Base directory is a file or link: {}".format(basePath)) return False # If not, it must be a directory, so we are ok else: return True # Create path and parents (or ignore if folder already exists) try: basePath.mkdir(parents=True, exist_ok=True) except Exception as error: logging.print_error( "Could not create parent folder \"{}\" due to following error: {}". format(basePath, error)) return False else: logging.print_verbose( "Parent folder dosent exist and was created: {}".format(basePath)) return True
def create_symlink(source, destination): """ Creates symlink from source to destination """ try: os.symlink(source, destination) except OSError as error: logging.print_error("Unable to create symlink: {}".format(error)) else: logging.print_verbose("Linking {} <-> {}".format(source, destination))
def copy_folder(source, destination): """ Copies frolder from source to destination """ try: copytree(source, destination) except IOError as error: logging.print_error("Unable to copy folder: {}".format(error)) else: logging.print_verbose("Copied {} --> {}".format(source, destination))
def delete_file(path): """ Deletes file """ try: os.remove(path) except OSError as error: logging.print_error("File Deletion Failed: {}".format(path)) else: logging.print_verbose("File Successfully Deleted: {}".format(path))
def create_folder(path): """ Creates a folder """ try: os.makedirs(path) except OSError as error: logging.print_error( "Could not create folder \"{}\" due to following error: {}".format( path, error)) else: logging.print_verbose( "Folder doesn't exist and was created: {}".format(path))
def run_commands(unparsed_command_list, package_name=None): """ Runs commands passed in """ for command in unparsed_command_list: parsed_command = command.split() try: runner = run(parsed_command) except Exception as error: logging.print_fatal( "Error occured during command \"{}\": {}".format( command, error), package_name) else: logging.print_verbose( "Command \"{}\" finished with exit code: {}".format( command, runner.returncode), package_name)
def delete_folder(path): """ Deletes a folder """ basePath = pathlib.Path(path) if not basePath.exists(): logging.print_error("Folder doesn't exist: {}".format(path)) return try: if basePath.is_symlink(): basePath.unlink() else: shutil.rmtree(basePath) except OSError as error: logging.print_error( "Folder Deletion/Unsymlink Failed: {}".format(error)) else: logging.print_verbose("Folder Deleted Successfully: {}".format(path))