Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | """ |
| 2 | BitBake 'Fetch' implementation for svn. |
| 3 | |
| 4 | """ |
| 5 | |
| 6 | # Copyright (C) 2003, 2004 Chris Larson |
| 7 | # Copyright (C) 2004 Marcin Juszkiewicz |
| 8 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 9 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | # |
| 11 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 12 | |
| 13 | import os |
| 14 | import sys |
| 15 | import logging |
| 16 | import bb |
| 17 | import re |
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 MissingParameterError |
| 21 | from bb.fetch2 import runfetchcmd |
| 22 | from bb.fetch2 import logger |
| 23 | |
| 24 | class Svn(FetchMethod): |
| 25 | """Class to fetch a module or modules from svn repositories""" |
| 26 | def supports(self, ud, d): |
| 27 | """ |
| 28 | Check to see if a given url can be fetched with svn. |
| 29 | """ |
| 30 | return ud.type in ['svn'] |
| 31 | |
| 32 | def urldata_init(self, ud, d): |
| 33 | """ |
| 34 | init svn specific variable within url data |
| 35 | """ |
| 36 | if not "module" in ud.parm: |
| 37 | raise MissingParameterError('module', ud.url) |
| 38 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 39 | ud.basecmd = d.getVar("FETCHCMD_svn") or "/usr/bin/env svn --non-interactive --trust-server-cert" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | |
| 41 | ud.module = ud.parm["module"] |
| 42 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 43 | if not "path_spec" in ud.parm: |
| 44 | ud.path_spec = ud.module |
| 45 | else: |
| 46 | ud.path_spec = ud.parm["path_spec"] |
| 47 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | # Create paths to svn checkouts |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 49 | svndir = d.getVar("SVNDIR") or (d.getVar("DL_DIR") + "/svn") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | relpath = self._strip_leading_slashes(ud.path) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 51 | ud.pkgdir = os.path.join(svndir, ud.host, relpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | ud.moddir = os.path.join(ud.pkgdir, ud.module) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 53 | # Protects the repository from concurrent updates, e.g. from two |
| 54 | # recipes fetching different revisions at the same time |
| 55 | ud.svnlock = os.path.join(ud.pkgdir, "svn.lock") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 57 | ud.setup_revisions(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | |
| 59 | if 'rev' in ud.parm: |
| 60 | ud.revision = ud.parm['rev'] |
| 61 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 62 | ud.localfile = d.expand('%s_%s_%s_%s_.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | |
| 64 | def _buildsvncommand(self, ud, d, command): |
| 65 | """ |
| 66 | Build up an svn commandline based on ud |
| 67 | command is "fetch", "update", "info" |
| 68 | """ |
| 69 | |
| 70 | proto = ud.parm.get('protocol', 'svn') |
| 71 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 72 | svn_ssh = None |
| 73 | if proto == "svn+ssh" and "ssh" in ud.parm: |
| 74 | svn_ssh = ud.parm["ssh"] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | |
| 76 | svnroot = ud.host + ud.path |
| 77 | |
| 78 | options = [] |
| 79 | |
| 80 | options.append("--no-auth-cache") |
| 81 | |
| 82 | if ud.user: |
| 83 | options.append("--username %s" % ud.user) |
| 84 | |
| 85 | if ud.pswd: |
| 86 | options.append("--password %s" % ud.pswd) |
| 87 | |
| 88 | if command == "info": |
| 89 | svncmd = "%s info %s %s://%s/%s/" % (ud.basecmd, " ".join(options), proto, svnroot, ud.module) |
| 90 | elif command == "log1": |
| 91 | svncmd = "%s log --limit 1 %s %s://%s/%s/" % (ud.basecmd, " ".join(options), proto, svnroot, ud.module) |
| 92 | else: |
| 93 | suffix = "" |
| 94 | if ud.revision: |
| 95 | options.append("-r %s" % ud.revision) |
| 96 | suffix = "@%s" % (ud.revision) |
| 97 | |
| 98 | if command == "fetch": |
| 99 | transportuser = ud.parm.get("transportuser", "") |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 100 | svncmd = "%s co %s %s://%s%s/%s%s %s" % (ud.basecmd, " ".join(options), proto, transportuser, svnroot, ud.module, suffix, ud.path_spec) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | elif command == "update": |
| 102 | svncmd = "%s update %s" % (ud.basecmd, " ".join(options)) |
| 103 | else: |
| 104 | raise FetchError("Invalid svn command %s" % command, ud.url) |
| 105 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 106 | if svn_ssh: |
| 107 | svncmd = "SVN_SSH=\"%s\" %s" % (svn_ssh, svncmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | |
| 109 | return svncmd |
| 110 | |
| 111 | def download(self, ud, d): |
| 112 | """Fetch url""" |
| 113 | |
| 114 | logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'") |
| 115 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 116 | lf = bb.utils.lockfile(ud.svnlock) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 118 | try: |
| 119 | if os.access(os.path.join(ud.moddir, '.svn'), os.R_OK): |
| 120 | svnupdatecmd = self._buildsvncommand(ud, d, "update") |
| 121 | logger.info("Update " + ud.url) |
| 122 | # We need to attempt to run svn upgrade first in case its an older working format |
| 123 | try: |
| 124 | runfetchcmd(ud.basecmd + " upgrade", d, workdir=ud.moddir) |
| 125 | except FetchError: |
| 126 | pass |
| 127 | logger.debug(1, "Running %s", svnupdatecmd) |
| 128 | bb.fetch2.check_network_access(d, svnupdatecmd, ud.url) |
| 129 | runfetchcmd(svnupdatecmd, d, workdir=ud.moddir) |
| 130 | else: |
| 131 | svnfetchcmd = self._buildsvncommand(ud, d, "fetch") |
| 132 | logger.info("Fetch " + ud.url) |
| 133 | # check out sources there |
| 134 | bb.utils.mkdirhier(ud.pkgdir) |
| 135 | logger.debug(1, "Running %s", svnfetchcmd) |
| 136 | bb.fetch2.check_network_access(d, svnfetchcmd, ud.url) |
| 137 | runfetchcmd(svnfetchcmd, d, workdir=ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 139 | scmdata = ud.parm.get("scmdata", "") |
| 140 | if scmdata == "keep": |
| 141 | tar_flags = "" |
| 142 | else: |
| 143 | tar_flags = "--exclude='.svn'" |
| 144 | |
| 145 | # tar them up to a defined filename |
| 146 | runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, ud.path_spec), d, |
| 147 | cleanup=[ud.localpath], workdir=ud.pkgdir) |
| 148 | finally: |
| 149 | bb.utils.unlockfile(lf) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 150 | |
| 151 | def clean(self, ud, d): |
| 152 | """ Clean SVN specific files and dirs """ |
| 153 | |
| 154 | bb.utils.remove(ud.localpath) |
| 155 | bb.utils.remove(ud.moddir, True) |
| 156 | |
| 157 | |
| 158 | def supports_srcrev(self): |
| 159 | return True |
| 160 | |
| 161 | def _revision_key(self, ud, d, name): |
| 162 | """ |
| 163 | Return a unique key for the url |
| 164 | """ |
| 165 | return "svn:" + ud.moddir |
| 166 | |
| 167 | def _latest_revision(self, ud, d, name): |
| 168 | """ |
| 169 | Return the latest upstream revision number |
| 170 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 171 | bb.fetch2.check_network_access(d, self._buildsvncommand(ud, d, "log1"), ud.url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 172 | |
| 173 | output = runfetchcmd("LANG=C LC_ALL=C " + self._buildsvncommand(ud, d, "log1"), d, True) |
| 174 | |
| 175 | # skip the first line, as per output of svn log |
| 176 | # then we expect the revision on the 2nd line |
| 177 | revision = re.search('^r([0-9]*)', output.splitlines()[1]).group(1) |
| 178 | |
| 179 | return revision |
| 180 | |
| 181 | def sortable_revision(self, ud, d, name): |
| 182 | """ |
| 183 | Return a sortable revision number which in our case is the revision number |
| 184 | """ |
| 185 | |
| 186 | return False, self._build_revision(ud, d) |
| 187 | |
| 188 | def _build_revision(self, ud, d): |
| 189 | return ud.revision |