def test_build_ext(self): support.copy_xxmodule_c(self.tmp_dir) xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') xx_ext = Extension('xx', [xx_c]) dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) dist.package_dir = self.tmp_dir cmd = build_ext(dist) support.fixup_build_ext(cmd) cmd.build_lib = self.tmp_dir cmd.build_temp = self.tmp_dir cmd.ensure_finalized() cmd.run() code = textwrap.dedent("""\ import sys sys.path.insert(0, %r) import xx for attr in ('error', 'foo', 'new', 'roj'): assert hasattr(xx, attr) assert xx.foo(2, 5) == 7 assert xx.foo(13, 15) == 28 assert xx.new().demo() is None doc = 'This is a template module just for instruction.' assert xx.__doc__ == doc assert isinstance(xx.Null(), xx.Null) assert isinstance(xx.Str(), xx.Str) """) code = code % self.tmp_dir assert_python_ok('-c', code)
def test_cfg_to_args(self): opts = {'description-file': 'README', 'extra-files': '', 'setup-hooks': 'distutils2.tests.test_config.version_hook'} self.write_file('setup.cfg', SETUP_CFG % opts, encoding='utf-8') self.write_file('README', 'loooong description') args = cfg_to_args() # use Distribution to get the contents of the setup.cfg file dist = Distribution() dist.parse_config_files() metadata = dist.metadata self.assertEqual(args['name'], metadata['Name']) # + .dev1 because the test SETUP_CFG also tests a hook function in # test_config.py for appending to the version string self.assertEqual(args['version'] + '.dev1', metadata['Version']) self.assertEqual(args['author'], metadata['Author']) self.assertEqual(args['author_email'], metadata['Author-Email']) self.assertEqual(args['maintainer'], metadata['Maintainer']) self.assertEqual(args['maintainer_email'], metadata['Maintainer-Email']) self.assertEqual(args['description'], metadata['Summary']) self.assertEqual(args['long_description'], metadata['Description']) self.assertEqual(args['classifiers'], metadata['Classifier']) self.assertEqual(args['requires'], metadata['Requires-Dist']) self.assertEqual(args['provides'], metadata['Provides-Dist']) self.assertEqual(args['package_dir'].get(''), dist.package_dir) self.assertEqual(args['packages'], dist.packages) self.assertEqual(args['scripts'], dist.scripts) self.assertEqual(args['py_modules'], dist.py_modules)
def test_installation(self): source = self.mkdtemp() expected = [] def write_script(name, text): expected.append(name) with open(os.path.join(source, name), "w") as f: f.write(text) write_script("script1.py", ("#! /usr/bin/env python2.3\n" "# bogus script w/ Python sh-bang\n" "pass\n")) write_script("script2.py", ("#!/usr/bin/python\n" "# bogus script w/ Python sh-bang\n" "pass\n")) write_script("shell.sh", ("#!/bin/sh\n" "# bogus shell script w/ sh-bang\n" "exit 0\n")) target = self.mkdtemp() dist = Distribution() dist.command_obj["build"] = support.DummyCommand(build_scripts=source) dist.command_obj["install_dist"] = support.DummyCommand( install_scripts=target, force=True, skip_build=True, ) cmd = install_scripts(dist) cmd.finalize_options() cmd.run() installed = os.listdir(target) for name in expected: self.assertIn(name, installed)
def _metadata(dispatcher, args, **kw): opts = _parse_args(args[1:], 'f:', []) if opts['args']: name = opts['args'][0] dist = get_distribution(name, use_egg_info=True) if dist is None: logger.warning('%r not installed', name) return 1 elif os.path.isfile('setup.cfg'): logger.info('searching local dir for metadata') dist = Distribution() # XXX use config module dist.parse_config_files() else: logger.warning('no argument given and no local setup.cfg found') return 1 metadata = dist.metadata if 'f' in opts: keys = (k for k in opts['f'] if k in metadata) else: keys = metadata.keys() for key in keys: if key in metadata: print metadata._convert_name(key) + ':' value = metadata[key] if isinstance(value, list): for v in value: print ' ', v else: print ' ', value.replace('\n', '\n ')
def _metadata(dispatcher, args, **kw): opts = _parse_args(args[1:], 'f:', []) if opts['args']: name = opts['args'][0] dist = get_distribution(name, use_egg_info=True) if dist is None: logger.warning('%r not installed', name) return 1 elif os.path.isfile('setup.cfg'): logger.info('searching local dir for metadata') dist = Distribution() # XXX use config module dist.parse_config_files() else: logger.warning('no argument given and no local setup.cfg found') return 1 metadata = dist.metadata if 'f' in opts: keys = (k for k in opts['f'] if k in metadata) else: keys = metadata.keys() for key in keys: if key in metadata: print(metadata._convert_name(key) + ':') value = metadata[key] if isinstance(value, list): for v in value: print(' ', v) else: print(' ', value.replace('\n', '\n '))
def _run(dispatcher, args, **kw): parser = dispatcher.parser args = args[1:] # Find and parse the config file(s): they will override options from # the setup script, but be overridden by the command line. # XXX call the functions from config and kill the Distribution class # (merging it into Dispatcher) dist = Distribution() dist.parse_config_files() commands = STANDARD_COMMANDS # FIXME display extra commands if args == ['--list-commands']: print 'List of available commands:' for cmd in commands: cls = dispatcher.cmdclass.get(cmd) or get_command_class(cmd) desc = getattr(cls, 'description', '(no description available)') print ' %s: %s' % (cmd, desc) return while args: args = dispatcher._parse_command_opts(parser, args) if args is None: return for cmd in dispatcher.commands: # FIXME need to catch MetadataMissingError here (from the check command # e.g.)--or catch any exception, print an error message and exit with 1 dist.run_command(cmd, dispatcher.command_options[cmd]) return 0
def test_show_help(self): # smoke test, just makes sure some help is displayed dist = Distribution() sys.argv = [] dist.help = 1 dist.script_name = "setup.py" __, stdout = captured_stdout(dist.parse_command_line) output = [line for line in stdout.split("\n") if line.strip() != ""] self.assertTrue(len(output) > 0)
def test_get_command_packages(self): dist = Distribution() self.assertEquals(dist.command_packages, None) cmds = dist.get_command_packages() self.assertEquals(cmds, ["distutils2.command"]) self.assertEquals(dist.command_packages, ["distutils2.command"]) dist.command_packages = "one,two" cmds = dist.get_command_packages() self.assertEquals(cmds, ["distutils2.command", "one", "two"])
def _run_packaging_install(path, dest): # XXX check for a valid setup.cfg? dist = Distribution() dist.parse_config_files() try: dist.run_command('install_dist', {'prefix': (None, dest)}) name = dist.metadata['Name'] return database.get_distribution(name) is not None except (IOError, os.error, PackagingError, CCompilerError), msg: raise ValueError("Failed to install, " + str(msg))
def test_package_data(self): sources = self.mkdtemp() pkg_dir = os.path.join(sources, 'pkg') os.mkdir(pkg_dir) f = open(os.path.join(pkg_dir, "__init__.py"), "w") try: f.write("# Pretend this is a package.") finally: f.close() # let's have two files to make sure globbing works f = open(os.path.join(pkg_dir, "README.txt"), "w") try: f.write("Info about this package") finally: f.close() f = open(os.path.join(pkg_dir, "HACKING.txt"), "w") try: f.write("How to contribute") finally: f.close() destination = self.mkdtemp() dist = Distribution({"packages": ["pkg"], "package_dir": sources}) dist.command_obj["build"] = support.DummyCommand( force=False, build_lib=destination, use_2to3_fixers=None, convert_2to3_doctests=None, use_2to3=False) dist.packages = ["pkg"] dist.package_data = {"pkg": ["*.txt"]} dist.package_dir = sources cmd = build_py(dist) cmd.compile = True cmd.ensure_finalized() self.assertEqual(cmd.package_data, dist.package_data) cmd.run() # This makes sure the list of outputs includes byte-compiled # files for Python modules but not for package data files # (there shouldn't *be* byte-code files for those!). # FIXME the test below is not doing what the comment above says, and # if it did it would show a code bug: if we add a demo.py file to # package_data, it gets byte-compiled! outputs = cmd.get_outputs() self.assertEqual(len(outputs), 4, outputs) pkgdest = os.path.join(destination, "pkg") files = os.listdir(pkgdest) self.assertEqual(sorted(files), ["HACKING.txt", "README.txt", "__init__.py", "__init__.pyc"])
def get_cmd(self, metadata=None): """Returns a cmd""" if metadata is None: metadata = {'name': 'fake', 'version': '1.0', 'home_page': 'xxx', 'author': 'xxx', 'author_email': 'xxx'} dist = Distribution(metadata) dist.packages = ['somecode'] cmd = sdist(dist) cmd.dist_dir = 'dist' return dist, cmd
def test_get_command_packages(self): dist = Distribution() self.assertEqual(dist.command_packages, None) cmds = dist.get_command_packages() self.assertEqual(cmds, ['distutils2.command']) self.assertEqual(dist.command_packages, ['distutils2.command']) dist.command_packages = 'one,two' cmds = dist.get_command_packages() self.assertEqual(cmds, ['distutils2.command', 'one', 'two'])
def test_builds_before_running_tests(self): self.addCleanup(set_command, 'distutils2.command.build.build') set_command('distutils2.tests.test_command_test.MockBuildCmd') dist = Distribution() dist.get_command_obj('build')._record = record = [] cmd = test(dist) cmd.runner = self.prepare_named_function(lambda: None) cmd.ensure_finalized() cmd.run() self.assertEqual(['build has run'], record)
def get_build_scripts_cmd(self, target, scripts, executable=sys.executable): dist = Distribution() dist.scripts = scripts dist.command_obj["build"] = support.DummyCommand( build_scripts=target, force=False, executable=executable, use_2to3=False, use_2to3_fixers=None, convert_2to3_doctests=None, ) return build_scripts(dist)
def _try_compile_deployment_target(self, operator, target): orig_environ = os.environ os.environ = orig_environ.copy() self.addCleanup(setattr, os, 'environ', orig_environ) if target is None: if os.environ.get('MACOSX_DEPLOYMENT_TARGET'): del os.environ['MACOSX_DEPLOYMENT_TARGET'] else: os.environ['MACOSX_DEPLOYMENT_TARGET'] = target deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c') fp = open(deptarget_c, 'w') try: fp.write(textwrap.dedent('''\ #include <AvailabilityMacros.h> int dummy; #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED #else #error "Unexpected target" #endif ''' % operator)) finally: fp.close() # get the deployment target that the interpreter was built with target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') target = tuple(map(int, target.split('.'))) target = '%02d%01d0' % target deptarget_ext = Extension( 'deptarget', [deptarget_c], extra_compile_args=['-DTARGET=%s' % (target,)], ) dist = Distribution({ 'name': 'deptarget', 'ext_modules': [deptarget_ext], }) dist.package_dir = self.tmp_dir cmd = build_ext(dist) cmd.build_lib = self.tmp_dir cmd.build_temp = self.tmp_dir try: cmd.ensure_finalized() cmd.run() except CompileError: self.fail("Wrong deployment target during compilation")
def get_build_scripts_cmd(self, target, scripts, executable=sys.executable): dist = Distribution() dist.scripts = scripts dist.command_obj["build"] = support.DummyCommand( build_scripts=target, force=False, executable=executable, use_2to3=False, use_2to3_fixers=None, convert_2to3_doctests=None) return build_scripts(dist)
def _try_compile_deployment_target(self, operator, target): orig_environ = os.environ os.environ = orig_environ.copy() self.addCleanup(setattr, os, 'environ', orig_environ) if target is None: if os.environ.get('MACOSX_DEPLOYMENT_TARGET'): del os.environ['MACOSX_DEPLOYMENT_TARGET'] else: os.environ['MACOSX_DEPLOYMENT_TARGET'] = target deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c') with open(deptarget_c, 'w') as fp: fp.write(textwrap.dedent('''\ #include <AvailabilityMacros.h> int dummy; #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED #else #error "Unexpected target" #endif ''' % operator)) # get the deployment target that the interpreter was built with target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') target = tuple(map(int, target.split('.'))) target = '%02d%01d0' % target deptarget_ext = Extension( 'deptarget', [deptarget_c], extra_compile_args=['-DTARGET=%s' % (target,)], ) dist = Distribution({ 'name': 'deptarget', 'ext_modules': [deptarget_ext], }) dist.package_dir = self.tmp_dir cmd = build_ext(dist) cmd.build_lib = self.tmp_dir cmd.build_temp = self.tmp_dir try: cmd.ensure_finalized() cmd.run() except CompileError: self.fail("Wrong deployment target during compilation")
def test_builds_before_running_tests(self): dist = Distribution() cmd = test(dist) cmd.runner = self.prepare_named_function(lambda: None) record = [] class MockBuildCmd(Command): build_lib = "mock build lib" def initialize_options(self): pass def finalize_options(self): pass def run(self): record.append("build run") dist.cmdclass['build'] = MockBuildCmd cmd.ensure_finalized() cmd.run() self.assertEqual(record, ['build run'])
def test_optional_extension(self): # this extension will fail, but let's ignore this failure # with the optional argument. modules = [Extension('foo', ['xxx'], optional=False)] dist = Distribution({'name': 'xx', 'ext_modules': modules}) cmd = build_ext(dist) cmd.ensure_finalized() self.assertRaises((UnknownFileError, CompileError), cmd.run) # should raise an error modules = [Extension('foo', ['xxx'], optional=True)] dist = Distribution({'name': 'xx', 'ext_modules': modules}) cmd = build_ext(dist) cmd.ensure_finalized() cmd.run() # should pass
def test_handle_extra_path(self): dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'}) cmd = install_dist(dist) # two elements cmd.handle_extra_path() self.assertEqual(cmd.extra_path, ['path', 'dirs']) self.assertEqual(cmd.extra_dirs, 'dirs') self.assertEqual(cmd.path_file, 'path') # one element cmd.extra_path = ['path'] cmd.handle_extra_path() self.assertEqual(cmd.extra_path, ['path']) self.assertEqual(cmd.extra_dirs, 'path') self.assertEqual(cmd.path_file, 'path') # none dist.extra_path = cmd.extra_path = None cmd.handle_extra_path() self.assertEqual(cmd.extra_path, None) self.assertEqual(cmd.extra_dirs, '') self.assertEqual(cmd.path_file, None) # three elements (no way !) cmd.extra_path = 'path,dirs,again' self.assertRaises(PackagingOptionError, cmd.handle_extra_path)
def test_user_site(self): dist = Distribution({'name': 'xx'}) cmd = build_ext(dist) # making sure the user option is there options = [name for name, short, label in cmd.user_options] self.assertIn('user', options) # setting a value cmd.user = True # setting user based lib and include lib = os.path.join(site.USER_BASE, 'lib') incl = os.path.join(site.USER_BASE, 'include') os.mkdir(lib) os.mkdir(incl) # let's run finalize cmd.ensure_finalized() # see if include_dirs and library_dirs # were set self.assertIn(lib, cmd.library_dirs) self.assertIn(lib, cmd.rpath) self.assertIn(incl, cmd.include_dirs)
def test_saved_password(self): # file with no password self.write_file(self.rc, PYPIRC_NOPASSWORD) # make sure it passes dist = Distribution() cmd = upload(dist) cmd.ensure_finalized() self.assertEqual(cmd.password, None) # make sure we get it as well, if another command # initialized it at the dist level dist.password = '******' cmd = upload(dist) cmd.finalize_options() self.assertEqual(cmd.password, 'xxx')
def test_finalize_options_unsigned_identity_raises_exception(self): self.write_file(self.rc, PYPIRC) dist = Distribution() cmd = upload(dist) cmd.identity = True cmd.sign = False self.assertRaises(PackagingOptionError, cmd.finalize_options)
def test_simple_built(self): # let's create a simple package tmp_dir = self.mkdtemp() pkg_dir = os.path.join(tmp_dir, 'foo') os.mkdir(pkg_dir) self.write_file((pkg_dir, 'foo.py'), '#') self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py') self.write_file((pkg_dir, 'README'), '') dist = Distribution({'name': 'foo', 'version': '0.1', 'py_modules': ['foo'], 'home_page': 'xxx', 'author': 'xxx', 'author_email': 'xxx'}) os.chdir(pkg_dir) cmd = bdist_dumb(dist) # so the output is the same no matter # what is the platform cmd.format = 'zip' cmd.ensure_finalized() cmd.run() # see what we have dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name) if os.name == 'os2': base = base.replace(':', '-') self.assertEqual(dist_created, [base]) # now let's check what we have in the zip file fp = zipfile.ZipFile(os.path.join('dist', base)) try: contents = fp.namelist() finally: fp.close if sys.version_info[:2] == (3, 1): pyc = 'foo.pyc' else: pyc = 'foo.%s.pyc' % imp.get_tag() contents = sorted(os.path.basename(fn) for fn in contents) wanted = ['foo.py', pyc, 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] self.assertEqual(contents, sorted(wanted))
def setUp(self): super(UploadDocsTestCase, self).setUp() self.tmp_dir = self.mkdtemp() self.rc = os.path.join(self.tmp_dir, '.pypirc') os.environ['HOME'] = self.tmp_dir self.dist = Distribution() self.dist.metadata['Name'] = "distr-name" self.cmd = upload_docs(self.dist)
def test_checks_requires(self): dist = Distribution() cmd = test(dist) phony_project = 'ohno_ohno-impossible_1234-name_stop-that!' cmd.tests_require = [phony_project] cmd.ensure_finalized() logs = self.get_logs() self.assertIn(phony_project, logs[-1])
def test_empty_package_dir(self): # See SF 1668596/1720897. # create the distribution files. sources = self.mkdtemp() pkg = os.path.join(sources, 'pkg') os.mkdir(pkg) open(os.path.join(pkg, "__init__.py"), "wb").close() testdir = os.path.join(pkg, "doc") os.mkdir(testdir) open(os.path.join(testdir, "testfile"), "wb").close() os.chdir(sources) dist = Distribution({ "packages": ["pkg"], "package_dir": sources, "package_data": { "pkg": ["doc/*"] } }) dist.script_args = ["build"] dist.parse_command_line() try: dist.run_commands() except PackagingFileError: self.fail("failed package_data test when package_dir is ''")
def test_calls_discover(self): self.safely_replace(ut1.TestLoader, "discover", delete=True) mock_ut2 = self.prepare_mock_ut2() record = [] mock_ut2.TestLoader.discover = lambda self, path: record.append(path) dist = Distribution() cmd = test(dist) cmd.run() self.assertEqual([os.curdir], record)
def test_finalize_options(self): attrs = {'keywords': 'one,two', 'platform': 'one,two'} dist = Distribution(attrs=attrs) dist.finalize_options() # finalize_option splits platforms and keywords self.assertEqual(dist.metadata['platform'], ['one', 'two']) self.assertEqual(dist.metadata['keywords'], ['one', 'two'])
def test_gets_unittest_discovery(self): mock_ut2 = self.prepare_mock_ut2() dist = Distribution() cmd = test(dist) self.safely_replace(ut1.TestLoader, "discover", lambda: None) self.assertEqual(cmd.get_ut_with_discovery(), ut1) del ut1.TestLoader.discover self.assertEqual(cmd.get_ut_with_discovery(), mock_ut2)
def test_custom_runner(self): dist = Distribution() cmd = test(dist) record = [] cmd.runner = self.prepare_named_function( lambda: record.append("runner called")) cmd.ensure_finalized() cmd.run() self.assertEqual(["runner called"], record)
def test_home_installation_scheme(self): # This ensure two things: # - that --home generates the desired set of directory names # - test --home is supported on all platforms builddir = self.mkdtemp() destination = os.path.join(builddir, "installation") dist = Distribution({"name": "foopkg"}) dist.command_obj["build"] = support.DummyCommand( build_base=builddir, build_lib=os.path.join(builddir, "lib"), ) old_posix_prefix = _SCHEMES.get('posix_prefix', 'platinclude') old_posix_home = _SCHEMES.get('posix_home', 'platinclude') new_path = '{platbase}/include/python{py_version_short}' _SCHEMES.set('posix_prefix', 'platinclude', new_path) _SCHEMES.set('posix_home', 'platinclude', '{platbase}/include/python') try: cmd = install_dist(dist) cmd.home = destination cmd.ensure_finalized() finally: _SCHEMES.set('posix_prefix', 'platinclude', old_posix_prefix) _SCHEMES.set('posix_home', 'platinclude', old_posix_home) self.assertEqual(cmd.install_base, destination) self.assertEqual(cmd.install_platbase, destination) def check_path(got, expected): got = os.path.normpath(got) expected = os.path.normpath(expected) self.assertEqual(got, expected) libdir = os.path.join(destination, "lib", "python") check_path(cmd.install_lib, libdir) check_path(cmd.install_platlib, libdir) check_path(cmd.install_purelib, libdir) check_path(cmd.install_headers, os.path.join(destination, "include", "python", "foopkg")) check_path(cmd.install_scripts, os.path.join(destination, "bin")) check_path(cmd.install_data, destination)
def test_simple_built(self): # let's create a simple package tmp_dir = self.mkdtemp() pkg_dir = os.path.join(tmp_dir, 'foo') os.mkdir(pkg_dir) self.write_file((pkg_dir, 'foo.py'), '#') self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py') self.write_file((pkg_dir, 'README'), '') dist = Distribution({'name': 'foo', 'version': '0.1', 'py_modules': ['foo'], 'home_page': 'xxx', 'author': 'xxx', 'author_email': 'xxx'}) os.chdir(pkg_dir) cmd = bdist_dumb(dist) # so the output is the same no matter # what is the platform cmd.format = 'zip' cmd.ensure_finalized() cmd.run() # see what we have dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name) if os.name == 'os2': base = base.replace(':', '-') self.assertEqual(dist_created, [base]) # now let's check what we have in the zip file fp = zipfile.ZipFile(os.path.join('dist', base)) try: contents = fp.namelist() finally: fp.close() contents = sorted(os.path.basename(fn) for fn in contents) wanted = ['foo.py', 'foo.pyc', 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] self.assertEqual(contents, sorted(wanted))
def test_finalize_options(self): # new format self.write_file(self.rc, PYPIRC) dist = Distribution() cmd = upload(dist) cmd.finalize_options() for attr, expected in (('username', 'me'), ('password', 'secret'), ('realm', 'pypi'), ('repository', 'http://pypi.python.org/pypi')): self.assertEqual(getattr(cmd, attr), expected)
def test_compiler_option(self): # cmd.compiler is an option and # should not be overriden by a compiler instance # when the command is run dist = Distribution() cmd = build_ext(dist) cmd.compiler = 'unix' cmd.ensure_finalized() cmd.run() self.assertEqual(cmd.compiler, 'unix')
def test_user_site(self): # test install with --user # preparing the environment for the test self.old_user_base = get_config_var('userbase') self.old_user_site = get_path('purelib', '%s_user' % os.name) self.tmpdir = self.mkdtemp() self.user_base = os.path.join(self.tmpdir, 'B') self.user_site = os.path.join(self.tmpdir, 'S') _CONFIG_VARS['userbase'] = self.user_base scheme = '%s_user' % os.name _SCHEMES.set(scheme, 'purelib', self.user_site) def _expanduser(path): if path[0] == '~': path = os.path.normpath(self.tmpdir) + path[1:] return path self.old_expand = os.path.expanduser os.path.expanduser = _expanduser def cleanup(): _CONFIG_VARS['userbase'] = self.old_user_base _SCHEMES.set(scheme, 'purelib', self.old_user_site) os.path.expanduser = self.old_expand self.addCleanup(cleanup) schemes = get_scheme_names() for key in ('nt_user', 'posix_user', 'os2_home'): self.assertIn(key, schemes) dist = Distribution({'name': 'xx'}) cmd = install_dist(dist) # making sure the user option is there options = [name for name, short, lable in cmd.user_options] self.assertIn('user', options) # setting a value cmd.user = True # user base and site shouldn't be created yet self.assertFalse(os.path.exists(self.user_base)) self.assertFalse(os.path.exists(self.user_site)) # let's run finalize cmd.ensure_finalized() # now they should self.assertTrue(os.path.exists(self.user_base)) self.assertTrue(os.path.exists(self.user_site)) self.assertIn('userbase', cmd.config_vars) self.assertIn('usersite', cmd.config_vars)
def test_default_settings(self): dist = Distribution() dist.command_obj["build"] = support.DummyCommand( build_scripts="/foo/bar") dist.command_obj["install_dist"] = support.DummyCommand( install_scripts="/splat/funk", force=True, skip_build=True, ) cmd = install_scripts(dist) self.assertFalse(cmd.force) self.assertFalse(cmd.skip_build) self.assertIs(cmd.build_dir, None) self.assertIs(cmd.install_dir, None) cmd.finalize_options() self.assertTrue(cmd.force) self.assertTrue(cmd.skip_build) self.assertEqual(cmd.build_dir, "/foo/bar") self.assertEqual(cmd.install_dir, "/splat/funk")
def test_bad_attr(self): Distribution( attrs={ 'author': 'xxx', 'name': 'xxx', 'version': '1.2', 'home_page': 'xxxx', 'badoptname': 'xxx' }) logs = self.get_logs() self.assertEqual(len(logs), 1) self.assertIn('unknown argument', logs[0])
def _run(dispatcher, args, **kw): parser = dispatcher.parser args = args[1:] # Find and parse the config file(s): they will override options from # the setup script, but be overridden by the command line. # XXX call the functions from config and kill the Distribution class # (merging it into Dispatcher) dist = Distribution() dist.parse_config_files() commands = STANDARD_COMMANDS # FIXME display extra commands if args == ['--list-commands']: print('List of available commands:') for cmd in commands: cls = dispatcher.cmdclass.get(cmd) or get_command_class(cmd) desc = getattr(cls, 'description', '(no description available)') print(' %s: %s' % (cmd, desc)) return while args: args = dispatcher._parse_command_opts(parser, args) if args is None: return for cmd in dispatcher.commands: # FIXME need to catch MetadataMissingError here (from the check command # e.g.)--or catch any exception, print an error message and exit with 1 dist.run_command(cmd, dispatcher.command_options[cmd]) return 0
def test_empty_options(self): # an empty options dictionary should not stay in the # list of attributes dist = Distribution( attrs={ 'author': 'xxx', 'name': 'xxx', 'version': '1.2', 'home_page': 'xxxx', 'options': {} }) self.assertEqual(self.get_logs(), []) self.assertNotIn('options', dir(dist))
def test_installation(self): source = self.mkdtemp() expected = [] def write_script(name, text): expected.append(name) f = open(os.path.join(source, name), "w") try: f.write(text) finally: f.close() write_script("script1.py", ("#! /usr/bin/env python2.3\n" "# bogus script w/ Python sh-bang\n" "pass\n")) write_script("script2.py", ("#!/usr/bin/python\n" "# bogus script w/ Python sh-bang\n" "pass\n")) write_script("shell.sh", ("#!/bin/sh\n" "# bogus shell script w/ sh-bang\n" "exit 0\n")) target = self.mkdtemp() dist = Distribution() dist.command_obj["build"] = support.DummyCommand(build_scripts=source) dist.command_obj["install_dist"] = support.DummyCommand( install_scripts=target, force=True, skip_build=True, ) cmd = install_scripts(dist) cmd.finalize_options() cmd.run() installed = os.listdir(target) for name in expected: self.assertIn(name, installed)
def _run_packaging_install(path, dest): # XXX check for a valid setup.cfg? dist = Distribution() dist.parse_config_files() try: dist.run_command('install_dist', {'prefix': (None, dest)}) name = dist.metadata['Name'] return database.get_distribution(name) is not None except (IOError, os.error, PackagingError, CCompilerError) as msg: raise ValueError("Failed to install, " + str(msg))
def test_custom_pydistutils(self): # Bug #2166: make sure pydistutils.cfg is found if os.name == 'posix': user_filename = ".pydistutils.cfg" else: user_filename = "pydistutils.cfg" temp_dir = self.mkdtemp() user_filename = os.path.join(temp_dir, user_filename) with open(user_filename, 'w') as f: f.write('.') dist = Distribution() os.environ['HOME'] = temp_dir files = dist.find_config_files() self.assertIn(user_filename, files)
def test_empty_package_dir(self): # See SF 1668596/1720897. # create the distribution files. sources = self.mkdtemp() pkg = os.path.join(sources, 'pkg') os.mkdir(pkg) open(os.path.join(pkg, "__init__.py"), "wb").close() testdir = os.path.join(pkg, "doc") os.mkdir(testdir) open(os.path.join(testdir, "testfile"), "wb").close() os.chdir(sources) dist = Distribution({"packages": ["pkg"], "package_dir": sources, "package_data": {"pkg": ["doc/*"]}}) dist.script_args = ["build"] dist.parse_command_line() try: dist.run_commands() except PackagingFileError: self.fail("failed package_data test when package_dir is ''")
def get_dist(self): dist = Distribution() dist.parse_config_files() return dist