2009-01-09 00:11:44 -04:00
|
|
|
"""Tests for distutils.command.sdist."""
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import shutil
|
|
|
|
import zipfile
|
|
|
|
from os.path import join
|
2009-01-26 13:23:20 -04:00
|
|
|
import sys
|
2009-02-14 10:35:51 -04:00
|
|
|
import tempfile
|
2009-05-16 13:52:13 -03:00
|
|
|
import warnings
|
2009-01-09 00:11:44 -04:00
|
|
|
|
2009-05-16 13:52:13 -03:00
|
|
|
from test.support import check_warnings
|
2009-05-14 09:45:48 -03:00
|
|
|
from test.support import captured_stdout
|
|
|
|
|
2009-01-09 00:11:44 -04:00
|
|
|
from distutils.command.sdist import sdist
|
2009-05-14 09:45:48 -03:00
|
|
|
from distutils.command.sdist import show_formats
|
2009-01-09 00:11:44 -04:00
|
|
|
from distutils.core import Distribution
|
|
|
|
from distutils.tests.test_config import PyPIRCCommandTestCase
|
2009-05-14 12:21:26 -03:00
|
|
|
from distutils.errors import DistutilsExecError, DistutilsOptionError
|
2009-01-29 19:54:06 -04:00
|
|
|
from distutils.spawn import find_executable
|
2009-02-16 17:49:12 -04:00
|
|
|
from distutils.tests import support
|
2009-05-16 13:52:13 -03:00
|
|
|
from distutils.log import WARN
|
2009-05-14 09:45:48 -03:00
|
|
|
from distutils.archive_util import ARCHIVE_FORMATS
|
2009-01-09 00:11:44 -04:00
|
|
|
|
|
|
|
SETUP_PY = """
|
|
|
|
from distutils.core import setup
|
|
|
|
import somecode
|
|
|
|
|
|
|
|
setup(name='fake')
|
|
|
|
"""
|
|
|
|
|
2009-02-16 17:49:12 -04:00
|
|
|
MANIFEST = """\
|
|
|
|
README
|
2009-02-17 19:10:18 -04:00
|
|
|
inroot.txt
|
2009-02-16 17:49:12 -04:00
|
|
|
setup.py
|
2009-02-16 17:51:13 -04:00
|
|
|
data%(sep)sdata.dt
|
|
|
|
scripts%(sep)sscript.py
|
2009-02-17 05:47:25 -04:00
|
|
|
some%(sep)sfile.txt
|
|
|
|
some%(sep)sother_file.txt
|
2009-02-16 17:51:13 -04:00
|
|
|
somecode%(sep)s__init__.py
|
|
|
|
somecode%(sep)sdoc.dat
|
|
|
|
somecode%(sep)sdoc.txt
|
2009-01-09 00:11:44 -04:00
|
|
|
"""
|
|
|
|
|
2009-05-16 13:52:13 -03:00
|
|
|
class SDistTestCase(PyPIRCCommandTestCase):
|
2009-01-09 00:11:44 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
2009-02-14 10:35:51 -04:00
|
|
|
# PyPIRCCommandTestCase creates a temp dir already
|
|
|
|
# and put it in self.tmp_dir
|
2009-05-16 13:52:13 -03:00
|
|
|
super(SDistTestCase, self).setUp()
|
2009-02-14 10:35:51 -04:00
|
|
|
# setting up an environment
|
2009-01-09 00:11:44 -04:00
|
|
|
self.old_path = os.getcwd()
|
2009-02-14 10:35:51 -04:00
|
|
|
os.mkdir(join(self.tmp_dir, 'somecode'))
|
|
|
|
os.mkdir(join(self.tmp_dir, 'dist'))
|
2009-02-16 17:49:12 -04:00
|
|
|
# a package, and a README
|
|
|
|
self.write_file((self.tmp_dir, 'README'), 'xxx')
|
|
|
|
self.write_file((self.tmp_dir, 'somecode', '__init__.py'), '#')
|
|
|
|
self.write_file((self.tmp_dir, 'setup.py'), SETUP_PY)
|
2009-02-14 10:35:51 -04:00
|
|
|
os.chdir(self.tmp_dir)
|
2009-01-09 00:11:44 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
2009-02-14 10:35:51 -04:00
|
|
|
# back to normal
|
2009-01-09 00:11:44 -04:00
|
|
|
os.chdir(self.old_path)
|
2009-05-16 13:52:13 -03:00
|
|
|
super(SDistTestCase, self).tearDown()
|
2009-02-16 17:49:12 -04:00
|
|
|
|
|
|
|
def get_cmd(self, metadata=None):
|
|
|
|
"""Returns a cmd"""
|
|
|
|
if metadata is None:
|
|
|
|
metadata = {'name': 'fake', 'version': '1.0',
|
|
|
|
'url': 'xxx', 'author': 'xxx',
|
|
|
|
'author_email': 'xxx'}
|
|
|
|
dist = Distribution(metadata)
|
|
|
|
dist.script_name = 'setup.py'
|
|
|
|
dist.packages = ['somecode']
|
|
|
|
dist.include_package_data = True
|
|
|
|
cmd = sdist(dist)
|
|
|
|
cmd.dist_dir = 'dist'
|
|
|
|
def _warn(*args):
|
|
|
|
pass
|
|
|
|
cmd.warn = _warn
|
|
|
|
return dist, cmd
|
2009-01-09 00:11:44 -04:00
|
|
|
|
|
|
|
def test_prune_file_list(self):
|
|
|
|
# this test creates a package with some vcs dirs in it
|
|
|
|
# and launch sdist to make sure they get pruned
|
|
|
|
# on all systems
|
|
|
|
|
|
|
|
# creating VCS directories with some files in them
|
2009-02-14 10:35:51 -04:00
|
|
|
os.mkdir(join(self.tmp_dir, 'somecode', '.svn'))
|
2009-02-16 17:49:12 -04:00
|
|
|
self.write_file((self.tmp_dir, 'somecode', '.svn', 'ok.py'), 'xxx')
|
2009-01-09 00:11:44 -04:00
|
|
|
|
2009-02-14 10:35:51 -04:00
|
|
|
os.mkdir(join(self.tmp_dir, 'somecode', '.hg'))
|
2009-02-16 17:49:12 -04:00
|
|
|
self.write_file((self.tmp_dir, 'somecode', '.hg',
|
2009-01-09 00:11:44 -04:00
|
|
|
'ok'), 'xxx')
|
|
|
|
|
2009-02-14 10:35:51 -04:00
|
|
|
os.mkdir(join(self.tmp_dir, 'somecode', '.git'))
|
2009-02-16 17:49:12 -04:00
|
|
|
self.write_file((self.tmp_dir, 'somecode', '.git',
|
2009-01-09 00:11:44 -04:00
|
|
|
'ok'), 'xxx')
|
|
|
|
|
|
|
|
# now building a sdist
|
2009-02-16 17:49:12 -04:00
|
|
|
dist, cmd = self.get_cmd()
|
2009-01-09 00:11:44 -04:00
|
|
|
|
|
|
|
# zip is available universally
|
|
|
|
# (tar might not be installed under win32)
|
|
|
|
cmd.formats = ['zip']
|
2009-02-16 17:49:12 -04:00
|
|
|
|
|
|
|
cmd.ensure_finalized()
|
2009-01-09 00:11:44 -04:00
|
|
|
cmd.run()
|
|
|
|
|
|
|
|
# now let's check what we have
|
2009-02-14 10:35:51 -04:00
|
|
|
dist_folder = join(self.tmp_dir, 'dist')
|
2009-01-09 00:11:44 -04:00
|
|
|
files = os.listdir(dist_folder)
|
|
|
|
self.assertEquals(files, ['fake-1.0.zip'])
|
|
|
|
|
|
|
|
zip_file = zipfile.ZipFile(join(dist_folder, 'fake-1.0.zip'))
|
|
|
|
try:
|
|
|
|
content = zip_file.namelist()
|
|
|
|
finally:
|
|
|
|
zip_file.close()
|
|
|
|
|
|
|
|
# making sure everything has been pruned correctly
|
|
|
|
self.assertEquals(len(content), 4)
|
|
|
|
|
2009-01-26 13:23:20 -04:00
|
|
|
def test_make_distribution(self):
|
|
|
|
|
2009-01-29 19:54:06 -04:00
|
|
|
# check if tar and gzip are installed
|
|
|
|
if (find_executable('tar') is None or
|
|
|
|
find_executable('gzip') is None):
|
|
|
|
return
|
2009-01-26 13:23:20 -04:00
|
|
|
|
|
|
|
# now building a sdist
|
2009-02-16 17:49:12 -04:00
|
|
|
dist, cmd = self.get_cmd()
|
2009-01-26 13:23:20 -04:00
|
|
|
|
|
|
|
# creating a gztar then a tar
|
|
|
|
cmd.formats = ['gztar', 'tar']
|
2009-02-16 17:49:12 -04:00
|
|
|
cmd.ensure_finalized()
|
2009-01-26 13:23:20 -04:00
|
|
|
cmd.run()
|
|
|
|
|
|
|
|
# making sure we have two files
|
2009-02-14 10:35:51 -04:00
|
|
|
dist_folder = join(self.tmp_dir, 'dist')
|
2009-01-26 13:23:20 -04:00
|
|
|
result = os.listdir(dist_folder)
|
|
|
|
result.sort()
|
|
|
|
self.assertEquals(result,
|
|
|
|
['fake-1.0.tar', 'fake-1.0.tar.gz'] )
|
|
|
|
|
|
|
|
os.remove(join(dist_folder, 'fake-1.0.tar'))
|
|
|
|
os.remove(join(dist_folder, 'fake-1.0.tar.gz'))
|
|
|
|
|
|
|
|
# now trying a tar then a gztar
|
|
|
|
cmd.formats = ['tar', 'gztar']
|
2009-02-16 17:49:12 -04:00
|
|
|
|
|
|
|
cmd.ensure_finalized()
|
2009-01-26 13:23:20 -04:00
|
|
|
cmd.run()
|
|
|
|
|
|
|
|
result = os.listdir(dist_folder)
|
|
|
|
result.sort()
|
|
|
|
self.assertEquals(result,
|
|
|
|
['fake-1.0.tar', 'fake-1.0.tar.gz'])
|
|
|
|
|
2009-02-16 17:49:12 -04:00
|
|
|
def test_add_defaults(self):
|
|
|
|
|
|
|
|
# http://bugs.python.org/issue2279
|
|
|
|
|
|
|
|
# add_default should also include
|
|
|
|
# data_files and package_data
|
|
|
|
dist, cmd = self.get_cmd()
|
|
|
|
|
|
|
|
# filling data_files by pointing files
|
|
|
|
# in package_data
|
|
|
|
dist.package_data = {'': ['*.cfg', '*.dat'],
|
|
|
|
'somecode': ['*.txt']}
|
|
|
|
self.write_file((self.tmp_dir, 'somecode', 'doc.txt'), '#')
|
|
|
|
self.write_file((self.tmp_dir, 'somecode', 'doc.dat'), '#')
|
|
|
|
|
|
|
|
# adding some data in data_files
|
|
|
|
data_dir = join(self.tmp_dir, 'data')
|
|
|
|
os.mkdir(data_dir)
|
|
|
|
self.write_file((data_dir, 'data.dt'), '#')
|
2009-02-17 05:47:25 -04:00
|
|
|
some_dir = join(self.tmp_dir, 'some')
|
|
|
|
os.mkdir(some_dir)
|
2009-02-17 19:10:18 -04:00
|
|
|
self.write_file((self.tmp_dir, 'inroot.txt'), '#')
|
2009-02-17 05:47:25 -04:00
|
|
|
self.write_file((some_dir, 'file.txt'), '#')
|
|
|
|
self.write_file((some_dir, 'other_file.txt'), '#')
|
|
|
|
|
2009-02-17 19:10:18 -04:00
|
|
|
dist.data_files = [('data', ['data/data.dt',
|
|
|
|
'inroot.txt',
|
|
|
|
'notexisting']),
|
2009-02-17 05:47:25 -04:00
|
|
|
'some/file.txt',
|
|
|
|
'some/other_file.txt']
|
2009-02-16 17:49:12 -04:00
|
|
|
|
|
|
|
# adding a script
|
|
|
|
script_dir = join(self.tmp_dir, 'scripts')
|
|
|
|
os.mkdir(script_dir)
|
|
|
|
self.write_file((script_dir, 'script.py'), '#')
|
|
|
|
dist.scripts = [join('scripts', 'script.py')]
|
|
|
|
|
|
|
|
cmd.formats = ['zip']
|
|
|
|
cmd.use_defaults = True
|
|
|
|
|
|
|
|
cmd.ensure_finalized()
|
|
|
|
cmd.run()
|
|
|
|
|
|
|
|
# now let's check what we have
|
|
|
|
dist_folder = join(self.tmp_dir, 'dist')
|
|
|
|
files = os.listdir(dist_folder)
|
|
|
|
self.assertEquals(files, ['fake-1.0.zip'])
|
|
|
|
|
|
|
|
zip_file = zipfile.ZipFile(join(dist_folder, 'fake-1.0.zip'))
|
|
|
|
try:
|
|
|
|
content = zip_file.namelist()
|
|
|
|
finally:
|
|
|
|
zip_file.close()
|
|
|
|
|
|
|
|
# making sure everything was added
|
2009-02-17 19:10:18 -04:00
|
|
|
self.assertEquals(len(content), 11)
|
2009-02-16 17:49:12 -04:00
|
|
|
|
|
|
|
# checking the MANIFEST
|
|
|
|
manifest = open(join(self.tmp_dir, 'MANIFEST')).read()
|
2009-02-16 17:51:13 -04:00
|
|
|
self.assertEquals(manifest, MANIFEST % {'sep': os.sep})
|
2009-02-16 17:49:12 -04:00
|
|
|
|
2009-05-16 13:52:13 -03:00
|
|
|
def test_metadata_check_option(self):
|
|
|
|
# testing the `medata-check` option
|
|
|
|
dist, cmd = self.get_cmd(metadata={})
|
|
|
|
|
|
|
|
# this should raise some warnings !
|
|
|
|
# with the `check` subcommand
|
|
|
|
cmd.ensure_finalized()
|
|
|
|
cmd.run()
|
|
|
|
warnings = self.get_logs(WARN)
|
|
|
|
self.assertEquals(len(warnings), 2)
|
|
|
|
|
|
|
|
# trying with a complete set of metadata
|
|
|
|
self.clear_logs()
|
|
|
|
dist, cmd = self.get_cmd()
|
|
|
|
cmd.ensure_finalized()
|
|
|
|
cmd.metadata_check = 0
|
|
|
|
cmd.run()
|
|
|
|
warnings = self.get_logs(WARN)
|
|
|
|
self.assertEquals(len(warnings), 0)
|
|
|
|
|
|
|
|
def test_check_metadata_deprecated(self):
|
|
|
|
# makes sure make_metadata is deprecated
|
|
|
|
dist, cmd = self.get_cmd()
|
|
|
|
with check_warnings() as w:
|
|
|
|
warnings.simplefilter("always")
|
|
|
|
cmd.check_metadata()
|
|
|
|
self.assertEquals(len(w.warnings), 1)
|
|
|
|
|
2009-05-14 09:45:48 -03:00
|
|
|
def test_show_formats(self):
|
|
|
|
with captured_stdout() as stdout:
|
|
|
|
show_formats()
|
|
|
|
|
|
|
|
# the output should be a header line + one line per format
|
|
|
|
num_formats = len(ARCHIVE_FORMATS.keys())
|
|
|
|
output = [line for line in stdout.getvalue().split('\n')
|
|
|
|
if line.strip().startswith('--formats=')]
|
|
|
|
self.assertEquals(len(output), num_formats)
|
|
|
|
|
2009-05-14 12:21:26 -03:00
|
|
|
def test_finalize_options(self):
|
|
|
|
|
|
|
|
dist, cmd = self.get_cmd()
|
|
|
|
cmd.finalize_options()
|
|
|
|
|
|
|
|
# default options set by finalize
|
|
|
|
self.assertEquals(cmd.manifest, 'MANIFEST')
|
|
|
|
self.assertEquals(cmd.template, 'MANIFEST.in')
|
|
|
|
self.assertEquals(cmd.dist_dir, 'dist')
|
|
|
|
|
|
|
|
# formats has to be a string splitable on (' ', ',') or
|
|
|
|
# a stringlist
|
|
|
|
cmd.formats = 1
|
|
|
|
self.assertRaises(DistutilsOptionError, cmd.finalize_options)
|
|
|
|
cmd.formats = ['zip']
|
|
|
|
cmd.finalize_options()
|
|
|
|
|
|
|
|
# formats has to be known
|
|
|
|
cmd.formats = 'supazipa'
|
|
|
|
self.assertRaises(DistutilsOptionError, cmd.finalize_options)
|
|
|
|
|
|
|
|
|
2010-05-17 07:48:29 -03:00
|
|
|
def test_get_file_list(self):
|
|
|
|
# make sure MANIFEST is recalculated
|
|
|
|
dist, cmd = self.get_cmd()
|
|
|
|
|
|
|
|
# filling data_files by pointing files in package_data
|
|
|
|
dist.package_data = {'somecode': ['*.txt']}
|
|
|
|
self.write_file((self.tmp_dir, 'somecode', 'doc.txt'), '#')
|
|
|
|
cmd.ensure_finalized()
|
|
|
|
cmd.run()
|
|
|
|
|
|
|
|
f = open(cmd.manifest)
|
|
|
|
try:
|
|
|
|
manifest = [line.strip() for line in f.read().split('\n')
|
|
|
|
if line.strip() != '']
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
self.assertEquals(len(manifest), 4)
|
|
|
|
|
|
|
|
# adding a file
|
|
|
|
self.write_file((self.tmp_dir, 'somecode', 'doc2.txt'), '#')
|
|
|
|
|
|
|
|
# make sure build_py is reinitinialized, like a fresh run
|
|
|
|
build_py = dist.get_command_obj('build_py')
|
|
|
|
build_py.finalized = False
|
|
|
|
build_py.ensure_finalized()
|
|
|
|
|
|
|
|
cmd.run()
|
|
|
|
|
|
|
|
f = open(cmd.manifest)
|
|
|
|
try:
|
|
|
|
manifest2 = [line.strip() for line in f.read().split('\n')
|
|
|
|
if line.strip() != '']
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# do we have the new file in MANIFEST ?
|
|
|
|
self.assertEquals(len(manifest2), 5)
|
|
|
|
self.assertIn('doc2.txt', manifest2[-1])
|
|
|
|
|
|
|
|
|
2009-01-09 00:11:44 -04:00
|
|
|
def test_suite():
|
2009-05-16 13:52:13 -03:00
|
|
|
return unittest.makeSuite(SDistTestCase)
|
2009-01-09 00:11:44 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main(defaultTest="test_suite")
|