| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 """ |
| 3 Helper script to rebuild virtualenv.py from virtualenv_support |
| 4 """ |
| 5 from __future__ import print_function |
| 6 |
| 7 import os |
| 8 import re |
| 9 import codecs |
| 10 from zlib import crc32 as _crc32 |
| 11 |
| 12 |
| 13 def crc32(data): |
| 14 """Python version idempotent""" |
| 15 return _crc32(data) & 0xffffffff |
| 16 |
| 17 |
| 18 here = os.path.dirname(__file__) |
| 19 script = os.path.join(here, '..', 'virtualenv.py') |
| 20 |
| 21 gzip = codecs.lookup('zlib') |
| 22 b64 = codecs.lookup('base64') |
| 23 |
| 24 file_regex = re.compile( |
| 25 br'##file (.*?)\n([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*convert\("""\n(.*?)"""\)', |
| 26 re.S) |
| 27 file_template = b'##file %(filename)s\n%(varname)s = convert("""\n%(data)s""")' |
| 28 |
| 29 |
| 30 def rebuild(script_path): |
| 31 with open(script_path, 'rb') as f: |
| 32 script_content = f.read() |
| 33 parts = [] |
| 34 last_pos = 0 |
| 35 match = None |
| 36 for match in file_regex.finditer(script_content): |
| 37 parts += [script_content[last_pos:match.start()]] |
| 38 last_pos = match.end() |
| 39 filename, fn_decoded = match.group(1), match.group(1).decode() |
| 40 varname = match.group(2) |
| 41 data = match.group(3) |
| 42 |
| 43 print('Found file %s' % fn_decoded) |
| 44 pathname = os.path.join(here, '..', 'virtualenv_embedded', fn_decoded) |
| 45 |
| 46 with open(pathname, 'rb') as f: |
| 47 embedded = f.read() |
| 48 new_crc = crc32(embedded) |
| 49 new_data = b64.encode(gzip.encode(embedded)[0])[0] |
| 50 |
| 51 if new_data == data: |
| 52 print(' File up to date (crc: %08x)' % new_crc) |
| 53 parts += [match.group(0)] |
| 54 continue |
| 55 # Else: content has changed |
| 56 crc = crc32(gzip.decode(b64.decode(data)[0])[0]) |
| 57 print(' Content changed (crc: %08x -> %08x)' % |
| 58 (crc, new_crc)) |
| 59 new_match = file_template % { |
| 60 b'filename': filename, |
| 61 b'varname': varname, |
| 62 b'data': new_data |
| 63 } |
| 64 parts += [new_match] |
| 65 |
| 66 parts += [script_content[last_pos:]] |
| 67 new_content = b''.join(parts) |
| 68 |
| 69 if new_content != script_content: |
| 70 print('Content updated; overwriting... ', end='') |
| 71 with open(script_path, 'wb') as f: |
| 72 f.write(new_content) |
| 73 print('done.') |
| 74 else: |
| 75 print('No changes in content') |
| 76 if match is None: |
| 77 print('No variables were matched/found') |
| 78 |
| 79 if __name__ == '__main__': |
| 80 rebuild(script) |
| OLD | NEW |