Esempio n. 1
0
def parse_args(parser, arg_strings=None):
    '''
    Parses input arguments and handles more complex defaults.
    - conda_build_configs: If not passed in the default is with the env file,
                          if is passed in, otherwise it is  expected to be in
                          the local path.
    '''
    args = parser.parse_args(arg_strings)

    _create_env_config_paths(args)

    if "conda_build_configs" in vars(args).keys():
        if args.conda_build_configs is None:
            if "env_config_file" in vars(args).keys() and args.env_config_file:
                args.conda_build_configs = os.path.join(
                    os.path.dirname(args.env_config_file[0]),
                    utils.CONDA_BUILD_CONFIG_FILE)
            else:
                args.conda_build_configs = utils.DEFAULT_CONDA_BUILD_CONFIG

        configs = utils.get_conda_build_configs(
            parse_arg_list(args.conda_build_configs))
        args.conda_build_configs = configs

        if not configs:
            show_warning(Error.CONDA_BUILD_CONFIG_NOT_FOUND,
                         utils.CONDA_BUILD_CONFIG_FILE)

    return args
Esempio n. 2
0
def cleanup(local_conda_channel):
    '''
    Removes TEMP_FILES directory from the specified directory
    '''
    try:
        shutil.rmtree(os.path.join(local_conda_channel, TEMP_FILES))
    except OSError:
        show_warning(Error.TEMP_BUILD_IMAGE_FILES)
Esempio n. 3
0
def _get_source_from_conda_package(pkg_dir):
    """
    Download the source for a conda package
    """
    #pylint: disable=import-outside-toplevel
    import conda_build.source
    source_folder = os.path.join(utils.TMP_LICENSE_DIR,
                                 os.path.basename(pkg_dir))
    if not os.path.exists(source_folder):
        os.makedirs(source_folder)

        # Find the recipe's meta.yaml file and download the values within the "source" field.
        with open(os.path.join(pkg_dir, "info", "recipe",
                               "meta.yaml")) as file_stream:
            recipe_data = open_ce.yaml_utils.load(file_stream)

        if not recipe_data.get("source"):
            return source_folder

        sources = recipe_data["source"]
        if not isinstance(sources, list):
            sources = [sources]

        for source in sources:
            # If the source comes from a url, use conda-build's download_to_cache function.
            if source.get("url"):
                try:
                    local_path, _ = conda_build.source.download_to_cache(
                        source_folder, pkg_dir, source, False)
                    _extract(local_path, source_folder)
                except RuntimeError:
                    show_warning(Error.UNABLE_DOWNLOAD_SOURCE,
                                 os.path.basename(pkg_dir))
            elif source.get("git_url"):
                git_url = source["git_url"]
                try:
                    utils.git_clone(git_url, source.get("git_rev"),
                                    source_folder)
                except OpenCEError as error:
                    try:
                        # If the URL is from a private GIT server, try and use an equivalent URL on GitHub
                        parsed_url = urllib.parse.urlsplit(git_url)
                        netloc = parsed_url.netloc.split(".")
                        if netloc[0] == "git":
                            parsed_url = parsed_url._replace(
                                netloc="github.com")
                            parsed_url = parsed_url._replace(path=os.path.join(
                                netloc[1], os.path.basename(parsed_url.path)))
                            git_url = urllib.parse.urlunsplit(parsed_url)
                            utils.git_clone(git_url, source.get("git_rev"),
                                            source_folder)
                        else:
                            raise error
                    except OpenCEError:
                        show_warning(Error.UNABLE_CLONE_SOURCE,
                                     os.path.basename(pkg_dir))

    return source_folder
Esempio n. 4
0
def _get_runtime_image_file(container_tool):
    tool_ver = utils.get_container_tool_ver(container_tool)
    image_file = os.path.join(RUNTIME_IMAGE_PATH, "docker/Dockerfile")
    if not tool_ver:
        show_warning(Error.CONTAINER_VERSION, container_tool)
        # If we couldn't get the version of the tool, should use the older docker
        # supported Dockerfile
        return image_file

    tool_ver = tool_ver.replace(".", "")
    if (container_tool == "docker" and int(tool_ver) >= 1709) or \
       (container_tool == "podman" and int(tool_ver) >= 200):
        # Use the newer docker/podman supported Dockerfile
        image_file = os.path.join(RUNTIME_IMAGE_PATH, "podman/Dockerfile")

    print("INFO: Dockerfile being used: ", image_file)
    return image_file
Esempio n. 5
0
def get_open_ce_version(conda_env_file):
    '''
    Parses conda environment files to retrieve Open-CE version
    '''
    conda_file = None
    version = "open-ce"
    try:
        with open(conda_env_file, 'r') as conda_file:
            lines = conda_file.readlines()
            for line in lines:
                matched = re.match(r'(#' + OPEN_CE_VERSION_STRING + ':(.*))',
                                   line)
                if matched:
                    version = matched.group(2)
                    break

    except IOError:
        show_warning(Error.CONDA_IO_ERROR, conda_env_file)
    finally:
        if conda_file:
            conda_file.close()
    return version
Esempio n. 6
0
def _validate_config_file(env_file, variants):
    '''Perform some validation on the environment file after loading it.'''
    # pylint: disable=import-outside-toplevel
    from open_ce import conda_utils

    try:
        if utils.is_url(env_file):
            env_file = utils.download_file(env_file)

        # First, partially render yaml to validate builder version number.
        version_check_obj = conda_utils.render_yaml(
            env_file, permit_undefined_jinja=True)
        if Key.builder_version.name in version_check_obj.keys():
            if not conda_utils.version_matches_spec(
                    version_check_obj.get(Key.builder_version.name)):
                raise OpenCEError(
                    Error.SCHEMA_VERSION_MISMATCH, env_file,
                    version_check_obj.get(Key.builder_version.name),
                    open_ce_version)

        meta_obj = None
        try:
            meta_obj = conda_utils.render_yaml(env_file,
                                               variants=variants,
                                               schema=_ENV_CONFIG_SCHEMA)
            if not (Key.packages.name in meta_obj.keys()
                    or Key.imported_envs.name in meta_obj.keys()):
                raise OpenCEError(Error.CONFIG_CONTENT)
            meta_obj[Key.opence_env_file_path.name] = env_file
        except OpenCEError as exc:
            if Key.builder_version.name not in version_check_obj.keys():
                show_warning(Error.SCHEMA_VERSION_NOT_FOUND, env_file,
                             Key.builder_version.name)
            raise exc
        return meta_obj
    except (Exception, SystemExit) as exc:  #pylint: disable=broad-except
        raise OpenCEError(Error.ERROR,
                          "Error in {}:\n  {}".format(env_file,
                                                      str(exc))) from exc
Esempio n. 7
0
    def _get_licenses_from_info_file_helper(self, info):
        if info in self._licenses:
            return None

        source_folder = os.path.join(utils.TMP_LICENSE_DIR,
                                     info.name + "-" + str(info.version))
        if not os.path.exists(source_folder):
            os.makedirs(source_folder)

            # Download the source from each URL
            for url in info.url:
                if url.endswith(".git"):
                    try:
                        utils.git_clone(url, info.version, source_folder)
                    except OpenCEError:
                        show_warning(Error.UNABLE_CLONE_SOURCE, info.name)
                else:
                    try:
                        res = requests.get(url)
                        local_path = os.path.join(source_folder,
                                                  os.path.basename(url))
                        with open(local_path, 'wb') as file_stream:
                            file_stream.write(res.content)
                        _extract(local_path, source_folder)

                    #pylint: disable=broad-except
                    except Exception:
                        show_warning(Error.UNABLE_DOWNLOAD_SOURCE, info.name)

        # Find every license file within the downloaded source
        info.license_files = _find_license_files(source_folder,
                                                 info.license_files)

        # Get copyright information from the downloaded source (unless the copyright string is provided)
        if not info.copyrights:
            info.copyrights = _get_copyrights_from_files(info.license_files)

        return info