source: dist/scripts/rebuild.py @ 3885

Revision 3885, 3.3 KB checked in by wehart, 3 years ago (diff)

Bug fix in rebuild.py ... this script needs to update coopr.py.

Updating coopr_install given recent change in coopr.misc (to excise coopr_install).
The script is now smaller...

Line 
1#!/usr/bin/env python
2"""
3Helper script to rebuild coopr.py using a preinstall ZIP file.
4"""
5
6# This script is adapted from the virtualenv rebuild-script.py file.
7# That script is distributed with the following license:
8"""
9Copyright (c) 2007 Ian Bicking and Contributors
10Copyright (c) 2009 Ian Bicking, The Open Planning Project
11Copyright (c) 2011 The virtualenv developers
12
13Permission is hereby granted, free of charge, to any person obtaining
14a copy of this software and associated documentation files (the
15"Software"), to deal in the Software without restriction, including
16without limitation the rights to use, copy, modify, merge, publish,
17distribute, sublicense, and/or sell copies of the Software, and to
18permit persons to whom the Software is furnished to do so, subject to
19the following conditions:
20
21The above copyright notice and this permission notice shall be
22included in all copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31"""
32
33import re
34import os
35import sys
36
37here = os.path.dirname(__file__)
38script = os.path.join(here, 'coopr.py')
39
40file_regex = re.compile(
41    r'##file (.*?)\n([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*"""\n(.*?)"""\.decode\("base64"\)\.decode\("zlib"\)\n',
42    re.S)
43file_template = '##file %(filename)s\n%(varname)s = """\n%(data)s""".decode("base64").decode("zlib")\n'
44
45def rebuild():
46    f = open(script, 'rb')
47    content = f.read()
48    f.close()
49    parts = []
50    last_pos = 0
51    match = None
52    for match in file_regex.finditer(content):
53        parts.append(content[last_pos:match.start()])
54        last_pos = match.end()
55        filename = match.group(1)
56        varname = match.group(2)
57        data = match.group(3)
58        print('Found reference to file %s' % filename)
59        f = open(os.path.join(here, 'coopr_release', filename), 'rb')
60        c = f.read()
61        f.close()
62        new_data = c.encode('zlib').encode('base64')
63        if new_data == data:
64            print('  Reference up to date (%s bytes)' % len(c))
65            parts.append(match.group(0))
66            continue
67        print('  Content changed (%s bytes -> %s bytes)' % (
68            zipped_len(data), len(c)))
69        new_match = file_template % dict(
70            filename=filename,
71            varname=varname,
72            data=new_data)
73        parts.append(new_match)
74    parts.append(content[last_pos:])
75    new_content = ''.join(parts)
76    if new_content != content:
77        sys.stdout.write('Content updated; overwriting... ')
78        f = open(script, 'wb')
79        f.write(new_content)
80        f.close()
81        print('done.')
82    else:
83        print('No changes in content')
84    if match is None:
85        print('No variables were matched/found')
86
87def zipped_len(data):
88    if not data:
89        return 'no data'
90    try:
91        return len(data.decode('base64').decode('zlib'))
92    except:
93        return 'unknown'
94
95if __name__ == '__main__':
96    rebuild()
97   
98
Note: See TracBrowser for help on using the repository browser.