示例#1
0
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")
示例#2
0
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")
示例#3
0
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")
示例#4
0
def download(url, dst_file):
    req = urllib2.Request(url, headers={"User-Agent": "Mozilla/5.0"})
    u = urllib2.urlopen(req)

    with open(dst_file, "wb") as f:
        meta = u.info()
        meta_func = meta.getheaders if hasattr(meta,
                                               "getheaders") else meta.get_all
        meta_length = meta_func("Content-Length")
        file_size = None

        if meta_length:
            file_size = int(meta_length[0])

        if file_size:
            log.info("Download file size: {0}".format(
                util.readable_file_size(file_size)))

        file_size_dl = 0
        block_sz = 8192
        block_count = 0

        while True:
            dbuffer = u.read(block_sz)

            if not dbuffer:
                break

            dbuffer_len = len(dbuffer)
            file_size_dl += dbuffer_len
            block_count += 1

            f.write(dbuffer)

            download_hook(block_count, block_sz, file_size)
            sys.stdout.flush()

        log.normal("")
示例#5
0
def show_help(params):
    log.colored("List of available verb actions:\n", log.PURPLE)
    log.normal("  - generate")
    log.normal("  - download")
    log.normal("  - upload")
示例#6
0
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)
示例#7
0
def show_help(params):
    log.colored("Available actions:\n", log.PURPLE)
    log.normal("  - setup")
示例#8
0
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
示例#9
0
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("")