Skip to content

Commit

Permalink
Merge pull request #30 from ByteInternet/convert_non_editable_github_…
Browse files Browse the repository at this point in the history
…urls_with_environment_markers_to_https_git_urls

Also convert non-editable git urls with pip env markers
  • Loading branch information
Bono de Visser authored Mar 9, 2021
2 parents 2ac032c + f1b6792 commit 5abe24e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 39 deletions.
81 changes: 43 additions & 38 deletions pip_install_privates/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,11 @@ def convert_to_github_url_with_token(url, token):
return url


def convert_to_editable_github_url_with_token(url, token):
"""
Convert a Github URL to an editable git+https url that identifies via an Oauth token. The Oauth identification
allows for installation of private packages. The editable flag means the package
will be installed as source (i.e. all files will be present instead of it just being installed as a package).
:param url: The url to convert into a Github access token oauth url.
:param token: The Github access token to use for the oauth url.
:return: list: The editable flag for pip and a git+https url with Oauth identification.
"""
url = convert_to_github_url_with_token(url, token)
return ['-e', url]


def convert_to_editable_github_url(url):
"""
Convert a Github URL to a git+https url (does not work for private packages). The editable flag means the package
will be installed as source (i.e. all files will be present instead of it just being installed as a package).
:param url: The url to convert into a Github access token oauth url.
:return: list: The editable flag for pip and a git+https url.
"""
def convert_to_github_url(url):
for prefix in [GIT_SSH_PREFIX, GIT_GIT_PREFIX]:
if url.startswith(prefix):
url = 'git+https://github.com/{}'.format(url[len(prefix):])
return ['-e', url]
return url


def can_convert_url(url):
Expand Down Expand Up @@ -112,25 +93,10 @@ def collect_requirements(fname, transform_with_token=None):
# -e '[email protected]:ByteInternet/[email protected]#egg=my-repo ; python_version=="3.7"'
# Do not remove double quotes, since these could be used in the environment marker string i.e. "3.7".
stripped_tokens = [token.replace("'", "") for token in tokens]

if can_convert_url(stripped_tokens[1]):
if transform_with_token:
flag, github_url = convert_to_editable_github_url_with_token(
stripped_tokens[1], transform_with_token)

github_url = add_potential_pip_environment_markers_to_requirement(stripped_tokens, github_url)
collected += [flag, github_url]
else:
flag, github_url = convert_to_editable_github_url(stripped_tokens[1])

github_url = add_potential_pip_environment_markers_to_requirement(stripped_tokens, github_url)
collected += [flag, github_url]
else:
url = add_potential_pip_environment_markers_to_requirement(stripped_tokens, stripped_tokens[1])
collected += ['-e', url]
collected += convert_potential_editable_github_url(stripped_tokens[1], stripped_tokens, transform_with_token)

elif ';' in tokens:
collected += [add_potential_pip_environment_markers_to_requirement(tokens, tokens[0])]
collected += convert_potential_git_url(tokens[0], tokens, transform_with_token)

# No special casing for the rest. Just pass everything to pip
else:
Expand Down Expand Up @@ -158,6 +124,45 @@ def add_potential_pip_environment_markers_to_requirement(stripped_tokens, requir
return '{} ; {}'.format(requirement, stripped_tokens[environment_index])


def convert_potential_git_url(requirement, tokens, transform_with_token=False):
"""
Convert a potential Github URL to an git+https url that possibly identifies via an Oauth token. The Oauth
identification allows for installation of private packages.
:param requirement: The potential url to convert into a Github access token oauth url.
:param tokens: All specifications provided for the requirement (i.e. editable flag, the requirement, pip
environment markers, comments)
:param transform_with_token: Whether or not to transform to a github url with OAuth authentication
:return: list: The git+https url with possible Oauth identification.
"""
if can_convert_url(requirement):
if transform_with_token:
github_url = convert_to_github_url_with_token(requirement, transform_with_token)
github_url = add_potential_pip_environment_markers_to_requirement(tokens, github_url)
return [github_url]
else:
github_url = convert_to_github_url(requirement)
github_url = add_potential_pip_environment_markers_to_requirement(tokens, github_url)
return [github_url]
else:
return [add_potential_pip_environment_markers_to_requirement(tokens, requirement)]


def convert_potential_editable_github_url(requirement, tokens, transform_with_token=False):
"""
Convert a potential Github URL to an editable git+https url that possibly identifies via an Oauth token. The Oauth
identification allows for installation of private packages. The editable flag means the package
will be installed as source (i.e. all files will be present instead of it just being installed as a package).
:param requirement: The potential url to convert into a Github access token oauth url.
:param tokens: All specifications provided for the requirement (i.e. editable flag, the requirement, pip
environment markers, comments)
:param transform_with_token: Whether or not to transform to a github url with OAuth authentication
:return: list: The editable flag for pip and a git+https url with possible Oauth identification.
"""
requirement_tokens = convert_potential_git_url(requirement, tokens, transform_with_token)
requirement_tokens.insert(0, '-e')
return requirement_tokens


def install():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def readfile(filename):

setup(
name='pip_install_privates',
version='0.6.2',
version='0.6.3',
description='Install pip packages from private repositories without an ssh agent',
long_description=readfile('README.rst'),
long_description_content_type='text/x-rst',
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,72 @@ def test_transforms_requirement_if_pip_environment_marker_in_tokens_with_inline_
'fso==0.3.1'
])

def test_transforms_non_editable_github_url_with_pip_environment_markers_to_correct_requirement(self):
file = self._create_reqs_file([
'mock==2.0.0',
'git+ssh://[email protected]/ByteInternet/... ; python_version=="3.7"',
'nose==1.3.7'
])
fname = self._create_reqs_file(['-r {}'.format(file), 'fso==0.3.1'])

ret = collect_requirements(fname)

self.assertEqual(ret, [
'mock==2.0.0',
'git+https://github.com/ByteInternet/... ; python_version=="3.7"',
'nose==1.3.7',
'fso==0.3.1'
])

def test_transforms_non_editable_github_url_with_pip_environment_markers_to_correct_requirement_with_token(self):
file = self._create_reqs_file([
'mock==2.0.0',
'git+ssh://[email protected]/ByteInternet/... ; python_version=="3.7"',
'nose==1.3.7'
])
fname = self._create_reqs_file(['-r {}'.format(file), 'fso==0.3.1'])

ret = collect_requirements(fname, transform_with_token=True)

self.assertEqual(ret, [
'mock==2.0.0',
'git+https://True:[email protected]/ByteInternet/... ; python_version=="3.7"',
'nose==1.3.7',
'fso==0.3.1'
])

def test_transforms_non_editable_github_url_with_pip_environment_markers_to_correct_requirement_with_comment(self):
file = self._create_reqs_file([
'mock==2.0.0',
'git+ssh://[email protected]/ByteInternet/... ; python_version=="3.7" # We need this',
'nose==1.3.7'
])
fname = self._create_reqs_file(['-r {}'.format(file), 'fso==0.3.1'])

ret = collect_requirements(fname)

self.assertEqual(ret, [
'mock==2.0.0',
'git+https://github.com/ByteInternet/... ; python_version=="3.7"',
'nose==1.3.7',
'fso==0.3.1'
])

def test_transforms_non_editable_github_url_with_pip_environment_markers_to_correct_requirement_with_token_and_comment(self):
file = self._create_reqs_file([
'mock==2.0.0',
'git+ssh://[email protected]/ByteInternet/... ; python_version=="3.7" # We need this because reasons',
'nose==1.3.7'
])
fname = self._create_reqs_file(['-r {}'.format(file), 'fso==0.3.1'])

ret = collect_requirements(fname, transform_with_token=True)

self.assertEqual(ret, [
'mock==2.0.0',
'git+https://True:[email protected]/ByteInternet/... ; python_version=="3.7"',
'nose==1.3.7',
'fso==0.3.1'
])


0 comments on commit 5abe24e

Please sign in to comment.