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 | # |
| 13 | # This program is free software; you can redistribute it and/or modify |
| 14 | # it under the terms of the GNU General Public License version 2 as |
| 15 | # published by the Free Software Foundation. |
| 16 | # |
| 17 | # This program is distributed in the hope that it will be useful, |
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | # GNU General Public License for more details. |
| 21 | # |
| 22 | # You should have received a copy of the GNU General Public License along |
| 23 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 | |
| 26 | import os |
| 27 | import sys |
| 28 | import logging |
| 29 | import bb |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | from bb.fetch2 import FetchMethod |
| 31 | from bb.fetch2 import FetchError |
| 32 | from bb.fetch2 import runfetchcmd |
| 33 | from bb.fetch2 import logger |
| 34 | |
| 35 | class Bzr(FetchMethod): |
| 36 | def supports(self, ud, d): |
| 37 | return ud.type in ['bzr'] |
| 38 | |
| 39 | def urldata_init(self, ud, d): |
| 40 | """ |
| 41 | init bzr specific variable within url data |
| 42 | """ |
| 43 | # Create paths to bzr checkouts |
| 44 | relpath = self._strip_leading_slashes(ud.path) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 45 | ud.pkgdir = os.path.join(d.expand('${BZRDIR}'), ud.host, relpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 47 | ud.setup_revisions(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | |
| 49 | if not ud.revision: |
| 50 | ud.revision = self.latest_revision(ud, d) |
| 51 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 52 | 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] | 53 | |
| 54 | def _buildbzrcommand(self, ud, d, command): |
| 55 | """ |
| 56 | Build up an bzr commandline based on ud |
| 57 | command is "fetch", "update", "revno" |
| 58 | """ |
| 59 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 60 | basecmd = d.expand('${FETCHCMD_bzr}') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | |
| 62 | proto = ud.parm.get('protocol', 'http') |
| 63 | |
| 64 | bzrroot = ud.host + ud.path |
| 65 | |
| 66 | options = [] |
| 67 | |
| 68 | if command == "revno": |
| 69 | bzrcmd = "%s revno %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot) |
| 70 | else: |
| 71 | if ud.revision: |
| 72 | options.append("-r %s" % ud.revision) |
| 73 | |
| 74 | if command == "fetch": |
| 75 | bzrcmd = "%s branch %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot) |
| 76 | elif command == "update": |
| 77 | bzrcmd = "%s pull %s --overwrite" % (basecmd, " ".join(options)) |
| 78 | else: |
| 79 | raise FetchError("Invalid bzr command %s" % command, ud.url) |
| 80 | |
| 81 | return bzrcmd |
| 82 | |
| 83 | def download(self, ud, d): |
| 84 | """Fetch url""" |
| 85 | |
| 86 | if os.access(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir), '.bzr'), os.R_OK): |
| 87 | bzrcmd = self._buildbzrcommand(ud, d, "update") |
| 88 | logger.debug(1, "BZR Update %s", ud.url) |
| 89 | bb.fetch2.check_network_access(d, bzrcmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 90 | 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] | 91 | else: |
| 92 | bb.utils.remove(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir)), True) |
| 93 | bzrcmd = self._buildbzrcommand(ud, d, "fetch") |
| 94 | bb.fetch2.check_network_access(d, bzrcmd, ud.url) |
| 95 | logger.debug(1, "BZR Checkout %s", ud.url) |
| 96 | bb.utils.mkdirhier(ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | logger.debug(1, "Running %s", bzrcmd) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 98 | runfetchcmd(bzrcmd, d, workdir=ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 99 | |
| 100 | scmdata = ud.parm.get("scmdata", "") |
| 101 | if scmdata == "keep": |
| 102 | tar_flags = "" |
| 103 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 104 | tar_flags = "--exclude='.bzr' --exclude='.bzrtags'" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | |
| 106 | # tar them up to a defined filename |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 107 | runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, os.path.basename(ud.pkgdir)), |
| 108 | d, cleanup=[ud.localpath], workdir=ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | |
| 110 | def supports_srcrev(self): |
| 111 | return True |
| 112 | |
| 113 | def _revision_key(self, ud, d, name): |
| 114 | """ |
| 115 | Return a unique key for the url |
| 116 | """ |
| 117 | return "bzr:" + ud.pkgdir |
| 118 | |
| 119 | def _latest_revision(self, ud, d, name): |
| 120 | """ |
| 121 | Return the latest upstream revision number |
| 122 | """ |
| 123 | logger.debug(2, "BZR fetcher hitting network for %s", ud.url) |
| 124 | |
| 125 | bb.fetch2.check_network_access(d, self._buildbzrcommand(ud, d, "revno"), ud.url) |
| 126 | |
| 127 | output = runfetchcmd(self._buildbzrcommand(ud, d, "revno"), d, True) |
| 128 | |
| 129 | return output.strip() |
| 130 | |
| 131 | def sortable_revision(self, ud, d, name): |
| 132 | """ |
| 133 | Return a sortable revision number which in our case is the revision number |
| 134 | """ |
| 135 | |
| 136 | return False, self._build_revision(ud, d) |
| 137 | |
| 138 | def _build_revision(self, ud, d): |
| 139 | return ud.revision |