git @ Cat's Eye Technologies Chrysoberyl / migrate-away-from-automata script / create-shelf-catalog
migrate-away-from-automata

Tree @migrate-away-from-automata (Download .tar.gz)

create-shelf-catalog @migrate-away-from-automataraw · history · blame

#!/usr/bin/env python3
# encoding: UTF-8

from argparse import ArgumentParser
import json
import os
import re
import sys


def create_shelf_catalog(options):
    """Create a shelf catalogue from distribution nodes.

    """

    with open('distribution/distributions.json', 'r') as f:
        distributions = json.loads(f.read())

    with open('distribution/forks.json', 'r') as f:
        forks = json.loads(f.read())

    if options.include_forks:
        distributions.update(forks)

    # For now, this fork is always included...
    distributions.update({
        "OpenZz distribution": {
            "entries": [
                "OpenZz",
            ],
            "fixed-tag": "1.0.4-4ce1",
            "releases": [],
            "reponame": "OpenZz",
        },
    })

    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
        key = repo.lower() if os.getenv('LOWERCASE_REPOS') else repo
        infos[key] = (repo, tag)

    lines = []
    for (key, (repo, tag)) in sorted(infos.items()):
        lines.append('%s@%s' % (key, tag))
    
    for line in sorted(lines):
        try:
            sys.stdout.write(line.encode('utf-8'))
        except:
            sys.stdout.write(line)
        sys.stdout.write('\n')


if __name__ == '__main__':
    argparser = ArgumentParser()
    argparser.add_argument('--include-forks', action='store_true', default=False,
                           help='Include forks in the generated catalogue.')
    options = argparser.parse_args(sys.argv[1:])
    create_shelf_catalog(options)