diff options
author | Donald Stufft <donald@stufft.io> | 2016-01-19 19:55:31 -0500 |
---|---|---|
committer | Donald Stufft <donald@stufft.io> | 2016-01-19 19:55:31 -0500 |
commit | 4ac44d2a1508e86d78e06fa158373e723f3b4dcb (patch) | |
tree | bdcd6e4b760bf325d4f079a8f4c1612927df4cf4 /bin/rebuild-script.py | |
parent | 134c9a6c67081cda67befd61bae68a999ed00f48 (diff) | |
parent | 130399fdd13152d09e5cb133bf72abd95dc7e5b0 (diff) | |
download | virtualenv-14.0.0.tar.gz |
Merge branch 'develop'14.0.0
Diffstat (limited to 'bin/rebuild-script.py')
-rwxr-xr-x | bin/rebuild-script.py | 88 |
1 files changed, 45 insertions, 43 deletions
diff --git a/bin/rebuild-script.py b/bin/rebuild-script.py index 44fb129..a816af3 100755 --- a/bin/rebuild-script.py +++ b/bin/rebuild-script.py @@ -2,70 +2,72 @@ """ Helper script to rebuild virtualenv.py from virtualenv_support """ +from __future__ import print_function -import re import os -import sys +import re +import codecs +from zlib import crc32 here = os.path.dirname(__file__) script = os.path.join(here, '..', 'virtualenv.py') +gzip = codecs.lookup('zlib') +b64 = codecs.lookup('base64') + file_regex = re.compile( - r'##file (.*?)\n([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*convert\("""(.*?)"""\)', + br'##file (.*?)\n([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*convert\("""\n(.*?)"""\)', re.S) -file_template = '##file %(filename)s\n%(varname)s = convert("""\n%(data)s""")' +file_template = b'##file %(filename)s\n%(varname)s = convert("""\n%(data)s""")' -def rebuild(): - f = open(script, 'rb') - content = f.read() - f.close() +def rebuild(script_path): + with open(script_path, 'rb') as f: + script_content = f.read() parts = [] last_pos = 0 match = None - for match in file_regex.finditer(content): - parts.append(content[last_pos:match.start()]) + for match in file_regex.finditer(script_content): + parts += [script_content[last_pos:match.start()]] last_pos = match.end() - filename = match.group(1) + filename, fn_decoded = match.group(1), match.group(1).decode() varname = match.group(2) data = match.group(3) - print('Found reference to file %s' % filename) - pathname = os.path.join(here, '..', 'virtualenv_embedded', filename) - f = open(pathname, 'rb') - c = f.read() - f.close() - new_data = c.encode('zlib').encode('base64') + + print('Found file %s' % fn_decoded) + pathname = os.path.join(here, '..', 'virtualenv_embedded', fn_decoded) + + with open(pathname, 'rb') as f: + embedded = f.read() + new_crc = crc32(embedded) + new_data = b64.encode(gzip.encode(embedded)[0])[0] + if new_data == data: - print(' Reference up to date (%s bytes)' % len(c)) - parts.append(match.group(0)) + print(' File up to date (crc: %s)' % new_crc) + parts += [match.group(0)] continue - print(' Content changed (%s bytes -> %s bytes)' % ( - zipped_len(data), len(c))) - new_match = file_template % dict( - filename=filename, - varname=varname, - data=new_data) - parts.append(new_match) - parts.append(content[last_pos:]) - new_content = ''.join(parts) - if new_content != content: - sys.stdout.write('Content updated; overwriting... ') - f = open(script, 'wb') - f.write(new_content) - f.close() + # Else: content has changed + crc = crc32(gzip.decode(b64.decode(data)[0])[0]) + print(' Content changed (crc: %s -> %s)' % + (crc, new_crc)) + new_match = file_template % { + b'filename': filename, + b'varname': varname, + b'data': new_data + } + parts += [new_match] + + parts += [script_content[last_pos:]] + new_content = b''.join(parts) + + if new_content != script_content: + print('Content updated; overwriting... ', end='') + with open(script_path, 'wb') as f: + f.write(new_content) print('done.') else: print('No changes in content') if match is None: print('No variables were matched/found') -def zipped_len(data): - if not data: - return 'no data' - try: - return len(data.decode('base64').decode('zlib')) - except: - return 'unknown' - if __name__ == '__main__': - rebuild() - + rebuild(script) |