xref: /openbmc/openbmc/poky/bitbake/lib/bb/fetch2/bzr.py (revision d1e89497)
1eb8dc403SDave Cobbley"""
2eb8dc403SDave CobbleyBitBake 'Fetch' implementation for bzr.
3eb8dc403SDave Cobbley
4eb8dc403SDave Cobbley"""
5eb8dc403SDave Cobbley
6eb8dc403SDave Cobbley# Copyright (C) 2007 Ross Burton
7eb8dc403SDave Cobbley# Copyright (C) 2007 Richard Purdie
8eb8dc403SDave Cobbley#
9eb8dc403SDave Cobbley#   Classes for obtaining upstream sources for the
10eb8dc403SDave Cobbley#   BitBake build tools.
11eb8dc403SDave Cobbley#   Copyright (C) 2003, 2004  Chris Larson
12eb8dc403SDave Cobbley#
13c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only
14eb8dc403SDave Cobbley#
15eb8dc403SDave Cobbley
16eb8dc403SDave Cobbleyimport os
17eb8dc403SDave Cobbleyimport bb
18eb8dc403SDave Cobbleyfrom bb.fetch2 import FetchMethod
19eb8dc403SDave Cobbleyfrom bb.fetch2 import FetchError
20eb8dc403SDave Cobbleyfrom bb.fetch2 import runfetchcmd
21eb8dc403SDave Cobbleyfrom bb.fetch2 import logger
22eb8dc403SDave Cobbley
23eb8dc403SDave Cobbleyclass Bzr(FetchMethod):
24eb8dc403SDave Cobbley    def supports(self, ud, d):
25eb8dc403SDave Cobbley        return ud.type in ['bzr']
26eb8dc403SDave Cobbley
27eb8dc403SDave Cobbley    def urldata_init(self, ud, d):
28eb8dc403SDave Cobbley        """
29eb8dc403SDave Cobbley        init bzr specific variable within url data
30eb8dc403SDave Cobbley        """
31eb8dc403SDave Cobbley        # Create paths to bzr checkouts
321a4b7ee2SBrad Bishop        bzrdir = d.getVar("BZRDIR") or (d.getVar("DL_DIR") + "/bzr")
33eb8dc403SDave Cobbley        relpath = self._strip_leading_slashes(ud.path)
341a4b7ee2SBrad Bishop        ud.pkgdir = os.path.join(bzrdir, ud.host, relpath)
35eb8dc403SDave Cobbley
36eb8dc403SDave Cobbley        ud.setup_revisions(d)
37eb8dc403SDave Cobbley
38eb8dc403SDave Cobbley        if not ud.revision:
39eb8dc403SDave Cobbley            ud.revision = self.latest_revision(ud, d)
40eb8dc403SDave Cobbley
41eb8dc403SDave Cobbley        ud.localfile = d.expand('bzr_%s_%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.revision))
42eb8dc403SDave Cobbley
43eb8dc403SDave Cobbley    def _buildbzrcommand(self, ud, d, command):
44eb8dc403SDave Cobbley        """
45eb8dc403SDave Cobbley        Build up an bzr commandline based on ud
46eb8dc403SDave Cobbley        command is "fetch", "update", "revno"
47eb8dc403SDave Cobbley        """
48eb8dc403SDave Cobbley
491a4b7ee2SBrad Bishop        basecmd = d.getVar("FETCHCMD_bzr") or "/usr/bin/env bzr"
50eb8dc403SDave Cobbley
51eb8dc403SDave Cobbley        proto =  ud.parm.get('protocol', 'http')
52eb8dc403SDave Cobbley
53eb8dc403SDave Cobbley        bzrroot = ud.host + ud.path
54eb8dc403SDave Cobbley
55eb8dc403SDave Cobbley        options = []
56eb8dc403SDave Cobbley
57eb8dc403SDave Cobbley        if command == "revno":
58eb8dc403SDave Cobbley            bzrcmd = "%s revno %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
59eb8dc403SDave Cobbley        else:
60eb8dc403SDave Cobbley            if ud.revision:
61eb8dc403SDave Cobbley                options.append("-r %s" % ud.revision)
62eb8dc403SDave Cobbley
63eb8dc403SDave Cobbley            if command == "fetch":
64eb8dc403SDave Cobbley                bzrcmd = "%s branch %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
65eb8dc403SDave Cobbley            elif command == "update":
66eb8dc403SDave Cobbley                bzrcmd = "%s pull %s --overwrite" % (basecmd, " ".join(options))
67eb8dc403SDave Cobbley            else:
68eb8dc403SDave Cobbley                raise FetchError("Invalid bzr command %s" % command, ud.url)
69eb8dc403SDave Cobbley
70eb8dc403SDave Cobbley        return bzrcmd
71eb8dc403SDave Cobbley
72eb8dc403SDave Cobbley    def download(self, ud, d):
73eb8dc403SDave Cobbley        """Fetch url"""
74eb8dc403SDave Cobbley
75eb8dc403SDave Cobbley        if os.access(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir), '.bzr'), os.R_OK):
76eb8dc403SDave Cobbley            bzrcmd = self._buildbzrcommand(ud, d, "update")
77*d1e89497SAndrew Geissler            logger.debug("BZR Update %s", ud.url)
78eb8dc403SDave Cobbley            bb.fetch2.check_network_access(d, bzrcmd, ud.url)
79eb8dc403SDave Cobbley            runfetchcmd(bzrcmd, d, workdir=os.path.join(ud.pkgdir, os.path.basename(ud.path)))
80eb8dc403SDave Cobbley        else:
81eb8dc403SDave Cobbley            bb.utils.remove(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir)), True)
82eb8dc403SDave Cobbley            bzrcmd = self._buildbzrcommand(ud, d, "fetch")
83eb8dc403SDave Cobbley            bb.fetch2.check_network_access(d, bzrcmd, ud.url)
84*d1e89497SAndrew Geissler            logger.debug("BZR Checkout %s", ud.url)
85eb8dc403SDave Cobbley            bb.utils.mkdirhier(ud.pkgdir)
86*d1e89497SAndrew Geissler            logger.debug("Running %s", bzrcmd)
87eb8dc403SDave Cobbley            runfetchcmd(bzrcmd, d, workdir=ud.pkgdir)
88eb8dc403SDave Cobbley
89eb8dc403SDave Cobbley        scmdata = ud.parm.get("scmdata", "")
90eb8dc403SDave Cobbley        if scmdata == "keep":
91eb8dc403SDave Cobbley            tar_flags = ""
92eb8dc403SDave Cobbley        else:
93eb8dc403SDave Cobbley            tar_flags = "--exclude='.bzr' --exclude='.bzrtags'"
94eb8dc403SDave Cobbley
95eb8dc403SDave Cobbley        # tar them up to a defined filename
96eb8dc403SDave Cobbley        runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, os.path.basename(ud.pkgdir)),
97eb8dc403SDave Cobbley                    d, cleanup=[ud.localpath], workdir=ud.pkgdir)
98eb8dc403SDave Cobbley
99eb8dc403SDave Cobbley    def supports_srcrev(self):
100eb8dc403SDave Cobbley        return True
101eb8dc403SDave Cobbley
102eb8dc403SDave Cobbley    def _revision_key(self, ud, d, name):
103eb8dc403SDave Cobbley        """
104eb8dc403SDave Cobbley        Return a unique key for the url
105eb8dc403SDave Cobbley        """
106eb8dc403SDave Cobbley        return "bzr:" + ud.pkgdir
107eb8dc403SDave Cobbley
108eb8dc403SDave Cobbley    def _latest_revision(self, ud, d, name):
109eb8dc403SDave Cobbley        """
110eb8dc403SDave Cobbley        Return the latest upstream revision number
111eb8dc403SDave Cobbley        """
112*d1e89497SAndrew Geissler        logger.debug2("BZR fetcher hitting network for %s", ud.url)
113eb8dc403SDave Cobbley
114eb8dc403SDave Cobbley        bb.fetch2.check_network_access(d, self._buildbzrcommand(ud, d, "revno"), ud.url)
115eb8dc403SDave Cobbley
116eb8dc403SDave Cobbley        output = runfetchcmd(self._buildbzrcommand(ud, d, "revno"), d, True)
117eb8dc403SDave Cobbley
118eb8dc403SDave Cobbley        return output.strip()
119eb8dc403SDave Cobbley
120eb8dc403SDave Cobbley    def sortable_revision(self, ud, d, name):
121eb8dc403SDave Cobbley        """
122eb8dc403SDave Cobbley        Return a sortable revision number which in our case is the revision number
123eb8dc403SDave Cobbley        """
124eb8dc403SDave Cobbley
125eb8dc403SDave Cobbley        return False, self._build_revision(ud, d)
126eb8dc403SDave Cobbley
127eb8dc403SDave Cobbley    def _build_revision(self, ud, d):
128eb8dc403SDave Cobbley        return ud.revision
129