我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用setuptools.dist.Distribution()。
def parse_configuration( distribution, command_options, ignore_option_errors=False): """Performs additional parsing of configuration options for a distribution. Returns a list of used option handlers. :param Distribution distribution: :param dict command_options: :param bool ignore_option_errors: Whether to silently ignore options, values of which could not be resolved (e.g. due to exceptions in directives such as file:, attr:, etc.). If False exceptions are propagated as expected. :rtype: list """ meta = ConfigMetadataHandler( distribution.metadata, command_options, ignore_option_errors) meta.parse() options = ConfigOptionsHandler( distribution, command_options, ignore_option_errors) options.parse() return [meta, options]
def test_create_zipfile(self): # Test to make sure zipfile creation handles common cases. # This explicitly includes a folder containing an empty folder. dist = Distribution() cmd = upload_docs(dist) cmd.upload_dir = self.upload_dir cmd.target_dir = self.upload_dir tmp_dir = tempfile.mkdtemp() tmp_file = os.path.join(tmp_dir, 'foo.zip') try: zip_file = cmd.create_zipfile(tmp_file) assert zipfile.is_zipfile(tmp_file) zip_file = zipfile.ZipFile(tmp_file) # woh... assert zip_file.namelist() == ['index.html'] zip_file.close() finally: shutil.rmtree(tmp_dir)
def test_package_data_in_sdist(self): """Regression test for pull request #4: ensures that files listed in package_data are included in the manifest even if they're not added to version control. """ dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # squelch output quiet() try: cmd.run() finally: unquiet() manifest = cmd.filelist.files self.assertTrue(os.path.join('sdist_test', 'a.txt') in manifest) self.assertTrue(os.path.join('sdist_test', 'b.txt') in manifest) self.assertTrue(os.path.join('sdist_test', 'c.rst') not in manifest)
def notest_develop_with_setup_requires(self): wanted = ("Could not find suitable distribution for " "Requirement.parse('I-DONT-EXIST')") old_dir = os.getcwd() os.chdir(self.dir) try: try: dist = Distribution({'setup_requires': ['I_DONT_EXIST']}) except DistutilsError: e = sys.exc_info()[1] error = str(e) if error == wanted: pass finally: os.chdir(old_dir)
def test_no_find_links(self): # new option '--no-find-links', that blocks find-links added at # the project level dist = Distribution() cmd = easy_install(dist) cmd.check_pth_processing = lambda: True cmd.no_find_links = True cmd.find_links = ['link1', 'link2'] cmd.install_dir = os.path.join(tempfile.mkdtemp(), 'ok') cmd.args = ['ok'] cmd.ensure_finalized() self.assertEqual(cmd.package_index.scanned_urls, {}) # let's try without it (default behavior) cmd = easy_install(dist) cmd.check_pth_processing = lambda: True cmd.find_links = ['link1', 'link2'] cmd.install_dir = os.path.join(tempfile.mkdtemp(), 'ok') cmd.args = ['ok'] cmd.ensure_finalized() keys = sorted(cmd.package_index.scanned_urls.keys()) self.assertEqual(keys, ['link1', 'link2'])
def monkeypatch_method(cls): """A function decorator to monkey-patch a method of the same name on the given class. """ def wrapper(func): orig = getattr(cls, func.__name__, None) if orig and not hasattr(orig, '_orig'): # Already patched setattr(func, '_orig', orig) setattr(cls, func.__name__, func) return func return wrapper # The following classes are used to hack Distribution.command_options a bit
def test_view(self): stub_mod_call(self, cli) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['yarn', '--view'], name='foo', )) dist.parse_command_line() dist.run_commands() self.assertFalse(exists(join(tmpdir, 'package.json'))) # also log handlers removed. self.assertEqual(len(getLogger('calmjs.cli').handlers), 0) # written to stdout with the correct indentation level. self.assertIn('\n "jquery": "~1.11.0"', sys.stdout.getvalue())
def test_install_no_init_nodevnoprod(self): # install implies init stub_mod_call(self, cli) stub_base_which(self, which_yarn) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['yarn', '--install'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) # The cli will still automatically write to that, as install # implies init. self.assertEqual(result, { 'dependencies': {'jquery': '~1.11.0'}, 'devDependencies': {}, 'name': 'foo', }) self.assertEqual(self.call_args[0], ([which_yarn, 'install'],))
def test_install_init_install_production(self): stub_mod_call(self, cli) stub_base_which(self, which_yarn) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['yarn', '--init', '--install', '--production'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) self.assertEqual(result, { 'dependencies': {'jquery': '~1.11.0'}, 'devDependencies': {}, 'name': 'foo', }) # Should still invoke install self.assertEqual(self.call_args[0], ( [which_yarn, 'install', '--production=true'],))
def test_install_init_install_develop(self): stub_mod_call(self, cli) stub_base_which(self, which_yarn) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['yarn', '--init', '--install', '--development'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) self.assertEqual(result, { 'dependencies': {'jquery': '~1.11.0'}, 'devDependencies': {}, 'name': 'foo', }) # Should still invoke install self.assertEqual(self.call_args[0], ( [which_yarn, 'install', '--production=false'],))
def test_install_view(self): stub_mod_call(self, cli) stub_base_which(self, which_yarn) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['yarn', '--install', '--view'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) self.assertEqual(result, { 'dependencies': {'jquery': '~1.11.0'}, 'devDependencies': {}, 'name': 'foo', }) self.assertEqual(self.call_args[0], ([which_yarn, 'install'],))
def test_view(self): stub_mod_call(self, cli) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--view'], name='foo', )) dist.parse_command_line() dist.run_commands() self.assertFalse(exists(join(tmpdir, 'package.json'))) # also log handlers removed. self.assertEqual(len(getLogger('calmjs.cli').handlers), 0) # written to stdout with the correct indentation level. self.assertIn('\n "jquery": "~1.11.0"', sys.stdout.getvalue())
def test_install_no_init_nodevnoprod(self): # install implies init stub_mod_call(self, cli) stub_base_which(self, which_npm) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--install'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) # The cli will still automatically write to that, as install # implies init. self.assertEqual(result, { 'dependencies': {'jquery': '~1.11.0'}, 'devDependencies': {}, 'name': 'foo', }) self.assertEqual(self.call_args[0], ([which_npm, 'install'],))
def test_install_init_install_production(self): stub_mod_call(self, cli) stub_base_which(self, which_npm) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--init', '--install', '--production'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) self.assertEqual(result, { 'dependencies': {'jquery': '~1.11.0'}, 'devDependencies': {}, 'name': 'foo', }) # Should still invoke install self.assertEqual(self.call_args[0], ( [which_npm, 'install', '--production=true'],))
def test_install_init_install_develop(self): stub_mod_call(self, cli) stub_base_which(self, which_npm) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--init', '--install', '--development'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) self.assertEqual(result, { 'dependencies': {'jquery': '~1.11.0'}, 'devDependencies': {}, 'name': 'foo', }) # Should still invoke install self.assertEqual(self.call_args[0], ( [which_npm, 'install', '--production=false'],))
def test_install_dryrun(self): stub_mod_call(self, cli) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--install', '--dry-run'], name='foo', )) dist.parse_command_line() dist.run_commands() self.assertFalse(exists(join(tmpdir, 'package.json'))) # Ensure that install is NOT called. self.assertIsNone(self.call_args) # also log handlers removed. self.assertEqual(len(getLogger('calmjs.cli').handlers), 0) # However, default action is view, the package.json should be # written to stdout with the correct indentation level. self.assertIn('\n "jquery": "~1.11.0"', sys.stdout.getvalue())
def test_install_view(self): stub_mod_call(self, cli) stub_base_which(self, which_npm) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--install', '--view'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) self.assertEqual(result, { 'dependencies': {'jquery': '~1.11.0'}, 'devDependencies': {}, 'name': 'foo', }) self.assertEqual(self.call_args[0], ([which_npm, 'install'],))