summaryrefslogtreecommitdiff
path: root/source/release_notes.py
blob: fc3bbb3b0114456cf91776970cb998ad5ebc3ff6 (plain) (blame)
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
#!/usr/bin/env python
from __future__ import print_function

import os
import math
from collections import OrderedDict

try:
    from urllib.request import urlopen
except ImportError:
    from urllib2 import urlopen

try:
    import github3
    from github3 import organization
    from github3 import GitHubError
except ImportError:
    print('github3.py required.')
    print('pip install https+git://github.com/sigmavirus24/github3.py')
    exit(1)

from package_manifest import ARCHITECTURE, get_repodata


header = """
Release Notes
=============

For individual package release notes, please follow the links below.
Note that not all packages have release notes, and may simply provide
numbered releases.

"""

item = """
-  `{} <{}>`__
"""

#-------------------------------------------------------------------------------

def any_releases(url):
    """Check if there are any releases in a given github release page

    Parameters
    ----------
    url: str
        URL of a valid github reposoitory release pages

    Returns
    -------
    any_releases: bool
        True if there are found releases, False otherwise

    """

    no_release_msg = b"<h3>There aren\xe2\x80\x99t any releases here</h3>"

    if no_release_msg in urlopen(url).read():
        return False

    return True

#-------------------------------------------------------------------------------

def pull_release_notes(org, outfile):
    """ Get urls for release notes for all astroconda packages

    Only repositories that are included in ANY astroconda distribution (linux or OSX)
    with tagged github releases will be included in the output release notes
    rst file.

    Parameters
    ----------
    org: github3 organization instance
        All repositories from this org will be parsed for releases
    outfile: str
        Filename to write urls to.

    """

    astroconda_packages = {item['name'] for arch in ARCHITECTURE for item in get_repodata(arch).values()}

    with open(outfile, 'w+') as mdout:
        # Write header (main title)
        print(header, file=mdout)

        # Sort repositories inline alphabetically, because otherwise its seemingly random order
        for repository in sorted(list(org.iter_repos()), key=lambda k: k.name):

            if not repository.name in astroconda_packages:
                continue

            release_url = repository.html_url + '/releases'

            if not any_releases(release_url):
                continue

            print(item.format(repository.name.upper(), release_url), file=mdout)

#-------------------------------------------------------------------------------

def generate_release_notes():
    """Main function to create the release_notes.rst pages
    """

    owner = 'spacetelescope'
    org = github3.organization(owner)

    outfile = os.path.join('source', 'release_notes.rst')

    try:
        pull_release_notes(org, outfile)
    except GitHubError as e:
        print(e)
        exit(1)

#-------------------------------------------------------------------------------

if __name__ == '__main__':
    generate_release_notes()