Skip to content

Commit

Permalink
Merge branch 'fix/remove-giturlparse' of https://github.com/philipsd6…
Browse files Browse the repository at this point in the history
…/gilt into philipsd6-fix/remove-giturlparse
  • Loading branch information
retr0h committed Mar 14, 2020
2 parents 2b47606 + 744bfae commit 5d31d6a
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 32 deletions.
70 changes: 50 additions & 20 deletions gilt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import collections
import errno
import os
import urllib.parse

import giturlparse
import yaml

from gilt import interpolation
Expand Down Expand Up @@ -60,6 +60,36 @@ def config(filename):
return [Config(**d) for d in _get_config_generator(filename)]


def _parse_repo_uri(uri, scm="git"):
"""Construct and return a `ParsedRepo` object.
:param uri: A SCM repository URI.
:return: ParsedRepo
"""
ParsedRepo = collections.namedtuple('ParsedRepo', [
'hostname',
'owner',
'name'
])

scm_ext = "." + scm
o = urllib.parse.urlparse(uri)
if not o.hostname: # scp-style "URI", so fake it
uri = "ssh://" + uri.replace(":/", "/").replace(":", "/")
o = urllib.parse.urlparse(uri)
path = o.path
if path.endswith(scm_ext):
path = path[0:-len(scm_ext)]
try:
name, owner = path.rsplit("/", 2)[-1:-3:-1]
owner = owner.lstrip("~")
except ValueError:
# if repo is the root of the path, then there is no owner
owner = ''

return ParsedRepo(o.hostname, owner, name)


def _get_files_config(src_dir, files_list):
"""Construct `FileConfig` object and return a list.
Expand All @@ -84,24 +114,29 @@ def _get_config_generator(filename):
:return: dict
"""
for d in _get_config(filename):
repo = d["git"]
parsedrepo = giturlparse.parse(repo)
name = "{}.{}".format(parsedrepo.owner, parsedrepo.name)
src_dir = os.path.join(_get_clone_dir(), name)
files = d.get("files")
post_commands = d.get("post_commands", [])
repo = d['git']
parsedrepo = _parse_repo_uri(repo)
if parsedrepo.owner:
name = '{}.{}'.format(parsedrepo.owner, parsedrepo.name)
else:
name = parsedrepo.name
src_dir = os.path.join(_get_clone_dir(), parsedrepo.hostname, name)
files = d.get('files')
post_commands = d.get('post_commands', [])
dst_dir = None
if not files:
dst_dir = _get_dst_dir(d["dst"])
yield {
"git": repo,
"lock_file": _get_lock_file(name),
"version": d["version"],
"name": name,
"src": src_dir,
"dst": dst_dir,
"files": _get_files_config(src_dir, files),
"post_commands": post_commands,
'git': repo,
'lock_file': os.path.join(
_get_lock_dir(), parsedrepo.hostname, name
),
'version': d['version'],
'name': name,
'src': src_dir,
'dst': dst_dir,
'files': _get_files_config(src_dir, files),
'post_commands': post_commands,
}


Expand Down Expand Up @@ -153,11 +188,6 @@ def _get_dst_dir(dst_dir):
return os.path.join(wd, dst_dir)


def _get_lock_file(name):
"""Return the lock file for the given name. """
return os.path.join(_get_lock_dir(), name,)


def _get_base_dir():
"""Return gilt's base working directory. """
return os.path.expanduser(BASE_WORKING_DIR)
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ install_requires =
click
colorama
fasteners
git-url-parse
PyYAML
sh

Expand Down
187 changes: 176 additions & 11 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def test_config(gilt_config_file):
gilt_root = os.path.basename(config.BASE_WORKING_DIR)

r = result[0]
assert "https://github.com/retr0h/ansible-etcd.git" == r.git
assert "master" == r.version
assert "retr0h.ansible-etcd" == r.name
assert (gilt_root, "clone", "retr0h.ansible-etcd") == os_split(r.src)[-3:]
assert (gilt_root, "lock", "retr0h.ansible-etcd") == os_split(r.lock_file)[
-3:
]
assert ("roles", "retr0h.ansible-etcd", "") == os_split(r.dst)[-3:]
assert 'https://github.com/retr0h/ansible-etcd.git' == r.git
assert 'master' == r.version
assert 'retr0h.ansible-etcd' == r.name
assert (gilt_root, 'clone', 'github.com',
'retr0h.ansible-etcd') == os_split(r.src)[-4:]
assert (gilt_root, 'lock', 'github.com',
'retr0h.ansible-etcd') == os_split(r.lock_file)[-4:]
assert ('roles', 'retr0h.ansible-etcd', '') == os_split(r.dst)[-3:]
assert [] == r.files

r = result[1]
Expand All @@ -52,9 +52,174 @@ def test_config(gilt_config_file):
assert r.dst is None

f = r.files[0]
x = (gilt_root, "clone", "lorin.openstack-ansible-modules", "*_manage")
assert x == os_split(f.src)[-4:]
assert ("library", "") == os_split(f.dst)[-2:]
x = (gilt_root, 'clone', 'github.com',
'lorin.openstack-ansible-modules', '*_manage')
assert x == os_split(f.src)[-5:]
assert ('library', '') == os_split(f.dst)[-2:]


gilt_repos = [
(
"example.com:/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"example.com:owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"example.com:repo.git",
{"name": "repo", "owner": "", "hostname": "example.com"},
),
(
"git+https://example.com/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"git+ssh://example.com/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"git+ssh://[email protected]/~philip.sd6/test.repo.git",
{
"name": "test.repo",
"owner": "philip.sd6",
"hostname": "git.example.com",
},
),
(
"git://example.com/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"http://example.com/owner/repo",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"http://example.com/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"http://example.com/repo",
{"name": "repo", "owner": "", "hostname": "example.com"},
),
(
"http://example.com:29418/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"http://[email protected]/user/repo",
{"name": "repo", "owner": "user", "hostname": "example.com"},
),
(
"http://[email protected]:29418/user/repo",
{"name": "repo", "owner": "user", "hostname": "example.com"},
),
(
"https://example.com/git/scm/project/my-sample.repo.git",
{
"name": "my-sample.repo",
"owner": "project",
"hostname": "example.com",
},
),
(
"https://example.com/git/scm/~philip.sd6/my-sample.repo.git",
{
"name": "my-sample.repo",
"owner": "philip.sd6",
"hostname": "example.com",
},
),
(
"https://example.com/owner/repo",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"https://example.com/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"https://example.com/repo",
{"name": "repo", "owner": "", "hostname": "example.com"},
),
(
"https://example.com:29418/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"https://github.com/sphinx-doc/sphinx.git",
{"name": "sphinx", "owner": "sphinx-doc", "hostname": "github.com"},
),
(
"https://github.com/tterranigma/Stouts.openvpn",
{
"name": "Stouts.openvpn",
"owner": "tterranigma",
"hostname": "github.com",
},
),
(
"https://github.com/tterranigma/Stouts.openvpn.git",
{
"name": "Stouts.openvpn",
"owner": "tterranigma",
"hostname": "github.com",
},
),
(
"https://[email protected]/user/repo",
{"name": "repo", "owner": "user", "hostname": "example.com"},
),
(
"https://[email protected]:29418/user/repo",
{"name": "repo", "owner": "user", "hostname": "example.com"},
),
(
"rsync://example.com/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"ssh://example.com/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"ssh://example.com:29418/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"ssh://[email protected]/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"ssh://[email protected]:29418/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"[email protected]:/owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"[email protected]:owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "example.com"},
),
(
"[email protected]:repo.git",
{"name": "repo", "owner": "", "hostname": "example.com"},
),
(
"[email protected]:owner/repo.git",
{"name": "repo", "owner": "owner", "hostname": "foo-example.com"},
),
]


@pytest.mark.parametrize('uri,expected', gilt_repos)
def test_config_repo(uri, expected):
parsedrepo = config._parse_repo_uri(uri)
assert parsedrepo.hostname == expected['hostname']
assert parsedrepo.owner == expected['owner']
assert parsedrepo.name == expected['name']


@pytest.fixture()
Expand Down

0 comments on commit 5d31d6a

Please sign in to comment.