File: update.py

package info (click to toggle)
python-astropy-affiliated 1.5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 92 kB
  • sloc: python: 113; makefile: 4
file content (134 lines) | stat: -rwxr-xr-x 4,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! /usr/bin/env python3

import urllib.request
import json


def add_debian_packages(packages):
    import apt
    c = apt.Cache()

    for pkg in packages:
        for pkg_name in pkg['pypi_name'].lower(), pkg['name'].lower():
            if pkg_name.startswith('python-'):
                pkg_name = pkg_name[len('python-'):]
            for prefix in 'python-', 'python3-':
                debian_pkg = c.get(prefix + pkg_name)
                if debian_pkg:
                    l = pkg.setdefault('debian_pkg', [])
                    if debian_pkg.name not in l:
                        l.append(debian_pkg.name)

def add_github_stars(packages):
    import github
    import os
    g = github.Github(os.environ['GITHUB_TOKEN'])

    for pkg in packages:
        github_name = pkg['repo_url']
        if github_name.startswith('https://github.com/'):
            github_name = github_name[len('https://github.com/'):]
            repo = g.get_repo(github_name)
            pkg['repo_stars'] = len(list(repo.get_stargazers()))
        else:
            print(pkg['name'], github_name)
            pkg['repo_stars'] = -1


def add_pypi(packages):
    for pkg in packages:
        r = urllib.request.urlopen('https://pypi.python.org/pypi/'
                                   + pkg['pypi_name']
                                   + '/json')
        j = json.load(r)
        pkg['pypi_license'] = j['info']['license']
        pkg['pypi_version'] = j['info']['version']

def write_package_table(fp, packages):
    fp.write("Total {} packages, {} of them are already in Debian\n\n"
             .format(len(packages),
                     len(list(filter(lambda p: 'debian_pkg' in p, packages)))))
    for pkg in packages:
        fp.write("{debian:1} {stars:4} {api:1} {prov:1} {name}\n".format(
            debian = '*' if 'debian_pkg' in pkg else ' ',
            stars = pkg.get('repo_stars', 0),
            api = 'S' if pkg['stable'] else 'U',
            prov = 'P' if pkg['provisional'] else 'A',
            name = pkg['name']
        ))

    fp.write('''
Columns:
 - Debian Status:
    * package in Debian
 - Github Stars
 - API stability
    S stable
    U unstable
 - Accepted as Astropy Affiliated package
    A accepted
    P provisionally accepted
 - Name
''')

def update_control(packages):
    from debian import deb822
    import itertools
    cname = 'debian/control'
    with open(cname) as fp:
        header, py3 = deb822.Deb822.iter_paragraphs(fp)
    
    py3_list = sorted(itertools.chain(*([p for p in pkg.get('debian_pkg', [])
                                         if p.startswith("python3-")]
                                        for pkg in packages)))
    py3_old = sorted(p.strip() for p in py3['Recommends'].split(','))
    py3['Recommends'] = ',\n            '.join(py3_list)
    
    with open(cname, 'w') as fp:
        fp.write(('\n\n'.join('\n'.join(key + ": " + val
                                        for key, val in par.items())
                              for par in (header, py3))))
        fp.write('\n')

    py3_added = set(py3_list) - set(py3_old)
    if len(py3_added):
        print(' * Added ' + ', '.join(py3_added)
              + ' to python3-astropy-affilliated')
    py3_removed = set(py3_old) - set(py3_list)
    if len(py3_removed):
        print(' * Removed ' + ', '.join(py3_removed)
              + ' from python3-astropy-affilliated')

def write_todo(fp, packages):
    for pkg in packages:
        if 'debian_pkg' in pkg:
            continue
        fp.write('''* Package name     : {name}
  Version          : {pypi_version}
  Upstream Author  : {maintainer}
* URL              : {home_url}
* License          : {pypi_license}
  Programming lang : Python
  Upstream git     : {repo_url} ({repo_stars} stars)
  Pypi URL         : https://pypi.python.org/pypi/{pypi_name}
  Description      : {description}

'''.format(**pkg))

r = urllib.request.urlopen("https://www.astropy.org/affiliated/registry.json")
registry = json.load(r)
packages = registry['packages']
add_debian_packages(packages)
update_control(packages)
          
add_pypi(packages)
add_github_stars(packages)
with open('affiliated.json', 'w') as fp:
    json.dump(registry, fp, indent=4)

packages.sort(key=lambda pkg: pkg.get('repo_stars', -1), reverse=True)
with open('affiliated-status.txt', 'w') as fp:
    write_package_table(fp, packages)

with open('TODO', 'w') as fp:
    write_todo(fp, packages)