Created
March 29, 2019 11:30
-
-
Save eclipseo/1ee6519e6d1a9dc0ff684e333b23cd9a to your computer and use it in GitHub Desktop.
Tool to register Golang packages in Anitya
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiohttp | |
import sys | |
import os | |
from subprocess import CalledProcessError, check_output | |
def get_data(pkg): | |
data = {} | |
spec = os.path.join(pkg, pkg + '.spec') | |
try: | |
rpmspec = check_output(['rpmspec', '-P', spec], universal_newlines=True).strip() | |
except CalledProcessError: | |
raise BaseException('Can\'t evaluate rpmspec') | |
data['url'] = [line for line in rpmspec.split('\n') | |
if 'Source:' in line or 'Source0:' in line][0].split()[1] | |
try: | |
data['url'].split('/')[2] | |
except IndexError: | |
data['backend'] = 'unknown' | |
data['homepage'] = [line for line in rpmspec.split('\n') | |
if 'URL:' in line][0].split()[1] | |
return data | |
if 'github.com' in data['url'].split('/')[2]: | |
data['backend'] = 'GitHub' | |
elif 'bitbucket.org' in data['url'].split('/')[2]: | |
data['backend'] = 'BitBucket' | |
else: | |
data['backend'] = 'unknown' | |
data['homepage'] = [line for line in rpmspec.split('\n') | |
if 'URL:' in line][0].split()[1] | |
return data | |
data['homepage'] = 'https://' + '/'.join(data['url'].split('/')[2:5]) | |
data['name'] = data['url'].split('/')[4] | |
data['version_url'] = '/'.join(data['url'].split('/')[3:5]) | |
data['version_prefix'] = '' if not 'archive/v' in data['url'] else 'v' | |
return data | |
def get_subdirectories(dir): | |
return [name for name in os.listdir(dir) | |
if os.path.isdir(os.path.join(dir, name))] | |
async def main(paths): | |
if os.path.exists('TODO_anitya_golang.txt'): | |
os.remove('TODO_anitya_golang.txt') | |
pkgs = [pkg for pkg in paths if pkg.startswith('golang-') and os.path.exists(os.path.join(pkg, pkg + '.spec'))] | |
for pkg in pkgs: | |
print(pkg) | |
async with aiohttp.ClientSession() as session: | |
# Check if the package is tracked *for Fedora* | |
url = f'https://release-monitoring.org/api/project/fedora/{pkg}' | |
async with session.get(url) as resp: | |
jsonresp = await resp.json() | |
if 'error' in jsonresp: | |
print(jsonresp['error']) | |
pkg_data = get_data(pkg) | |
# Check if the package is already tracked | |
url = f'https://release-monitoring.org/api/projects/?homepage={pkg_data["homepage"]}' | |
async with session.get(url) as resp: | |
jsonresp = await resp.json() | |
# Not tracked | |
if jsonresp['total'] == 0: | |
if not 'unknown' in pkg_data['backend']: | |
url = f'https://release-monitoring.org/api/v2/projects/' | |
pdata = {'backend': pkg_data['backend'], | |
'homepage': pkg_data['homepage'], | |
'name': pkg_data['name'], | |
'version_url': pkg_data['version_url'], | |
'version_prefix': pkg_data['version_prefix'], | |
'check_release': True} | |
async with session.post(url, json=pdata, headers={'Authorization': 'token FIXME'}) as resp: | |
print(await resp.json()) | |
#resp.raise_for_status() | |
url = f'https://release-monitoring.org/api/v2/packages/' | |
pdata = {'distribution': 'Fedora', | |
'package_name': pkg, | |
'project_name': pkg_data['name'], | |
'project_ecosystem': pkg_data['homepage']} | |
async with session.post(url, json=pdata, headers={'Authorization': 'token FIXME'}) as resp: | |
print(await resp.json()) | |
#resp.raise_for_status() | |
# Unknown backend: we add it to a TODO list for manual review | |
else: | |
with open('TODO_anitya_golang.txt', 'a') as file: | |
file.write(pkg + '\n') | |
# tracked: we link it to the Fedora package | |
else: | |
url = f'https://release-monitoring.org/api/v2/packages/' | |
pdata = {'distribution': 'Fedora', | |
'package_name': pkg, | |
'project_name': pkg_data['name'], | |
'project_ecosystem': pkg_data['homepage']} | |
async with session.post(url, json=pdata, headers={'Authorization': 'token FIXME'}) as resp: | |
print(await resp.json()) | |
#resp.raise_for_status() | |
else: | |
print(f'{pkg} is already set up.') | |
if __name__ == '__main__': | |
if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]): | |
asyncio.run(main(get_subdirectories(sys.argv[1]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment