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