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