2017-12-28 02:51:19 +00:00
|
|
|
#!/usr/bin/env python3
|
2015-06-20 01:04:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Create a standalone, executable 'pygmentize' bundle.
|
|
|
|
Author: Ori Livneh
|
|
|
|
|
|
|
|
"""
|
|
|
|
import hashlib
|
|
|
|
import io
|
2020-11-23 21:23:06 +00:00
|
|
|
import json
|
2015-06-20 01:04:36 +00:00
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
import textwrap
|
2017-12-28 02:51:19 +00:00
|
|
|
import urllib.request
|
2015-06-20 01:04:36 +00:00
|
|
|
import zipfile
|
|
|
|
|
|
|
|
|
2017-12-28 02:51:19 +00:00
|
|
|
PYGMENTIZE_LAUNCHER = textwrap.dedent('''\
|
|
|
|
#!/usr/bin/env python3
|
2015-06-20 01:04:36 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import pygments.cmdline
|
|
|
|
try:
|
|
|
|
sys.exit(pygments.cmdline.main(sys.argv))
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(1)
|
|
|
|
''')
|
|
|
|
|
|
|
|
|
|
|
|
print('Querying PyPI for the latest Pygments release...')
|
2020-11-23 21:23:06 +00:00
|
|
|
req = urllib.request.urlopen('https://pypi.python.org/pypi/Pygments/json')
|
|
|
|
data = json.loads(req.read().decode('utf-8'))
|
|
|
|
latest_version = data['info']['version']
|
2019-01-09 22:43:22 +00:00
|
|
|
url = None
|
2020-11-23 21:23:06 +00:00
|
|
|
for release in data['releases'][latest_version]:
|
2015-06-20 01:04:36 +00:00
|
|
|
if (release['packagetype'] == 'bdist_wheel' and
|
2020-02-14 15:12:35 +00:00
|
|
|
'py3' in release['python_version']):
|
2015-06-20 01:04:36 +00:00
|
|
|
url = release['url']
|
2020-11-23 21:23:06 +00:00
|
|
|
digest = release['digests']['sha256']
|
2015-06-20 01:04:36 +00:00
|
|
|
break
|
2019-01-09 22:43:22 +00:00
|
|
|
|
|
|
|
if not url:
|
2015-06-20 01:04:36 +00:00
|
|
|
raise RuntimeError('No suitable package found.')
|
|
|
|
|
2016-03-03 19:44:01 +00:00
|
|
|
print('Retrieving version %s (%s)...' % (latest_version, url))
|
2017-12-28 02:51:19 +00:00
|
|
|
req = urllib.request.urlopen(url)
|
2015-06-20 01:04:36 +00:00
|
|
|
buf = io.BytesIO(req.read())
|
|
|
|
|
|
|
|
print('Verifying...')
|
2020-11-23 21:23:06 +00:00
|
|
|
if hashlib.sha256(buf.getvalue()).hexdigest() != digest:
|
|
|
|
raise RuntimeError('checksum mismatch!')
|
2015-06-20 01:04:36 +00:00
|
|
|
|
|
|
|
print('Creating executable ZIP bundle...')
|
|
|
|
with zipfile.ZipFile(buf, 'a') as zf:
|
|
|
|
zf.writestr('__main__.py', PYGMENTIZE_LAUNCHER)
|
|
|
|
|
|
|
|
data = buf.getvalue()
|
|
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
file_path = os.path.join(script_dir, 'pygmentize')
|
2017-12-28 02:51:19 +00:00
|
|
|
with open(file_path, 'wb') as f:
|
|
|
|
f.write(b'#!/usr/bin/env python3\n')
|
2015-06-20 01:04:36 +00:00
|
|
|
f.write(data)
|
|
|
|
|
|
|
|
file_st = os.stat(file_path)
|
|
|
|
os.chmod(file_path, file_st.st_mode | stat.S_IEXEC)
|
|
|
|
|
2016-03-03 19:44:01 +00:00
|
|
|
with open(os.path.join(script_dir, 'VERSION'), 'w') as f:
|
|
|
|
f.write(latest_version + '\n')
|
|
|
|
|
2015-06-20 01:04:36 +00:00
|
|
|
print('Done. Wrote %s bytes to %s' % (len(data), file_path))
|
2020-11-23 21:23:06 +00:00
|
|
|
print("Don't forget to run updateCSS.php and updateLexerList.php (in ../maintenance).")
|