Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(127231)

Side by Side Diff: Lib/distutils/tests/test_archive_util.py

Issue 6516: reset owner/group to root for distutils tarballs
Patch Set: Created 6 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/distutils/command/sdist.py ('k') | Lib/distutils/tests/test_sdist.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 """Tests for distutils.archive_util.""" 2 """Tests for distutils.archive_util."""
3 import unittest 3 import unittest
4 import os 4 import os
5 import sys 5 import sys
6 import tarfile 6 import tarfile
7 from os.path import splitdrive 7 from os.path import splitdrive
8 import warnings 8 import warnings
9 9
10 from distutils import archive_util 10 from distutils import archive_util
11 from distutils.archive_util import (check_archive_formats, make_tarball, 11 from distutils.archive_util import (check_archive_formats, make_tarball,
12 make_zipfile, make_archive, 12 make_zipfile, make_archive,
13 ARCHIVE_FORMATS) 13 ARCHIVE_FORMATS)
14 from distutils.spawn import find_executable, spawn 14 from distutils.spawn import find_executable, spawn
15 from distutils.tests import support 15 from distutils.tests import support
16 from test.support import check_warnings, run_unittest, patch 16 from test.support import check_warnings, run_unittest, patch
17
18 try:
19 import grp
20 import pwd
21 UID_GID_SUPPORT = True
22 except ImportError:
23 UID_GID_SUPPORT = False
17 24
18 try: 25 try:
19 import zipfile 26 import zipfile
20 ZIP_SUPPORT = True 27 ZIP_SUPPORT = True
21 except ImportError: 28 except ImportError:
22 ZIP_SUPPORT = find_executable('zip') 29 ZIP_SUPPORT = find_executable('zip')
23 30
24 try: 31 try:
25 import zlib 32 import zlib
26 ZLIB_SUPPORT = True 33 ZLIB_SUPPORT = True
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 def _make_tarball(self, target_name): 77 def _make_tarball(self, target_name):
71 # creating something to tar 78 # creating something to tar
72 tmpdir = self.mkdtemp() 79 tmpdir = self.mkdtemp()
73 self.write_file([tmpdir, 'file1'], 'xxx') 80 self.write_file([tmpdir, 'file1'], 'xxx')
74 self.write_file([tmpdir, 'file2'], 'xxx') 81 self.write_file([tmpdir, 'file2'], 'xxx')
75 os.mkdir(os.path.join(tmpdir, 'sub')) 82 os.mkdir(os.path.join(tmpdir, 'sub'))
76 self.write_file([tmpdir, 'sub', 'file3'], 'xxx') 83 self.write_file([tmpdir, 'sub', 'file3'], 'xxx')
77 84
78 tmpdir2 = self.mkdtemp() 85 tmpdir2 = self.mkdtemp()
79 unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], 86 unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
80 "Source and target should be on same drive") 87 "source and target should be on same drive")
81 88
82 base_name = os.path.join(tmpdir2, target_name) 89 base_name = os.path.join(tmpdir2, target_name)
83 90
84 # working with relative paths to avoid tar warnings 91 # working with relative paths to avoid tar warnings
85 old_dir = os.getcwd() 92 old_dir = os.getcwd()
86 os.chdir(tmpdir) 93 os.chdir(tmpdir)
87 try: 94 try:
88 make_tarball(splitdrive(base_name)[1], '.') 95 make_tarball(splitdrive(base_name)[1], '.')
89 finally: 96 finally:
90 os.chdir(old_dir) 97 os.chdir(old_dir)
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 raise RuntimeError() 274 raise RuntimeError()
268 ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file') 275 ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file')
269 try: 276 try:
270 try: 277 try:
271 make_archive('xxx', 'xxx', root_dir=self.mkdtemp()) 278 make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
272 except: 279 except:
273 pass 280 pass
274 self.assertEqual(os.getcwd(), current_dir) 281 self.assertEqual(os.getcwd(), current_dir)
275 finally: 282 finally:
276 del ARCHIVE_FORMATS['xxx'] 283 del ARCHIVE_FORMATS['xxx']
284
285 def test_make_archive_owner_group(self):
286 # testing make_archive with owner and group, with various combinations
287 # this works even if there's not gid/uid support
288 if UID_GID_SUPPORT:
289 group = grp.getgrgid(0)[0]
290 owner = pwd.getpwuid(0)[0]
291 else:
292 group = owner = 'root'
293
294 base_dir, root_dir, base_name = self._create_files()
295 base_name = os.path.join(self.mkdtemp() , 'archive')
296 res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
297 group=group)
298 self.assertTrue(os.path.exists(res))
299
300 res = make_archive(base_name, 'zip', root_dir, base_dir)
301 self.assertTrue(os.path.exists(res))
302
303 res = make_archive(base_name, 'tar', root_dir, base_dir,
304 owner=owner, group=group)
305 self.assertTrue(os.path.exists(res))
306
307 res = make_archive(base_name, 'tar', root_dir, base_dir,
308 owner='kjhkjhkjg', group='oihohoh')
309 self.assertTrue(os.path.exists(res))
310
311 @unittest.skipUnless(zlib, "Requires zlib")
312 @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
313 def test_tarfile_root_owner(self):
314 tmpdir, tmpdir2, base_name = self._create_files()
315 old_dir = os.getcwd()
316 os.chdir(tmpdir)
317 group = grp.getgrgid(0)[0]
318 owner = pwd.getpwuid(0)[0]
319 try:
320 archive_name = make_tarball(base_name, 'dist', compress=None,
321 owner=owner, group=group)
322 finally:
323 os.chdir(old_dir)
324
325 # check if the compressed tarball was created
326 self.assertTrue(os.path.exists(archive_name))
327
328 # now checks the rights
329 archive = tarfile.open(archive_name)
330 try:
331 for member in archive.getmembers():
332 self.assertEquals(member.uid, 0)
333 self.assertEquals(member.gid, 0)
334 finally:
335 archive.close()
277 336
278 def test_suite(): 337 def test_suite():
279 return unittest.makeSuite(ArchiveUtilTestCase) 338 return unittest.makeSuite(ArchiveUtilTestCase)
280 339
281 if __name__ == "__main__": 340 if __name__ == "__main__":
282 run_unittest(test_suite()) 341 run_unittest(test_suite())
OLDNEW
« no previous file with comments | « Lib/distutils/command/sdist.py ('k') | Lib/distutils/tests/test_sdist.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+