Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | """ |
michael@0 | 3 | Helper script to rebuild virtualenv.py from virtualenv_support |
michael@0 | 4 | """ |
michael@0 | 5 | |
michael@0 | 6 | import re |
michael@0 | 7 | import os |
michael@0 | 8 | import sys |
michael@0 | 9 | |
michael@0 | 10 | here = os.path.dirname(__file__) |
michael@0 | 11 | script = os.path.join(here, '..', 'virtualenv.py') |
michael@0 | 12 | |
michael@0 | 13 | file_regex = re.compile( |
michael@0 | 14 | r'##file (.*?)\n([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*convert\("""(.*?)"""\)', |
michael@0 | 15 | re.S) |
michael@0 | 16 | file_template = '##file %(filename)s\n%(varname)s = convert("""\n%(data)s""")' |
michael@0 | 17 | |
michael@0 | 18 | def rebuild(): |
michael@0 | 19 | f = open(script, 'rb') |
michael@0 | 20 | content = f.read() |
michael@0 | 21 | f.close() |
michael@0 | 22 | parts = [] |
michael@0 | 23 | last_pos = 0 |
michael@0 | 24 | match = None |
michael@0 | 25 | for match in file_regex.finditer(content): |
michael@0 | 26 | parts.append(content[last_pos:match.start()]) |
michael@0 | 27 | last_pos = match.end() |
michael@0 | 28 | filename = match.group(1) |
michael@0 | 29 | varname = match.group(2) |
michael@0 | 30 | data = match.group(3) |
michael@0 | 31 | print('Found reference to file %s' % filename) |
michael@0 | 32 | pathname = os.path.join(here, '..', 'virtualenv_embedded', filename) |
michael@0 | 33 | f = open(pathname, 'rb') |
michael@0 | 34 | c = f.read() |
michael@0 | 35 | f.close() |
michael@0 | 36 | new_data = c.encode('zlib').encode('base64') |
michael@0 | 37 | if new_data == data: |
michael@0 | 38 | print(' Reference up to date (%s bytes)' % len(c)) |
michael@0 | 39 | parts.append(match.group(0)) |
michael@0 | 40 | continue |
michael@0 | 41 | print(' Content changed (%s bytes -> %s bytes)' % ( |
michael@0 | 42 | zipped_len(data), len(c))) |
michael@0 | 43 | new_match = file_template % dict( |
michael@0 | 44 | filename=filename, |
michael@0 | 45 | varname=varname, |
michael@0 | 46 | data=new_data) |
michael@0 | 47 | parts.append(new_match) |
michael@0 | 48 | parts.append(content[last_pos:]) |
michael@0 | 49 | new_content = ''.join(parts) |
michael@0 | 50 | if new_content != content: |
michael@0 | 51 | sys.stdout.write('Content updated; overwriting... ') |
michael@0 | 52 | f = open(script, 'wb') |
michael@0 | 53 | f.write(new_content) |
michael@0 | 54 | f.close() |
michael@0 | 55 | print('done.') |
michael@0 | 56 | else: |
michael@0 | 57 | print('No changes in content') |
michael@0 | 58 | if match is None: |
michael@0 | 59 | print('No variables were matched/found') |
michael@0 | 60 | |
michael@0 | 61 | def zipped_len(data): |
michael@0 | 62 | if not data: |
michael@0 | 63 | return 'no data' |
michael@0 | 64 | try: |
michael@0 | 65 | return len(data.decode('base64').decode('zlib')) |
michael@0 | 66 | except: |
michael@0 | 67 | return 'unknown' |
michael@0 | 68 | |
michael@0 | 69 | if __name__ == '__main__': |
michael@0 | 70 | rebuild() |
michael@0 | 71 |