2 from support
import PipeThroughCommand
, escape
, Tmp
7 def pipe_through_command(command
, src
, dst
):
9 assert not current_command
14 current_command
= PipeThroughCommand(command
, src
, dst
)
16 current_command
.wait()
18 current_command
= None
24 def __init__(self
, extension
):
25 operations
.append(self
)
26 self
.extension
= extension
28 def can_handle(self
, data
):
29 return isinstance(data
, FileData
)
31 def save_to_stream(self
, data
, stream
):
32 pipe_through_command(self
.command
, data
.source
, stream
)
34 class Compress(Operation
):
35 "Compress a stream into another stream."
38 def __init__(self
, extension
, command
, type):
39 Operation
.__init
__(self
, extension
)
40 self
.command
= command
44 return 'Compress as .%s' % self
.extension
46 class Decompress(Operation
):
47 "Decompress a stream into another stream."
50 def __init__(self
, extension
, command
):
51 Operation
.__init
__(self
, extension
)
52 self
.command
= command
55 return 'Decompress .%s' % self
.extension
57 class Extract(Operation
):
58 "Extract an archive to a directory."
59 type = 'inode/directory'
61 def __init__(self
, extension
, command
):
62 "If command has a %s then the source path is inserted, else uses stdin."
63 Operation
.__init
__(self
, extension
)
64 self
.command
= command
67 return 'Extract from a .%s' % self
.extension
69 def save_to_stream(self
, data
, stream
):
70 raise Exception('This operation creates a directory, so you have '
71 'to drag to a filer window on the local machine')
73 def save_to_file(self
, data
, path
):
74 if os
.path
.exists(path
):
75 if not os
.path
.isdir(path
):
76 raise Exception("'%s' already exists and is not a directory!" %
78 if not os
.path
.exists(path
):
81 command
= self
.command
83 if command
.find("'%s'") != -1:
84 command
= command
% escape(source
.name
)
87 pipe_through_command(command
, source
, None)
90 os
.rmdir(path
) # Will only succeed if it's empty
95 def pull_up(self
, path
):
96 # If we created only a single subdirectory, move it up.
97 dirs
= os
.listdir(path
)
101 unneeded_path
= os
.path
.join(path
, dir)
102 if not os
.path
.isdir(unneeded_path
):
105 tmp_path
= os
.path
.join(path
, 'tmp-' + `random
.randint(0, 100000)`
)
106 os
.rename(unneeded_path
, tmp_path
)
107 for file in os
.listdir(tmp_path
):
108 os
.rename(os
.path
.join(tmp_path
, file), os
.path
.join(path
, file))
111 class Archive(Operation
):
112 "Create an archive from a directory."
115 def __init__(self
, extension
, command
, type):
116 assert command
.find("'%s'") != -1
118 Operation
.__init
__(self
, extension
)
119 self
.command
= command
123 return 'Create .%s archive' % self
.extension
125 def can_handle(self
, data
):
126 return isinstance(data
, DirData
)
128 def save_to_stream(self
, data
, stream
):
129 os
.chdir(os
.path
.dirname(data
.path
))
130 command
= self
.command
% escape(os
.path
.basename(data
.path
))
131 pipe_through_command(command
, None, stream
)
133 tgz
= Extract('tgz', "gunzip -c - | tar xf -")
134 tbz
= Extract('tar.bz2', "bunzip2 -c - | tar xf -")
135 rar
= Extract('rar', "rar x -")
136 tar
= Extract('tar', "tar xf -")
137 rpm
= Extract('rpm', "rpm2cpio - | cpio -id --quiet")
138 cpio
= Extract('cpio', "cpio -id --quiet")
139 deb
= Extract('deb', "ar x '%s'")
140 zip = Extract('zip', "unzip -q '%s'")
141 jar
= Extract('jar', "unzip -q '%s'")
143 make_tgz
= Archive('tgz', "tar cf - '%s' | gzip", 'application/x-compressed-tar')
144 Archive('tar.gz', "tar cf - '%s' | gzip", 'application/x-compressed-tar')
145 Archive('tar.bz2', "tar cf - '%s' | bzip2", 'application/x-bzip-compressed-tar')
146 Archive('zip', "zip -qr - '%s'", 'application/zip'),
147 Archive('jar', "zip -qr - '%s'", 'application/x-jar')
148 Archive('tar', "tar cf - '%s'", 'application/x-tar')
150 # Note: these go afterwards so that .tar.gz matches before .gz
151 make_gz
= Compress('gz', "gzip -c -", 'application/x-gzip')
152 Compress('bz2', "bzip2 -c -", 'application/x-bzip')
153 Compress('uue', "uuencode /dev/stdout", 'application/x-uuencoded')
155 gz
= Decompress('gz', "gunzip -c -")
156 bz2
= Decompress('bz2', "bunzip2 -ck -")
157 uue
= Decompress('uue', "uudecode -o /dev/stdout")
160 # Can bzip2 read bzip files?
168 known_extensions
= {}
171 known_extensions
[x
.extension
] = None
172 except AttributeError:
176 "A file on the local filesystem."
178 def __init__(self
, path
):
186 self
.mode
= os
.stat(path
).st_mode
188 rox
.report_exception()
192 start
= source
.read(300)
194 if source
is sys
.stdin
:
195 raise "Always copy stdin!"
199 # Input is not a regular, local, seekable file, so copy it
200 # to a local temp file.
205 shutil
.copyfileobj(source
, tmp
)
209 self
.default
= self
.guess_format(start
)
215 for ext
in known_extensions
:
216 if path
.endswith('.' + ext
):
217 new
= path
[:-len(ext
)-1]
218 if len(new
) < len(name
):
220 if self
.default
.add_extension
:
221 name
+= '.' + self
.default
.extension
222 self
.default_name
= name
224 def guess_format(self
, data
):
225 "Return a good default Operation, judging by the first 300 bytes or so."
227 def string(offset
, match
):
228 return data
[offset
:offset
+ len(match
)] == match
229 def short(offset
, match
):
233 return ((a
== match
& 0xff) and (b
== (match
>> 8))) or \
234 (b
== match
& 0xff) and (a
== (match
>> 8))
238 if string(257, 'ustar\0') or string(257, 'ustar\040\040\0'):
240 if short(0, 070707) or short(0, 0143561) or string(0, '070707') or \
241 string(0, '070701') or string(0, '070702'):
243 if string(0, '!<arch>') or string(0, '\\<ar>') or string(0, '<ar>'):
244 if string(7, '\ndebian'):
246 if string(0, 'Rar!'): return rar
247 if string(0, 'PK\003\004'): return zip
248 if string(0, '\xed\xab\xee\xdb'): return rpm
251 if string(0, '\037\213'):
252 if self
.path
.endswith('.tar.gz') or self
.path
.endswith('.tgz'):
255 if string(0, 'BZh') or string(0, 'BZ'):
256 if self
.path
.endswith('.tar.bz') or self
.path
.endswith('.tar.bz2') or \
257 self
.path
.endswith('.tbz') or self
.path
.endswith('.tbz2'):
260 if string(0, 'begin '):
267 def __init__(self
, path
):
269 self
.default
= make_tgz
270 self
.default_name
= path
+ '.' + self
.default
.extension
273 test_data
= 'Hello\0World\n'
277 data
= FileData(src
.name
)
278 for comp
in operations
:
279 if not isinstance(comp
, Compress
): continue
280 dec
= [o
for o
in operations
if isinstance(o
, Decompress
) and
281 o
.extension
== comp
.extension
]
284 print "Test %s / %s" % (comp
, dec
)
286 comp
.save_to_stream(data
, middle
)
288 dec
.save_to_stream(FileData(middle
.name
), out
)
290 assert file(out
.name
).read() == test_data
294 dir = '/tmp/archive-regression-test'
296 if not os
.path
.exists(dir): os
.mkdir(dir)
297 print >>file(dir + '/test', 'w'), test_data
300 for archive
in operations
:
301 if not isinstance(archive
, Archive
): continue
302 extract
= [o
for o
in operations
if isinstance(o
, Extract
) and
303 o
.extension
== archive
.extension
]
305 print "(skipping %s; no extractor)" % archive
308 if os
.path
.exists(out
): os
.system("rm -r '%s'" % out
)
310 assert len(extract
) == 1
312 print "Test %s / %s" % (archive
, extract
)
315 archive
.save_to_stream(data
, middle
)
316 extract
.save_to_file(FileData(middle
.name
), dir + '.out')
318 assert os
.listdir(dir) == os
.listdir(out
)
319 assert file(dir + '/test').read() == file(out
+ '/test').read()
322 os
.unlink(dir + '/test')
324 if os
.path
.exists(out
): os
.system("rm -r '%s'" % out
)
326 if __name__
== '__main__': test()