Run Pygments using Python 3

Python 2 is dying, <https://pythonclock.org/>, it's time to move on.

The create_pygmentize_bundle script now requires Python 3 to run, and will
generate a bundle with a python3 shebang. Technically the bundle is still
compatible with Python 2 as long as the shebang is modified.

Bug: T182851
Change-Id: Ifb9d0abf092e2c08d9a638a7dda3bda0bc808789
This commit is contained in:
Kunal Mehta 2017-12-27 18:51:19 -08:00
parent 2abcdb3b56
commit e50bce1036
2 changed files with 9 additions and 13 deletions

View file

@ -1,26 +1,22 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
Create a standalone, executable 'pygmentize' bundle. Create a standalone, executable 'pygmentize' bundle.
Author: Ori Livneh Author: Ori Livneh
""" """
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import hashlib import hashlib
import io import io
import os import os
import stat import stat
import textwrap import textwrap
import urllib2 import urllib.request
import xmlrpclib import xmlrpc.client
import zipfile import zipfile
PYGMENTIZE_LAUNCHER = textwrap.dedent(b'''\ PYGMENTIZE_LAUNCHER = textwrap.dedent('''\
#!/usr/bin/env python #!/usr/bin/env python3
import sys import sys
import pygments.cmdline import pygments.cmdline
@ -32,7 +28,7 @@ PYGMENTIZE_LAUNCHER = textwrap.dedent(b'''\
print('Querying PyPI for the latest Pygments release...') print('Querying PyPI for the latest Pygments release...')
pypi = xmlrpclib.ServerProxy('https://pypi.python.org/pypi') pypi = xmlrpc.client.ServerProxy('https://pypi.python.org/pypi')
latest_version = pypi.package_releases('Pygments')[0] latest_version = pypi.package_releases('Pygments')[0]
for release in pypi.release_urls('Pygments', latest_version): for release in pypi.release_urls('Pygments', latest_version):
if (release['packagetype'] == 'bdist_wheel' and if (release['packagetype'] == 'bdist_wheel' and
@ -44,7 +40,7 @@ for release in pypi.release_urls('Pygments', latest_version):
raise RuntimeError('No suitable package found.') raise RuntimeError('No suitable package found.')
print('Retrieving version %s (%s)...' % (latest_version, url)) print('Retrieving version %s (%s)...' % (latest_version, url))
req = urllib2.urlopen(url) req = urllib.request.urlopen(url)
buf = io.BytesIO(req.read()) buf = io.BytesIO(req.read())
print('Verifying...') print('Verifying...')
@ -58,8 +54,8 @@ with zipfile.ZipFile(buf, 'a') as zf:
data = buf.getvalue() data = buf.getvalue()
script_dir = os.path.dirname(os.path.abspath(__file__)) script_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(script_dir, 'pygmentize') file_path = os.path.join(script_dir, 'pygmentize')
with open(file_path, 'w') as f: with open(file_path, 'wb') as f:
f.write('#!/usr/bin/env python\n') f.write(b'#!/usr/bin/env python3\n')
f.write(data) f.write(data)
file_st = os.stat(file_path) file_st = os.stat(file_path)

Binary file not shown.