1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
import os
import tempfile
import shutil
import string
import pybaz.util
import errors
__docformat__ = "restructuredtext"
__doc__ = "General Utility functions"
def linktree(src, dest, top_exists = False):
"""Produces a hard-linked clone of the source.
:param src: The directory to clone
:type src: str
:param dest: The name of the new directory to create
:type dest: str
"""
if not top_exists:
os.mkdir(dest)
shutil.copymode(src, dest)
for my_file in os.listdir(src):
srcpath = "%s/%s" % (src, my_file)
# avoid trouble if dest is inside src
if os.path.samefile(srcpath, dest):
continue
destpath = "%s/%s" % (dest, my_file)
if os.path.isdir(srcpath) and not os.path.islink(srcpath):
linktree(srcpath, destpath)
else:
os.link(srcpath, destpath)
class NewFileVersion:
def __init__(self, final_filename):
self.final_filename = final_filename
(directory, suffix) = os.path.split(final_filename)
(self.fd, self.temp_filename) = tempfile.mkstemp(dir=directory)
self.file = os.fdopen(self.fd, "w")
def write(self, str):
self.file.write(str)
def commit(self):
os.chmod(self.temp_filename, os.stat(self.final_filename).st_mode)
os.rename(self.temp_filename, self.final_filename)
self.file.close()
def regex_escape(str):
newstr = str[0:]
special = "\{}()+?$^.*[]|"
for char in special:
newstr = string.replace(newstr, char, "\\"+char)
return newstr
def _escape_helper(str, rep):
return string.replace(str, rep, "\\"+rep)
def safe_unlink(my_file):
if my_file is not None:
os.unlink(my_file)
def tmpdir(parent=None):
"""
Creates a temporary directory, and returns its name.
:return: the directory name
:rtype: string
"""
return tempfile.mkdtemp("", ",,fai-", parent)
class iter_delete_wrapper:
def __init__(self, iter, dir):
self.iter = iter
self.dir = dir
def __iter__(self):
return self.iter
def __del__(self):
if self.dir is not None:
shutil.rmtree(self.dir)
def iter_pairs(iterator):
"""Returns a seqence of values as a series of pairs of even/odd values
:param iter: The iterator to reinterpret as a sequence of pairs
:type: iter
:return: iterator of even/odd values
:rtype: iter of (val, val)
"""
iterator = iterator.__iter__()
while True:
key = iterator.next()
value = iterator.next()
yield (key, value)
def shortest(vals):
"""Given a series of values, generates the shortest one.
:param vals: List of values to examine
:type vals: list of str
:return: the shortest values, or None for empty sequences.
:rtype: str or NoneType
"""
maximum = None
for value in vals:
if maximum is None or len(value) < len(maximum):
maximum = value
return maximum
def difference_index(atext, btext):
"""Find the indext of the first character that differs betweeen two texts
:param atext: The first text
:type atext: str
:param btext: The second text
:type str: str
:return: The index, or None if there are no differences within the range
:rtype: int or NoneType
"""
length = len(atext)
if len(btext) < length:
length = len(btext)
for i in range(length):
if atext[i] != btext[i]:
return i;
return None
def iter_untar(file, output_dir, compression = "gzip"):
"""Untars a file, with incremental status output.
:param file: The file to untar
:type file: str
:param output_dir: The directory to output to
:type output_dir: str
:param compression: The compression method: "gzip", "bzip2" or None
:type: str or NoneType
:return: an iterator of the list of untarred files
:rtype: iter of str
"""
args = ["-xvf", file, "-C", output_dir]
if compression is not None:
if (compression != "gzip" and compression != "bzip2"):
raise errors.UnknownCompressionMethod(compression)
args.append("--"+compression)
tar_iter = pybaz.util.exec_safe_iter_stdout("tar", args)
for line in tar_iter:
yield line.rstrip("\n")
def untar_parent(file, output_dir, compression = "gzip"):
"""Untars a file, returns the top-level directory in the tar
:param file: The file to untar
:type file: str
:param output_dir: The directory to output to
:type output_dir: str
:param compression: The compression method: "gzip", "bzip2" or None
:type: str or NoneType
:return: The top-level directory of the tar, or None if ambigious
:rtype: str or NoneType
"""
untar_iter = iter_untar(file, output_dir, compression)
try:
parent_dir = untar_iter.next()
except StopIteration:
raise errors.NoUnambiguousParent(file, parent_dir)
for file in untar_iter:
if not file.startswith(parent_dir):
raise errors.NoUnambiguousParent(file, parent_dir)
return parent_dir
def cmp_func(function):
"""Returns a lambda that uses the output of a function as a cmp key
:param function: A function to produce the key value
:return: A lambda that uses the output of the function for comparison
"""
return lambda x, y: cmp(function(x), function(y))
# arch-tag: 0cf2f861-4c7e-4d88-a0dd-378c3241564a
|