PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Lib/distutils/tests/test_archive_util.py

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