1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Temporary module for allowing relax to support both Python 2 and 3."""
24
25
26 try:
27 import platform
28 except ImportError:
29 print("The platform module cannot be imported. For Python <= 2.2, try copying the platform.py file from http://hg.python.org/cpython/file/2.3/Lib/platform.py into your lib/pythonX.X/ directory.")
30 raise
31
32
33 import dep_check
34
35
36 if dep_check.bz2_module:
37 import bz2
38 from bz2 import BZ2File
39 else:
40 BZ2File = object
41 if dep_check.gzip_module:
42 import gzip
43 else:
44 gzip = object
45 from copy import deepcopy
46 if dep_check.io_module:
47 import io
48 import os
49 import platform
50 import sys
51 import threading
52
53
54
55 SYSTEM = platform.uname()[0]
56
57
58 PY_VERSION = sys.version_info[0]
59
60
61
62
63
64
65 if PY_VERSION == 2:
66 import __builtin__ as builtins
67 else:
68 import builtins
69
70
71 if PY_VERSION == 2:
72 import Queue as queue
73 else:
74 import queue
75
76
77 if PY_VERSION == 2:
78 from Queue import Queue as Queue2
79 else:
80 from queue import Queue as Queue3
81 Queue2 = Queue3
82
83
84 if PY_VERSION == 2:
85 from cStringIO import StringIO
86 elif dep_check.io_module:
87 from io import StringIO
88 else:
89 StringIO = None
90
91
92 try:
93 from unittest import TextTestResult
94 except ImportError:
95 from unittest import _TextTestResult as TextTestResult
96
97
98 if PY_VERSION == 2:
99 import cPickle as pickle
100 else:
101 import pickle
102
103
105 """Abstract the numerous ways BZ2 files are handled in Python.
106
107 @param file: The file path to open.
108 @type file: str
109 @keyword mode: The mode to open the file with. Only the values of 'r' and 'w' for reading and writing respectively are supported.
110 @type model: str
111 @return: The bzip2 file object.
112 @rtype: file object
113 """
114
115
116 if mode not in ['r', 'w']:
117 raise RelaxError("The mode '%s' must be one or 'r' or 'w'." % mode)
118
119
120 if not dep_check.bz2_module:
121 if mode == 'r':
122 raise RelaxError("Cannot open the file %s, try uncompressing first. %s." % (file, dep_check.bz2_module_message))
123 else:
124 raise RelaxError("Cannot create bzip2 file %s, the bz2 Python module cannot be found." % file)
125
126
127 if mode == 'r':
128
129 if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
130 file_obj = bz2.open(file, 't')
131
132
133 elif sys.version_info[0] == 3 and sys.version_info[1] < 3:
134 file_obj = io.TextIOWrapper(Bzip2Fixed(file, 'r'))
135
136
137 else:
138 file_obj = bz2.BZ2File(file, 'r')
139
140
141 elif mode == 'w':
142
143 if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
144 file_obj = bz2.open(file, 'wt')
145
146
147 elif sys.version_info[0] == 3 and sys.version_info[1] < 3:
148 file_obj = io.TextIOWrapper(Bzip2Fixed(file, 'w'))
149
150
151 else:
152 file_obj = bz2.BZ2File(file, 'w')
153
154
155 return file_obj
156
157
159 """Abstract the numerous ways gzipped files are handled in Python.
160
161 @param file: The file path to open.
162 @type file: str
163 @keyword mode: The mode to open the file with. Only the values of 'r' and 'w' for reading and writing respectively are supported.
164 @type model: str
165 @return: The gzipped file object.
166 @rtype: file object
167 """
168
169
170 if mode not in ['r', 'w']:
171 raise RelaxError("The mode '%s' must be one or 'r' or 'w'." % mode)
172
173
174 if not dep_check.gzip_module:
175 if mode == 'r':
176 raise RelaxError("Cannot open the file %s, try uncompressing first. %s." % (file, dep_check.gzip_module_message))
177 else:
178 raise RelaxError("Cannot create gzipped file %s, the bz2 Python module cannot be found." % file)
179
180
181 if mode == 'r':
182
183 if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
184 file_obj = gzip.open(file, 'rt')
185
186
187 elif sys.version_info[0] == 3 and sys.version_info[1] < 3:
188 file_obj = io.TextIOWrapper(GzipFixed(file, 'r'))
189
190
191 else:
192 file_obj = gzip.GzipFile(file, 'r')
193
194
195 elif mode == 'w':
196
197 if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
198 file_obj = gzip.open(file, 'wt')
199
200
201 elif sys.version_info[0] == 3 and sys.version_info[1] < 3:
202 file_obj = io.TextIOWrapper(GzipFixed(file, 'w'))
203
204
205 else:
206 file_obj = gzip.GzipFile(file, 'w')
207
208
209 return file_obj
210
211
213 """Python 2.3 and earlier replacement function for the builtin sorted() function."""
214
215
216 new_data = deepcopy(data)
217
218
219 new_data.sort()
220
221
222 return new_data
223
224
225
227 """Incredibly nasty hack for bzip2 files support in Python 3.0, 3.1 and 3.2."""
228
231
234
237
240
243
244
245
247 """Incredibly nasty hack for gzipped files support in Python 3.0, 3.1 and 3.2."""
248
249 closed = False
250
253
256
259
262
263
264
266 """Python 2.4 and earlier Queuing class replacement.
267
268 This code comes from http://code.activestate.com/recipes/475160/ and is part of the Python sources from 2.5 onwards.
269 """
270
272 Queue2.__init__(self)
273 self.all_tasks_done = threading.Condition(self.mutex)
274 self.unfinished_tasks = 0
275
276 - def _put(self, item):
277 Queue2._put(self, item)
278 self.unfinished_tasks += 1
279
281 """Indicate that a formerly enqueued task is complete.
282
283 Used by Queue consumer threads. For each get() used to fetch a task,
284 a subsequent call to task_done() tells the queue that the processing
285 on the task is complete.
286
287 If a join() is currently blocking, it will resume when all items
288 have been processed (meaning that a task_done() call was received
289 for every item that had been put() into the queue).
290
291 Raises a ValueError if called more times than there were items
292 placed in the queue.
293 """
294 self.all_tasks_done.acquire()
295 try:
296 unfinished = self.unfinished_tasks - 1
297 if unfinished <= 0:
298 if unfinished < 0:
299 raise ValueError('task_done() called too many times')
300 self.all_tasks_done.notifyAll()
301 self.unfinished_tasks = unfinished
302 finally:
303 self.all_tasks_done.release()
304
306 """Blocks until all items in the Queue have been gotten and processed.
307
308 The count of unfinished tasks goes up whenever an item is added to the
309 queue. The count goes down whenever a consumer thread calls task_done()
310 to indicate the item was retrieved and all work on it is complete.
311
312 When the count of unfinished tasks drops to zero, join() unblocks.
313 """
314 self.all_tasks_done.acquire()
315 try:
316 while self.unfinished_tasks:
317 self.all_tasks_done.wait()
318 finally:
319 self.all_tasks_done.release()
320
321
322
323 if PY_VERSION == 2 and sys.version_info[1] <= 4:
324 Queue = TaskQueue
325 elif PY_VERSION == 2:
326 Queue = Queue2
327 else:
328 Queue = Queue3
329
330
331
332 if PY_VERSION == 2:
333
334
335 builtins.range = builtins.xrange
336
337
338 if sys.version_info[1] <= 3:
339 setattr(builtins, 'sorted', sorted)
340
341
342 if sys.version_info[1] <= 3:
343 if SYSTEM == 'Linux':
344 os.devnull = '/dev/null'
345 elif SYSTEM == 'Windows' or SYSTEM == 'Microsoft':
346 os.devnull = 'nul'
347 elif SYSTEM == 'Darwin':
348 os.devnull = 'Dev:Null'
349 else:
350 os.devnull = None
351
352
353
354 if PY_VERSION == 3:
355
356 builtins.unicode = builtins.str
357