xref: /openbmc/openbmc/poky/bitbake/lib/bb/fetch2/osc.py (revision e34f8962)
1#
2# SPDX-License-Identifier: GPL-2.0-only
3#
4"""
5Bitbake "Fetch" implementation for osc (Opensuse build service client).
6Based on the svn "Fetch" implementation.
7
8"""
9
10import logging
11import os
12import  bb
13from    bb.fetch2 import FetchMethod
14from    bb.fetch2 import FetchError
15from    bb.fetch2 import MissingParameterError
16from    bb.fetch2 import runfetchcmd
17
18logger = logging.getLogger(__name__)
19
20class Osc(FetchMethod):
21    """Class to fetch a module or modules from Opensuse build server
22       repositories."""
23
24    def supports(self, ud, d):
25        """
26        Check to see if a given url can be fetched with osc.
27        """
28        return ud.type in ['osc']
29
30    def urldata_init(self, ud, d):
31        if not "module" in ud.parm:
32            raise MissingParameterError('module', ud.url)
33
34        ud.module = ud.parm["module"]
35
36        # Create paths to osc checkouts
37        oscdir = d.getVar("OSCDIR") or (d.getVar("DL_DIR") + "/osc")
38        relpath = self._strip_leading_slashes(ud.path)
39        ud.pkgdir = os.path.join(oscdir, ud.host)
40        ud.moddir = os.path.join(ud.pkgdir, relpath, ud.module)
41
42        if 'rev' in ud.parm:
43            ud.revision = ud.parm['rev']
44        else:
45            pv = d.getVar("PV", False)
46            rev = bb.fetch2.srcrev_internal_helper(ud, d)
47            if rev:
48                ud.revision = rev
49            else:
50                ud.revision = ""
51
52        ud.localfile = d.expand('%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.path.replace('/', '.'), ud.revision))
53
54    def _buildosccommand(self, ud, d, command):
55        """
56        Build up an ocs commandline based on ud
57        command is "fetch", "update", "info"
58        """
59
60        basecmd = d.getVar("FETCHCMD_osc") or "/usr/bin/env osc"
61
62        proto = ud.parm.get('protocol', 'ocs')
63
64        options = []
65
66        config = "-c %s" % self.generate_config(ud, d)
67
68        if ud.revision:
69            options.append("-r %s" % ud.revision)
70
71        coroot = self._strip_leading_slashes(ud.path)
72
73        if command == "fetch":
74            osccmd = "%s %s co %s/%s %s" % (basecmd, config, coroot, ud.module, " ".join(options))
75        elif command == "update":
76            osccmd = "%s %s up %s" % (basecmd, config, " ".join(options))
77        else:
78            raise FetchError("Invalid osc command %s" % command, ud.url)
79
80        return osccmd
81
82    def download(self, ud, d):
83        """
84        Fetch url
85        """
86
87        logger.debug2("Fetch: checking for module directory '" + ud.moddir + "'")
88
89        if os.access(os.path.join(d.getVar('OSCDIR'), ud.path, ud.module), os.R_OK):
90            oscupdatecmd = self._buildosccommand(ud, d, "update")
91            logger.info("Update "+ ud.url)
92            # update sources there
93            logger.debug("Running %s", oscupdatecmd)
94            bb.fetch2.check_network_access(d, oscupdatecmd, ud.url)
95            runfetchcmd(oscupdatecmd, d, workdir=ud.moddir)
96        else:
97            oscfetchcmd = self._buildosccommand(ud, d, "fetch")
98            logger.info("Fetch " + ud.url)
99            # check out sources there
100            bb.utils.mkdirhier(ud.pkgdir)
101            logger.debug("Running %s", oscfetchcmd)
102            bb.fetch2.check_network_access(d, oscfetchcmd, ud.url)
103            runfetchcmd(oscfetchcmd, d, workdir=ud.pkgdir)
104
105        # tar them up to a defined filename
106        runfetchcmd("tar -czf %s %s" % (ud.localpath, ud.module), d,
107                    cleanup=[ud.localpath], workdir=os.path.join(ud.pkgdir + ud.path))
108
109    def supports_srcrev(self):
110        return False
111
112    def generate_config(self, ud, d):
113        """
114        Generate a .oscrc to be used for this run.
115        """
116
117        config_path = os.path.join(d.getVar('OSCDIR'), "oscrc")
118        if (os.path.exists(config_path)):
119            os.remove(config_path)
120
121        f = open(config_path, 'w')
122        f.write("[general]\n")
123        f.write("apisrv = %s\n" % ud.host)
124        f.write("scheme = http\n")
125        f.write("su-wrapper = su -c\n")
126        f.write("build-root = %s\n" % d.getVar('WORKDIR'))
127        f.write("urllist = %s\n" % d.getVar("OSCURLLIST"))
128        f.write("extra-pkgs = gzip\n")
129        f.write("\n")
130        f.write("[%s]\n" % ud.host)
131        f.write("user = %s\n" % ud.parm["user"])
132        f.write("pass = %s\n" % ud.parm["pswd"])
133        f.close()
134
135        return config_path
136