예제 #1
0
파일: common.py 프로젝트: SPriyal/tern
def add_base_packages(image_layer, binary, shell):  # pylint: disable=too-many-locals
    '''Given the image layer, the binary to invoke and shell:
        1. get the listing from the base.yml
        2. Invoke any commands against the base layer
        3. Make a list of packages and add them to the layer'''
    origin_layer = 'Layer: ' + image_layer.fs_hash[:10]
    if image_layer.created_by:
        image_layer.origins.add_notice_to_origins(
            origin_layer,
            Notice(
                formats.layer_created_by.format(
                    created_by=image_layer.created_by), 'info'))
    else:
        image_layer.origins.add_notice_to_origins(
            origin_layer, Notice(formats.no_created_by, 'warning'))
    origin_command_lib = formats.invoking_base_commands
    # find the binary
    listing = command_lib.get_base_listing(binary)
    if listing:
        # put info notice about what is going to be invoked
        snippet_msg = formats.invoke_for_base + '\n' + \
            content.print_base_invoke(binary)
        image_layer.origins.add_notice_to_origins(origin_layer,
                                                  Notice(snippet_msg, 'info'))
        shell, _ = command_lib.get_image_shell(listing)
        if not shell:
            shell = constants.shell
        # get all the packages in the base layer
        names, n_msg = command_lib.get_pkg_attr_list(shell, listing['names'])
        versions, v_msg = command_lib.get_pkg_attr_list(
            shell, listing['versions'])
        licenses, l_msg = command_lib.get_pkg_attr_list(
            shell, listing['licenses'])
        src_urls, u_msg = command_lib.get_pkg_attr_list(
            shell, listing['src_urls'])
        # add a notice to the image if something went wrong
        invoke_msg = n_msg + v_msg + l_msg + u_msg
        if invoke_msg:
            image_layer.origins.add_notice_to_origins(
                origin_layer, Notice(invoke_msg, 'error'))
        if names and len(names) > 1:
            for index, name in enumerate(names):
                pkg = Package(name)
                if len(versions) == len(names):
                    pkg.version = versions[index]
                if len(licenses) == len(names):
                    pkg.license = licenses[index]
                if len(src_urls) == len(names):
                    pkg.src_url = src_urls[index]
                image_layer.add_package(pkg)
    # if there is no listing add a notice
    else:
        image_layer.origins.add_notice_to_origins(
            origin_command_lib,
            Notice(errors.no_listing_for_base_key.format(listing_key=binary),
                   'error'))
예제 #2
0
파일: common.py 프로젝트: gahlberg/tern
def fill_package_metadata(pkg_obj, pkg_listing, shell):
    '''Given a Package object and the Package listing from the command
    library, fill in the attribute value returned from looking up the
    data and methods of the package listing.
    Fill out: version, license and proj_url
    If there are errors, fill out notices'''
    # create a NoticeOrigin for the package
    origin_str = 'command_lib/snippets.yml'
    # version
    version_listing, listing_msg = command_lib.check_library_key(
        pkg_listing, 'version')
    if version_listing:
        version_list, invoke_msg = command_lib.get_pkg_attr_list(
            shell, version_listing, package_name=pkg_obj.name)
        if version_list:
            pkg_obj.version = version_list[0]
        else:
            pkg_obj.origins.add_notice_to_origins(origin_str,
                                                  Notice(invoke_msg, 'error'))
    else:
        pkg_obj.origins.add_notice_to_origins(origin_str,
                                              Notice(listing_msg, 'warning'))
    # license
    license_listing, listing_msg = command_lib.check_library_key(
        pkg_listing, 'license')
    if license_listing:
        license_list, invoke_msg = command_lib.get_pkg_attr_list(
            shell, license_listing, package_name=pkg_obj.name)
        if license_list:
            pkg_obj.license = license_list[0]
        else:
            pkg_obj.origins.add_notice_to_origins(origin_str,
                                                  Notice(invoke_msg, 'error'))
    else:
        pkg_obj.origins.add_notice_to_origins(origin_str,
                                              Notice(listing_msg, 'warning'))
    # proj_urls
    url_listing, listing_msg = command_lib.check_library_key(
        pkg_listing, 'proj_url')
    if url_listing:
        url_list, invoke_msg = command_lib.get_pkg_attr_list(
            shell, url_listing, package_name=pkg_obj.name)
        if url_list:
            pkg_obj.proj_url = url_list[0]
        else:
            pkg_obj.origins.add_notice_to_origins(origin_str,
                                                  Notice(invoke_msg, 'error'))
    else:
        pkg_obj.origins.add_notice_to_origins(origin_str,
                                              Notice(listing_msg, 'warning'))
예제 #3
0
파일: common.py 프로젝트: TTMaZa/tern
def collate_list_metadata(shell, listing, work_dir, envs):
    '''Given the shell and the listing for the package manager, collect
    metadata that gets returned as a list'''
    pkg_dict = {}
    msgs = ''
    warnings = ''
    if not shell:
        msgs = "Cannot invoke commands without a shell\n"
        return pkg_dict, msgs, warnings
    for item in command_lib.base_keys:
        if item in listing.keys():
            items, msg = command_lib.get_pkg_attr_list(shell, listing[item],
                                                       work_dir, envs)
            msgs = msgs + msg
            if item == 'files':
                # convert this data into a list before adding it to the
                # package dictionary
                file_list = []
                for files_str in items:
                    # convert the string into a list
                    files = []
                    for filepath in filter(bool, files_str.split('\n')):
                        files.append(filepath.lstrip('/'))
                    file_list.append(files)
                pkg_dict.update({item: file_list})
            else:
                pkg_dict.update({item: items})
        else:
            warnings = warnings + errors.no_listing_for_base_key.format(
                listing_key=item)
    return pkg_dict, msgs, warnings
예제 #4
0
파일: common.py 프로젝트: gahlberg/tern
def get_package_dependencies(package_listing, package_name, shell):
    '''The package listing is the result of looking up the command name in the
    command library. Given this listing, the package name and the shell
    return a list of package dependency names'''
    deps_listing, deps_msg = command_lib.check_library_key(
        package_listing, 'deps')
    if deps_listing:
        deps_list, invoke_msg = command_lib.get_pkg_attr_list(
            shell, deps_listing, package_name=package_name)
        if deps_list:
            return list(set(deps_list)), ''
        return [], invoke_msg
    return [], deps_msg
예제 #5
0
파일: common.py 프로젝트: brphelps/tern
def collate_list_metadata(shell, listing):
    '''Given the shell and the listing for the package manager, collect
    metadata that gets returned as a list'''
    pkg_dict = {}
    msgs = ''
    warnings = ''
    for item in command_lib.base_keys:
        if item in listing.keys():
            items, msg = command_lib.get_pkg_attr_list(shell, listing[item])
            msgs = msgs + msg
            pkg_dict.update({item: items})
        else:
            warnings = warnings + errors.no_listing_for_base_key.format(
                listing_key=item)
    return pkg_dict, msgs, warnings
예제 #6
0
def collate_list_metadata(shell, listing, work_dir, envs):
    '''Given the shell and the listing for the package manager, collect
    metadata that gets returned as a list'''
    pkg_dict = {}
    msgs = ''
    warnings = ''
    if not shell:
        msgs = "Cannot invoke commands without a shell\n"
        return pkg_dict, msgs, warnings
    for item in command_lib.base_keys:
        if item in listing.keys():
            items, msg = command_lib.get_pkg_attr_list(shell, listing[item],
                                                       work_dir, envs)
            msgs = msgs + msg
            pkg_dict.update({item: items})
        else:
            warnings = warnings + errors.no_listing_for_base_key.format(
                listing_key=item)
    return pkg_dict, msgs, warnings
예제 #7
0
        rootfs.prep_rootfs(target)
        # invoke commands in chroot
        # if we're looking up the snippets library
        # we should see 'snippets' in the keys
        if 'snippets' in args.keys and 'packages' in args.keys:
            # get the package info that corresponds to the package name
            # or get the default
            last = args.keys.pop()
            info_list = look_up_lib(args.keys)
            info_dict = command_lib.check_for_unique_package(
                info_list, args.package)[last]
        else:
            info_dict = look_up_lib(args.keys)
        # try to invoke the commands
        try:
            result = command_lib.get_pkg_attr_list(args.shell, info_dict,
                                                   args.package)
            print('Output list: ' + ' '.join(result[0]))
            print('Error messages: ' + result[1])
            print('Number of elements: ' + str(len(result[0])))
        except subprocess.CalledProcessError as error:
            print(error.output)
        # undo the mounts
        rootfs.undo_mount()
        rootfs.unmount_rootfs()
    else:
        print("Something when wrong in loading the image")
    report.teardown()
    report.clean_image_tars(image_obj)
    report.clean_working_dir()