|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | # -*- encoding: utf-8 -*-
|
3 | 3 | # Kw’s Release Tools/Python Project Template
|
4 |
| -# GitHub Releases Creator |
| 4 | +# GitHub Release Creator |
5 | 5 | # Copyright © 2013-2015, Chris Warrick.
|
6 | 6 | # All rights reserved.
|
7 | 7 | #
|
|
33 | 33 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
34 | 34 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
35 | 35 |
|
36 |
| -# Arguments: |
37 |
| -# FILE BASEDIR REPO TAG |
38 |
| - |
39 | 36 | """
|
40 | 37 | Create GitHub releases out of changelogs.
|
41 | 38 |
|
42 |
| -Usage: .pypt/commitlog FILE BASEDIR REPO TAG, where |
| 39 | +Usage: .pypt/commitlog FILE BASEDIR REPOSITORY TAG, where |
43 | 40 | FILE is the path to the file to use, which can be
|
44 | 41 | a plain .md file or a CMFN file,
|
45 | 42 | BASEDIR is the project directory,
|
46 |
| - REPO is the full GitHub repository name (user/repo), |
| 43 | + REPOSITORY is the full GitHub repository name (user/repo), |
47 | 44 | TAG is the tag to write to.
|
48 | 45 | All paths should be absolute.
|
49 | 46 | """
|
50 | 47 |
|
| 48 | +import argparse |
| 49 | +import json |
51 | 50 | import re
|
52 | 51 | import requests
|
53 |
| -import json |
| 52 | +import sys |
54 | 53 | from os.path import join as pjoin
|
55 |
| -from sys import argv |
56 | 54 |
|
57 |
| -_script, FILE, BASEDIR, REPO, TAG = argv |
58 | 55 |
|
59 |
| -with open(pjoin(BASEDIR, '.pypt', 'gh-token')) as fh: |
60 |
| - TOKEN = fh.read().strip() |
| 56 | +def main(): |
| 57 | + """ghrel main function.""" |
| 58 | + parser = argparse.ArgumentParser( |
| 59 | + description="GitHub Release Creator " |
| 60 | + "(part of Chris Warrick's Python Project Template)") |
| 61 | + parser.add_argument('filename', metavar='FILE', nargs=1, |
| 62 | + help='File to parse (Markdown or commitlog)') |
| 63 | + parser.add_argument('basedir', metavar='BASEDIR', nargs=1, |
| 64 | + help='Project directory (must contain .pypt/gh-token)') |
| 65 | + parser.add_argument('repo', metavar='REPOSITORY', nargs=1, |
| 66 | + help='GitHub repository (owner/repo)') |
| 67 | + parser.add_argument('tag', metavar='TAG', nargs=1, |
| 68 | + help='Tag to create release for (vX.Y.Z)') |
| 69 | + args = parser.parse_args() |
| 70 | + # nargs gets you lists, not strings |
| 71 | + filename = args.filename[0] |
| 72 | + basedir = args.basedir[0] |
| 73 | + repo = args.repo[0] |
| 74 | + tag = args.tag[0] |
61 | 75 |
|
62 |
| -HEADERS = { |
63 |
| - 'User-Agent': 'Kwpolska/python-project-template', |
64 |
| - 'Authorization': 'token ' + TOKEN, |
65 |
| -} |
| 76 | + with open(pjoin(basedir, '.pypt', 'gh-token')) as fh: |
| 77 | + token = fh.read().strip() |
66 | 78 |
|
67 |
| -with open(FILE) as fh: |
68 |
| - fdata = fh.read() |
69 |
| - e = re.findall( |
70 |
| - '#~ CHANGELOG MESSAGE START ~#\n(.*?)\n#~ CHANGELOG MESSAGE END ~#', |
71 |
| - fdata, flags=re.S) |
| 79 | + headers = { |
| 80 | + 'User-Agent': 'Kwpolska/python-project-template', |
| 81 | + 'Authorization': 'token ' + token, |
| 82 | + } |
72 | 83 |
|
73 |
| - if e: |
74 |
| - # parse as a CMFN file, replace backticks (reST->Markdown) |
75 |
| - message = e[0].replace('``', '`') |
76 |
| - else: |
77 |
| - # parse as a plain Markdown file |
78 |
| - message = fdata |
| 84 | + with open(filename) as fh: |
| 85 | + fdata = fh.read() |
| 86 | + e = re.findall( |
| 87 | + '#~ CHANGELOG MESSAGE START ~#\n(.*?)\n' |
| 88 | + '#~ CHANGELOG MESSAGE END ~#', |
| 89 | + fdata, flags=re.S) |
| 90 | + |
| 91 | + if e: |
| 92 | + # parse as a CMFN file, replace backticks (reST -> Markdown) |
| 93 | + message = e[0].replace('``', '`') |
| 94 | + else: |
| 95 | + # parse as a plain Markdown file |
| 96 | + message = fdata |
79 | 97 |
|
| 98 | + r = requests.post( |
| 99 | + 'https://api.github.com/repos/{0}/releases'.format(repo), |
| 100 | + data=json.dumps({'tag_name': tag, 'body': message}), |
| 101 | + headers=headers) |
80 | 102 |
|
81 |
| -r = requests.post( |
82 |
| - 'https://api.github.com/repos/{0}/releases'.format(REPO), |
83 |
| - data=json.dumps({'tag_name': TAG, 'body': message}), |
84 |
| - headers=HEADERS) |
| 103 | + if r.status_code == 201: |
| 104 | + print("GitHub Release created: {0}".format(r.json()['html_url'])) |
| 105 | + else: |
| 106 | + print("GitHub Release failed: {0}".format(r.text)) |
| 107 | + return 1 |
85 | 108 |
|
86 |
| -if r.status_code == 201: |
87 |
| - print("GitHub Release created: {0}".format(r.json()['html_url'])) |
88 |
| -else: |
89 |
| - print("GitHub Release failed: {0}".format(r.text)) |
90 |
| - exit(1) |
| 109 | +if __name__ == '__main__': |
| 110 | + sys.exit(main()) |
0 commit comments