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