/
dir_tree.py
367 lines (293 loc) · 11.6 KB
/
dir_tree.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
This module provides class DirTree which represents the directory tree and class
DirList which represents the list of possibly unrelated directories.
"""
from __future__ import print_function
import os
import sys
import subprocess
import re
from os.path import join as ospjoin
PREFIX_SUFFIX_LENGTH = 3
COMPRESSION_TYPE = {
"gzip": "-z",
"bzip": "-j",
"xz": "-J",
}
COMPRESSION_EXT = {
"gzip": ".gz",
"bzip": ".bz",
"xz": ".xz",
"none": "",
}
class ArchivingProblemError(Exception):
"""Exception for problems with archiving."""
pass
class NoMatchingError(Exception):
"""Exception for problems with find matching directory."""
pass
def _call_tar(args, verbose=False):
"""
Function that uses subprocess.call() to call tar
@param args: args to be passed to tar
@type args: list
@param verbose: whether to pipe tar's output to /dev/null or not
@type verbose: boolean
"""
cmd = ["tar"]
cmd.extend(args)
if verbose:
subprocess.call(cmd)
else:
null_file = file("/dev/null", "w")
subprocess.call(cmd, stdout=null_file)
null_file.close()
def _find_matching_item(name_part, dest):
"""
Function that finds directory in dest directory whose name matches the first
argument.
@param name_part: part of the name to be matched
@type name_part: string
@param dest: destination directory where to look for
@type dest: string
@return: full path of the matching directory (if any)
@raise NoMatchError: if destination directory doesn't contain any matching
directory
"""
try:
for item in os.listdir(dest):
if re.match(name_part + ".*", item, re.IGNORECASE):
return ospjoin(dest, item)
raise NoMatchError("No matching directory found.")
except IOError as ioerr:
raise NoMatchError(str(ioerr))
class _DirNode(object):
"""This class represents a single directory in DirTree."""
def __init__(self, path, parent=None):
"""
Constructor of the DirNode object. If parent is None this node is
considered to be the root of the DirTree.
@param path: path to the directory
@type path: string
@param parent: DirNode that has this node in children
@type parent: DirNode
"""
self.path = path
self.parent = parent
self.name = os.path.basename(self.path.rstrip("/"))
self.children = []
self.files = []
def _get_items(self, take_hidden=False):
"""
Method that populates self.children with directories contained in itself
as a list of DirNodes and self.files with files contained in itself as a
list of path strigs.
@param skip_hidden: whether to skip hidden directories or not
@type skip_hidden: boolean
"""
for item in os.listdir(self.path):
if item.startswith(".") and not take_hidden:
continue
if os.path.isdir(ospjoin(self.path, item)):
child_path = ospjoin(self.path, item)
self.children.append(_DirNode(child_path, self))
else:
self.files.append(item)
def build_subtree(self, take_hidden=False):
"""Method that builds a subtree of directories and files."""
self._get_items(take_hidden)
for child in self.children:
child.build_subtree()
def _draw_node(self, indent, counts=False):
"""
Method that prints textual representation of the node.
@param indent: how many spaces will be printed before the name
@type indent: int
"""
spaces = indent * " "
suffix = ""
if self.children:
suffix = " +"
elif not self.files and not counts:
suffix = " o"
else:
suffix = " :"
if counts:
suffix += " {0}".format(len(self.files))
print("{0}| {1}{2}".format(spaces, self.name, suffix))
def _draw_files(self, indent):
"""
Method that prints textual representation of contained files.
@param indent: how many spaces will be printed before filenames
@type indent: int
"""
spaces = indent * " "
for file_ in self.files:
print("{0}* {1}".format(spaces, file_))
def draw_subtree(self, indent, draw_files=False, counts=False):
"""
Method that prints textual representation of the subtree.
@param indent: how many spaces will be used before directory's name
@type indent: int
@param draw_files: whether files shoudl be also printed or not
@type draw_files: boolean
"""
self._draw_node(indent, counts)
if draw_files:
self._draw_files(indent + len(self.name) +
PREFIX_SUFFIX_LENGTH)
for child in self.children:
child.draw_subtree(indent + len(self.name) +
PREFIX_SUFFIX_LENGTH, draw_files, counts)
def archive_subtree(self, dest, compression="gzip", verbose=False):
"""
Method that archives subtree. It makes tarballs from directories
containing only files or it makes same-named directories in the
destination directory for directories containing subdirectories and it
makes tarballs from files contained in these directories.
@param dest: destination directory where to make subtree
@type dest: string
@param compression: compression algorithm to be used (possible values
are "gzip", "bzip", "xz", "none")
@type compression: string
@param verbose: whether to pipe tar's output to stdin or not
@type verbose: boolean
@raise ArchivingProblemError: in case there was some problem while
archiving (i.e. tar's return code was
not 0)
"""
print("Archiving files in {0}...".format(self.path), end="")
sys.stdout.flush()
retcode = 0
args = []
if verbose:
args.append("-v")
if compression != "none":
args.append(COMPRESSION_TYPE[compression])
if not self.children:
args.extend([
"-c",
#don't want absolute paths
"-C", os.path.dirname(self.path.rstrip("/")),
"-f",
ospjoin(dest,
self.name + ".tar" + COMPRESSION_EXT[compression]),
#option -C changes directory, so we can use names
self.name,
])
_call_tar(args, verbose)
else:
if not os.path.exists(ospjoin(dest, self.name)):
try:
os.mkdir(ospjoin(dest, self.name))
except OSError as oserr:
raise ArchivingProblemError(
"Cannot create directory {0}.\n{1}".format(
ospjoin(dest, self.name), oserr)
)
if self.files:
args.extend([
"-c",
"-C", self.path,
"-f",
ospjoin(dest, self.name,
self.name + ".tar" + COMPRESSION_EXT[compression]),
])
args.extend(self.files)
_call_tar(args, verbose)
print("done")
if retcode != 0:
raise ArchivingProblemError(
"Return code of tar (archiving {0}) was: {1}".format(
self.path, retcode)
)
for child in self.children:
child.archive_subtree(ospjoin(dest, self.name),
compression, verbose)
class DirTree(_DirNode):
"""
This class represents a directory tree. In fact, it's just a wrapper
around the _DirNode class.
"""
def __init__(self, path, take_hidden=False):
"""
Constructor of this class.
@param path: path to the root of the directory tree
@type path: string
@param skip_hidden: whether also include hidden directories or not
@type skip_hidden: boolean
"""
super(DirTree, self).__init__(path)
self.build_subtree(take_hidden)
def draw_tree(self, draw_files=False, counts=False):
"""
Prints textual representation of the directory tree.
@param draw_files: whether also print contained files or not
@type draw_files: boolean
"""
self.draw_subtree(0, draw_files, counts)
def archive_tree(self, dest, compression="gzip", verbose=False):
"""
Archives the directory tree to the destination directory.
@param dest: destination directory
@type dest: string
@param compression: type of compression to be used
@type compression: string
@param verbose: whether to show tar's output or not
@type verbose: boolean
"""
self.archive_subtree(dest, compression, verbose)
class DirList(object):
"""
This class represents a set of directories. Compared to DirTree it doesn't
need root directory so it's items doesn't have to have common parent dir.
"""
def __init__():
"""Constructor of the class DirSet that only initializes atributes."""
self.items = [] #list of tuples (_DirNode, dest_path)
def list_from_file(self, path):
"""
Method that populates self.items with directories listed with their
destination directories (see documentation for more details) in a file.
@param path: path of the file listing directories
@type path: string
@raise IOError: when file doesn't exist
"""
list_file = open(path, "r")
for line in list_file:
path, dest = line.split(" -> ", 2)
if os.path.abspath(path):
self.items.append((_DirNode(path), dest))
else:
full_path = ospjoin(os.path.getcwd(), path)
self.items.append(_DirNode(full_path, dest))
def list_by_prefix(self, source, dest, delimiter="__"):
"""
Method that populates self.items with directories contained in given
directory having their destination directories contained in their name
(see documentation for more details).
@param source: path to the source directory containing special-named
directories
@type source: string
@param dest: path to the destination directory (were to look for
matches)
@type dest: string
@param delimiter: delimiter used for spliting directories' names
@type delimiter: string
@raise NoMatchError: when one or more parsed patterns doesn't match any
directory in the dest. directory (or destination
directory doesn't exist at all)
"""
for item in os.listdir(source):
parts = item.split(delimiter)
matched = dest
for part in parts[:-1]:
matched = _find_matching_item(part, matched)
self.items.append((_DirNode(parts[-1]), matched))
def archive_list(self, compression="gzip", verbose="False"):
"""
Method that archives list of directories and their subdirectories. See
documentation of method _DirNode.archive() for more details.
"""
for (dirnode, dest) in self.items():
dirnode.archive_subtree(dest, compression, verbose)