#25 dist-git lookaside
Merged 3 years ago by carlwgeorge. Opened 3 years ago by lrossett.
centos/ lrossett/centpkg fork-url  into  develop

file modified
+1
@@ -7,3 +7,4 @@ 

  .pytest_cache

  test.conf

  .venv/

+ venv/ 

\ No newline at end of file

file modified
+144 -7
@@ -19,18 +19,153 @@ 

  import six

  import sys

  

- 

- from pyrpkg.errors import InvalidHashType, UploadError

+ from pyrpkg.errors import InvalidHashType, UploadError, LayoutError

  from pyrpkg.lookaside import CGILookasideCache

+ from pyrpkg.layout.layouts import DistGitLayout

+ 

+ from . import utils

+ 

+ 

+ def is_dist_git(folder):

+     """

+     Indicates if a folder is using a dist-git layout.

+ 

+     Parameters

+     ----------

+     folder: str

+         The directory to inspect.

+ 

+     Returns

+     -------

+     bool

+         A bool flag indicating if `folder` is using

+         a dist-git layout format.

+     """

+     result = False

+ 

+     try:

+         DistGitLayout.from_path(folder)

+         result = True

+     except LayoutError:

+         result = False

+     finally:

+         return result

  

  

  class StreamLookasideCache(CGILookasideCache):

-     def __init__(self, hashtype, download_url, upload_url):

+     """

+     CentosStream lookaside specialized class.

+ 

+     It inherits most of its behavior from `pyrpkg.lookasideCGILookasideCache`.

+     """

+ 

+     def __init__(self, hashtype, download_url, upload_url, client_cert=None, ca_cert=None):

          super(StreamLookasideCache, self).__init__(

-             hashtype, download_url, upload_url)

+             hashtype, download_url, upload_url,

+             client_cert=client_cert, ca_cert=ca_cert)

  

-         self.download_path = (

-             '%(name)s/%(filename)s/%(hashtype)s/%(hash)s/%(filename)s')

+     def remote_file_exists(self, name, filename, hashstr):

+         """

+         Check if a remote file exists.

+ 

+         This method inherits the behavior of its parent class from pyrpkg.

+ 

+         It uses the internal `utils.get_repo_name` mehtod to parse the name in case

+         it is a scm url. 

+ 

+         Parameters

+         ----------

+         name: str

+             The repository name and org.

+ 

+         filename: str

+             The filename (something.tar.gz).

+ 

+         hash:

+             The hash string for the file.

+ 

+         Returns

+         -------

+         bool

+             A boolean value to inditicate if the file exists.

+         """

+         _name = utils.get_repo_name(name) if is_dist_git(os.getcwd()) else name

+ 

+         return super(StreamLookasideCache, self).remote_file_exists(

+                 _name, filename, hashstr)

+ 

+     def upload(self, name, filename, hashstr, offline=False):

+         """

+         Uploads a file to lookaside cache.

+ 

+         This method inherits the behavior of its parent class from pyrpkg.

+ 

+         It uses the internal `utils.get_repo_name` mehtod to parse the name in case

+         it is a scm url. 

+ 

+         Parameters

+         ----------

+         name: str

+             The repository name and org.

+ 

+         filename: str

+             The filename (something.tar.gz).

+ 

+         hash:

+             The hash string for the file.

+ 

+         Raises

+         ------

+         pyrpkg.errors.rpkgError

+             Raises specialized classes that inherits from pyrokg base errors.

+ 

+         Returns

+         -------

+         None

+             Does not return anything

+         """

+         _name = utils.get_repo_name(name) if is_dist_git(os.getcwd()) else name

+ 

+         return super(StreamLookasideCache, self).upload(

+             _name, filename, hashstr)

+ 

+     def download(self, name, filename, hashstr, outfile, hashtype=None, **kwargs):

+         """

+         Downloads a file from lookaside cache to the local filesystem.

+ 

+         This method inherits the behavior of its parent class from pyrpkg.

+ 

+         It uses the internal `utils.get_repo_name` mehtod to parse the name in case

+         it is a scm url. 

+ 

+         Parameters

+         ----------

+         name: str

+             The repository name and org.

+ 

+         filename: str

+             The filename (something.tar.gz).

+ 

+         hash: str

+             The hash string for the file.

+ 

+         outfile: str

+ 

+ 

+         Raises

+         ------

+         pyrpkg.errors.rpkgError

+             Raises specialized implementations of  `yrpkg.errors.rpkgError`.

+ 

+         Returns

+         -------

+         None

+             Does not return anything

+         """

+         _name = utils.get_repo_name(name) if is_dist_git(os.getcwd()) else name

+ 

+         return super(StreamLookasideCache, self).download(

+            _name, filename, hashstr, outfile, hashtype=hashtype, **kwargs)

  

  

  class SIGLookasideCache(CGILookasideCache):
@@ -56,13 +191,15 @@ 

          # type it would explode with "unsupported second type in tuple". Let's

          # convert to str just to be sure.

          # https://bugzilla.redhat.com/show_bug.cgi?id=1241059

+         _name = utils.get_repo_name(name) if is_dist_git(os.getcwd()) else name

+ 

          if six.PY2 and isinstance(filename, six.text_type):

              filename = filename.encode('utf-8')

  

          if six.PY2 and isinstance(self.branch, six.text_type):

              self.branch = self.branch.encode('utf-8')

  

-         post_data = [('name', name),

+         post_data = [('name', _name),

                       ('%ssum' % self.hashtype, hash),

                       ('branch', self.branch),

                       ('filename', filename)]

file modified
+29
@@ -10,6 +10,7 @@ 

  # option) any later version.  See http://www.gnu.org/copyleft/gpl.html for

  # the full text of the license.

  

+ import re

  import json

  

  import git
@@ -150,3 +151,31 @@ 

          raise rpkgError("{0}\n{1}".format(msg, hint))

      except Exception:

          raise

+ 

+ 

+ def get_repo_name(name, org='rpms'):

+     """

+     Try to parse the repository name in case it is a git url.

+ 

+     Parameters

+     ----------

+     name: str

+         The repository name, including the org name.

+         It will try to retrieve both  repository name and org in case "name" is an url.

+ 

+     org: str

+         The org to use in case name parsing is needed.

+ 

+     Returns

+     -------

+     str

+         A string containing the repository name: $ORG/$REPO`.

+         It will return the original `name` parameter in case of regex match failure.

+     """

+     if name.startswith(org):

+         return name

+ 

+     parsed = '/'.join(name.split('/')[1:])

+     repo_name = parsed.split('_')[-1:][0]

+ 

+     return '%s/%s' % (org, repo_name)

empty or binary file added
empty or binary file added
empty or binary file added
empty or binary file added
empty or binary file added
@@ -0,0 +1,18 @@ 

+ import os

+ import unittest

+ 

+ from centpkg import lookaside

+ 

+ 

+ class TestIsDistGit(unittest.TestCase):

+   def setUp(self):

+     cwd = os.path.dirname(os.path.realpath(__file__))

+     self.fixtures_dir = '%s/fixtures/layouts' % cwd

+ 

+   def test_dist_git(self):

+     path = '%s/dist-git' % self.fixtures_dir

+     assert lookaside.is_dist_git(path)

+ 

+   def test_dist_git_false(self):

+     path = '%s/srpm' % self.fixtures_dir

+     assert not lookaside.is_dist_git(path)

file added
+21
@@ -0,0 +1,21 @@ 

+ import unittest

+ 

+ from centpkg import utils

+ 

+ 

+ class TestGetRepoName(unittest.TestCase):

+   def test_rpms_org(self):

+     assert utils.get_repo_name('rpms/time') == 'rpms/time'

+ 

+   def test_gitlab_fork(self):

+     assert utils.get_repo_name('someuser/time') == 'rpms/time'

+ 

+   def test_centpkg_fork(self):

+     assert utils.get_repo_name('someuser/centos_rpms_binutils') == 'rpms/binutils'

+ 

+   def test_ssh_fork(self):

+     assert utils.get_repo_name('git@gitlab.com:nickc2/binutils') == 'rpms/binutils'

+ 

+ 

+ if __name__ == '__main__':

+     unittest.main()