def test_warnings(self): metadata = DistributionMetadata() # these should raise a warning values = (('Requires-Dist', 'Funky (Groovie)'), ('Requires-Python', '1-4')) from distutils2 import metadata as m old = m.warn m.warns = 0 def _warn(*args): m.warns += 1 m.warn = _warn try: for name, value in values: metadata.set(name, value) finally: m.warn = old res = m.warns del m.warns # we should have a certain amount of warnings num_wanted = len(values) self.assertEqual(num_wanted, res)
def test_check(self): metadata = DistributionMetadata() metadata['Version'] = 'rr' metadata['Requires-dist'] = ['Foo (a)'] if metadata.docutils_support: missing, warnings = metadata.check() self.assertEqual(len(warnings), 2) metadata.docutils_support = False missing, warnings = metadata.check() self.assertEqual(missing, ['Name', 'Home-page']) self.assertEqual(len(warnings), 2)
def test_metadata_read_write(self): PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') metadata = DistributionMetadata(PKG_INFO) res = StringIO() metadata.write_file(res) res.seek(0) res = res.read() f = open(PKG_INFO) wanted = f.read() self.assert_('Keywords: keyring,password,crypt' in res) f.close()
def test_instantiation(self): PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') fp = open(PKG_INFO) try: contents = fp.read() finally: fp.close() fp = StringIO(contents) m = DistributionMetadata() self.assertRaises(MetadataUnrecognizedVersionError, m.items) m = DistributionMetadata(PKG_INFO) self.assertEqual(len(m.items()), 22) m = DistributionMetadata(fileobj=fp) self.assertEqual(len(m.items()), 22) m = DistributionMetadata(mapping=dict(name='Test', version='1.0')) self.assertEqual(len(m.items()), 11) d = dict(m.items()) self.assertRaises(TypeError, DistributionMetadata, PKG_INFO, fileobj=fp) self.assertRaises(TypeError, DistributionMetadata, PKG_INFO, mapping=d) self.assertRaises(TypeError, DistributionMetadata, fileobj=fp, mapping=d) self.assertRaises(TypeError, DistributionMetadata, PKG_INFO, mapping=m, fileobj=fp)
def test_mapping_api(self): PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') content = open(PKG_INFO).read() content = content % sys.platform metadata = DistributionMetadata(fileobj=StringIO(content)) self.assertIn('Version', metadata.keys()) self.assertIn('0.5', metadata.values()) self.assertIn(('Version', '0.5'), metadata.items()) metadata.update({'version': '0.6'}) self.assertEqual(metadata['Version'], '0.6') metadata.update([('version', '0.7')]) self.assertEqual(metadata['Version'], '0.7')
def test_versions(self): metadata = DistributionMetadata() metadata['Obsoletes'] = 'ok' self.assertEqual(metadata['Metadata-Version'], '1.1') del metadata['Obsoletes'] metadata['Obsoletes-Dist'] = 'ok' self.assertEqual(metadata['Metadata-Version'], '1.2') self.assertRaises(MetadataConflictError, metadata.set, 'Obsoletes', 'ok') del metadata['Obsoletes'] del metadata['Obsoletes-Dist'] metadata['Version'] = '1' self.assertEqual(metadata['Metadata-Version'], '1.0') PKG_INFO = os.path.join(os.path.dirname(__file__), 'SETUPTOOLS-PKG-INFO') metadata.read_file(StringIO(open(PKG_INFO).read())) self.assertEqual(metadata['Metadata-Version'], '1.0') PKG_INFO = os.path.join(os.path.dirname(__file__), 'SETUPTOOLS-PKG-INFO2') metadata.read_file(StringIO(open(PKG_INFO).read())) self.assertEqual(metadata['Metadata-Version'], '1.1') metadata.version = '1.618' self.assertRaises(MetadataUnrecognizedVersionError, metadata.keys)
def test_metadata_read_write(self): PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') metadata = DistributionMetadata(PKG_INFO) out = StringIO() metadata.write_file(out) out.seek(0) res = DistributionMetadata() res.read_file(out) for k in metadata.keys(): self.assertTrue(metadata[k] == res[k])
def test_versions(self): metadata = DistributionMetadata() metadata['Obsoletes'] = 'ok' self.assertEquals(metadata['Metadata-Version'], '1.1') del metadata['Obsoletes'] metadata['Obsoletes-Dist'] = 'ok' self.assertEquals(metadata['Metadata-Version'], '1.2') del metadata['Obsoletes-Dist'] metadata['Version'] = '1' self.assertEquals(metadata['Metadata-Version'], '1.0') PKG_INFO = os.path.join(os.path.dirname(__file__), 'SETUPTOOLS-PKG-INFO') metadata.read_file(StringIO(open(PKG_INFO).read())) self.assertEquals(metadata['Metadata-Version'], '1.0') PKG_INFO = os.path.join(os.path.dirname(__file__), 'SETUPTOOLS-PKG-INFO2') metadata.read_file(StringIO(open(PKG_INFO).read())) self.assertEquals(metadata['Metadata-Version'], '1.1')
def test_mapper_apis(self): PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') content = open(PKG_INFO).read() content = content % sys.platform metadata = DistributionMetadata() metadata.read_file(StringIO(content)) self.assertIn('Version', metadata.keys()) self.assertIn('0.5', metadata.values()) self.assertIn(('Version', '0.5'), metadata.items())
def test_description(self): PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') content = open(PKG_INFO).read() content = content % sys.platform metadata = DistributionMetadata() metadata.read_file(StringIO(content)) # see if we can read the description now DESC = os.path.join(os.path.dirname(__file__), 'LONG_DESC.txt') wanted = open(DESC).read() self.assertEqual(wanted, metadata['Description']) # save the file somewhere and make sure we can read it back out = StringIO() metadata.write_file(out) out.seek(0) metadata.read_file(out) self.assertEqual(wanted, metadata['Description'])
def test_metadata_markers(self): # see if we can be platform-aware PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') content = open(PKG_INFO).read() content = content % sys.platform metadata = DistributionMetadata(platform_dependant=True) metadata.read_file(StringIO(content)) self.assertEquals(metadata['Requires-Dist'], ['bar']) # test with context context = {'sys.platform': 'okook'} metadata = DistributionMetadata(platform_dependant=True, execution_context=context) metadata.read_file(StringIO(content)) self.assertEquals(metadata['Requires-Dist'], ['foo'])
def test_project_urls(self): # project-url is a bit specific, make sure we write it # properly in PKG-INFO metadata = DistributionMetadata() metadata['Version'] = '1.0' metadata['Project-Url'] = [('one', 'http://ok')] self.assertEqual(metadata['Project-Url'], [('one', 'http://ok')]) file_ = StringIO() metadata.write_file(file_) file_.seek(0) res = file_.read().split('\n') self.assertIn('Project-URL: one,http://ok', res) file_.seek(0) metadata = DistributionMetadata() metadata.read_file(file_) self.assertEqual(metadata['Project-Url'], [('one', 'http://ok')])
def __init__(self, name, version, metadata=None, hidden=False, index=None, **kwargs): """ :param name: the name of the distribution :param version: the version of the distribution :param metadata: the metadata fields of the release. :type metadata: dict :param kwargs: optional arguments for a new distribution. """ self.set_index(index) self.name = name self._version = None self.version = version if metadata: self.metadata = DistributionMetadata(mapping=metadata) else: self.metadata = None self.dists = {} self.hidden = hidden if 'dist_type' in kwargs: dist_type = kwargs.pop('dist_type') self.add_distribution(dist_type, **kwargs)
def test_metadata_markers(self): # see if we can be platform-aware PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') content = open(PKG_INFO).read() content = content % sys.platform metadata = DistributionMetadata(platform_dependent=True) metadata.read_file(StringIO(content)) self.assertEqual(metadata['Requires-Dist'], ['bar']) metadata['Name'] = "baz; sys.platform == 'blah'" # FIXME is None or 'UNKNOWN' correct here? # where is that documented? self.assertEquals(metadata['Name'], None) # test with context context = {'sys.platform': 'okook'} metadata = DistributionMetadata(platform_dependent=True, execution_context=context) metadata.read_file(StringIO(content)) self.assertEqual(metadata['Requires-Dist'], ['foo'])
def __init__(self, attrs=None): """Construct a new Distribution instance: initialize all the attributes of a Distribution, and then use 'attrs' (a dictionary mapping attribute names to values) to assign some of those attributes their "real" values. (Any attributes not mentioned in 'attrs' will be assigned to some null value: 0, None, an empty list or dictionary, etc.) Most importantly, initialize the 'command_obj' attribute to the empty dictionary; this will be filled in with real command objects by 'parse_command_line()'. """ # Default values for our command-line options self.verbose = 1 self.dry_run = 0 self.help = 0 for attr in self.display_option_names: setattr(self, attr, 0) # Store the configuration self.config = Config(self) # Store the distribution metadata (name, version, author, and so # forth) in a separate object -- we're getting to have enough # information here (and enough command-line options) that it's # worth it. self.metadata = DistributionMetadata() # 'cmdclass' maps command names to class objects, so we # can 1) quickly figure out which class to instantiate when # we need to create a new command object, and 2) have a way # for the setup script to override command classes self.cmdclass = {} # 'command_packages' is a list of packages in which commands # are searched for. The factory for command 'foo' is expected # to be named 'foo' in the module 'foo' in one of the packages # named here. This list is searched from the left; an error # is raised if no named package provides the command being # searched for. (Always access using get_command_packages().) self.command_packages = None # 'script_name' and 'script_args' are usually set to sys.argv[0] # and sys.argv[1:], but they can be overridden when the caller is # not necessarily a setup script run from the command line. self.script_name = None self.script_args = None # 'command_options' is where we store command options between # parsing them (from config files, the command line, etc.) and when # they are actually needed -- ie. when the command in question is # instantiated. It is a dictionary of dictionaries of 2-tuples: # command_options = { command_name : { option : (source, value) } } self.command_options = {} # 'dist_files' is the list of (command, pyversion, file) that # have been created by any dist commands run so far. This is # filled regardless of whether the run is dry or not. pyversion # gives sysconfig.get_python_version() if the dist file is # specific to a Python version, 'any' if it is good for all # Python versions on the target platform, and '' for a source # file. pyversion should not be used to specify minimum or # maximum required Python versions; use the metainfo for that # instead. self.dist_files = [] # These options are really the business of various commands, rather # than of the Distribution itself. We provide aliases for them in # Distribution as a convenience to the developer. self.packages = [] self.package_data = {} self.package_dir = None self.py_modules = [] self.libraries = [] self.headers = [] self.ext_modules = [] self.ext_package = None self.include_dirs = [] self.extra_path = None self.scripts = [] self.data_files = [] self.password = '' self.use_2to3 = False self.convert_2to3_doctests = [] # And now initialize bookkeeping stuff that can't be supplied by # the caller at all. 'command_obj' maps command names to # Command instances -- that's how we enforce that every command # class is a singleton. self.command_obj = {} # 'have_run' maps command names to boolean values; it keeps track # of whether we have actually run a particular command, to make it # cheap to "run" a command whenever we think we might need to -- if # it's already been done, no need for expensive filesystem # operations, we just check the 'have_run' dictionary and carry on. # It's only safe to query 'have_run' for a command class that has # been instantiated -- a false value will be inserted when the # command object is created, and replaced with a true value when # the command is successfully run. Thus it's probably best to use # '.get()' rather than a straight lookup. self.have_run = {} # Now we'll use the attrs dictionary (ultimately, keyword args from # the setup script) to possibly override any or all of these # distribution options. if attrs is not None: # Pull out the set of command options and work on them # specifically. Note that this order guarantees that aliased # command options will override any supplied redundantly # through the general options dictionary. options = attrs.get('options') if options is not None: del attrs['options'] for (command, cmd_options) in options.items(): opt_dict = self.get_option_dict(command) for (opt, val) in cmd_options.items(): opt_dict[opt] = ("setup script", val) # Now work on the rest of the attributes. Any attribute that's # not already defined is invalid! for key, val in attrs.items(): if self.metadata.is_metadata_field(key): self.metadata[key] = val elif hasattr(self, key): setattr(self, key, val) else: msg = "Unknown distribution option: %r" % key warnings.warn(msg) # no-user-cfg is handled before other command line args # because other args override the config files, and this # one is needed before we can load the config files. # If attrs['script_args'] wasn't passed, assume false. # # This also make sure we just look at the global options self.want_user_cfg = True if self.script_args is not None: for arg in self.script_args: if not arg.startswith('-'): break if arg == '--no-user-cfg': self.want_user_cfg = False break self.finalize_options()
class Distribution(object): """The core of the Distutils. Most of the work hiding behind 'setup' is really done within a Distribution instance, which farms the work out to the Distutils commands specified on the command line. Setup scripts will almost never instantiate Distribution directly, unless the 'setup()' function is totally inadequate to their needs. However, it is conceivable that a setup script might wish to subclass Distribution for some specialized purpose, and then pass the subclass to 'setup()' as the 'distclass' keyword argument. If so, it is necessary to respect the expectations that 'setup' has of Distribution. See the code for 'setup()', in core.py, for details. """ # 'global_options' describes the command-line options that may be # supplied to the setup script prior to any actual commands. # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of # these global options. This list should be kept to a bare minimum, # since every global option is also valid as a command option -- and we # don't want to pollute the commands with too many options that they # have minimal control over. # The fourth entry for verbose means that it can be repeated. global_options = [('verbose', 'v', "run verbosely (default)", 1), ('quiet', 'q', "run quietly (turns verbosity off)"), ('dry-run', 'n', "don't actually do anything"), ('help', 'h', "show detailed help message"), ('no-user-cfg', None, 'ignore pydistutils.cfg in your home directory'), ] # 'common_usage' is a short (2-3 line) string describing the common # usage of the setup script. common_usage = """\ Common commands: (see '--help-commands' for more) setup.py build will build the package underneath 'build/' setup.py install will install the package """ # options that are not propagated to the commands display_options = [ ('help-commands', None, "list all available commands"), ('name', None, "print package name"), ('version', 'V', "print package version"), ('fullname', None, "print <package name>-<version>"), ('author', None, "print the author's name"), ('author-email', None, "print the author's email address"), ('maintainer', None, "print the maintainer's name"), ('maintainer-email', None, "print the maintainer's email address"), ('contact', None, "print the maintainer's name if known, else the author's"), ('contact-email', None, "print the maintainer's email address if known, else the author's"), ('url', None, "print the URL for this package"), ('license', None, "print the license of the package"), ('licence', None, "alias for --license"), ('description', None, "print the package description"), ('long-description', None, "print the long package description"), ('platforms', None, "print the list of platforms"), ('classifier', None, "print the list of classifiers"), ('keywords', None, "print the list of keywords"), ('provides', None, "print the list of packages/modules provided"), ('requires', None, "print the list of packages/modules required"), ('obsoletes', None, "print the list of packages/modules made obsolete") ] display_option_names = map(lambda x: translate_longopt(x[0]), display_options) # negative options are options that exclude other options negative_opt = {'quiet': 'verbose'} # -- Creation/initialization methods ------------------------------- def __init__ (self, attrs=None): """Construct a new Distribution instance: initialize all the attributes of a Distribution, and then use 'attrs' (a dictionary mapping attribute names to values) to assign some of those attributes their "real" values. (Any attributes not mentioned in 'attrs' will be assigned to some null value: 0, None, an empty list or dictionary, etc.) Most importantly, initialize the 'command_obj' attribute to the empty dictionary; this will be filled in with real command objects by 'parse_command_line()'. """ # Default values for our command-line options self.verbose = 1 self.dry_run = 0 self.help = 0 for attr in self.display_option_names: setattr(self, attr, 0) # Store the distribution meta-data (name, version, author, and so # forth) in a separate object -- we're getting to have enough # information here (and enough command-line options) that it's # worth it. Also delegate 'get_XXX()' methods to the 'metadata' # object in a sneaky and underhanded (but efficient!) way. self.metadata = DistributionMetadata() #for basename in self.metadata._METHOD_BASENAMES: # method_name = "get_" + basename # setattr(self, method_name, getattr(self.metadata, method_name)) # 'cmdclass' maps command names to class objects, so we # can 1) quickly figure out which class to instantiate when # we need to create a new command object, and 2) have a way # for the setup script to override command classes self.cmdclass = {} # 'command_packages' is a list of packages in which commands # are searched for. The factory for command 'foo' is expected # to be named 'foo' in the module 'foo' in one of the packages # named here. This list is searched from the left; an error # is raised if no named package provides the command being # searched for. (Always access using get_command_packages().) self.command_packages = None # 'script_name' and 'script_args' are usually set to sys.argv[0] # and sys.argv[1:], but they can be overridden when the caller is # not necessarily a setup script run from the command-line. self.script_name = None self.script_args = None # 'command_options' is where we store command options between # parsing them (from config files, the command-line, etc.) and when # they are actually needed -- ie. when the command in question is # instantiated. It is a dictionary of dictionaries of 2-tuples: # command_options = { command_name : { option : (source, value) } } self.command_options = {} # 'dist_files' is the list of (command, pyversion, file) that # have been created by any dist commands run so far. This is # filled regardless of whether the run is dry or not. pyversion # gives sysconfig.get_python_version() if the dist file is # specific to a Python version, 'any' if it is good for all # Python versions on the target platform, and '' for a source # file. pyversion should not be used to specify minimum or # maximum required Python versions; use the metainfo for that # instead. self.dist_files = [] # These options are really the business of various commands, rather # than of the Distribution itself. We provide aliases for them in # Distribution as a convenience to the developer. self.packages = None self.package_data = {} self.package_dir = None self.py_modules = None self.libraries = None self.headers = None self.ext_modules = None self.ext_package = None self.include_dirs = None self.extra_path = None self.scripts = None self.data_files = None self.password = '' # And now initialize bookkeeping stuff that can't be supplied by # the caller at all. 'command_obj' maps command names to # Command instances -- that's how we enforce that every command # class is a singleton. self.command_obj = {} # 'have_run' maps command names to boolean values; it keeps track # of whether we have actually run a particular command, to make it # cheap to "run" a command whenever we think we might need to -- if # it's already been done, no need for expensive filesystem # operations, we just check the 'have_run' dictionary and carry on. # It's only safe to query 'have_run' for a command class that has # been instantiated -- a false value will be inserted when the # command object is created, and replaced with a true value when # the command is successfully run. Thus it's probably best to use # '.get()' rather than a straight lookup. self.have_run = {} # Now we'll use the attrs dictionary (ultimately, keyword args from # the setup script) to possibly override any or all of these # distribution options. if attrs is not None: # Pull out the set of command options and work on them # specifically. Note that this order guarantees that aliased # command options will override any supplied redundantly # through the general options dictionary. options = attrs.get('options') if options is not None: del attrs['options'] for (command, cmd_options) in options.items(): opt_dict = self.get_option_dict(command) for (opt, val) in cmd_options.items(): opt_dict[opt] = ("setup script", val) # Now work on the rest of the attributes. Any attribute that's # not already defined is invalid! for key, val in attrs.items(): if self.metadata.is_metadata_field(key): self.metadata[key] = val elif hasattr(self, key): setattr(self, key, val) else: msg = "Unknown distribution option: %s" % repr(key) if warnings is not None: warnings.warn(msg) else: sys.stderr.write(msg + "\n") # no-user-cfg is handled before other command line args # because other args override the config files, and this # one is needed before we can load the config files. # If attrs['script_args'] wasn't passed, assume false. # # This also make sure we just look at the global options self.want_user_cfg = True if self.script_args is not None: for arg in self.script_args: if not arg.startswith('-'): break if arg == '--no-user-cfg': self.want_user_cfg = False break self.finalize_options() def get_option_dict(self, command): """Get the option dictionary for a given command. If that command's option dictionary hasn't been created yet, then create it and return the new dictionary; otherwise, return the existing option dictionary. """ dict = self.command_options.get(command) if dict is None: dict = self.command_options[command] = {} return dict def get_fullname(self): return self.metadata.get_fullname() def dump_option_dicts(self, header=None, commands=None, indent=""): from pprint import pformat if commands is None: # dump all command option dicts commands = self.command_options.keys() commands.sort() if header is not None: self.announce(indent + header) indent = indent + " " if not commands: self.announce(indent + "no commands known yet") return for cmd_name in commands: opt_dict = self.command_options.get(cmd_name) if opt_dict is None: self.announce(indent + "no option dict for '%s' command" % cmd_name) else: self.announce(indent + "option dict for '%s' command:" % cmd_name) out = pformat(opt_dict) for line in out.split('\n'): self.announce(indent + " " + line) # -- Config file finding/parsing methods --------------------------- def find_config_files(self): """Find as many configuration files as should be processed for this platform, and return a list of filenames in the order in which they should be parsed. The filenames returned are guaranteed to exist (modulo nasty race conditions). There are three possible config files: distutils.cfg in the Distutils installation directory (ie. where the top-level Distutils __inst__.py file lives), a file in the user's home directory named .pydistutils.cfg on Unix and pydistutils.cfg on Windows/Mac; and setup.cfg in the current directory. The file in the user's home directory can be disabled with the --no-user-cfg option. """ files = [] check_environ() # Where to look for the system-wide Distutils config file sys_dir = os.path.dirname(sys.modules['distutils2'].__file__) # Look for the system config file sys_file = os.path.join(sys_dir, "distutils.cfg") if os.path.isfile(sys_file): files.append(sys_file) # What to call the per-user config file if os.name == 'posix': user_filename = ".pydistutils.cfg" else: user_filename = "pydistutils.cfg" # And look for the user config file if self.want_user_cfg: user_file = os.path.join(os.path.expanduser('~'), user_filename) if os.path.isfile(user_file): files.append(user_file) # All platforms support local setup.cfg local_file = "setup.cfg" if os.path.isfile(local_file): files.append(local_file) log.debug("using config files: %s" % ', '.join(files)) return files def parse_config_files(self, filenames=None): from ConfigParser import ConfigParser if filenames is None: filenames = self.find_config_files() log.debug("Distribution.parse_config_files():") parser = ConfigParser() for filename in filenames: log.debug(" reading %s" % filename) parser.read(filename) for section in parser.sections(): options = parser.options(section) opt_dict = self.get_option_dict(section) for opt in options: if opt != '__name__': val = parser.get(section,opt) opt = opt.replace('-', '_') opt_dict[opt] = (filename, val) # Make the ConfigParser forget everything (so we retain # the original filenames that options come from) parser.__init__() # If there was a "global" section in the config file, use it # to set Distribution options. if 'global' in self.command_options: for (opt, (src, val)) in self.command_options['global'].items(): alias = self.negative_opt.get(opt) try: if alias: setattr(self, alias, not strtobool(val)) elif opt in ('verbose', 'dry_run'): # ugh! setattr(self, opt, strtobool(val)) else: setattr(self, opt, val) except ValueError, msg: raise DistutilsOptionError, msg
os.path.join(self.build_base, 'install_manifest.txt'), self.record) # Add the .egg-info directory and its contents to the install_record.txt open( self.record, 'a+').write('\n'.join(install_egg_info_cmd.get_outputs()) + '\n') else: _install.run(self) # Read the metadata out of the project's .ryppl/METADATA file metadata = DistributionMetadata(path=os.path.join(os.getcwd(), '.ryppl', 'METADATA'), platform_dependant=True) def metadata_to_setup_keywords(metadata): """ Convert a Distutils2 metadata object into a dict of named arguments to be passed to setup() """ if use_distutils2: # Everything *should* be this easy. return dict(metadata.items()) else: class item_to_attribute(object): """
class ReleaseInfo(IndexReference): """Represent a release of a project (a project with a specific version). The release contain the _metadata informations related to this specific version, and is also a container for distribution related informations. See the DistInfo class for more information about distributions. """ def __init__(self, name, version, metadata=None, hidden=False, index=None, **kwargs): """ :param name: the name of the distribution :param version: the version of the distribution :param metadata: the metadata fields of the release. :type metadata: dict :param kwargs: optional arguments for a new distribution. """ self.set_index(index) self.name = name self._version = None self.version = version if metadata: self.metadata = DistributionMetadata(mapping=metadata) else: self.metadata = None self.dists = {} self.hidden = hidden if 'dist_type' in kwargs: dist_type = kwargs.pop('dist_type') self.add_distribution(dist_type, **kwargs) def set_version(self, version): try: self._version = NormalizedVersion(version) except IrrationalVersionError: suggestion = suggest_normalized_version(version) if suggestion: self.version = suggestion else: raise IrrationalVersionError(version) def get_version(self): return self._version version = property(get_version, set_version) def fetch_metadata(self): """If the metadata is not set, use the indexes to get it""" if not self.metadata: self._index.get_metadata(self.name, '%s' % self.version) return self.metadata @property def is_final(self): """proxy to version.is_final""" return self.version.is_final def fetch_distributions(self): if self.dists is None: self._index.get_distributions(self.name, '%s' % self.version) if self.dists is None: self.dists = {} return self.dists def add_distribution(self, dist_type='sdist', python_version=None, **params): """Add distribution informations to this release. If distribution information is already set for this distribution type, add the given url paths to the distribution. This can be useful while some of them fails to download. :param dist_type: the distribution type (eg. "sdist", "bdist", etc.) :param params: the fields to be passed to the distribution object (see the :class:DistInfo constructor). """ if dist_type not in DIST_TYPES: raise ValueError(dist_type) if dist_type in self.dists: self.dists[dist_type].add_url(**params) else: self.dists[dist_type] = DistInfo(self, dist_type, index=self._index, **params) if python_version: self.dists[dist_type].python_version = python_version def get_distribution(self, dist_type=None, prefer_source=True): """Return a distribution. If dist_type is set, find first for this distribution type, and just act as an alias of __get_item__. If prefer_source is True, search first for source distribution, and if not return one existing distribution. """ if len(self.dists) == 0: raise LookupError() if dist_type: return self[dist_type] if prefer_source: if "sdist" in self.dists: dist = self["sdist"] else: dist = self.dists.values()[0] return dist def download(self, temp_path=None, prefer_source=True): """Download the distribution, using the requirements. If more than one distribution match the requirements, use the last version. Download the distribution, and put it in the temp_path. If no temp_path is given, creates and return one. Returns the complete absolute path to the downloaded archive. """ return self.get_distribution(prefer_source=prefer_source)\ .download(path=temp_path) def set_metadata(self, metadata): if not self.metadata: self.metadata = DistributionMetadata() self.metadata.update(metadata) def __getitem__(self, item): """distributions are available using release["sdist"]""" return self.dists[item] def _check_is_comparable(self, other): if not isinstance(other, ReleaseInfo): raise TypeError("cannot compare %s and %s" % (type(self).__name__, type(other).__name__)) elif self.name != other.name: raise TypeError("cannot compare %s and %s" % (self.name, other.name)) def __repr__(self): return "<%s %s>" % (self.name, self.version) def __eq__(self, other): self._check_is_comparable(other) return self.version == other.version def __lt__(self, other): self._check_is_comparable(other) return self.version < other.version def __ne__(self, other): return not self.__eq__(other) def __gt__(self, other): return not (self.__lt__(other) or self.__eq__(other)) def __le__(self, other): return self.__eq__(other) or self.__lt__(other) def __ge__(self, other): return self.__eq__(other) or self.__gt__(other) # See http://docs.python.org/reference/datamodel#object.__hash__ __hash__ = object.__hash__
class Distribution(object): """The core of the Distutils. Most of the work hiding behind 'setup' is really done within a Distribution instance, which farms the work out to the Distutils commands specified on the command line. Setup scripts will almost never instantiate Distribution directly, unless the 'setup()' function is totally inadequate to their needs. However, it is conceivable that a setup script might wish to subclass Distribution for some specialized purpose, and then pass the subclass to 'setup()' as the 'distclass' keyword argument. If so, it is necessary to respect the expectations that 'setup' has of Distribution. See the code for 'setup()', in core.py, for details. """ # 'global_options' describes the command-line options that may be # supplied to the setup script prior to any actual commands. # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of # these global options. This list should be kept to a bare minimum, # since every global option is also valid as a command option -- and we # don't want to pollute the commands with too many options that they # have minimal control over. # The fourth entry for verbose means that it can be repeated. global_options = [('verbose', 'v', "run verbosely (default)", 1), ('quiet', 'q', "run quietly (turns verbosity off)"), ('dry-run', 'n', "don't actually do anything"), ('help', 'h', "show detailed help message"), ('no-user-cfg', None, 'ignore pydistutils.cfg in your home directory'), ] # 'common_usage' is a short (2-3 line) string describing the common # usage of the setup script. common_usage = """\ Common commands: (see '--help-commands' for more) setup.py build will build the package underneath 'build/' setup.py install will install the package """ # options that are not propagated to the commands display_options = [ ('help-commands', None, "list all available commands"), ('name', None, "print package name"), ('version', 'V', "print package version"), ('fullname', None, "print <package name>-<version>"), ('author', None, "print the author's name"), ('author-email', None, "print the author's email address"), ('maintainer', None, "print the maintainer's name"), ('maintainer-email', None, "print the maintainer's email address"), ('contact', None, "print the maintainer's name if known, else the author's"), ('contact-email', None, "print the maintainer's email address if known, else the author's"), ('url', None, "print the URL for this package"), ('license', None, "print the license of the package"), ('licence', None, "alias for --license"), ('description', None, "print the package description"), ('long-description', None, "print the long package description"), ('platforms', None, "print the list of platforms"), ('classifier', None, "print the list of classifiers"), ('keywords', None, "print the list of keywords"), ('provides', None, "print the list of packages/modules provided"), ('requires', None, "print the list of packages/modules required"), ('obsoletes', None, "print the list of packages/modules made obsolete"), ('use-2to3', None, "use 2to3 to make source python 3.x compatible"), ('convert-2to3-doctests', None, "use 2to3 to convert doctests in seperate text files"), ] display_option_names = [x[0].replace('-', '_') for x in display_options] # negative options are options that exclude other options negative_opt = {'quiet': 'verbose'} # -- Creation/initialization methods ------------------------------- def __init__(self, attrs=None): """Construct a new Distribution instance: initialize all the attributes of a Distribution, and then use 'attrs' (a dictionary mapping attribute names to values) to assign some of those attributes their "real" values. (Any attributes not mentioned in 'attrs' will be assigned to some null value: 0, None, an empty list or dictionary, etc.) Most importantly, initialize the 'command_obj' attribute to the empty dictionary; this will be filled in with real command objects by 'parse_command_line()'. """ # Default values for our command-line options self.verbose = 1 self.dry_run = 0 self.help = 0 for attr in self.display_option_names: setattr(self, attr, 0) # Store the configuration self.config = Config(self) # Store the distribution metadata (name, version, author, and so # forth) in a separate object -- we're getting to have enough # information here (and enough command-line options) that it's # worth it. self.metadata = DistributionMetadata() # 'cmdclass' maps command names to class objects, so we # can 1) quickly figure out which class to instantiate when # we need to create a new command object, and 2) have a way # for the setup script to override command classes self.cmdclass = {} # 'command_packages' is a list of packages in which commands # are searched for. The factory for command 'foo' is expected # to be named 'foo' in the module 'foo' in one of the packages # named here. This list is searched from the left; an error # is raised if no named package provides the command being # searched for. (Always access using get_command_packages().) self.command_packages = None # 'script_name' and 'script_args' are usually set to sys.argv[0] # and sys.argv[1:], but they can be overridden when the caller is # not necessarily a setup script run from the command line. self.script_name = None self.script_args = None # 'command_options' is where we store command options between # parsing them (from config files, the command line, etc.) and when # they are actually needed -- ie. when the command in question is # instantiated. It is a dictionary of dictionaries of 2-tuples: # command_options = { command_name : { option : (source, value) } } self.command_options = {} # 'dist_files' is the list of (command, pyversion, file) that # have been created by any dist commands run so far. This is # filled regardless of whether the run is dry or not. pyversion # gives sysconfig.get_python_version() if the dist file is # specific to a Python version, 'any' if it is good for all # Python versions on the target platform, and '' for a source # file. pyversion should not be used to specify minimum or # maximum required Python versions; use the metainfo for that # instead. self.dist_files = [] # These options are really the business of various commands, rather # than of the Distribution itself. We provide aliases for them in # Distribution as a convenience to the developer. self.packages = [] self.package_data = {} self.package_dir = None self.py_modules = [] self.libraries = [] self.headers = [] self.ext_modules = [] self.ext_package = None self.include_dirs = [] self.extra_path = None self.scripts = [] self.data_files = [] self.password = '' self.use_2to3 = False self.convert_2to3_doctests = [] # And now initialize bookkeeping stuff that can't be supplied by # the caller at all. 'command_obj' maps command names to # Command instances -- that's how we enforce that every command # class is a singleton. self.command_obj = {} # 'have_run' maps command names to boolean values; it keeps track # of whether we have actually run a particular command, to make it # cheap to "run" a command whenever we think we might need to -- if # it's already been done, no need for expensive filesystem # operations, we just check the 'have_run' dictionary and carry on. # It's only safe to query 'have_run' for a command class that has # been instantiated -- a false value will be inserted when the # command object is created, and replaced with a true value when # the command is successfully run. Thus it's probably best to use # '.get()' rather than a straight lookup. self.have_run = {} # Now we'll use the attrs dictionary (ultimately, keyword args from # the setup script) to possibly override any or all of these # distribution options. if attrs is not None: # Pull out the set of command options and work on them # specifically. Note that this order guarantees that aliased # command options will override any supplied redundantly # through the general options dictionary. options = attrs.get('options') if options is not None: del attrs['options'] for (command, cmd_options) in options.items(): opt_dict = self.get_option_dict(command) for (opt, val) in cmd_options.items(): opt_dict[opt] = ("setup script", val) # Now work on the rest of the attributes. Any attribute that's # not already defined is invalid! for key, val in attrs.items(): if self.metadata.is_metadata_field(key): self.metadata[key] = val elif hasattr(self, key): setattr(self, key, val) else: msg = "Unknown distribution option: %r" % key warnings.warn(msg) # no-user-cfg is handled before other command line args # because other args override the config files, and this # one is needed before we can load the config files. # If attrs['script_args'] wasn't passed, assume false. # # This also make sure we just look at the global options self.want_user_cfg = True if self.script_args is not None: for arg in self.script_args: if not arg.startswith('-'): break if arg == '--no-user-cfg': self.want_user_cfg = False break self.finalize_options() def get_option_dict(self, command): """Get the option dictionary for a given command. If that command's option dictionary hasn't been created yet, then create it and return the new dictionary; otherwise, return the existing option dictionary. """ d = self.command_options.get(command) if d is None: d = self.command_options[command] = {} return d def get_fullname(self): return self.metadata.get_fullname() def dump_option_dicts(self, header=None, commands=None, indent=""): from pprint import pformat if commands is None: # dump all command option dicts commands = self.command_options.keys() commands.sort() if header is not None: self.announce(indent + header) indent = indent + " " if not commands: self.announce(indent + "no commands known yet") return for cmd_name in commands: opt_dict = self.command_options.get(cmd_name) if opt_dict is None: self.announce(indent + "no option dict for '%s' command" % cmd_name) else: self.announce(indent + "option dict for '%s' command:" % cmd_name) out = pformat(opt_dict) for line in out.split('\n'): self.announce(indent + " " + line) # -- Config file finding/parsing methods --------------------------- # XXX to be removed def parse_config_files(self, filenames=None): return self.config.parse_config_files(filenames) def find_config_files(self): return self.config.find_config_files() # -- Command-line parsing methods ---------------------------------- def parse_command_line(self): """Parse the setup script's command line, taken from the 'script_args' instance attribute (which defaults to 'sys.argv[1:]' -- see 'setup()' in core.py). This list is first processed for "global options" -- options that set attributes of the Distribution instance. Then, it is alternately scanned for Distutils commands and options for that command. Each new command terminates the options for the previous command. The allowed options for a command are determined by the 'user_options' attribute of the command class -- thus, we have to be able to load command classes in order to parse the command line. Any error in that 'options' attribute raises DistutilsGetoptError; any error on the command line raises DistutilsArgError. If no Distutils commands were found on the command line, raises DistutilsArgError. Return true if command line was successfully parsed and we should carry on with executing commands; false if no errors but we shouldn't execute commands (currently, this only happens if user asks for help). """ # # We now have enough information to show the Macintosh dialog # that allows the user to interactively specify the "command line". # toplevel_options = self._get_toplevel_options() # We have to parse the command line a bit at a time -- global # options, then the first command, then its options, and so on -- # because each command will be handled by a different class, and # the options that are valid for a particular class aren't known # until we have loaded the command class, which doesn't happen # until we know what the command is. self.commands = [] parser = FancyGetopt(toplevel_options + self.display_options) parser.set_negative_aliases(self.negative_opt) parser.set_aliases({'licence': 'license'}) args = parser.getopt(args=self.script_args, object=self) option_order = parser.get_option_order() log.set_verbosity(self.verbose) # for display options we return immediately if self.handle_display_options(option_order): return while args: args = self._parse_command_opts(parser, args) if args is None: # user asked for help (and got it) return # Handle the cases of --help as a "global" option, ie. # "setup.py --help" and "setup.py --help command ...". For the # former, we show global options (--verbose, --dry-run, etc.) # and display-only options (--name, --version, etc.); for the # latter, we omit the display-only options and show help for # each command listed on the command line. if self.help: self._show_help(parser, display_options=len(self.commands) == 0, commands=self.commands) return # Oops, no commands found -- an end-user error if not self.commands: raise DistutilsArgError("no commands supplied") # All is well: return true return 1 def _get_toplevel_options(self): """Return the non-display options recognized at the top level. This includes options that are recognized *only* at the top level as well as options recognized for commands. """ return self.global_options + [ ("command-packages=", None, "list of packages that provide distutils commands"), ] def _parse_command_opts(self, parser, args): """Parse the command-line options for a single command. 'parser' must be a FancyGetopt instance; 'args' must be the list of arguments, starting with the current command (whose options we are about to parse). Returns a new version of 'args' with the next command at the front of the list; will be the empty list if there are no more commands on the command line. Returns None if the user asked for help on this command. """ # late import because of mutual dependence between these modules from distutils2.command.cmd import Command # Pull the current command from the head of the command line command = args[0] if not command_re.match(command): raise SystemExit("invalid command name '%s'" % command) self.commands.append(command) # Dig up the command class that implements this command, so we # 1) know that it's a valid command, and 2) know which options # it takes. try: cmd_class = self.get_command_class(command) except DistutilsModuleError, msg: raise DistutilsArgError(msg) # Require that the command class be derived from Command -- want # to be sure that the basic "command" interface is implemented. if not issubclass(cmd_class, Command): raise DistutilsClassError( "command class %s must subclass Command" % cmd_class) # Also make sure that the command object provides a list of its # known options. if not (hasattr(cmd_class, 'user_options') and isinstance(cmd_class.user_options, list)): raise DistutilsClassError( ("command class %s must provide " "'user_options' attribute (a list of tuples)") % cmd_class) # If the command class has a list of negative alias options, # merge it in with the global negative aliases. negative_opt = self.negative_opt if hasattr(cmd_class, 'negative_opt'): negative_opt = negative_opt.copy() negative_opt.update(cmd_class.negative_opt) # Check for help_options in command class. They have a different # format (tuple of four) so we need to preprocess them here. if (hasattr(cmd_class, 'help_options') and isinstance(cmd_class.help_options, list)): help_options = fix_help_options(cmd_class.help_options) else: help_options = [] # All commands support the global options too, just by adding # in 'global_options'. parser.set_option_table(self.global_options + cmd_class.user_options + help_options) parser.set_negative_aliases(negative_opt) (args, opts) = parser.getopt(args[1:]) if hasattr(opts, 'help') and opts.help: self._show_help(parser, display_options=0, commands=[cmd_class]) return if (hasattr(cmd_class, 'help_options') and isinstance(cmd_class.help_options, list)): help_option_found = 0 for (help_option, short, desc, func) in cmd_class.help_options: if hasattr(opts, help_option.replace('-', '_')): help_option_found = 1 if hasattr(func, '__call__'): func() else: raise DistutilsClassError( "invalid help function %r for help option '%s': " "must be a callable object (function, etc.)" % (func, help_option)) if help_option_found: return # Put the options from the command line into their official # holding pen, the 'command_options' dictionary. opt_dict = self.get_option_dict(command) for (name, value) in vars(opts).items(): opt_dict[name] = ("command line", value) return args
def create_projects(env, **projects): from distutils2.metadata import DistributionMetadata index = env.scratch_path / "index" mktree(index) repo = env.scratch_path / "repo" open(index / "index.html", "w").write( "\n".join( ["<html><head><title>Simple Index</title></head><body>"] + ["<a href=%r/>%s</a><br/>" % (p, p) for p in projects] + ["</body></html>"] ) ) paths = {} for p, metadata in projects.items(): mktree(index / p) root = repo / p paths[p] = root mktree(root) env.run("git", "init", cwd=root) dot_ryppl = root / ".ryppl" if isinstance(metadata, basestring): m = DistributionMetadata() m.read_file(StringIO(metadata)) metadata = m elif isinstance(metadata, dict): m = DistributionMetadata() for k, v in metadata.items(): m[k] = v metadata = m metadata["Name"] = p if metadata["Version"] == "UNKNOWN": metadata["Version"] = "0.9" if metadata["Requires-Python"] == "UNKNOWN": metadata["Requires-Python"] = ">2.0" mktree(dot_ryppl) metadata.write(dot_ryppl / "METADATA") # mktree(root/p) # open(root/p/'__init__.py', 'w').write('print "importing module %s"' % p) env.run("git", "add", "*", Path(".ryppl") / "*", cwd=root) env.run("git", "commit", "-m", "initial checkin", cwd=root) repo_url = pathname2url(root) open(index / p / "index.html", "w").write( "<html><head><title>Links for %(p)s</title></head>" "<body>" "<h1>Links for %(p)s</h1>" '<a href="git+file://%(repo_url)s#egg=%(p)s">Git Repository</a><br/>' "</body></html>" % locals() ) return index, paths
def create_projects(env, **proj_metadata): """ Create git repositories and an index for projects within env. env should be an Environment proj_metadata should be a dict mapping project names to DistributionMetadata objects representing their metadata. Returns (index,projdirs) where: * index is the path to the locally-generated index * projdirs is a dict mapping project names to the paths to the locally-generated git repositories. """ from distutils2.metadata import DistributionMetadata index = env.scratch_path/'index' mktree(index) repo = env.scratch_path/'repo' open(index/'index.html', 'w').write( '\n'.join( ['<html><head><title>Simple Index</title></head><body>'] + [ '<a href=%r/>%s</a><br/>' % (p,p) for p in proj_metadata ] + ['</body></html>']) ) projects = {} for p,metadata in proj_metadata.items(): mktree(index/p) root = repo/p projects[p] = Project(p, root, env) mktree(root) env.run('git', 'init', cwd = root) dot_ryppl = root/'.ryppl' if isinstance(metadata, basestring): m = DistributionMetadata() m.read_file(StringIO(metadata)) metadata = m elif isinstance(metadata, dict): m = DistributionMetadata() for k,v in metadata.items(): m[k] = v metadata = m metadata['Name'] = p if metadata['Version'] == 'UNKNOWN': metadata['Version'] = '0.9' if metadata['Requires-Python'] == 'UNKNOWN': metadata['Requires-Python'] = '>2.0' mktree(dot_ryppl) metadata.write(dot_ryppl/'METADATA') # mktree(root/p) # open(root/p/'__init__.py', 'w').write('print "importing module %s"' % p) env.run('git', 'add', '*', Path('.ryppl')/'*', cwd = root) env.run('git', 'commit', '-m', 'initial checkin', cwd = root) repo_url = pathname2url(root) open(index/p/'index.html', 'w').write( '<html><head><title>Links for %(p)s</title></head>' '<body>' '<h1>Links for %(p)s</h1>' '<a href="git+file://%(repo_url)s#egg=%(p)s">Git Repository</a><br/>' '</body></html>' % locals() ) return 'file://'+pathname2url(index),projects
def set_metadata(self, metadata): if not self.metadata: self.metadata = DistributionMetadata() self.metadata.update(metadata)