#!/usr/bin/env python3
# SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense
from argparse import ArgumentParser
import json
import os
import re
import sys
def load_distributions(distribution_specs):
distributions = {}
for distribution_spec in distribution_specs:
with open(distribution_spec, 'r') as f:
distributions.update(json.loads(f.read()))
# For now, this fork is always included...
distributions.update({
"OpenZz distribution": {
"entries": [
"OpenZz",
],
"fixed-tag": "1.0.4-4ce1",
"releases": [],
"reponame": "OpenZz",
},
})
return distributions
def filter_distributions(distributions, for_repohost):
d = {}
for key, value in distributions.items():
repohosts = value.get("repohosts", [])
if for_repohost in repohosts:
d[key] = value
return d
def obtain_infos(distributions):
infos = {}
for (key, node) in sorted(distributions.items()):
repo = node.get('reponame')
if not repo:
#print "# skipping {}".format(key)
continue
releases = node.get('releases', [])
new_style = node.get('tag-style', 'old') == 'new'
tag = 'master'
fixed_tag = node.get('fixed-tag', None)
if fixed_tag == 'OMIT':
continue
if fixed_tag is not None:
tag = fixed_tag
elif releases:
version = releases[-1]['version']
revision = releases[-1]['revision']
if new_style:
tag = '%s-%s' % (version, revision)
if revision == '0.0':
tag = version
else:
version = re.sub(r'\.', r'_', str(version))
revision = re.sub(r'\.', r'_', str(revision))
tag = 'rel_%s_%s' % (version, revision)
if revision == '0_0':
tag = 'rel_%s' % version
infos[key] = (repo, tag)
return infos
def create_shelf_catalog(distribution_specs, for_repohost=None):
"""Create a shelf catalogue from distribution nodes.
"""
distributions = load_distributions(distribution_specs)
if for_repohost:
distributions = filter_distributions(distributions, for_repohost)
infos = obtain_infos(distributions)
lines = []
for (key, (repo, tag)) in sorted(infos.items()):
lines.append('%s@%s' % (repo, tag))
for line in sorted(lines):
try:
sys.stdout.write(line.encode('utf-8'))
except:
sys.stdout.write(line)
sys.stdout.write('\n')
def main(args):
argparser = ArgumentParser()
argparser.add_argument(
'--distribution-specs',
type=str,
default='distribution/distributions.json',
help="comma-seperated list of filenames of JSON files that "
"describe the distributions from which to create the "
"shelf catalogue file"
)
argparser.add_argument(
'--for-repohost',
type=str,
default=None,
help="generate a catalogue file for only this repohost"
)
options = argparser.parse_args(args)
distribution_specs = [x.strip() for x in options.distribution_specs.split(',')]
create_shelf_catalog(distribution_specs, for_repohost=options.for_repohost)
if __name__ == '__main__':
main(sys.argv[1:])