def run(args, cwd): ret = subprocess.call(args, cwd=cwd) if ret > 0: log.normal("{2}COMMAND:{3} {0}\n" "{4}WORKING DIR:{5} {1}".format(" ".join(args), cwd, log.YELLOW, log.ENDC, log.YELLOW, log.ENDC)) log.error("Command execution has failed")
def show_help(params): proj_path = params["proj_path"] targets = target.get_all_targets(proj_path) if targets and len(targets) > 0: log.colored("List of available targets:\n", log.PURPLE) for target_item in targets: log.normal(" - {0}".format(target_item)) else: log.error("No targets available")
def run_as_shell(args, cwd): ret = subprocess.call(args, cwd=cwd, shell=True) if ret > 0: if not isinstance(args, str): args = " ".join(args) log.normal("{2}COMMAND:{3} {0}\n" "{4}WORKING DIR:{5} {1}".format(args, cwd, log.YELLOW, log.ENDC, log.YELLOW, log.ENDC)) log.error("Command execution has failed")
def show_help(params): log.colored("Available actions:\n", log.PURPLE) log.normal(" - setup")
def show_help(params): log.colored("List of available verb actions:\n", log.PURPLE) log.normal(" - generate") log.normal(" - download") log.normal(" - upload")
def run(params): args = params["args"] proj_path = params["proj_path"] targets = target.get_all_targets(proj_path) show_target_list = False if len(args) > 0: target_item = args[0] args.pop(0) if target_item in targets: target_verbs = target.get_all_target_verbs(proj_path, target_item) target_verbs = list( util.filter_list(target_verbs, const.TARGET_VERBS_INTERNAL)) show_target_verb_list = False if len(args) > 0: verb_name = args[0] if verb_name in target_verbs: log.info('Running "{0}" on target "{1}"...'.format( verb_name, target_item)) target_verb_folder = os.path.join( proj_path, const.DIR_NAME_FILES, const.DIR_NAME_FILES_TARGETS, target_item, const.DIR_NAME_FILES_TARGET_VERBS, ) params["target_name"] = target_item runner.run_external( path=target_verb_folder, module_name=verb_name, command_name="run", command_params=params, show_log=False, show_error_log=True, throw_error=True, ) else: show_target_verb_list = True else: show_target_verb_list = True if show_target_verb_list: if target_verbs and len(target_verbs) > 0: log.colored("List of available target verbs:\n", log.PURPLE) for target_verb in target_verbs: log.normal(" - {0}".format(target_verb)) else: log.error("No target verbs available") else: show_target_list = True else: show_target_list = True if show_target_list: show_help(params)
def upload( proj_path, version, force, dist_file_path, dist_file_name, dist_folder, aws_key_id, aws_secret_key, aws_bucket_name, aws_bucket_path, ): import boto3 # version if not version or len(version) == 0: log.error("You need define version name (parameter: --version)") log.info("Version defined: {0}".format(version)) # prepare to upload if not os.path.isfile(dist_file_path): log.error("Distribution file not exists: {0}".format(dist_file_path)) # prepare aws sdk log.info("Initializing AWS bucket and SDK...") if not aws_key_id or not aws_secret_key: log.fail("Your AWS credentials are invalid") s3_client = boto3.client( service_name="s3", aws_secret_access_key=aws_secret_key, aws_access_key_id=aws_key_id, ) # checking for existing version log.info("Checking if version exists...") object_name = "{0}/{1}/{2}".format( aws_bucket_path, version, dist_file_name, ) has_version = s3_key_exists(s3_client, aws_bucket_name, object_name) if has_version: if force: log.info("The version {0} already exists, removing...".format(version)) s3_prefix_delete( s3_client, aws_bucket_name, object_name, aws_secret_key, aws_key_id, ) else: log.error("The version {0} already exists".format(version)) # upload log.info( 'Uploading file "{0}" to S3 bucket "{1}"...'.format( dist_file_path, aws_bucket_name ) ) s3_client.upload_file( dist_file_path, aws_bucket_name, object_name, ExtraArgs={"ACL": "public-read"}, Callback=ProgressPercentage(dist_file_path), ) log.normal("") log.ok("")
def run_external( path, module_name, command_name, command_params, show_log=False, show_error_log=False, throw_error=False, ): """ Execute external command inside path and return the command result. :param path: path where python file is located :param module_name: module name :param command_name: command name :param command_params: command params :param show_log: show log :param show_error_log: show log if exception :param throw_error: throw error if exception :return: command result """ result = None sys_path = list(sys.path) original_cwd = os.getcwd() target_module = None command = None try: sys.path.insert(0, path) target_module = importlib.import_module(module_name) command = getattr(target_module, command_name) result = command(params=command_params) if show_log: log.normal( 'Command "{0}" finished with success'.format(command_name)) except Exception as e: if show_error_log: log.error( 'Error while call "{0}" on module "{1}": {2}'.format( command_name, module_name, e), fatal=(not throw_error), ) if throw_error: raise finally: if module_name in sys.modules: del sys.modules[module_name] if target_module is not None: del target_module if command is not None: del command sys.path = sys_path os.chdir(original_cwd) return result