summaryrefslogtreecommitdiff
path: root/scripts/find-artifacts
blob: d6c978f79dcca59bbdef2b407aa05a45e6db276d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
#
# Copyright (C) 2011-2012  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# This is a program to read the metadata of a baserock image to produce a list
# of all the chunks needed to build said image.

import json
import os
import tempfile
import gzip
import sys
import glob

import cliapp

path = os.path.dirname(os.path.dirname(__file__))
sys.path.append(path)
import morphlib

class FindArtifacts(morphlib.app.Morph):

    def process_args(self, args):
        artifacts_dir = os.path.join(self.settings['cachedir'], 'artifacts')

        # args[0] is the path to the built image.
        artifact = args[0]

        def find_artifacts(dirname):
            metadirs = [
                os.path.join(dirname, 'factory', 'baserock'),
                os.path.join(dirname, 'baserock')
            ]
            existing_metadirs = [x for x in metadirs if os.path.isdir(x)]
            if not existing_metadirs:
                raise NotASystemArtifactError(artifact)
            metadir = existing_metadirs[0]
            for basename in glob.glob(os.path.join(metadir, '*.meta')):
                metafile = os.path.join(metadir, basename)
                metadata = json.load(open(metafile))
                cache_key = metadata['cache-key']
                artifact_glob = os.path.join(artifacts_dir, cache_key) + '*'
                found_artifacts = glob.glob(artifact_glob)
                if not found_artifacts:
                    raise cliapp.AppException('Could not find cache-key '
                                              + cache_key + ' for artifact '
                                              + metajson['artifact-name'])
                for cached_file in found_artifacts:
                    self.output.write(cached_file + "\n")

        morphlib.bins.call_in_artifact_directory(self, artifact, find_artifacts)

FindArtifacts().run()