| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Create a standalone, executable 'pygmentize' bundle. |
| 5 |
Author: Ori Livneh |
| 6 |
|
| 7 |
""" |
| 8 |
import hashlib |
| 9 |
import io |
| 10 |
import json |
| 11 |
import os |
| 12 |
import stat |
| 13 |
import textwrap |
| 14 |
import urllib.request |
| 15 |
import zipfile |
| 16 |
|
| 17 |
|
| 18 |
PYGMENTIZE_LAUNCHER = textwrap.dedent('''\ |
| 19 |
#!/usr/bin/env python3 |
| 20 |
|
| 21 |
import sys |
| 22 |
import pygments.cmdline |
| 23 |
try: |
| 24 |
sys.exit(pygments.cmdline.main(sys.argv)) |
| 25 |
except KeyboardInterrupt: |
| 26 |
sys.exit(1) |
| 27 |
''') |
| 28 |
|
| 29 |
|
| 30 |
print('Querying PyPI for the latest Pygments release...') |
| 31 |
req = urllib.request.urlopen('https://pypi.python.org/pypi/Pygments/json') |
| 32 |
data = json.loads(req.read().decode('utf-8')) |
| 33 |
latest_version = data['info']['version'] |
| 34 |
url = None |
| 35 |
for release in data['releases'][latest_version]: |
| 36 |
if (release['packagetype'] == 'bdist_wheel' and |
| 37 |
'py3' in release['python_version']): |
| 38 |
url = release['url'] |
| 39 |
digest = release['digests']['sha256'] |
| 40 |
break |
| 41 |
|
| 42 |
if not url: |
| 43 |
raise RuntimeError('No suitable package found.') |
| 44 |
|
| 45 |
print('Retrieving version %s (%s)...' % (latest_version, url)) |
| 46 |
req = urllib.request.urlopen(url) |
| 47 |
buf = io.BytesIO(req.read()) |
| 48 |
|
| 49 |
print('Verifying...') |
| 50 |
if hashlib.sha256(buf.getvalue()).hexdigest() != digest: |
| 51 |
raise RuntimeError('checksum mismatch!') |
| 52 |
|
| 53 |
print('Creating executable ZIP bundle...') |
| 54 |
with zipfile.ZipFile(buf, 'a') as zf: |
| 55 |
zf.writestr('__main__.py', PYGMENTIZE_LAUNCHER) |
| 56 |
|
| 57 |
data = buf.getvalue() |
| 58 |
script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 59 |
file_path = os.path.join(script_dir, 'pygmentize') |
| 60 |
with open(file_path, 'wb') as f: |
| 61 |
f.write(b'#!/usr/bin/env python3\n') |
| 62 |
f.write(data) |
| 63 |
|
| 64 |
file_st = os.stat(file_path) |
| 65 |
os.chmod(file_path, file_st.st_mode | stat.S_IEXEC) |
| 66 |
|
| 67 |
with open(os.path.join(script_dir, 'VERSION'), 'w') as f: |
| 68 |
f.write(latest_version + '\n') |
| 69 |
|
| 70 |
with open(os.path.join(script_dir, 'AUTHORS'), 'w') as f: |
| 71 |
url = 'https://raw.githubusercontent.com/pygments/pygments/refs/tags/' + latest_version + '/AUTHORS' |
| 72 |
req = urllib.request.urlopen(url) |
| 73 |
authors_text = req.read().decode('utf-8') |
| 74 |
f.write(authors_text) |
| 75 |
|
| 76 |
print('Done. Wrote %s bytes to %s' % (len(data), file_path)) |
| 77 |
print("Don't forget to run updateCSS.php and updateLexerList.php (in ../maintenance).") |