xref: /openbmc/openbmc/poky/bitbake/lib/bb/fetch2/crate.py (revision ac13d5f3)
1# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3"""
4BitBake 'Fetch' implementation for crates.io
5"""
6
7# Copyright (C) 2016 Doug Goldstein
8#
9# SPDX-License-Identifier: GPL-2.0-only
10#
11# Based on functions from the base bb module, Copyright 2003 Holger Schurig
12
13import hashlib
14import json
15import os
16import subprocess
17import bb
18from   bb.fetch2 import logger, subprocess_setup, UnpackError
19from   bb.fetch2.wget import Wget
20
21
22class Crate(Wget):
23
24    """Class to fetch crates via wget"""
25
26    def _cargo_bitbake_path(self, rootdir):
27        return os.path.join(rootdir, "cargo_home", "bitbake")
28
29    def supports(self, ud, d):
30        """
31        Check to see if a given url is for this fetcher
32        """
33        return ud.type in ['crate']
34
35    def recommends_checksum(self, urldata):
36        return True
37
38    def urldata_init(self, ud, d):
39        """
40        Sets up to download the respective crate from crates.io
41        """
42
43        if ud.type == 'crate':
44            self._crate_urldata_init(ud, d)
45
46        super(Crate, self).urldata_init(ud, d)
47
48    def _crate_urldata_init(self, ud, d):
49        """
50        Sets up the download for a crate
51        """
52
53        # URL syntax is: crate://NAME/VERSION
54        # break the URL apart by /
55        parts = ud.url.split('/')
56        if len(parts) < 5:
57            raise bb.fetch2.ParameterError("Invalid URL: Must be crate://HOST/NAME/VERSION", ud.url)
58
59        # version is expected to be the last token
60        # but ignore possible url parameters which will be used
61        # by the top fetcher class
62        version = parts[-1].split(";")[0]
63        # second to last field is name
64        name = parts[-2]
65        # host (this is to allow custom crate registries to be specified
66        host = '/'.join(parts[2:-2])
67
68        # if using upstream just fix it up nicely
69        if host == 'crates.io':
70            host = 'crates.io/api/v1/crates'
71
72        ud.url = "https://%s/%s/%s/download" % (host, name, version)
73        ud.parm['downloadfilename'] = "%s-%s.crate" % (name, version)
74        if 'name' not in ud.parm:
75            ud.parm['name'] = '%s-%s' % (name, version)
76
77        logger.debug2("Fetching %s to %s" % (ud.url, ud.parm['downloadfilename']))
78
79    def unpack(self, ud, rootdir, d):
80        """
81        Uses the crate to build the necessary paths for cargo to utilize it
82        """
83        if ud.type == 'crate':
84            return self._crate_unpack(ud, rootdir, d)
85        else:
86            super(Crate, self).unpack(ud, rootdir, d)
87
88    def _crate_unpack(self, ud, rootdir, d):
89        """
90        Unpacks a crate
91        """
92        thefile = ud.localpath
93
94        # possible metadata we need to write out
95        metadata = {}
96
97        # change to the rootdir to unpack but save the old working dir
98        save_cwd = os.getcwd()
99        os.chdir(rootdir)
100
101        bp = d.getVar('BP')
102        if bp == ud.parm.get('name'):
103            cmd = "tar -xz --no-same-owner -f %s" % thefile
104            ud.unpack_tracer.unpack("crate-extract", rootdir)
105        else:
106            cargo_bitbake = self._cargo_bitbake_path(rootdir)
107            ud.unpack_tracer.unpack("cargo-extract", cargo_bitbake)
108
109            cmd = "tar -xz --no-same-owner -f %s -C %s" % (thefile, cargo_bitbake)
110
111            # ensure we've got these paths made
112            bb.utils.mkdirhier(cargo_bitbake)
113
114            # generate metadata necessary
115            with open(thefile, 'rb') as f:
116                # get the SHA256 of the original tarball
117                tarhash = hashlib.sha256(f.read()).hexdigest()
118
119            metadata['files'] = {}
120            metadata['package'] = tarhash
121
122        path = d.getVar('PATH')
123        if path:
124            cmd = "PATH=\"%s\" %s" % (path, cmd)
125        bb.note("Unpacking %s to %s/" % (thefile, os.getcwd()))
126
127        ret = subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True)
128
129        os.chdir(save_cwd)
130
131        if ret != 0:
132            raise UnpackError("Unpack command %s failed with return value %s" % (cmd, ret), ud.url)
133
134        # if we have metadata to write out..
135        if len(metadata) > 0:
136            cratepath = os.path.splitext(os.path.basename(thefile))[0]
137            bbpath = self._cargo_bitbake_path(rootdir)
138            mdfile = '.cargo-checksum.json'
139            mdpath = os.path.join(bbpath, cratepath, mdfile)
140            with open(mdpath, "w") as f:
141                json.dump(metadata, f)
142