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:] 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 # create the Distribution class # need to feed setup.cfg here ! dist = Distribution() # Find and parse the config file(s): they will override options from # the setup script, but be overridden by the command line. # XXX still need to be extracted from Distribution dist.parse_config_files() 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_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 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_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) pycache_dir = os.path.join(pkgdest, "__pycache__") self.assertIn("__init__.py", files) self.assertIn("README.txt", files) self.assertIn("HACKING.txt", files) pyc_files = os.listdir(pycache_dir) self.assertEqual(["__init__.%s.pyc" % imp.get_tag()], pyc_files)
def _run_packaging_install(path): # XXX check for a valid setup.cfg? dist = Distribution() dist.parse_config_files() try: dist.run_command('install_dist') 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 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_builds_before_running_tests(self): self.addCleanup(set_command, 'packaging.command.build.build') set_command('packaging.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): dist = Distribution() dist.scripts = scripts dist.command_obj["build"] = support.DummyCommand( build_scripts=target, force=True, executable=sys.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_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_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 with zipfile.ZipFile(os.path.join('dist', base)) as fp: contents = fp.namelist() contents = sorted(os.path.basename(fn) for fn in contents) wanted = ['foo.py', 'foo.%s.pyc' % imp.get_tag(), 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] self.assertEqual(contents, sorted(wanted))
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_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