summaryrefslogtreecommitdiff
path: root/extensions/SyntaxHighlight_GeSHi/pygments/create_pygmentize_bundle
blob: 9f638b9c10f63f2978361835c26dda101e1f7ce0 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
  Create a standalone, executable 'pygmentize' bundle.
  Author: Ori Livneh

"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import hashlib
import io
import os
import stat
import textwrap
import urllib2
import xmlrpclib
import zipfile


PYGMENTIZE_LAUNCHER = textwrap.dedent(b'''\
  #!/usr/bin/env python

  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...')
pypi = xmlrpclib.ServerProxy('https://pypi.python.org/pypi')
latest_version = pypi.package_releases('Pygments')[0]
for release in pypi.release_urls('Pygments', latest_version):
    if (release['packagetype'] == 'bdist_wheel' and
            release['python_version'].startswith('2')):
        url = release['url']
        md5_digest = release['md5_digest']
        break
    else:
        raise RuntimeError('No suitable package found.')

print('Retreiving version %s (%s)...' % (latest_version, url))
req = urllib2.urlopen(url)
buf = io.BytesIO(req.read())

print('Verifying...')
if hashlib.md5(buf.getvalue()).hexdigest() != md5_digest:
    raise RuntimeError('MD5 checksum mismatch.')

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')
with open(file_path, 'w') as f:
    f.write('#!/usr/bin/env python\n')
    f.write(data)

file_st = os.stat(file_path)
os.chmod(file_path, file_st.st_mode | stat.S_IEXEC)

print('Done. Wrote %s bytes to %s' % (len(data), file_path))