Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # ex:ts=4:sw=4:sts=4:et |
| 2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 3 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 4 | BitBake 'Fetch' implementation for perforce |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 5 | |
| 6 | """ |
| 7 | |
| 8 | # Copyright (C) 2003, 2004 Chris Larson |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 9 | # Copyright (C) 2016 Kodak Alaris, Inc. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | # |
| 11 | # This program is free software; you can redistribute it and/or modify |
| 12 | # it under the terms of the GNU General Public License version 2 as |
| 13 | # published by the Free Software Foundation. |
| 14 | # |
| 15 | # This program is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License along |
| 21 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | # |
| 24 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 25 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | import os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | import logging |
| 28 | import bb |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | from bb.fetch2 import FetchMethod |
| 30 | from bb.fetch2 import FetchError |
| 31 | from bb.fetch2 import logger |
| 32 | from bb.fetch2 import runfetchcmd |
| 33 | |
| 34 | class Perforce(FetchMethod): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 35 | """ Class to fetch from perforce repositories """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | def supports(self, ud, d): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 37 | """ Check to see if a given url can be fetched with perforce. """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | return ud.type in ['p4'] |
| 39 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | def urldata_init(self, ud, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 42 | Initialize perforce specific variables within url data. If P4CONFIG is |
| 43 | provided by the env, use it. If P4PORT is specified by the recipe, use |
| 44 | its values, which may override the settings in P4CONFIG. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 46 | ud.basecmd = d.getVar('FETCHCMD_p4') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 47 | if not ud.basecmd: |
| 48 | ud.basecmd = "/usr/bin/env p4" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 50 | ud.dldir = d.getVar('P4DIR') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 51 | if not ud.dldir: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 52 | ud.dldir = '%s/%s' % (d.getVar('DL_DIR'), 'p4') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 54 | path = ud.url.split('://')[1] |
| 55 | path = path.split(';')[0] |
| 56 | delim = path.find('@'); |
| 57 | if delim != -1: |
| 58 | (ud.user, ud.pswd) = path.split('@')[0].split(':') |
| 59 | ud.path = path.split('@')[1] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 61 | ud.path = path |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 63 | ud.usingp4config = False |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 64 | p4port = d.getVar('P4PORT') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 66 | if p4port: |
| 67 | logger.debug(1, 'Using recipe provided P4PORT: %s' % p4port) |
| 68 | ud.host = p4port |
| 69 | else: |
| 70 | logger.debug(1, 'Trying to use P4CONFIG to automatically set P4PORT...') |
| 71 | ud.usingp4config = True |
| 72 | p4cmd = '%s info | grep "Server address"' % ud.basecmd |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 73 | bb.fetch2.check_network_access(d, p4cmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 74 | ud.host = runfetchcmd(p4cmd, d, True) |
| 75 | ud.host = ud.host.split(': ')[1].strip() |
| 76 | logger.debug(1, 'Determined P4PORT to be: %s' % ud.host) |
| 77 | if not ud.host: |
| 78 | raise FetchError('Could not determine P4PORT from P4CONFIG') |
| 79 | |
| 80 | if ud.path.find('/...') >= 0: |
| 81 | ud.pathisdir = True |
| 82 | else: |
| 83 | ud.pathisdir = False |
| 84 | |
| 85 | cleanedpath = ud.path.replace('/...', '').replace('/', '.') |
| 86 | cleanedhost = ud.host.replace(':', '.') |
| 87 | ud.pkgdir = os.path.join(ud.dldir, cleanedhost, cleanedpath) |
| 88 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 89 | ud.setup_revisions(d) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 90 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 91 | ud.localfile = d.expand('%s_%s_%s.tar.gz' % (cleanedhost, cleanedpath, ud.revision)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 92 | |
| 93 | def _buildp4command(self, ud, d, command, depot_filename=None): |
| 94 | """ |
| 95 | Build a p4 commandline. Valid commands are "changes", "print", and |
| 96 | "files". depot_filename is the full path to the file in the depot |
| 97 | including the trailing '#rev' value. |
| 98 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 99 | p4opt = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 101 | if ud.user: |
| 102 | p4opt += ' -u "%s"' % (ud.user) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 104 | if ud.pswd: |
| 105 | p4opt += ' -P "%s"' % (ud.pswd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 107 | if ud.host and not ud.usingp4config: |
| 108 | p4opt += ' -p %s' % (ud.host) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 110 | if hasattr(ud, 'revision') and ud.revision: |
| 111 | pathnrev = '%s@%s' % (ud.path, ud.revision) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 113 | pathnrev = '%s' % (ud.path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 115 | if depot_filename: |
| 116 | if ud.pathisdir: # Remove leading path to obtain filename |
| 117 | filename = depot_filename[len(ud.path)-1:] |
| 118 | else: |
| 119 | filename = depot_filename[depot_filename.rfind('/'):] |
| 120 | filename = filename[:filename.find('#')] # Remove trailing '#rev' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 122 | if command == 'changes': |
| 123 | p4cmd = '%s%s changes -m 1 //%s' % (ud.basecmd, p4opt, pathnrev) |
| 124 | elif command == 'print': |
| 125 | if depot_filename != None: |
| 126 | p4cmd = '%s%s print -o "p4/%s" "%s"' % (ud.basecmd, p4opt, filename, depot_filename) |
| 127 | else: |
| 128 | raise FetchError('No depot file name provided to p4 %s' % command, ud.url) |
| 129 | elif command == 'files': |
| 130 | p4cmd = '%s%s files //%s' % (ud.basecmd, p4opt, pathnrev) |
| 131 | else: |
| 132 | raise FetchError('Invalid p4 command %s' % command, ud.url) |
| 133 | |
| 134 | return p4cmd |
| 135 | |
| 136 | def _p4listfiles(self, ud, d): |
| 137 | """ |
| 138 | Return a list of the file names which are present in the depot using the |
| 139 | 'p4 files' command, including trailing '#rev' file revision indicator |
| 140 | """ |
| 141 | p4cmd = self._buildp4command(ud, d, 'files') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 142 | bb.fetch2.check_network_access(d, p4cmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 143 | p4fileslist = runfetchcmd(p4cmd, d, True) |
| 144 | p4fileslist = [f.rstrip() for f in p4fileslist.splitlines()] |
| 145 | |
| 146 | if not p4fileslist: |
| 147 | raise FetchError('Unable to fetch listing of p4 files from %s@%s' % (ud.host, ud.path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | |
| 149 | count = 0 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 150 | filelist = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 152 | for filename in p4fileslist: |
| 153 | item = filename.split(' - ') |
| 154 | lastaction = item[1].split() |
| 155 | logger.debug(1, 'File: %s Last Action: %s' % (item[0], lastaction[0])) |
| 156 | if lastaction[0] == 'delete': |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 157 | continue |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 158 | filelist.append(item[0]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 159 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 160 | return filelist |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 161 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 162 | def download(self, ud, d): |
| 163 | """ Get the list of files, fetch each one """ |
| 164 | filelist = self._p4listfiles(ud, d) |
| 165 | if not filelist: |
| 166 | raise FetchError('No files found in depot %s@%s' % (ud.host, ud.path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 167 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 168 | bb.utils.remove(ud.pkgdir, True) |
| 169 | bb.utils.mkdirhier(ud.pkgdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 170 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 171 | for afile in filelist: |
| 172 | p4fetchcmd = self._buildp4command(ud, d, 'print', afile) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 173 | bb.fetch2.check_network_access(d, p4fetchcmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 174 | runfetchcmd(p4fetchcmd, d, workdir=ud.pkgdir) |
| 175 | |
| 176 | runfetchcmd('tar -czf %s p4' % (ud.localpath), d, cleanup=[ud.localpath], workdir=ud.pkgdir) |
| 177 | |
| 178 | def clean(self, ud, d): |
| 179 | """ Cleanup p4 specific files and dirs""" |
| 180 | bb.utils.remove(ud.localpath) |
| 181 | bb.utils.remove(ud.pkgdir, True) |
| 182 | |
| 183 | def supports_srcrev(self): |
| 184 | return True |
| 185 | |
| 186 | def _revision_key(self, ud, d, name): |
| 187 | """ Return a unique key for the url """ |
| 188 | return 'p4:%s' % ud.pkgdir |
| 189 | |
| 190 | def _latest_revision(self, ud, d, name): |
| 191 | """ Return the latest upstream scm revision number """ |
| 192 | p4cmd = self._buildp4command(ud, d, "changes") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 193 | bb.fetch2.check_network_access(d, p4cmd, ud.url) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 194 | tip = runfetchcmd(p4cmd, d, True) |
| 195 | |
| 196 | if not tip: |
| 197 | raise FetchError('Could not determine the latest perforce changelist') |
| 198 | |
| 199 | tipcset = tip.split(' ')[1] |
| 200 | logger.debug(1, 'p4 tip found to be changelist %s' % tipcset) |
| 201 | return tipcset |
| 202 | |
| 203 | def sortable_revision(self, ud, d, name): |
| 204 | """ Return a sortable revision number """ |
| 205 | return False, self._build_revision(ud, d) |
| 206 | |
| 207 | def _build_revision(self, ud, d): |
| 208 | return ud.revision |
| 209 | |