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