An interpreted, interactive, object-oriented programming language
CentOS Sources
2017-08-01 71084d584ff953f5463757ec6536406320560b4d
commit | author | age
71084d 1 diff --git a/Lib/shutil.py b/Lib/shutil.py
CS 2 index 420802f..d0ff2ef 100644
3 --- a/Lib/shutil.py
4 +++ b/Lib/shutil.py
5 @@ -446,17 +446,24 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
6                          zip_filename, base_dir)
7  
8          if not dry_run:
9 -            zip = zipfile.ZipFile(zip_filename, "w",
10 -                                  compression=zipfile.ZIP_DEFLATED)
11 -
12 -            for dirpath, dirnames, filenames in os.walk(base_dir):
13 -                for name in filenames:
14 -                    path = os.path.normpath(os.path.join(dirpath, name))
15 -                    if os.path.isfile(path):
16 -                        zip.write(path, path)
17 +            with zipfile.ZipFile(zip_filename, "w",
18 +                                 compression=zipfile.ZIP_DEFLATED) as zf:
19 +                path = os.path.normpath(base_dir)
20 +                zf.write(path, path)
21 +                if logger is not None:
22 +                    logger.info("adding '%s'", path)
23 +                for dirpath, dirnames, filenames in os.walk(base_dir):
24 +                    for name in sorted(dirnames):
25 +                        path = os.path.normpath(os.path.join(dirpath, name))
26 +                        zf.write(path, path)
27                          if logger is not None:
28                              logger.info("adding '%s'", path)
29 -            zip.close()
30 +                    for name in filenames:
31 +                        path = os.path.normpath(os.path.join(dirpath, name))
32 +                        if os.path.isfile(path):
33 +                            zf.write(path, path)
34 +                            if logger is not None:
35 +                                logger.info("adding '%s'", path)
36  
37      return zip_filename
38  
39 diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
40 index 9bdb724..9238489 100644
41 --- a/Lib/test/test_shutil.py
42 +++ b/Lib/test/test_shutil.py
43 @@ -10,13 +10,13 @@ import os.path
44  import errno
45  from os.path import splitdrive
46  from distutils.spawn import find_executable, spawn
47 -from shutil import (_make_tarball, _make_zipfile, make_archive,
48 +from shutil import (make_archive,
49                      register_archive_format, unregister_archive_format,
50                      get_archive_formats)
51  import tarfile
52  import warnings
53  
54 -from test import test_support
55 +from test import test_support as support
56  from test.test_support import TESTFN, check_warnings, captured_stdout
57  
58  TESTFN2 = TESTFN + "2"
59 @@ -372,139 +372,135 @@ class TestShutil(unittest.TestCase):
60      @unittest.skipUnless(zlib, "requires zlib")
61      def test_make_tarball(self):
62          # creating something to tar
63 -        tmpdir = self.mkdtemp()
64 -        self.write_file([tmpdir, 'file1'], 'xxx')
65 -        self.write_file([tmpdir, 'file2'], 'xxx')
66 -        os.mkdir(os.path.join(tmpdir, 'sub'))
67 -        self.write_file([tmpdir, 'sub', 'file3'], 'xxx')
68 +        root_dir, base_dir = self._create_files('')
69  
70          tmpdir2 = self.mkdtemp()
71          # force shutil to create the directory
72          os.rmdir(tmpdir2)
73 -        unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
74 +        unittest.skipUnless(splitdrive(root_dir)[0] == splitdrive(tmpdir2)[0],
75                              "source and target should be on same drive")
76  
77          base_name = os.path.join(tmpdir2, 'archive')
78  
79          # working with relative paths to avoid tar warnings
80 -        old_dir = os.getcwd()
81 -        os.chdir(tmpdir)
82 -        try:
83 -            _make_tarball(splitdrive(base_name)[1], '.')
84 -        finally:
85 -            os.chdir(old_dir)
86 +        make_archive(splitdrive(base_name)[1], 'gztar', root_dir, '.')
87  
88          # check if the compressed tarball was created
89          tarball = base_name + '.tar.gz'
90 -        self.assertTrue(os.path.exists(tarball))
91 +        self.assertTrue(os.path.isfile(tarball))
92 +        self.assertTrue(tarfile.is_tarfile(tarball))
93 +        with tarfile.open(tarball, 'r:gz') as tf:
94 +            self.assertEqual(sorted(tf.getnames()),
95 +                             ['.', './file1', './file2',
96 +                              './sub', './sub/file3', './sub2'])
97  
98          # trying an uncompressed one
99          base_name = os.path.join(tmpdir2, 'archive')
100 -        old_dir = os.getcwd()
101 -        os.chdir(tmpdir)
102 -        try:
103 -            _make_tarball(splitdrive(base_name)[1], '.', compress=None)
104 -        finally:
105 -            os.chdir(old_dir)
106 +        make_archive(splitdrive(base_name)[1], 'tar', root_dir, '.')
107          tarball = base_name + '.tar'
108 -        self.assertTrue(os.path.exists(tarball))
109 +        self.assertTrue(os.path.isfile(tarball))
110 +        self.assertTrue(tarfile.is_tarfile(tarball))
111 +        with tarfile.open(tarball, 'r') as tf:
112 +            self.assertEqual(sorted(tf.getnames()),
113 +                             ['.', './file1', './file2',
114 +                              './sub', './sub/file3', './sub2'])
115  
116      def _tarinfo(self, path):
117 -        tar = tarfile.open(path)
118 -        try:
119 +        with tarfile.open(path) as tar:
120              names = tar.getnames()
121              names.sort()
122              return tuple(names)
123 -        finally:
124 -            tar.close()
125  
126 -    def _create_files(self):
127 +    def _create_files(self, base_dir='dist'):
128          # creating something to tar
129 -        tmpdir = self.mkdtemp()
130 -        dist = os.path.join(tmpdir, 'dist')
131 -        os.mkdir(dist)
132 -        self.write_file([dist, 'file1'], 'xxx')
133 -        self.write_file([dist, 'file2'], 'xxx')
134 +        root_dir = self.mkdtemp()
135 +        dist = os.path.join(root_dir, base_dir)
136 +        if not os.path.isdir(dist):
137 +            os.makedirs(dist)
138 +        self.write_file((dist, 'file1'), 'xxx')
139 +        self.write_file((dist, 'file2'), 'xxx')
140          os.mkdir(os.path.join(dist, 'sub'))
141 -        self.write_file([dist, 'sub', 'file3'], 'xxx')
142 +        self.write_file((dist, 'sub', 'file3'), 'xxx')
143          os.mkdir(os.path.join(dist, 'sub2'))
144 -        tmpdir2 = self.mkdtemp()
145 -        base_name = os.path.join(tmpdir2, 'archive')
146 -        return tmpdir, tmpdir2, base_name
147 +        if base_dir:
148 +            self.write_file((root_dir, 'outer'), 'xxx')
149 +        return root_dir, base_dir
150  
151      @unittest.skipUnless(zlib, "Requires zlib")
152 -    @unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
153 +    @unittest.skipUnless(find_executable('tar'),
154                           'Need the tar command to run')
155      def test_tarfile_vs_tar(self):
156 -        tmpdir, tmpdir2, base_name =  self._create_files()
157 -        old_dir = os.getcwd()
158 -        os.chdir(tmpdir)
159 -        try:
160 -            _make_tarball(base_name, 'dist')
161 -        finally:
162 -            os.chdir(old_dir)
163 +        root_dir, base_dir = self._create_files()
164 +        base_name = os.path.join(self.mkdtemp(), 'archive')
165 +        make_archive(base_name, 'gztar', root_dir, base_dir)
166  
167          # check if the compressed tarball was created
168          tarball = base_name + '.tar.gz'
169 -        self.assertTrue(os.path.exists(tarball))
170 +        self.assertTrue(os.path.isfile(tarball))
171  
172          # now create another tarball using `tar`
173 -        tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
174 -        tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
175 -        gzip_cmd = ['gzip', '-f9', 'archive2.tar']
176 -        old_dir = os.getcwd()
177 -        os.chdir(tmpdir)
178 -        try:
179 -            with captured_stdout() as s:
180 -                spawn(tar_cmd)
181 -                spawn(gzip_cmd)
182 -        finally:
183 -            os.chdir(old_dir)
184 +        tarball2 = os.path.join(root_dir, 'archive2.tar')
185 +        tar_cmd = ['tar', '-cf', 'archive2.tar', base_dir]
186 +        with support.change_cwd(root_dir), captured_stdout():
187 +            spawn(tar_cmd)
188  
189 -        self.assertTrue(os.path.exists(tarball2))
190 +        self.assertTrue(os.path.isfile(tarball2))
191          # let's compare both tarballs
192          self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))
193  
194          # trying an uncompressed one
195 -        base_name = os.path.join(tmpdir2, 'archive')
196 -        old_dir = os.getcwd()
197 -        os.chdir(tmpdir)
198 -        try:
199 -            _make_tarball(base_name, 'dist', compress=None)
200 -        finally:
201 -            os.chdir(old_dir)
202 +        make_archive(base_name, 'tar', root_dir, base_dir)
203          tarball = base_name + '.tar'
204 -        self.assertTrue(os.path.exists(tarball))
205 +        self.assertTrue(os.path.isfile(tarball))
206  
207          # now for a dry_run
208 -        base_name = os.path.join(tmpdir2, 'archive')
209 -        old_dir = os.getcwd()
210 -        os.chdir(tmpdir)
211 -        try:
212 -            _make_tarball(base_name, 'dist', compress=None, dry_run=True)
213 -        finally:
214 -            os.chdir(old_dir)
215 +        make_archive(base_name, 'tar', root_dir, base_dir, dry_run=True)
216          tarball = base_name + '.tar'
217 -        self.assertTrue(os.path.exists(tarball))
218 +        self.assertTrue(os.path.isfile(tarball))
219  
220      @unittest.skipUnless(zlib, "Requires zlib")
221      @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
222      def test_make_zipfile(self):
223 -        # creating something to tar
224 -        tmpdir = self.mkdtemp()
225 -        self.write_file([tmpdir, 'file1'], 'xxx')
226 -        self.write_file([tmpdir, 'file2'], 'xxx')
227 +        # creating something to zip
228 +        root_dir, base_dir = self._create_files()
229 +        base_name = os.path.join(self.mkdtemp(), 'archive')
230  
231 -        tmpdir2 = self.mkdtemp()
232 -        # force shutil to create the directory
233 -        os.rmdir(tmpdir2)
234 -        base_name = os.path.join(tmpdir2, 'archive')
235 -        _make_zipfile(base_name, tmpdir)
236 +        res = make_archive(base_name, 'zip', root_dir, base_dir)
237  
238 -        # check if the compressed tarball was created
239 -        tarball = base_name + '.zip'
240 -        self.assertTrue(os.path.exists(tarball))
241 +        self.assertEqual(res, base_name + '.zip')
242 +        self.assertTrue(os.path.isfile(res))
243 +        self.assertTrue(zipfile.is_zipfile(res))
244 +        with zipfile.ZipFile(res) as zf:
245 +            self.assertEqual(sorted(zf.namelist()),
246 +                             ['dist/', 'dist/file1', 'dist/file2',
247 +                             'dist/sub/', 'dist/sub/file3', 'dist/sub2/'])
248  
249 +    @unittest.skipUnless(zlib, "Requires zlib")
250 +    @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
251 +    @unittest.skipUnless(find_executable('zip'),
252 +                         'Need the zip command to run')
253 +    def test_zipfile_vs_zip(self):
254 +        root_dir, base_dir = self._create_files()
255 +        base_name = os.path.join(self.mkdtemp(), 'archive')
256 +        archive = make_archive(base_name, 'zip', root_dir, base_dir)
257 +
258 +        # check if ZIP file  was created
259 +        self.assertEqual(archive, base_name + '.zip')
260 +        self.assertTrue(os.path.isfile(archive))
261 +
262 +        # now create another ZIP file using `zip`
263 +        archive2 = os.path.join(root_dir, 'archive2.zip')
264 +        zip_cmd = ['zip', '-q', '-r', 'archive2.zip', base_dir]
265 +        with support.change_cwd(root_dir):
266 +            spawn(zip_cmd)
267 +
268 +        self.assertTrue(os.path.isfile(archive2))
269 +        # let's compare both ZIP files
270 +        with zipfile.ZipFile(archive) as zf:
271 +            names = zf.namelist()
272 +        with zipfile.ZipFile(archive2) as zf:
273 +            names2 = zf.namelist()
274 +        self.assertEqual(sorted(names), sorted(names2))
275  
276      def test_make_archive(self):
277          tmpdir = self.mkdtemp()
278 @@ -521,39 +517,36 @@ class TestShutil(unittest.TestCase):
279          else:
280              group = owner = 'root'
281  
282 -        base_dir, root_dir, base_name =  self._create_files()
283 -        base_name = os.path.join(self.mkdtemp() , 'archive')
284 +        root_dir, base_dir = self._create_files()
285 +        base_name = os.path.join(self.mkdtemp(), 'archive')
286          res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
287                             group=group)
288 -        self.assertTrue(os.path.exists(res))
289 +        self.assertTrue(os.path.isfile(res))
290  
291          res = make_archive(base_name, 'zip', root_dir, base_dir)
292 -        self.assertTrue(os.path.exists(res))
293 +        self.assertTrue(os.path.isfile(res))
294  
295          res = make_archive(base_name, 'tar', root_dir, base_dir,
296                             owner=owner, group=group)
297 -        self.assertTrue(os.path.exists(res))
298 +        self.assertTrue(os.path.isfile(res))
299  
300          res = make_archive(base_name, 'tar', root_dir, base_dir,
301                             owner='kjhkjhkjg', group='oihohoh')
302 -        self.assertTrue(os.path.exists(res))
303 +        self.assertTrue(os.path.isfile(res))
304  
305      @unittest.skipUnless(zlib, "Requires zlib")
306      @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
307      def test_tarfile_root_owner(self):
308 -        tmpdir, tmpdir2, base_name =  self._create_files()
309 -        old_dir = os.getcwd()
310 -        os.chdir(tmpdir)
311 +        root_dir, base_dir = self._create_files()
312 +        base_name = os.path.join(self.mkdtemp(), 'archive')
313          group = grp.getgrgid(0)[0]
314          owner = pwd.getpwuid(0)[0]
315 -        try:
316 -            archive_name = _make_tarball(base_name, 'dist', compress=None,
317 -                                         owner=owner, group=group)
318 -        finally:
319 -            os.chdir(old_dir)
320 +        with support.change_cwd(root_dir):
321 +            archive_name = make_archive(base_name, 'gztar', root_dir, 'dist',
322 +                                        owner=owner, group=group)
323  
324          # check if the compressed tarball was created
325 -        self.assertTrue(os.path.exists(archive_name))
326 +        self.assertTrue(os.path.isfile(archive_name))
327  
328          # now checks the rights
329          archive = tarfile.open(archive_name)
330 @@ -859,7 +852,7 @@ class TestCopyFile(unittest.TestCase):
331  
332  
333  def test_main():
334 -    test_support.run_unittest(TestShutil, TestMove, TestCopyFile)
335 +    support.run_unittest(TestShutil, TestMove, TestCopyFile)
336  
337  if __name__ == '__main__':
338      test_main()
339 diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
340 index 42c1b4d..98a9275 100644
341 --- a/Lib/test/test_support.py
342 +++ b/Lib/test/test_support.py
343 @@ -491,6 +491,33 @@ TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
344  SAVEDCWD = os.getcwd()
345  
346  @contextlib.contextmanager
347 +def change_cwd(path, quiet=False):
348 +    """Return a context manager that changes the current working directory.
349 +
350 +    Arguments:
351 +
352 +      path: the directory to use as the temporary current working directory.
353 +
354 +      quiet: if False (the default), the context manager raises an exception
355 +        on error.  Otherwise, it issues only a warning and keeps the current
356 +        working directory the same.
357 +
358 +    """
359 +    saved_dir = os.getcwd()
360 +    try:
361 +        os.chdir(path)
362 +    except OSError:
363 +        if not quiet:
364 +            raise
365 +        warnings.warn('tests may fail, unable to change CWD to: ' + path,
366 +                      RuntimeWarning, stacklevel=3)
367 +    try:
368 +        yield os.getcwd()
369 +    finally:
370 +        os.chdir(saved_dir)
371 +
372 +
373 +@contextlib.contextmanager
374  def temp_cwd(name='tempcwd', quiet=False):
375      """
376      Context manager that creates a temporary directory and set it as CWD.