Skip to content

Commit

Permalink
Added method to fetch project versions using PyPi API, removed hardco…
Browse files Browse the repository at this point in the history
…ded versions in tests.
  • Loading branch information
Onager committed Jan 17, 2019
1 parent 8adf320 commit 4f5cb4d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
33 changes: 33 additions & 0 deletions l2tdevtools/download_helpers/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import unicode_literals

import json
import re

# pylint: disable=wrong-import-position
Expand Down Expand Up @@ -78,6 +79,38 @@ def _GetAvailableVersions(self, version_strings):

return available_versions

def GetLatestVersionWithAPI(self, project_name, version_definition):
"""Retrieves the latest version for a given project using the PyPi API.
Args:
project_name (str): name of the project.
version_definition (ProjectVersionDefinition): project version definition
or None.
Returns:
str: latest version number or None if not available.
"""
earliest_version = None
latest_version = None

if version_definition:
earliest_version = version_definition.GetEarliestVersion()
if earliest_version and earliest_version[0] == '==':
return '.'.join(earliest_version[1:])

latest_version = version_definition.GetLatestVersion()

pypi_url = 'https://pypi.org/pypi/{0:s}/json'.format(project_name)
page_content = self.DownloadPageContent(pypi_url)

api_data = json.loads(page_content)
releases = api_data.get('releases', {})
version_strings = releases.keys()
available_versions = self._GetAvailableVersions(version_strings)

return self._GetLatestVersion(
earliest_version, latest_version, available_versions)

def GetLatestVersion(self, unused_project_name, version_definition):
"""Retrieves the latest version number for a given project name.
Expand Down
3 changes: 3 additions & 0 deletions tests/download_helpers/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def testGetLatestVersion(self):

latest_version = download_helper.GetLatestVersion(self._PROJECT_NAME, None)

latest_version = download_helper.GetLatestVersionWithAPI(
self._PROJECT_NAME, None)

self.assertEqual(latest_version, self._PROJECT_VERSION)

def testGetDownloadURL(self):
Expand Down

0 comments on commit 4f5cb4d

Please sign in to comment.