annotate treepkg/util.py @ 458:61e72399914c

Use an exception to use either hashlib or md5
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 24 Aug 2010 15:41:48 +0000
parents b90bb9f2bb77
children 454967511f5c
rev   line source
110
ba677506e95e Remove StatusFile. It has been superseded by the treepkg.util module
Bernhard Herzog <bh@intevation.de>
parents: 88
diff changeset
1 # Copyright (C) 2007, 2008 by Intevation GmbH
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
2 # Authors:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
3 # Bernhard Herzog <bh@intevation.de>
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
4 #
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
5 # This program is free software under the GPL (>=v2)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
6 # Read the file COPYING coming with the software for details.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
7
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
8 """Collection of functions that didn't fit elsewhere"""
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
9
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
10 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
11 import re
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
12 import tempfile
4
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
13 import shutil
169
261b75d7b972 Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents: 160
diff changeset
14 import fnmatch
344
f06f707d9fda merged branches/scratchbox into trunk
Bjoern Ricks <bricks@intevation.de>
parents: 172
diff changeset
15 import pwd
394
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
16 import os.path
458
61e72399914c Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents: 457
diff changeset
17 try:
61e72399914c Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents: 457
diff changeset
18 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
19 except ImportError:
61e72399914c Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents: 457
diff changeset
20 # 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
21 from md5 import new as new_md5
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
22 import run
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
23
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
24
4
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
25 def import_dotted_name(dotted_name):
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
26 module = __import__(dotted_name)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
27 for name in dotted_name.split(".")[1:]:
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
28 module = getattr(module, name)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
29 return module
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
30
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
31 def extract_value_for_key(lines, key):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
32 """Parses a sequence of strings for a key and returns the associated value
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
33
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
34 The function determines the first string in lines that starts with
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
35 key. It returns the rest of the lines stripped of leading and
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
36 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
37
83e48a76f759 Document what extract_value_for_key returns when the key is not found
Bernhard Herzog <bh@intevation.de>
parents: 57
diff changeset
38 If the key is not found in lines, the function returns None.
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
39 """
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
40 for line in lines:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
41 if line.startswith(key):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
42 return line[len(key):].strip()
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
43
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
44 def extract_lsm_version(lsm_file):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
45 return extract_value_for_key(open(lsm_file), "Version:")
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
46
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
47 def debian_changelog_version(changelog):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
48 """Returns the newest version in a debian changelog."""
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
49 output = run.capture_output(["dpkg-parsechangelog", "-l" + changelog])
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
50 return extract_value_for_key(output.splitlines(), "Version:")
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
51
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
52
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
53 def ensure_directory(directory):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
54 """Creates directory and all its parents.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
55
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
56 Unlike os.makedirs, this function doesn't throw an exception
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
57 """
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
58 if not os.path.isdir(directory):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
59 os.makedirs(directory)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
60
169
261b75d7b972 Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents: 160
diff changeset
61 def listdir_abs(directory, pattern=None):
261b75d7b972 Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents: 160
diff changeset
62 """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
63 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
64 returned by the function.
261b75d7b972 Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents: 160
diff changeset
65 """
261b75d7b972 Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents: 160
diff changeset
66 filenames = os.listdir(directory)
261b75d7b972 Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents: 160
diff changeset
67 if pattern is not None:
261b75d7b972 Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents: 160
diff changeset
68 filenames = fnmatch.filter(filenames, pattern)
261b75d7b972 Extend listdir_abs with an optional glob pattern
Bernhard Herzog <bh@intevation.de>
parents: 160
diff changeset
69 return [os.path.join(directory, filename) for filename in filenames]
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
70
4
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
71 def copytree(src, dst, symlinks=False):
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
72 """Recursively copy a directory tree using copy2().
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
73
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
74 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
75 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
76 directory already exists.
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
77
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
78 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
79 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
80 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
81 links are copied.
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
82 """
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
83 names = os.listdir(src)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
84 ensure_directory(dst)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
85 errors = []
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
86 for name in names:
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
87 srcname = os.path.join(src, name)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
88 dstname = os.path.join(dst, name)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
89 try:
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
90 if symlinks and os.path.islink(srcname):
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
91 linkto = os.readlink(srcname)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
92 os.symlink(linkto, dstname)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
93 elif os.path.isdir(srcname):
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
94 copytree(srcname, dstname, symlinks)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
95 else:
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
96 shutil.copy2(srcname, dstname)
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
97 # XXX What about devices, sockets etc.?
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
98 except (IOError, os.error), why:
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
99 errors.append((srcname, dstname, why))
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
100 if errors:
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
101 raise Error, errors
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
102
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
103
fee641fec94e Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
104
57
d0d08c7e7d37 allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents: 14
diff changeset
105 def writefile(filename, contents, permissions=None):
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
106 """Write contents to filename in an atomic way.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
107
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
108 The contents are first written to a temporary file in the same
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
109 directory as filename. Once the contents are written, the temporary
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
110 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
111
d0d08c7e7d37 allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents: 14
diff changeset
112 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
113 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
114 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
115 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
116 The permissions value is used as the second parameter to os.chmod.
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
117 """
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
118 dirname, basename = os.path.split(filename)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
119 fileno, tempname = tempfile.mkstemp("", basename, dirname)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
120 try:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
121 os.write(fileno, contents)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
122 if not contents.endswith("\n"):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
123 os.write(fileno, "\n")
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
124 os.close(fileno)
57
d0d08c7e7d37 allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents: 14
diff changeset
125 if permissions is not None:
d0d08c7e7d37 allow optionally set the file permissions in writefile
Bernhard Herzog <bh@intevation.de>
parents: 14
diff changeset
126 os.chmod(tempname, permissions)
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
127 os.rename(tempname, filename)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
128 finally:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
129 if os.path.exists(tempname):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
130 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
131
7f6fb8103db0 Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents: 110
diff changeset
132
7f6fb8103db0 Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents: 110
diff changeset
133 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
134 """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
135 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
136 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
137 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
138 re.sub.
160
017179427c7f Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents: 111
diff changeset
139
017179427c7f Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents: 111
diff changeset
140 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
141 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
142 """
7f6fb8103db0 Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py
Bernhard Herzog <bh@intevation.de>
parents: 110
diff changeset
143 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
144 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
145 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
146 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
147 f.close()
160
017179427c7f Make treepkg/utilreplace_in_file return whether any substitutions were made.
Bernhard Herzog <bh@intevation.de>
parents: 111
diff changeset
148 return modified != contents
172
06af36f915f2 Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents: 169
diff changeset
149
06af36f915f2 Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents: 169
diff changeset
150
06af36f915f2 Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents: 169
diff changeset
151 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
152 """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
153 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
154 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
155 'base_dir'.
06af36f915f2 Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents: 169
diff changeset
156 """
06af36f915f2 Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents: 169
diff changeset
157 def get(self):
06af36f915f2 Move the filenameproperty factory from treepkg/packager.py to
Bernhard Herzog <bh@intevation.de>
parents: 169
diff changeset
158 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
159 return property(get)
344
f06f707d9fda merged branches/scratchbox into trunk
Bjoern Ricks <bricks@intevation.de>
parents: 172
diff changeset
160
f06f707d9fda merged branches/scratchbox into trunk
Bjoern Ricks <bricks@intevation.de>
parents: 172
diff changeset
161 def getuser():
f06f707d9fda merged branches/scratchbox into trunk
Bjoern Ricks <bricks@intevation.de>
parents: 172
diff changeset
162 """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
163 return pwd.getpwuid(os.getuid())[0]
394
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
164
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
165 def md5sum(filename):
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
166 """ calculates the md5sum of a file """
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
167 if not os.path.isfile(filename):
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
168 raise RuntimeError("Could not create md5sum. File not found: %s"
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
169 % filename)
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
170 f = file(filename, 'rb')
458
61e72399914c Use an exception to use either hashlib or md5
Andre Heinecke <aheinecke@intevation.de>
parents: 457
diff changeset
171 m = new_md5()
394
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
172 while True:
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
173 d = f.read(8096)
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
174 if not d:
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
175 break
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
176 m.update(d)
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
177 f.close()
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
178 return m.hexdigest()
bfd1c6a155fa added md5sum function
Bjoern Ricks <bricks@intevation.de>
parents: 344
diff changeset
179
439
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
180 def remove_trailing_slashes(s):
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
181 return s.rstrip("/")
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
182
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
183 def expand_filename(filename):
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
184 """
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
185 Applies os.path.expanduser and os.path.expandvars to filename
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
186 """
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
187 return os.path.expandvars(os.path.expanduser(filename))
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
188
8e0c81870e5e cleanup modules
Bjoern Ricks <bricks@intevation.de>
parents: 418
diff changeset
189
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)