def test_get_paths(self): scheme = get_paths() default_scheme = _get_default_scheme() wanted = _expand_vars(default_scheme, None) wanted = sorted(wanted.items()) scheme = sorted(scheme.items()) self.assertEqual(scheme, wanted)
def expand_categories(self, path_with_categories): local_vars = get_paths() local_vars['distribution.name'] = self.distribution.metadata['Name'] expanded_path = format_value(path_with_categories, local_vars) expanded_path = format_value(expanded_path, local_vars) if '{' in expanded_path and '}' in expanded_path: logger.warning( '%s: unable to expand %s, some categories may be missing', self.get_command_name(), path_with_categories) return expanded_path
def select_scheme(self, name): """Set the install directories by applying the install schemes.""" # it's the caller's problem if they supply a bad name! scheme = get_paths(name, expand=False) for key, value in scheme.items(): if key == 'platinclude': key = 'headers' value = os.path.join(value, self.distribution.metadata['Name']) attrname = 'install_' + key if hasattr(self, attrname): if getattr(self, attrname) is None: setattr(self, attrname, value)
def setup_mock(**attrs): """Mock the setup(**attrs) in order to retrieve metadata.""" # TODO use config and metadata instead of Distribution from distutils.dist import Distribution dist = Distribution(attrs) dist.parse_config_files() # 1. retrieve metadata fields that are quite similar in # PEP 314 and PEP 345 labels = (('name', ) * 2, ('version', ) * 2, ('author', ) * 2, ('author_email', ) * 2, ('maintainer', ) * 2, ('maintainer_email', ) * 2, ('description', 'summary'), ('long_description', 'description'), ('url', 'home_page'), ('platforms', 'platform'), ('provides', 'provides-dist'), ('obsoletes', 'obsoletes-dist'), ('requires', 'requires-dist')) get = lambda lab: getattr(dist.metadata, lab.replace('-', '_')) data.update((new, get(old)) for old, new in labels if get(old)) # 2. retrieve data that requires special processing data['classifier'].update(dist.get_classifiers() or []) data['scripts'].extend(dist.scripts or []) data['packages'].extend(dist.packages or []) data['modules'].extend(dist.py_modules or []) # 2.1 data_files -> resources if dist.data_files: if (len(dist.data_files) < 2 or isinstance(dist.data_files[1], str)): dist.data_files = [('', dist.data_files)] # add tokens in the destination paths vars = {'distribution.name': data['name']} path_tokens = sysconfig.get_paths(vars=vars).items() # sort tokens to use the longest one first path_tokens = sorted(path_tokens, key=lambda x: len(x[1])) for dest, srcs in (dist.data_files or []): dest = os.path.join(sys.prefix, dest) dest = dest.replace(os.path.sep, '/') for tok, path in path_tokens: path = path.replace(os.path.sep, '/') if not dest.startswith(path): continue dest = ('{%s}' % tok) + dest[len(path):] files = [('/ '.join(src.rsplit('/', 1)), dest) for src in srcs] data['resources'].extend(files) # 2.2 package_data data['package_data'] = dist.package_data.copy() # Use README file if its content is the desciption if "description" in data: ref = md5( re.sub('\s', '', self.data['description']).lower().encode()) ref = ref.digest() for readme in glob.glob('README*'): with open(readme, encoding='utf-8') as fp: contents = fp.read() contents = re.sub('\s', '', contents.lower()).encode() val = md5(contents).digest() if val == ref: del data['description'] data['description-file'] = readme break
def setup_mock(**attrs): """Mock the setup(**attrs) in order to retrieve metadata.""" # TODO use config and metadata instead of Distribution from distutils.dist import Distribution dist = Distribution(attrs) dist.parse_config_files() # 1. retrieve metadata fields that are quite similar in # PEP 314 and PEP 345 labels = (('name',) * 2, ('version',) * 2, ('author',) * 2, ('author_email',) * 2, ('maintainer',) * 2, ('maintainer_email',) * 2, ('description', 'summary'), ('long_description', 'description'), ('url', 'home_page'), ('platforms', 'platform')) if sys.version_info[:2] >= (2, 5): labels += ( ('provides', 'provides-dist'), ('obsoletes', 'obsoletes-dist'), ('requires', 'requires-dist')) get = lambda lab: getattr(dist.metadata, lab.replace('-', '_')) data.update((new, get(old)) for old, new in labels if get(old)) # 2. retrieve data that requires special processing data['classifier'].update(dist.get_classifiers() or []) data['scripts'].extend(dist.scripts or []) data['packages'].extend(dist.packages or []) data['modules'].extend(dist.py_modules or []) # 2.1 data_files -> resources if dist.data_files: if (len(dist.data_files) < 2 or isinstance(dist.data_files[1], basestring)): dist.data_files = [('', dist.data_files)] # add tokens in the destination paths vars = {'distribution.name': data['name']} path_tokens = sysconfig.get_paths(vars=vars).items() # sort tokens to use the longest one first path_tokens = sorted(path_tokens, key=lambda x: len(x[1])) for dest, srcs in (dist.data_files or []): dest = os.path.join(sys.prefix, dest) dest = dest.replace(os.path.sep, '/') for tok, path in path_tokens: path = path.replace(os.path.sep, '/') if not dest.startswith(path): continue dest = ('{%s}' % tok) + dest[len(path):] files = [('/ '.join(src.rsplit('/', 1)), dest) for src in srcs] data['resources'].extend(files) # 2.2 package_data data['package_data'] = dist.package_data.copy() # Use README file if its content is the desciption if "description" in data: ref = md5(re.sub('\s', '', self.data['description']).lower().encode()) ref = ref.digest() for readme in glob.glob('README*'): fp = codecs.open(readme, encoding='utf-8') try: contents = fp.read() finally: fp.close() contents = re.sub('\s', '', contents.lower()).encode() val = md5(contents).digest() if val == ref: del data['description'] data['description-file'] = readme break