|
31 | 31 | from scanpipe import pipes |
32 | 32 | from scanpipe.models import CodebaseResource |
33 | 33 | from scanpipe.pipes import rootfs |
| 34 | +from scanpipe.pipes import scancode |
34 | 35 |
|
35 | 36 | logger = logging.getLogger(__name__) |
36 | 37 |
|
37 | 38 |
|
38 | | -def get_images_from_extracted_codebase(project): |
| 39 | +def extract_images_from_inputs(project): |
39 | 40 | """ |
40 | | - Yield images collected from the project "codebase" directory. The |
41 | | - image tarball should be extracted there first, with its manifest.json at the |
42 | | - root. |
| 41 | + Collect all the tarballs from the `project` input/ work directory, extract each |
| 42 | + tarball to the tmp/ work directory and collect the images. |
| 43 | + Return the `images` and `errors` that may have happen during the extraction. |
43 | 44 | """ |
44 | | - codebase = project.codebase_path.absolute() |
45 | | - for image in Image.get_images_from_dir(location=codebase): |
46 | | - yield image |
| 45 | + target_path = project.tmp_path |
| 46 | + images = [] |
| 47 | + errors = [] |
47 | 48 |
|
| 49 | + for input_tarball in project.inputs(pattern="*.tar*"): |
| 50 | + extract_target = target_path / f"{input_tarball.name}-extract" |
| 51 | + extract_errors = scancode.extract(input_tarball, extract_target) |
| 52 | + images.extend(Image.get_images_from_dir(extract_target)) |
| 53 | + errors.extend(extract_errors) |
48 | 54 |
|
49 | | -def get_and_extract_images_from_image_tarballs(project, force_extract=False): |
50 | | - """ |
51 | | - Yield images collected from the project "codebase" directory. |
52 | | - The process is to: |
53 | | - 1. collect all the tarballs from this directory. |
54 | | - 2. for each, extract that under a -extract directory and collect its images. |
| 55 | + return images, errors |
55 | 56 |
|
56 | | - If `force_extract` is False, do not extract if already extracted. |
57 | | - """ |
58 | | - all_images = [] |
59 | 57 |
|
60 | | - for tarball in project.inputs(pattern="*.tar*"): |
61 | | - tarball_location = str(tarball.absolute()) |
62 | | - if tarball_location.endswith("-extract"): |
63 | | - continue |
64 | | - extracted_location = tarball_location + "-extract" |
65 | | - tarball_images = Image.get_images_from_tarball( |
66 | | - location=tarball_location, |
67 | | - target_dir=extracted_location, |
68 | | - force_extract=force_extract, |
69 | | - ) |
| 58 | +def extract_layers_from_images(project, images): |
| 59 | + """ |
| 60 | + Extract all the layers from provided `images` into the `project` codebase/ work |
| 61 | + directory. |
| 62 | + Return the `errors` that may have happen during the extraction. |
| 63 | + """ |
| 64 | + errors = [] |
70 | 65 |
|
71 | | - for image in tarball_images: |
72 | | - logger.info("Collected Docker image: {}".format(image.image_id)) |
73 | | - all_images.append(image) |
| 66 | + for image in images: |
| 67 | + image_dirname = Path(image.base_location).name |
| 68 | + target_path = project.codebase_path / image_dirname |
74 | 69 |
|
75 | | - return all_images |
| 70 | + for layer in image.layers: |
| 71 | + extract_target = target_path / layer.layer_id |
| 72 | + extract_errors = scancode.extract(layer.layer_location, extract_target) |
| 73 | + errors.extend(extract_errors) |
| 74 | + layer.extracted_to_location = str(extract_target) |
76 | 75 |
|
77 | 76 |
|
78 | 77 | def get_image_data(image): |
|
0 commit comments