Mercurial > treepkg > treepkg
annotate treepkg/util.py @ 495:ca95be9d033a
add tests for determine debian upstream version
fix calling determine_upstream_version in determine_package_version
update authors
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Thu, 14 Oct 2010 07:49:50 +0000 |
parents | 31b64ebe4b42 |
children | aa90ea7778a5 |
rev | line source |
---|---|
495
ca95be9d033a
add tests for determine debian upstream version
Bjoern Ricks <bricks@intevation.de>
parents:
494
diff
changeset
|
1 # Copyright (C) 2007, 2008, 2010 by Intevation GmbH |
0 | 2 # Authors: |
3 # Bernhard Herzog <bh@intevation.de> | |
495
ca95be9d033a
add tests for determine debian upstream version
Bjoern Ricks <bricks@intevation.de>
parents:
494
diff
changeset
|
4 # Bjoern Ricks <bjoern.ricks@intevation.de> |
ca95be9d033a
add tests for determine debian upstream version
Bjoern Ricks <bricks@intevation.de>
parents:
494
diff
changeset
|
5 # Andre Heinecke <andre.heinecke@intevation.de> |
0 | 6 # |
7 # This program is free software under the GPL (>=v2) | |
8 # Read the file COPYING coming with the software for details. | |
9 | |
10 """Collection of functions that didn't fit elsewhere""" | |
11 | |
12 import os | |
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
13 import re |
0 | 14 import tempfile |
4
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
15 import shutil |
169
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
16 import fnmatch |
344
f06f707d9fda
merged branches/scratchbox into trunk
Bjoern Ricks <bricks@intevation.de>
parents:
172
diff
changeset
|
17 import pwd |
394 | 18 import os.path |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
19 |
458
61e72399914c
Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents:
457
diff
changeset
|
20 try: |
61e72399914c
Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents:
457
diff
changeset
|
21 from hashlib import md5 as new_md5 |
61e72399914c
Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents:
457
diff
changeset
|
22 except ImportError: |
61e72399914c
Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents:
457
diff
changeset
|
23 # fall back to md5 for Python versions before 2.5 |
61e72399914c
Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents:
457
diff
changeset
|
24 from md5 import new as new_md5 |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
25 |
0 | 26 import run |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
27 from cmdexpand import cmdexpand |
0 | 28 |
29 | |
4
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
30 def import_dotted_name(dotted_name): |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
31 module = __import__(dotted_name) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
32 for name in dotted_name.split(".")[1:]: |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
33 module = getattr(module, name) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
34 return module |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
35 |
0 | 36 def extract_value_for_key(lines, key): |
37 """Parses a sequence of strings for a key and returns the associated value | |
38 | |
39 The function determines the first string in lines that starts with | |
40 key. It returns the rest of the lines stripped of leading and | |
41 trailing whitespace. | |
83
83e48a76f759
Document what extract_value_for_key returns when the key is not found
Bernhard Herzog <bh@intevation.de>
parents:
57
diff
changeset
|
42 |
83e48a76f759
Document what extract_value_for_key returns when the key is not found
Bernhard Herzog <bh@intevation.de>
parents:
57
diff
changeset
|
43 If the key is not found in lines, the function returns None. |
0 | 44 """ |
45 for line in lines: | |
46 if line.startswith(key): | |
47 return line[len(key):].strip() | |
48 | |
49 def extract_lsm_version(lsm_file): | |
50 return extract_value_for_key(open(lsm_file), "Version:") | |
51 | |
52 def debian_changelog_version(changelog): | |
53 """Returns the newest version in a debian changelog.""" | |
54 output = run.capture_output(["dpkg-parsechangelog", "-l" + changelog]) | |
55 return extract_value_for_key(output.splitlines(), "Version:") | |
56 | |
494
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
57 def extract_cmakefile_version(cmakelist): |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
58 """ Returns the version mentioned in a CMakeList.txt """ |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
59 major = re.compile(r"VERSION_MAJOR\s+(\d+)", re.IGNORECASE) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
60 minor = re.compile(r"VERSION_MINOR\s+(\d+)", re.IGNORECASE) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
61 patch = re.compile(r"VERSION_PATCH\s+(\d+)", re.IGNORECASE) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
62 version = "" |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
63 try: |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
64 for line in open(cmakelist): |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
65 major_match = major.match(line) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
66 minor_match = minor.match(line) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
67 patch_match = patch.match(line) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
68 if major_match: |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
69 version = major_match.group(1) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
70 if minor_match and version: |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
71 version += "." + minor_match.group(1) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
72 if patch_match: |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
73 version += "." + patch_match.group(1) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
74 except: pass |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
75 finally: |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
76 return version |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
77 |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
78 def extract_configureac_version(configure_ac): |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
79 match = re.match(r"m4_define\(\[?my_version\]?, \[([^]]+)\]\)", |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
80 line) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
81 if match: |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
82 return match.group(1) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
83 |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
84 match = re.match(r"AC_INIT\([a-zA-Z_]+, ([0-9.]+)", line) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
85 if match: |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
86 return match.group(1) |
31b64ebe4b42
determine upstream_version of a package
Bjoern Ricks <bricks@intevation.de>
parents:
462
diff
changeset
|
87 return "" |
0 | 88 |
89 def ensure_directory(directory): | |
90 """Creates directory and all its parents. | |
91 | |
92 Unlike os.makedirs, this function doesn't throw an exception | |
93 """ | |
94 if not os.path.isdir(directory): | |
95 os.makedirs(directory) | |
96 | |
169
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
97 def listdir_abs(directory, pattern=None): |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
98 """Like os.listdir, but returns a list of absolute pathnames. |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
99 Optionally, a glob pattern can be given to restrict the names |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
100 returned by the function. |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
101 """ |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
102 filenames = os.listdir(directory) |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
103 if pattern is not None: |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
104 filenames = fnmatch.filter(filenames, pattern) |
261b75d7b972
Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents:
160
diff
changeset
|
105 return [os.path.join(directory, filename) for filename in filenames] |
0 | 106 |
4
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
107 def copytree(src, dst, symlinks=False): |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
108 """Recursively copy a directory tree using copy2(). |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
109 |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
110 This version is basically the same as the one in the shutil module |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
111 in the python standard library, however, it's OK if the destination |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
112 directory already exists. |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
113 |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
114 If the optional symlinks flag is true, symbolic links in the |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
115 source tree result in symbolic links in the destination tree; if |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
116 it is false, the contents of the files pointed to by symbolic |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
117 links are copied. |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
118 """ |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
119 names = os.listdir(src) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
120 ensure_directory(dst) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
121 errors = [] |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
122 for name in names: |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
123 srcname = os.path.join(src, name) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
124 dstname = os.path.join(dst, name) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
125 try: |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
126 if symlinks and os.path.islink(srcname): |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
127 linkto = os.readlink(srcname) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
128 os.symlink(linkto, dstname) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
129 elif os.path.isdir(srcname): |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
130 copytree(srcname, dstname, symlinks) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
131 else: |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
132 shutil.copy2(srcname, dstname) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
133 # XXX What about devices, sockets etc.? |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
134 except (IOError, os.error), why: |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
135 errors.append((srcname, dstname, why)) |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
136 if errors: |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
137 raise Error, errors |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
138 |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
139 |
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
140 |
57
d0d08c7e7d37
allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents:
14
diff
changeset
|
141 def writefile(filename, contents, permissions=None): |
0 | 142 """Write contents to filename in an atomic way. |
143 | |
144 The contents are first written to a temporary file in the same | |
145 directory as filename. Once the contents are written, the temporary | |
146 file is closed and renamed to filename. | |
57
d0d08c7e7d37
allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents:
14
diff
changeset
|
147 |
d0d08c7e7d37
allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents:
14
diff
changeset
|
148 The optional parameter permissions, if given, are the permissions |
d0d08c7e7d37
allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents:
14
diff
changeset
|
149 for the new file. By default, or if the parameter is None, the |
d0d08c7e7d37
allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents:
14
diff
changeset
|
150 default permissions set by the tempfile.mkstemp are used which means |
d0d08c7e7d37
allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents:
14
diff
changeset
|
151 that the file is only readable for the user that created the file. |
d0d08c7e7d37
allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents:
14
diff
changeset
|
152 The permissions value is used as the second parameter to os.chmod. |
0 | 153 """ |
154 dirname, basename = os.path.split(filename) | |
155 fileno, tempname = tempfile.mkstemp("", basename, dirname) | |
156 try: | |
157 os.write(fileno, contents) | |
158 if not contents.endswith("\n"): | |
159 os.write(fileno, "\n") | |
160 os.close(fileno) | |
57
d0d08c7e7d37
allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents:
14
diff
changeset
|
161 if permissions is not None: |
d0d08c7e7d37
allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents:
14
diff
changeset
|
162 os.chmod(tempname, permissions) |
0 | 163 os.rename(tempname, filename) |
164 finally: | |
165 if os.path.exists(tempname): | |
166 os.remove(tempname) | |
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
167 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
168 |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
169 def replace_in_file(filename, pattern, replacement): |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
170 """Replace all occurrences of pattern in a file with replacement. |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
171 The file is modified in place. The search and replace is done with |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
172 the re.sub function. The pattern and replacement parameter are passed |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
173 through to re.sub unmodified, so their semantics are determined by |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
174 re.sub. |
160
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
175 |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
176 The return value is True if the contents of the file have been |
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
177 changed, False otherwise. |
111
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
178 """ |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
179 contents = open(filename).read() |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
180 modified = re.sub(pattern, replacement, contents) |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
181 f = open(filename, "w") |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
182 f.write(modified) |
7f6fb8103db0
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents:
110
diff
changeset
|
183 f.close() |
160
017179427c7f
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents:
111
diff
changeset
|
184 return modified != contents |
172
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
185 |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
186 |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
187 def filenameproperty(filename, dir_attr="base_dir"): |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
188 """Create a property for a directory or filename. |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
189 If the filename is relative it is interpreted as relative to the |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
190 value of the attribute of self named by dir_attr which defaults to |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
191 'base_dir'. |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
192 """ |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
193 def get(self): |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
194 return os.path.join(getattr(self, dir_attr), filename) |
06af36f915f2
Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents:
169
diff
changeset
|
195 return property(get) |
344
f06f707d9fda
merged branches/scratchbox into trunk
Bjoern Ricks <bricks@intevation.de>
parents:
172
diff
changeset
|
196 |
f06f707d9fda
merged branches/scratchbox into trunk
Bjoern Ricks <bricks@intevation.de>
parents:
172
diff
changeset
|
197 def getuser(): |
f06f707d9fda
merged branches/scratchbox into trunk
Bjoern Ricks <bricks@intevation.de>
parents:
172
diff
changeset
|
198 """Returns the login name of the current user owning the proccess""" |
f06f707d9fda
merged branches/scratchbox into trunk
Bjoern Ricks <bricks@intevation.de>
parents:
172
diff
changeset
|
199 return pwd.getpwuid(os.getuid())[0] |
394 | 200 |
201 def md5sum(filename): | |
202 """ calculates the md5sum of a file """ | |
203 if not os.path.isfile(filename): | |
204 raise RuntimeError("Could not create md5sum. File not found: %s" | |
205 % filename) | |
206 f = file(filename, 'rb') | |
458
61e72399914c
Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents:
457
diff
changeset
|
207 m = new_md5() |
394 | 208 while True: |
209 d = f.read(8096) | |
210 if not d: | |
211 break | |
212 m.update(d) | |
213 f.close() | |
214 return m.hexdigest() | |
215 | |
439 | 216 def remove_trailing_slashes(s): |
217 return s.rstrip("/") | |
218 | |
219 def expand_filename(filename): | |
220 """ | |
221 Applies os.path.expanduser and os.path.expandvars to filename | |
222 """ | |
223 return os.path.expandvars(os.path.expanduser(filename)) | |
224 | |
225 | |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
226 def compress_all_logs(reference_log, cmd="gzip -9 $logfile"): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
227 """ |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
228 Takes the path of a reference log file and compresses all |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
229 files in same folder with the cmd command. |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
230 """ |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
231 if reference_log and os.path.exists(reference_log): |
462
058856954e2d
Make the compress_all_logs call builder independent and also compress
Andre Heinecke <aheinecke@intevation.de>
parents:
461
diff
changeset
|
232 log_dir = os.path.isdir(reference_log) and \ |
058856954e2d
Make the compress_all_logs call builder independent and also compress
Andre Heinecke <aheinecke@intevation.de>
parents:
461
diff
changeset
|
233 reference_log or os.path.dirname(reference_log) |
058856954e2d
Make the compress_all_logs call builder independent and also compress
Andre Heinecke <aheinecke@intevation.de>
parents:
461
diff
changeset
|
234 for log_file in [os.path.join(log_dir, f) |
461
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
235 for f in os.listdir(log_dir)]: |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
236 if os.path.isfile(log_file): |
454967511f5c
commit compress all logs patch from Sascha Teichmann
Bjoern Ricks <bricks@intevation.de>
parents:
458
diff
changeset
|
237 run.call(cmdexpand(cmd, logfile=log_file)) |