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