git @ Cat's Eye Technologies Chrysoberyl / master script / for-great-justice
master

Tree @master (Download .tar.gz)

for-great-justice @masterraw · history · blame

#!/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


THINGMAP = {
    'Py': 'Python',
    'PL': 'Perl',
    'C': 'C',
    'JS': 'Javascript',
    'HS': 'Haskell',
    'R5': 'Scheme',
    'Lua': 'Lua',
    'sh': 'Bourne shell',
    'BASIC': 'BASIC',
    'YASM': 'NASM',
    'Pixley': 'Pixley',
    'Befunge-93': 'Befunge-93',
    'ALPACA': 'ALPACA',
    'Py2': 'Python 2.7',  # NOTE: no node yet
    'Erlang': 'Erlang',
    'Java': 'Java',
    'Julia': 'Julia',  # NOTE: node redirects to Julia's website
    'Makefile': 'Makefile',  # NOTE: no node yet
    'OpenZz': 'OpenZz',
    'Ophis': 'Ophis',  # NOTE: no node yet
    '6502': '6502 machine code',
    'R': 'R',  # NOTE: redirects to R's website
    'Ruby': 'Ruby',
}

LANGS = sorted(THINGMAP.values())

DISPMAP = {
    '6502 machine code': '6502',
    'Befunge-93': 'Bef93',
    'Python 2.7': 'Py2',
    'Bourne shell': 'sh',
    'Javascript': 'JS',
}


def dump_project_matrix(reg):
    sys.stdout.write('Project | ')
    for lang in LANGS:
        sys.stdout.write(DISPMAP.get(lang, lang) + ' | ')
    sys.stdout.write('\n')

    sys.stdout.write('--- | ')
    for lang in LANGS:
        sys.stdout.write('--- | ')
    sys.stdout.write('\n')

    for key, value in sorted(reg.items()):
        line = '{}'.format(key)
        for lang in LANGS:
            if lang in value:
                line += '| {} '.format(DISPMAP.get(lang, lang))
            else:
                line += '| '
        sys.stdout.write(line + '\n')


def rewrite_distributions(reg):
    distributions_filename = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..', 'distribution', 'distributions.json')
    with open(distributions_filename, 'r') as f:
        distributions = json.loads(f.read())

    for key, value in sorted(reg.items()):
        reponame, version = key.split('@')

        found_distribution = False
        for distname, distribution in sorted(distributions.items()):
            if distribution.get('reponame') == reponame:
                found_distribution = distributions[distname]
        assert found_distribution, reponame
        found_distribution['impllangs'] = sorted(value)

    with open(distributions_filename, 'w') as f:
        f.write(json.dumps(distributions, indent=4, sort_keys=True))


def load_from_markdown_table(reg, file):

    # langs = set()
    for line in file:
        match = re.match(r'^([^|\s]*)\s*\|(.*?)$', line)
        if match and match.group(1):
            things = match.group(2).split('|')
            things = set([t.strip() for t in things if t.strip()])
            mapped_things = []
            for thing in things:
                if thing in ('(JS)', '--'):
                    continue
                mapped_things.append(THINGMAP[thing])
            reg[match.group(1)] = sorted(mapped_things)


def load_from_distributions_json(reg):

    distributions_filename = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..', 'distribution', 'distributions.json')
    with open(distributions_filename, 'r') as f:
        distributions = json.loads(f.read())

    for distname, distribution in sorted(distributions.items()):
        if not distribution.get('reponame'):
            continue
        key = distribution['reponame']
        # TODO: tack on latest version
        if 'impllangs' in distribution:
            reg[key] = distribution['impllangs']
        else:
            sys.stderr.write("WARNING: no impllangs on {}\n".format(distribution))


def main(options):

    reg = {}

    load_from_distributions_json(reg)

    # load_from_markdown_table(reg, sys.stdin)

    dump_project_matrix(reg)

    # rewrite_distributions(reg)


if __name__ == '__main__':
    argparser = ArgumentParser()
    options = argparser.parse_args(sys.argv[1:])
    main(options)