From fb231cd3844e62299ea53cad6e9cf73873cc4037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0abata?= Date: Sat, 9 Jul 2016 18:18:50 +0200 Subject: [PATCH] SCM cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removing the Fedora-centric functions; we won't need those here. Also dropping the GIT+SSH scheme; rida won't have SSH access to pretty much anywhere. Also removing the is_scm_url() method; we already perform similar check in __init__. Removing the koji copyright notice; all of the code is our own now. I've also changed to documentation so that apidoc can parse it. Signed-off-by: Petr Šabata --- rida/scm.py | 185 ++++++++++------------------------------------------ 1 file changed, 33 insertions(+), 152 deletions(-) diff --git a/rida/scm.py b/rida/scm.py index 04f02237..cc40c76d 100644 --- a/rida/scm.py +++ b/rida/scm.py @@ -21,30 +21,9 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # -# Some functions kindly copied and then heavily modified from the koji sources: koji/daemon.py -# -# Copyright (c) 2010-2016 Red Hat, Inc. -# -# This is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; -# version 2.1 of the License. -# -# This software is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this software; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -# Original Authors of the koji source: -# Mike McLean -# Mike Bonnet # Modified by: -# Karsten Hopp -# Petr Šabata +# Written by Karsten Hopp +# Petr Šabata """SCM handler functions.""" @@ -60,35 +39,27 @@ import tempfile class SCM(object): "SCM abstraction class" - types = {'GIT': ('git://', 'git+http://', 'git+https://', 'git+rsync://'), - 'GIT+SSH': ('git+ssh://',)} - - @staticmethod - def is_scm_url(url): - """ - Return True if the url appears to be a valid, accessible source location, False otherwise - """ - for schemes in SCM.types.values(): - for scheme in schemes: - if url.startswith(scheme): - return True - else: - return False + # Assuming git for HTTP schemas + types = { + "git": ("git://", "git+http://", "git+https://", + "git+rsync://", "http://", "https://") + } def __init__(self, url, allowed_scm=None): - """ - Initialize the SCM object using the specified url. + """Initialize the SCM object using the specified scmurl. + If url is not in the list of allowed_scm, an error will be raised. NOTE: only git URLs in the following formats are supported atm: git:// git+http:// git+https:// git+rsync:// - git+ssh:// + http:// + https:// - The initialized SCM object will have the following attributes: - - url (the unmodified url) - - allowed_scm (the list of allowed scm, optional) + :param str url: The unmodified scmurl + :param list allowed_scm: The list of allowed SCMs, optional + :raises: RuntimeError """ if allowed_scm: @@ -98,9 +69,6 @@ class SCM(object): else: raise RuntimeError('%s is not in the list of allowed SCMs' % url) - if not SCM.is_scm_url(url): - raise RuntimeError('Invalid SCM URL: %s' % url) - self.url = url for scmtype, schemes in SCM.types.items(): @@ -108,10 +76,9 @@ class SCM(object): self.scheme = scmtype break else: - # should never happen raise RuntimeError('Invalid SCM URL: %s' % url) - if self.scheme.startswith("GIT"): + if self.scheme == "git": match = re.search(r"^(?P.*/(?P[^?]*))(\?#(?P.*))?", url) self.repository = match.group("repository") self.name = match.group("name") @@ -150,14 +117,14 @@ class SCM(object): return status[1] def checkout(self, scmdir): - """ - Checkout the module from SCM. Accepts the following parameters: - - scmdir: the working directory + """Checkout the module from SCM. - Returns the directory that the module was checked-out into (a subdirectory of scmdir) + :param str scmdir: The working directory + :returns: str -- the directory that the module was checked-out into + :raises: RuntimeError """ # TODO: sanity check arguments - if self.scheme.startswith("GIT"): + if self.scheme == "git": sourcedir = '%s/%s' % (scmdir, self.name) module_clone_cmd = ['git', 'clone', '-q'] @@ -179,17 +146,19 @@ class SCM(object): return sourcedir def get_latest(self): - """Returns the latest commit ID, for example the git master HEAD.""" - if self.scheme.startswith("GIT"): + """Get the latest commit ID. + + :returns: str -- the latest commit ID, e.g. the git master HEAD + :raises: RuntimeError + """ + if self.scheme == "git": (status , output) = subprocess.getstatusoutput("git ls-remote %s" % self.repository) if status != 0: raise RuntimeError("Cannot get git hash of master HEAD in %s" % self.repository) for line in output.split(os.linesep): - # FIXME: Be more precise here, we don't want - # refs/heads/masterfoo, for example... - if 'refs/heads/master' in line: + if line.endswith("\trefs/heads/master"): return line.split("\t")[0] raise RuntimeError("Couldn't determine the git master HEAD hash in %s" % self.repository) @@ -197,10 +166,12 @@ class SCM(object): raise RuntimeError("get_latest: Unhandled SCM scheme.") def is_available(self): - """Returns whether the scmurl is available for checkout.""" - # XXX: For pagure.io/github.com hacks we need to map http/https repos - # to the git scheme. Also, the request path needs to be constructed - # from self.repository to work for forks. + """Check whether the scmurl is available for checkout. + + :returns: bool -- the scmurl is available for checkout + """ + # XXX: If implementing special hacks for pagure.io or github.com, don't + # forget about possible forks -- start with self.repository. if self.repository.startswith("-git://pkgs.fedoraproject.org/"): hc = http.client.HTTPConnection("pkgs.fedoraproject.org") hc.request("HEAD", @@ -261,93 +232,3 @@ class SCM(object): @name.setter def name(self, s): self._name = str(s) - -def get_fedpkg_url_git_master_head_pkgname(pkgname=None): - """ - Return the complete git URL to master HEAD of the given package. - Accepts the following parameters: - - pkgname: the package name - """ - pkghash = get_hash_of_git_master_head_pkgname(pkgname) - if pkghash is not '': - return 'git://pkgs.fedoraproject.org/rpms/' + pkgname + '?#' + pkghash - else: - return '' - -def get_hash_of_git_master_head_pkgname(pkgname=None): - """ - Return the git hash of master HEAD - Accepts the following parameters: - - pkgname: the package name - """ - if not isinstance(pkgname, str): - raise RuntimeError('pkgname needs to be a string') - gitrepo = 'git://pkgs.fedoraproject.org/rpms/' + pkgname - (status , output) = subprocess.getstatusoutput('git ls-remote %s' % gitrepo) - if status != 0: - raise RuntimeError('can\'t get git hash of master HEAD in %s' % gitrepo) - b = output.split(os.linesep) - ret = '' - for line in b: - if 'refs/heads/master' in line: - ret = line.split('\t')[0] - break - return ret - -def check_giturl_syntax(giturl=None): - """ - dist-pkg giturls are of the form - git://pkgs.fedoraproject.org/rpms/ed?#abc0235d4923930745ef05d873646f361a365457 - Returns True if giturl has this format, False otherwise. - """ - if not isinstance(giturl, str): - return False - if giturl[:6] != 'git://': - return False - if giturl[6:34] != 'pkgs.fedoraproject.org/rpms/' and giturl[6:38] != 'pkgs.stg.fedoraproject.org/rpms/': - return False - if not '?#' in giturl.split('/')[-1]: - return False - return True - -def convert_giturl_to_cgiturl(giturl=None): - """ - dist-pkg giturls are of the form - git://pkgs.fedoraproject.org/rpms/ed?#abc0235d4923930745ef05d873646f361a365457 - cgit urls look like this: - http://pkgs.fedoraproject.org/cgit/rpms/ed.git/commit/?id=abc0235d4923930745ef05d873646f361a365457 - This function takes a string with a dist-git url as parameter and returns a cgit url - Accepts the following parameters: - - giturl - dist-git url ('fedpkg giturl') - """ - try: - url = giturl[giturl.index('://')+3:] - except: - raise RuntimeError('%s is not a dist-git URL' % giturl) - url = url.replace('/rpms/','/cgit/rpms/') - url = url.replace('?#','.git/commit/?id=') - return 'http://' + url - -def check_if_remote_gitcommit_exists(giturl=None): - """ - Instead of checking out a git repo and then looking through all the - git hashes, this function uses http to connect to cgit and checks - for availability of p.e. - http://pkgs.fedoraproject.org/cgit/rpms/ed.git/commit/?id=abc0235d4923930745ef05d873646f361a365457 - Accepts the following parameters: - - giturl - dist-git url ('fedpkg giturl') - """ - if not check_giturl_syntax(giturl): - return False - import http.client - import os - cgiturl = convert_giturl_to_cgiturl(giturl) - urlpath = cgiturl[cgiturl.index('://')+3:] - urlpath = urlpath[urlpath.index('/'):] - http_obj = http.client.HTTPConnection('pkgs.fedoraproject.org') - http_obj.request('HEAD',urlpath) - res = http_obj.getresponse() - if res.status == 200: - return True - else: - return False