|
1 #!/usr/bin/env python |
|
2 |
|
3 import mozfile |
|
4 import os |
|
5 import shutil |
|
6 import tarfile |
|
7 import tempfile |
|
8 import stubs |
|
9 import unittest |
|
10 import zipfile |
|
11 |
|
12 |
|
13 class TestExtract(unittest.TestCase): |
|
14 """test extracting archives""" |
|
15 |
|
16 def ensure_directory_contents(self, directory): |
|
17 """ensure the directory contents match""" |
|
18 for f in stubs.files: |
|
19 path = os.path.join(directory, *f) |
|
20 exists = os.path.exists(path) |
|
21 if not exists: |
|
22 print "%s does not exist" % (os.path.join(f)) |
|
23 self.assertTrue(exists) |
|
24 if exists: |
|
25 contents = file(path).read().strip() |
|
26 self.assertTrue(contents == f[-1]) |
|
27 |
|
28 def test_extract_zipfile(self): |
|
29 """test extracting a zipfile""" |
|
30 _zipfile = self.create_zip() |
|
31 self.assertTrue(os.path.exists(_zipfile)) |
|
32 try: |
|
33 dest = tempfile.mkdtemp() |
|
34 try: |
|
35 mozfile.extract_zip(_zipfile, dest) |
|
36 self.ensure_directory_contents(dest) |
|
37 finally: |
|
38 shutil.rmtree(dest) |
|
39 finally: |
|
40 os.remove(_zipfile) |
|
41 |
|
42 def test_extract_tarball(self): |
|
43 """test extracting a tarball""" |
|
44 tarball = self.create_tarball() |
|
45 self.assertTrue(os.path.exists(tarball)) |
|
46 try: |
|
47 dest = tempfile.mkdtemp() |
|
48 try: |
|
49 mozfile.extract_tarball(tarball, dest) |
|
50 self.ensure_directory_contents(dest) |
|
51 finally: |
|
52 shutil.rmtree(dest) |
|
53 finally: |
|
54 os.remove(tarball) |
|
55 |
|
56 def test_extract(self): |
|
57 """test the generalized extract function""" |
|
58 |
|
59 # test extracting a tarball |
|
60 tarball = self.create_tarball() |
|
61 self.assertTrue(os.path.exists(tarball)) |
|
62 try: |
|
63 dest = tempfile.mkdtemp() |
|
64 try: |
|
65 mozfile.extract(tarball, dest) |
|
66 self.ensure_directory_contents(dest) |
|
67 finally: |
|
68 shutil.rmtree(dest) |
|
69 finally: |
|
70 os.remove(tarball) |
|
71 |
|
72 # test extracting a zipfile |
|
73 _zipfile = self.create_zip() |
|
74 self.assertTrue(os.path.exists(_zipfile)) |
|
75 try: |
|
76 dest = tempfile.mkdtemp() |
|
77 try: |
|
78 mozfile.extract_zip(_zipfile, dest) |
|
79 self.ensure_directory_contents(dest) |
|
80 finally: |
|
81 shutil.rmtree(dest) |
|
82 finally: |
|
83 os.remove(_zipfile) |
|
84 |
|
85 # test extracting some non-archive; this should fail |
|
86 fd, filename = tempfile.mkstemp() |
|
87 os.write(fd, 'This is not a zipfile or tarball') |
|
88 os.close(fd) |
|
89 exception = None |
|
90 try: |
|
91 dest = tempfile.mkdtemp() |
|
92 mozfile.extract(filename, dest) |
|
93 except Exception, exception: |
|
94 pass |
|
95 finally: |
|
96 os.remove(filename) |
|
97 os.rmdir(dest) |
|
98 self.assertTrue(isinstance(exception, Exception)) |
|
99 |
|
100 ### utility functions |
|
101 |
|
102 def create_tarball(self): |
|
103 """create a stub tarball for testing""" |
|
104 tempdir = stubs.create_stub() |
|
105 filename = tempfile.mktemp(suffix='.tar') |
|
106 archive = tarfile.TarFile(filename, mode='w') |
|
107 try: |
|
108 for path in stubs.files: |
|
109 archive.add(os.path.join(tempdir, *path), arcname=os.path.join(*path)) |
|
110 except: |
|
111 os.remove(archive) |
|
112 raise |
|
113 finally: |
|
114 shutil.rmtree(tempdir) |
|
115 archive.close() |
|
116 return filename |
|
117 |
|
118 def create_zip(self): |
|
119 """create a stub zipfile for testing""" |
|
120 |
|
121 tempdir = stubs.create_stub() |
|
122 filename = tempfile.mktemp(suffix='.zip') |
|
123 archive = zipfile.ZipFile(filename, mode='w') |
|
124 try: |
|
125 for path in stubs.files: |
|
126 archive.write(os.path.join(tempdir, *path), arcname=os.path.join(*path)) |
|
127 except: |
|
128 os.remove(filename) |
|
129 raise |
|
130 finally: |
|
131 shutil.rmtree(tempdir) |
|
132 archive.close() |
|
133 return filename |