Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#!/usr/bin/env python3
from setuptools import setup, find_packages
import nvchecker
# The complex upload command:
# rm -rf dist && python setup.py sdist && twine check dist/* && twine upload -s dist/*
setup(
name = 'nvchecker',
version = nvchecker.__version__,
author = 'lilydjwg',
author_email = 'lilydjwg@gmail.com',
description = 'New version checker for software',
license = 'MIT',
keywords = 'new version build check',
url = 'https://github.com/lilydjwg/nvchecker',
long_description = open('README.rst', encoding='utf-8').read(),
long_description_content_type = 'text/x-rst',
platforms = 'any',
zip_safe = False,
packages = find_packages(exclude=["tests"]),
install_requires = ['setuptools', 'structlog', 'tornado>=6', 'pycurl'],
extras_require = {
'vercmp': ['pyalpm'],
},
def sync_get_version(self, name, config):
def get_version_callback(name, version):
self.stop(version)
if isinstance(config, dict):
_config = configparser.ConfigParser(dict_type=dict, allow_no_value=True)
_config.read_dict({name: config})
config = _config[name]
get_version(name, config, get_version_callback)
return self.wait()
async def test_proxy(get_version, monkeypatch):
from nvchecker.source import session
async def fake_request(*args, proxy, **kwargs):
class fake_response():
status = 200
async def read():
return proxy.encode("ascii")
def release():
pass
return fake_response
monkeypatch.setattr(session, "nv_config", {"proxy": "255.255.255.255:65535"}, raising=False)
monkeypatch.setattr(aiohttp.ClientSession, "_request", fake_request)
assert await get_version("example", {"regex": "(.+)", "url": "deadbeef"}) == "255.255.255.255:65535"
assert await get_version("example", {"regex": "(.+)", "url": "deadbeef", "proxy": "0.0.0.0:0"}) == "0.0.0.0:0"
github = xxx
''')
f.flush()
test_conf = '''\
[example]
github = harry-sanabria/ReleaseTestRepo
[__config__]
keyfile = {name}
'''.format(name=f.name)
try:
version = await run_source(test_conf, clear_cache=True)
assert version is None # out of allowance
return
except HTTPError as e:
assert e.code == 401
return
raise Exception('expected 401 response')
async def get_version(name, conf, **kwargs):
pkg = conf.get('debianpkg') or name
strip_release = conf.getboolean('strip-release', False)
suite = conf.get('suite') or "sid"
url = URL % {"pkgname": pkg, "suite": suite}
async with session.get(url) as res:
data = await res.json()
if not data.get('versions'):
logger.error('Debian package not found', name=name)
return
r = data['versions'][0]
if strip_release:
version = r['version'].split("-")[0]
else:
version = r['version']
return version
async def get_version(name, conf, **kwargs):
repo = conf.get('bitbucket')
br = conf.get('branch', '')
use_max_tag = conf.getboolean('use_max_tag', False)
ignored_tags = conf.get("ignored_tags", "").split()
sort_version_key = sort_version_keys[conf.get("sort_version_key", "parse_version")]
if use_max_tag:
url = BITBUCKET_MAX_TAG % repo
max_page = conf.getint('max_page', 3)
data = await _get_tags(url, max_page=max_page)
else:
url = BITBUCKET_URL % (repo, br)
async with session.get(url) as res:
data = await res.json()
if use_max_tag:
data = [tag for tag in data if tag not in ignored_tags]
data.sort(key=sort_version_key)
version = data
else:
version = data['values'][0]['date'].split('T', 1)[0].replace('-', '')
return version
async def get_version(name, conf, **kwargs):
sparkle = conf['sparkle']
async with session.get(sparkle) as res:
resp = await res.read()
root = ElementTree.fromstring(resp)
item = root.find('./channel/item[1]/enclosure')
version_string = item.get('{http://www.andymatuschak.org/xml-namespaces/sparkle}shortVersionString')
build_number = item.get('{http://www.andymatuschak.org/xml-namespaces/sparkle}version')
if (version_string and version_string.isdigit()) and (build_number and not build_number.isdigit()):
version_string, build_number = build_number, version_string
version = []
if version_string:
version.append(version_string)
if build_number and (build_number not in version):
async def get_version(name, conf, **kwargs):
aurname = conf.get('aur') or name
use_last_modified = conf.getboolean('use_last_modified', False)
strip_release = conf.getboolean('strip-release', False)
async with session.get(AUR_URL, params={"v": 5, "type": "info", "arg[]": aurname}) as res:
data = await res.json()
if not data['results']:
logger.error('AUR upstream not found', name=name)
return
version = data['results'][0]['Version']
if use_last_modified:
version += '-' + datetime.utcfromtimestamp(data['results'][0]['LastModified']).strftime('%Y%m%d%H%M%S')
if strip_release and '-' in version:
version = version.rsplit('-', 1)[0]
return version
token = conf.get('token')
# Load token from environ
if token is None:
env_name = "NVCHECKER_GITLAB_TOKEN_" + host.upper().replace(".", "_").replace("/", "_")
token = os.environ.get(env_name)
# Load token from keyman
if token is None and 'keyman' in kwargs:
key_name = 'gitlab_' + host.lower().replace('.', '_').replace("/", "_")
token = kwargs['keyman'].get_key(key_name)
# Set private token if token exists.
headers = {}
if token:
headers["PRIVATE-TOKEN"] = token
async with session.get(url, headers=headers) as res:
data = await res.json()
if use_max_tag:
version = [tag["name"] for tag in data if tag["name"] not in ignored_tags]
else:
version = data[0]['created_at'].split('T', 1)[0].replace('-', '')
return version
async def get_latest_tag(name, conf, token):
repo = conf.get('github')
query = conf.get('query', '')
owner, reponame = repo.split('/')
headers = {
'Authorization': 'bearer %s' % token,
'Content-Type': 'application/json',
}
q = QUERY_LATEST_TAG.format(
owner = owner,
name = reponame,
query = query,
)
async with session.post(
GITHUB_GRAPHQL_URL,
headers = headers,
json = {'query': q},
) as res:
j = await res.json()
refs = j['data']['repository']['refs']['edges']
if not refs:
logger.error('no tag found', name=name)
return
return refs[0]['node']['name']