/Contents/Libraries/Shared/distutils/tests/test_archive_util.py

https://gitlab.com/sisfs/G_Music · Python · 269 lines · 210 code · 39 blank · 20 comment · 35 complexity · f56c8493fad795f178c6f95d70df7814 MD5 · raw file

  1. """Tests for distutils.archive_util."""
  2. __revision__ = "$Id$"
  3. import unittest
  4. import os
  5. import tarfile
  6. from os.path import splitdrive
  7. import warnings
  8. from distutils.archive_util import (check_archive_formats, make_tarball,
  9. make_zipfile, make_archive)
  10. from distutils.spawn import find_executable, spawn
  11. from distutils.tests import support
  12. from test.test_support import check_warnings
  13. try:
  14. import grp
  15. import pwd
  16. UID_GID_SUPPORT = True
  17. except ImportError:
  18. UID_GID_SUPPORT = False
  19. try:
  20. import zipfile
  21. ZIP_SUPPORT = True
  22. except ImportError:
  23. ZIP_SUPPORT = find_executable('zip')
  24. # some tests will fail if zlib is not available
  25. try:
  26. import zlib
  27. except ImportError:
  28. zlib = None
  29. class ArchiveUtilTestCase(support.TempdirManager,
  30. support.LoggingSilencer,
  31. unittest.TestCase):
  32. @unittest.skipUnless(zlib, "requires zlib")
  33. def test_make_tarball(self):
  34. # creating something to tar
  35. tmpdir = self.mkdtemp()
  36. self.write_file([tmpdir, 'file1'], 'xxx')
  37. self.write_file([tmpdir, 'file2'], 'xxx')
  38. os.mkdir(os.path.join(tmpdir, 'sub'))
  39. self.write_file([tmpdir, 'sub', 'file3'], 'xxx')
  40. tmpdir2 = self.mkdtemp()
  41. unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
  42. "source and target should be on same drive")
  43. base_name = os.path.join(tmpdir2, 'archive')
  44. # working with relative paths to avoid tar warnings
  45. old_dir = os.getcwd()
  46. os.chdir(tmpdir)
  47. try:
  48. make_tarball(splitdrive(base_name)[1], '.')
  49. finally:
  50. os.chdir(old_dir)
  51. # check if the compressed tarball was created
  52. tarball = base_name + '.tar.gz'
  53. self.assertTrue(os.path.exists(tarball))
  54. # trying an uncompressed one
  55. base_name = os.path.join(tmpdir2, 'archive')
  56. old_dir = os.getcwd()
  57. os.chdir(tmpdir)
  58. try:
  59. make_tarball(splitdrive(base_name)[1], '.', compress=None)
  60. finally:
  61. os.chdir(old_dir)
  62. tarball = base_name + '.tar'
  63. self.assertTrue(os.path.exists(tarball))
  64. def _tarinfo(self, path):
  65. tar = tarfile.open(path)
  66. try:
  67. names = tar.getnames()
  68. names.sort()
  69. return tuple(names)
  70. finally:
  71. tar.close()
  72. def _create_files(self):
  73. # creating something to tar
  74. tmpdir = self.mkdtemp()
  75. dist = os.path.join(tmpdir, 'dist')
  76. os.mkdir(dist)
  77. self.write_file([dist, 'file1'], 'xxx')
  78. self.write_file([dist, 'file2'], 'xxx')
  79. os.mkdir(os.path.join(dist, 'sub'))
  80. self.write_file([dist, 'sub', 'file3'], 'xxx')
  81. os.mkdir(os.path.join(dist, 'sub2'))
  82. tmpdir2 = self.mkdtemp()
  83. base_name = os.path.join(tmpdir2, 'archive')
  84. return tmpdir, tmpdir2, base_name
  85. @unittest.skipUnless(zlib, "Requires zlib")
  86. @unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
  87. 'Need the tar command to run')
  88. def test_tarfile_vs_tar(self):
  89. tmpdir, tmpdir2, base_name = self._create_files()
  90. old_dir = os.getcwd()
  91. os.chdir(tmpdir)
  92. try:
  93. make_tarball(base_name, 'dist')
  94. finally:
  95. os.chdir(old_dir)
  96. # check if the compressed tarball was created
  97. tarball = base_name + '.tar.gz'
  98. self.assertTrue(os.path.exists(tarball))
  99. # now create another tarball using `tar`
  100. tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
  101. tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
  102. gzip_cmd = ['gzip', '-f9', 'archive2.tar']
  103. old_dir = os.getcwd()
  104. os.chdir(tmpdir)
  105. try:
  106. spawn(tar_cmd)
  107. spawn(gzip_cmd)
  108. finally:
  109. os.chdir(old_dir)
  110. self.assertTrue(os.path.exists(tarball2))
  111. # let's compare both tarballs
  112. self.assertEquals(self._tarinfo(tarball), self._tarinfo(tarball2))
  113. # trying an uncompressed one
  114. base_name = os.path.join(tmpdir2, 'archive')
  115. old_dir = os.getcwd()
  116. os.chdir(tmpdir)
  117. try:
  118. make_tarball(base_name, 'dist', compress=None)
  119. finally:
  120. os.chdir(old_dir)
  121. tarball = base_name + '.tar'
  122. self.assertTrue(os.path.exists(tarball))
  123. # now for a dry_run
  124. base_name = os.path.join(tmpdir2, 'archive')
  125. old_dir = os.getcwd()
  126. os.chdir(tmpdir)
  127. try:
  128. make_tarball(base_name, 'dist', compress=None, dry_run=True)
  129. finally:
  130. os.chdir(old_dir)
  131. tarball = base_name + '.tar'
  132. self.assertTrue(os.path.exists(tarball))
  133. @unittest.skipUnless(find_executable('compress'),
  134. 'The compress program is required')
  135. def test_compress_deprecated(self):
  136. tmpdir, tmpdir2, base_name = self._create_files()
  137. # using compress and testing the PendingDeprecationWarning
  138. old_dir = os.getcwd()
  139. os.chdir(tmpdir)
  140. try:
  141. with check_warnings() as w:
  142. warnings.simplefilter("always")
  143. make_tarball(base_name, 'dist', compress='compress')
  144. finally:
  145. os.chdir(old_dir)
  146. tarball = base_name + '.tar.Z'
  147. self.assertTrue(os.path.exists(tarball))
  148. self.assertEquals(len(w.warnings), 1)
  149. # same test with dry_run
  150. os.remove(tarball)
  151. old_dir = os.getcwd()
  152. os.chdir(tmpdir)
  153. try:
  154. with check_warnings() as w:
  155. warnings.simplefilter("always")
  156. make_tarball(base_name, 'dist', compress='compress',
  157. dry_run=True)
  158. finally:
  159. os.chdir(old_dir)
  160. self.assertTrue(not os.path.exists(tarball))
  161. self.assertEquals(len(w.warnings), 1)
  162. @unittest.skipUnless(zlib, "Requires zlib")
  163. @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
  164. def test_make_zipfile(self):
  165. # creating something to tar
  166. tmpdir = self.mkdtemp()
  167. self.write_file([tmpdir, 'file1'], 'xxx')
  168. self.write_file([tmpdir, 'file2'], 'xxx')
  169. tmpdir2 = self.mkdtemp()
  170. base_name = os.path.join(tmpdir2, 'archive')
  171. make_zipfile(base_name, tmpdir)
  172. # check if the compressed tarball was created
  173. tarball = base_name + '.zip'
  174. def test_check_archive_formats(self):
  175. self.assertEquals(check_archive_formats(['gztar', 'xxx', 'zip']),
  176. 'xxx')
  177. self.assertEquals(check_archive_formats(['gztar', 'zip']), None)
  178. def test_make_archive(self):
  179. tmpdir = self.mkdtemp()
  180. base_name = os.path.join(tmpdir, 'archive')
  181. self.assertRaises(ValueError, make_archive, base_name, 'xxx')
  182. @unittest.skipUnless(zlib, "Requires zlib")
  183. def test_make_archive_owner_group(self):
  184. # testing make_archive with owner and group, with various combinations
  185. # this works even if there's not gid/uid support
  186. if UID_GID_SUPPORT:
  187. group = grp.getgrgid(0)[0]
  188. owner = pwd.getpwuid(0)[0]
  189. else:
  190. group = owner = 'root'
  191. base_dir, root_dir, base_name = self._create_files()
  192. base_name = os.path.join(self.mkdtemp() , 'archive')
  193. res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
  194. group=group)
  195. self.assertTrue(os.path.exists(res))
  196. res = make_archive(base_name, 'zip', root_dir, base_dir)
  197. self.assertTrue(os.path.exists(res))
  198. res = make_archive(base_name, 'tar', root_dir, base_dir,
  199. owner=owner, group=group)
  200. self.assertTrue(os.path.exists(res))
  201. res = make_archive(base_name, 'tar', root_dir, base_dir,
  202. owner='kjhkjhkjg', group='oihohoh')
  203. self.assertTrue(os.path.exists(res))
  204. @unittest.skipUnless(zlib, "Requires zlib")
  205. @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
  206. def test_tarfile_root_owner(self):
  207. tmpdir, tmpdir2, base_name = self._create_files()
  208. old_dir = os.getcwd()
  209. os.chdir(tmpdir)
  210. group = grp.getgrgid(0)[0]
  211. owner = pwd.getpwuid(0)[0]
  212. try:
  213. archive_name = make_tarball(base_name, 'dist', compress=None,
  214. owner=owner, group=group)
  215. finally:
  216. os.chdir(old_dir)
  217. # check if the compressed tarball was created
  218. self.assertTrue(os.path.exists(archive_name))
  219. # now checks the rights
  220. archive = tarfile.open(archive_name)
  221. try:
  222. for member in archive.getmembers():
  223. self.assertEquals(member.uid, 0)
  224. self.assertEquals(member.gid, 0)
  225. finally:
  226. archive.close()
  227. def test_suite():
  228. return unittest.makeSuite(ArchiveUtilTestCase)
  229. if __name__ == "__main__":
  230. unittest.main(defaultTest="test_suite")