Skip to content

Commit

Permalink
Got rid of six dependency and a bit extra test added
Browse files Browse the repository at this point in the history
  • Loading branch information
dvershinin committed Nov 15, 2023
1 parent a46780e commit 15fe3ac
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
/.pytest_cache
*.pyc
/site/
/cli.*
/build/
1 change: 1 addition & 0 deletions lastversion/BitBucketRepoSession.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""BitBucket repository session."""
from dateutil import parser

from .ProjectHolder import ProjectHolder
Expand Down
3 changes: 2 additions & 1 deletion lastversion/FeedRepoSession.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
from urllib.parse import urlparse

import feedparser

Expand All @@ -22,7 +23,7 @@ class FeedRepoSession(ProjectHolder):

# https://alex.miller.im/posts/python-3-feedfinder-rss-detection-from-url/
def find_feed(self, site):
from six.moves.urllib.parse import urlparse
"""Find the feed for a given site"""
# noinspection PyPep8Naming
from bs4 import BeautifulSoup as bs4
raw = self.get(site).text
Expand Down
5 changes: 3 additions & 2 deletions lastversion/GitHubRepoSession.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
from urllib.parse import unquote

import math
import os
import re
Expand All @@ -8,7 +10,6 @@

import feedparser
from dateutil import parser
from six.moves.urllib.parse import unquote

from .ProjectHolder import ProjectHolder
from .exceptions import ApiCredentialsError, BadProjectError
Expand Down Expand Up @@ -645,7 +646,7 @@ def set_matching_formal_release(self, ret, formal_release, version, pre_ok,
regex_matching = False
search = self.having_asset
if search.startswith('~'):
search = r'{}'.format(search.lstrip('~'))
search = fr'{search.lstrip("~")}'
regex_matching = True
found_asset = False
for asset in formal_release['assets']:
Expand Down
15 changes: 10 additions & 5 deletions lastversion/GiteaRepoSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import re
import time

import requests
from bs4 import BeautifulSoup
from dateutil import parser

Expand Down Expand Up @@ -55,11 +54,12 @@ class GiteaRepoSession(ProjectHolder):
SHORT_RELEASE_URL_FORMAT = RELEASE_URL_FORMAT

def find_repo_by_name_only(self, repo):
"""Find repo by name only using Gitea API."""
if self.is_link(repo):
return None
cache_repo_names_file = f"{self.cache_dir}/repos.json"
try:
with open(cache_repo_names_file, 'r') as reader:
with open(cache_repo_names_file, 'r', encoding='utf-8') as reader:
cache = json.load(reader)
except (IOError, ValueError):
cache = {}
Expand Down Expand Up @@ -98,7 +98,7 @@ def find_repo_by_name_only(self, repo):
'updated_at': int(time.time())
}
try:
with open(cache_repo_names_file, 'w') as writer:
with open(cache_repo_names_file, 'w', encoding='utf-8') as writer:
json.dump(cache, writer)
except (IOError, ValueError):
pass
Expand Down Expand Up @@ -196,7 +196,7 @@ def get(self, url, **kwargs):
'be reinstated'
)
else:
w = 'Waiting {} seconds for API quota reinstatement.'.format(wait_for)
w = f'Waiting {wait_for} seconds for API quota reinstatement.'
if "GITHUB_API_TOKEN" not in os.environ \
and 'GITHUB_TOKEN' not in os.environ:
w = f"{w} {TOKEN_PRO_TIP}"
Expand All @@ -214,14 +214,17 @@ def get(self, url, **kwargs):
return r

def rate_limit(self):
"""Get rate limit info."""
url = f'{self.api_base}/rate_limit'
return self.get(url)

def repo_query(self, uri):
"""Query the repo API."""
url = f'{self.api_base}/repos/{self.repo}{uri}'
return self.get(url)

def repo_license(self, tag):
"""Get the license file for a tag."""
r = self.repo_query(f'/license?ref={tag}')
if r.status_code == 200:
# unfortunately, unlike /readme, API always returns *latest* license, ignoring tag
Expand All @@ -234,12 +237,14 @@ def repo_license(self, tag):
return None

def repo_readme(self, tag):
"""Get the readme file for a tag."""
r = self.repo_query(f'/readme?ref={tag}')
if r.status_code == 200:
return r.json()
return None

def get_formal_release_for_tag(self, tag):
"""Get the formal release for a tag, if it exists."""
r = self.repo_query(f'/releases/tags/{tag}')
if r.status_code == 200:
# noinspection SpellCheckingInspection
Expand Down Expand Up @@ -315,7 +320,7 @@ def set_matching_formal_release(self, ret, formal_release, version, pre_ok,
regex_matching = False
search = self.having_asset
if search.startswith('~'):
search = r'{}'.format(search.lstrip('~'))
search = fr'{search.lstrip("~")}'
regex_matching = True
found_asset = False
for asset in formal_release['assets']:
Expand Down
1 change: 0 additions & 1 deletion lastversion/HelmChartRepoSession.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging

import yaml
from six.moves.urllib.parse import urlparse

from .ProjectHolder import ProjectHolder

Expand Down
3 changes: 2 additions & 1 deletion lastversion/HolderFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def guess_from_homepage(repo, hostname):

@staticmethod
def create_holder_from_known_repo(known_repo, project_hosting_class):
"""Create a holder from a known repo."""
repo = known_repo['repo']
# Known repo tells us hosted domain of e.g., mercurial web
hostname = known_repo.get('hostname')
Expand Down Expand Up @@ -169,5 +170,5 @@ def get_instance_for_repo(repo, at=None):
return GitHubRepoSession(repo)

raise BadProjectError(
'Could not find a holder for the repo %s' % repo
f'Could not find a holder for the repo {repo}'
)
2 changes: 2 additions & 0 deletions lastversion/ProjectHolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def __init__(self, name=None, hostname=None):
self.exclude = None
self.having_asset = None
self.hostname = hostname
if not self.hostname and self.DEFAULT_HOSTNAME:
self.hostname = self.DEFAULT_HOSTNAME
# identifies project on a given hostname
# normalize repo to number of meaningful parameters
self.repo = self.get_base_repo_from_repo_arg(name)
Expand Down
2 changes: 1 addition & 1 deletion lastversion/argparse_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __call__(self, parser, namespace, values, option_string=None):
if __version__ == str(last_version):
version += ', up to date'
else:
version += ', newer version {} available'.format(last_version)
version += f', newer version {last_version} available'
formatter = parser.formatter_class(prog=parser.prog)
formatter.add_text(version)
_sys.stdout.write(formatter.format_help())
Expand Down
9 changes: 4 additions & 5 deletions lastversion/lastversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import sys
from os.path import expanduser
from pathlib import Path
from urllib.parse import urlparse

import yaml
from packaging.version import InvalidVersion
from six.moves.urllib.parse import urlparse

from lastversion.TestProjectHolder import TestProjectHolder
from .TestProjectHolder import TestProjectHolder
from .GitHubRepoSession import TOKEN_PRO_TIP
from .HolderFactory import HolderFactory
from .ProjectHolder import ProjectHolder
from .Version import Version
from .__about__ import __self__
from .argparse_version import VersionAction
Expand Down Expand Up @@ -100,7 +99,7 @@ def get_repo_data_from_spec(repo):
repo_data['name'] = name
repo_data['spec_name'] = '%{name}'
if upstream_github:
repo = "{}/{}".format(upstream_github, repo_data['name'])
repo = f"{upstream_github}/{repo_data['name']}"
log.info('Discovered GitHub repo %s from .spec file', repo)
elif spec_repo:
repo = spec_repo
Expand All @@ -120,7 +119,7 @@ def get_repo_data_from_yml(repo):
repo_data['module_of'] = 'nginx'
name = os.path.splitext(os.path.basename(repo))[0]
if 'module_of' in repo_data:
name = '{}-module-{}'.format(repo_data['module_of'], name)
name = f'{repo_data["module_of"]}-module-{name}'
repo = repo_data['repo']
repo_data['name'] = name
return repo_data
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
'python-dateutil',
'PyYAML',
'tqdm',
'six',
'beautifulsoup4',
'distro',
# pin due to https://github.com/ionrock/cachecontrol/issues/292
Expand Down
9 changes: 9 additions & 0 deletions tests/test_github.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Test GitHub projects."""
import os
from operator import eq, ge
from tempfile import TemporaryDirectory

import pytest
from packaging import version

from lastversion import main
Expand Down Expand Up @@ -76,3 +78,10 @@ def test_github_extract_wordpress():
main(["extract", repo])
assert os.path.exists("index.php")
assert os.path.exists("wp-config-sample.php")


def test_github_search_python():
"""Test searching a GitHub project."""
repo = "python"
output = latest(repo)
assert output > version.parse("3.11")

0 comments on commit 15fe3ac

Please sign in to comment.