This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
update_ptext.py
91 lines (64 loc) · 2.15 KB
/
update_ptext.py
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
#!/usr/bin/env python3
"""Update the copy of ptext in Pygame Zero.
ptext is not yet packaged on PyPI; this script exists to mirror it into the
local repository to make it easily installable.
"""
import json
import base64
import subprocess
from urllib.parse import urljoin
from urllib.request import build_opener
FILE = 'ptext.py'
DEST = 'pgzero/ptext.py'
REPO_URL = 'https://api.github.com/repos/cosmologicon/pygame-text/'
HEADER = '''"""pygame-text - high-level text rendering with Pygame.
This module is directly copied from
https://github.com/cosmologicon/pygame-text
at revision {sha}
and used under CC0.
"""
'''
# Customise the opener here if you need to
opener = build_opener()
def read_json(url):
"""Download and decode a JSON resource from the given URL."""
resp = opener.open(url)
charset = resp.headers.get_content_charset()
data = resp.read().decode(charset)
return json.loads(data)
def get_tree():
"""Download the repository tree, returning a decoded JSON structure."""
print('Downloading repository tree...')
url = urljoin(REPO_URL, 'git/trees/HEAD')
return read_json(url)
def get_file(file):
"""Download the tree state and named file.
Return a tuple of the current repo version hash and the file's data.
"""
tree = get_tree()
for f in tree['tree']:
if f['path'] == file:
break
else:
raise ValueError("Could not find ptext module to download.")
url = f['url']
print('Downloading', file, 'module...')
blob = read_json(url)
data = base64.b64decode(blob['content']).decode('utf8')
return tree['sha'], data
def update_local():
"""Download a new copy of the file and write it to DEST.
Include a header based on the template HEADER.
"""
sha, data = get_file(FILE)
header = HEADER.format(sha=sha)
with open(DEST, 'w', encoding='utf8') as f:
f.write(header + data)
print("Updated", FILE, "to revision", sha[:7])
autopep8()
def autopep8():
"""Use autopep8 to fix formatting problems."""
print("Running autopep8")
subprocess.check_call(['autopep8', '-i', DEST])
if __name__ == '__main__':
update_local()