Ejemplo n.º 1
0
def collect_layer_data(layer_obj):
    '''Use scancode to collect data from a layer filesystem. This function will
    create a FileData object for every file found. After scanning, it will
    return a list of FileData objects.
    '''
    files = []
    # run scancode against a directory
    command = 'scancode -ilpcu --quiet --timeout 300 --json -'
    full_cmd = get_filesystem_command(layer_obj, command)
    origin_layer = 'Layer: ' + layer_obj.fs_hash[:10]
    result, error = rootfs.shell_command(True, full_cmd)
    if not result:
        logger.error("No scancode results for this layer: %s", str(error))
        layer_obj.origins.add_notice_to_origins(origin_layer,
                                                Notice(str(error), 'error'))
    else:
        # make FileData objects for each result
        data = json.loads(result)
        notice = data.get("headers")[0].get("notice")
        headers = layer_obj.extension_info.get("headers", set())
        headers.add(notice)
        layer_obj.extension_info["headers"] = headers
        for f in data['files']:
            if f['type'] == 'file' and f['size'] != 0:
                files.append(get_scancode_file(f))
    return files
Ejemplo n.º 2
0
def collect_layer_data(layer_obj):
    '''Use scancode to collect data from a layer filesystem. This function will
    create FileData and Package objects for every File and Package found. After
    scanning, it will return a tuple with a list of FileData and a list of
    Package objects.
    '''
    files = []
    packages = []
    # run scancode against a directory
    try:
        processes = len(os.sched_getaffinity(0))
        command = "scancode -ilpcu --quiet --timeout 300 -n {} --json -".format(processes)
    except (AttributeError, NotImplementedError):
        command = "scancode -ilpcu --quiet --timeout 300 --json -"
    full_cmd = get_filesystem_command(layer_obj, command)
    origin_layer = 'Layer {}'.format(layer_obj.layer_index)
    result, error = rootfs.shell_command(True, full_cmd)
    if not result:
        logger.error(
            "No scancode results for this layer: %s", str(error))
        layer_obj.origins.add_notice_to_origins(
            origin_layer, Notice(str(error), 'error'))
    else:
        # make FileData objects for each result
        data = json.loads(result)
        add_scancode_headers(layer_obj, data["headers"])
        for f in data['files']:
            if f['type'] == 'file' and f['size'] != 0:
                files.append(get_scancode_file(f))
                for package in f['packages']:
                    packages.append(get_scancode_package(package))
    return files, packages
Ejemplo n.º 3
0
def collect_layer_data(layer_obj):
    '''Use scancode to collect data from a layer filesystem. This function will
    create a FileData object for every file found. After scanning, it will
    return a list of FileData objects.
    '''
    files = []
    # run scancode against a directory
    command = 'scancode -ilpcu --quiet --json -'
    full_cmd = get_filesystem_command(layer_obj, command)
    origin_layer = 'Layer: ' + layer_obj.fs_hash[:10]
    result, error = rootfs.shell_command(True, full_cmd)
    if not result:
        logger.error("No scancode results for this layer: %s", str(error))
        layer_obj.origins.add_notice_to_origins(origin_layer,
                                                Notice(str(error), 'error'))
    else:
        # make FileData objects for each result
        data = json.loads(result)
        for f in data['files']:
            if f['type'] == 'file':
                # scancode records paths from the target directory onwards
                # which in tern's case is tern.utils.constants.untar_dir
                # removing that portion of the file path
                fspath = f['path'].replace(constants.untar_dir + os.path.sep,
                                           '')
                fd = FileData(f['name'], fspath, f['date'], f['file_type'])
                if f['licenses']:
                    fd.licenses = [l['short_name'] for l in f['licenses']]
                fd.license_expressions = f['license_expressions']
                if f['copyrights']:
                    fd.copyrights = [c['value'] for c in f['copyrights']]
                if f['urls']:
                    fd.urls = [u['url'] for u in f['urls']]
                fd.packages = f['packages']
                fd.authors = [a['value'] for a in f['authors']]
                if f['scan_errors']:
                    # for each scan error make a notice
                    for err in f['scan_errors']:
                        fd.origins.add_notice_to_origins(
                            'File: ' + fd.path, Notice(err, 'error'))
                files.append(fd)
    return files
Ejemplo n.º 4
0
def run_on_image(image_obj, command):
    '''Scancode errors out when it fails to scan any file it is given even
    if it is successful with other files. Hence we cannot use the available
    run_on_image function in the passthrough module. Instead we will check
    if a json object was returned or not'''
    if not command:
        logger.error("No command to execute. No report will be generated")
        return False
    for layer in image_obj.layers:
        layer.files_analyzed = True
        full_cmd = get_filesystem_command(layer, command)
        origin_layer = 'Layer: ' + layer.fs_hash[:10]
        result, error = rootfs.shell_command(True, full_cmd)
        if not result:
            logger.error(
                "No scancode results for this layer: %s", str(error))
            layer.origins.add_notice_to_origins(
                origin_layer, Notice(str(error), 'error'))
        layer.analyzed_output = result.decode()
    return True