| 1 | n/a | """Utilities to find and read config files used by packaging.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import sys |
|---|
| 5 | n/a | import logging |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | from shlex import split |
|---|
| 8 | n/a | from configparser import RawConfigParser |
|---|
| 9 | n/a | from packaging import logger |
|---|
| 10 | n/a | from packaging.errors import PackagingOptionError |
|---|
| 11 | n/a | from packaging.compiler.extension import Extension |
|---|
| 12 | n/a | from packaging.util import (check_environ, iglob, resolve_name, strtobool, |
|---|
| 13 | n/a | split_multiline) |
|---|
| 14 | n/a | from packaging.compiler import set_compiler |
|---|
| 15 | n/a | from packaging.command import set_command |
|---|
| 16 | n/a | from packaging.markers import interpret |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def _check_name(name, packages): |
|---|
| 20 | n/a | if '.' not in name: |
|---|
| 21 | n/a | return |
|---|
| 22 | n/a | parts = name.split('.') |
|---|
| 23 | n/a | parent = '.'.join(parts[:-1]) |
|---|
| 24 | n/a | if parent not in packages: |
|---|
| 25 | n/a | # we could log a warning instead of raising, but what's the use |
|---|
| 26 | n/a | # of letting people build modules they can't import? |
|---|
| 27 | n/a | raise PackagingOptionError( |
|---|
| 28 | n/a | 'parent package for extension %r not found' % name) |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | def _pop_values(values_dct, key): |
|---|
| 32 | n/a | """Remove values from the dictionary and convert them as a list""" |
|---|
| 33 | n/a | vals_str = values_dct.pop(key, '') |
|---|
| 34 | n/a | if not vals_str: |
|---|
| 35 | n/a | return |
|---|
| 36 | n/a | fields = [] |
|---|
| 37 | n/a | # the line separator is \n for setup.cfg files |
|---|
| 38 | n/a | for field in vals_str.split('\n'): |
|---|
| 39 | n/a | tmp_vals = field.split('--') |
|---|
| 40 | n/a | if len(tmp_vals) == 2 and not interpret(tmp_vals[1]): |
|---|
| 41 | n/a | continue |
|---|
| 42 | n/a | fields.append(tmp_vals[0]) |
|---|
| 43 | n/a | # Get bash options like `gcc -print-file-name=libgcc.a` XXX bash options? |
|---|
| 44 | n/a | vals = split(' '.join(fields)) |
|---|
| 45 | n/a | if vals: |
|---|
| 46 | n/a | return vals |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | def _rel_path(base, path): |
|---|
| 50 | n/a | # normalizes and returns a lstripped-/-separated path |
|---|
| 51 | n/a | base = base.replace(os.path.sep, '/') |
|---|
| 52 | n/a | path = path.replace(os.path.sep, '/') |
|---|
| 53 | n/a | assert path.startswith(base) |
|---|
| 54 | n/a | return path[len(base):].lstrip('/') |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | def get_resources_dests(resources_root, rules): |
|---|
| 58 | n/a | """Find destinations for resources files""" |
|---|
| 59 | n/a | destinations = {} |
|---|
| 60 | n/a | for base, suffix, dest in rules: |
|---|
| 61 | n/a | prefix = os.path.join(resources_root, base) |
|---|
| 62 | n/a | for abs_base in iglob(prefix): |
|---|
| 63 | n/a | abs_glob = os.path.join(abs_base, suffix) |
|---|
| 64 | n/a | for abs_path in iglob(abs_glob): |
|---|
| 65 | n/a | resource_file = _rel_path(resources_root, abs_path) |
|---|
| 66 | n/a | if dest is None: # remove the entry if it was here |
|---|
| 67 | n/a | destinations.pop(resource_file, None) |
|---|
| 68 | n/a | else: |
|---|
| 69 | n/a | rel_path = _rel_path(abs_base, abs_path) |
|---|
| 70 | n/a | rel_dest = dest.replace(os.path.sep, '/').rstrip('/') |
|---|
| 71 | n/a | destinations[resource_file] = rel_dest + '/' + rel_path |
|---|
| 72 | n/a | return destinations |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | class Config: |
|---|
| 76 | n/a | """Class used to work with configuration files""" |
|---|
| 77 | n/a | def __init__(self, dist): |
|---|
| 78 | n/a | self.dist = dist |
|---|
| 79 | n/a | self.setup_hooks = [] |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def run_hooks(self, config): |
|---|
| 82 | n/a | """Run setup hooks in the order defined in the spec.""" |
|---|
| 83 | n/a | for hook in self.setup_hooks: |
|---|
| 84 | n/a | hook(config) |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | def find_config_files(self): |
|---|
| 87 | n/a | """Find as many configuration files as should be processed for this |
|---|
| 88 | n/a | platform, and return a list of filenames in the order in which they |
|---|
| 89 | n/a | should be parsed. The filenames returned are guaranteed to exist |
|---|
| 90 | n/a | (modulo nasty race conditions). |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | There are three possible config files: packaging.cfg in the |
|---|
| 93 | n/a | Packaging installation directory (ie. where the top-level |
|---|
| 94 | n/a | Packaging __inst__.py file lives), a file in the user's home |
|---|
| 95 | n/a | directory named .pydistutils.cfg on Unix and pydistutils.cfg |
|---|
| 96 | n/a | on Windows/Mac; and setup.cfg in the current directory. |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | The file in the user's home directory can be disabled with the |
|---|
| 99 | n/a | --no-user-cfg option. |
|---|
| 100 | n/a | """ |
|---|
| 101 | n/a | files = [] |
|---|
| 102 | n/a | check_environ() |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | # Where to look for the system-wide Packaging config file |
|---|
| 105 | n/a | sys_dir = os.path.dirname(sys.modules['packaging'].__file__) |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | # Look for the system config file |
|---|
| 108 | n/a | sys_file = os.path.join(sys_dir, "packaging.cfg") |
|---|
| 109 | n/a | if os.path.isfile(sys_file): |
|---|
| 110 | n/a | files.append(sys_file) |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | # What to call the per-user config file |
|---|
| 113 | n/a | if os.name == 'posix': |
|---|
| 114 | n/a | user_filename = ".pydistutils.cfg" |
|---|
| 115 | n/a | else: |
|---|
| 116 | n/a | user_filename = "pydistutils.cfg" |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | # And look for the user config file |
|---|
| 119 | n/a | if self.dist.want_user_cfg: |
|---|
| 120 | n/a | user_file = os.path.join(os.path.expanduser('~'), user_filename) |
|---|
| 121 | n/a | if os.path.isfile(user_file): |
|---|
| 122 | n/a | files.append(user_file) |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | # All platforms support local setup.cfg |
|---|
| 125 | n/a | local_file = "setup.cfg" |
|---|
| 126 | n/a | if os.path.isfile(local_file): |
|---|
| 127 | n/a | files.append(local_file) |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | if logger.isEnabledFor(logging.DEBUG): |
|---|
| 130 | n/a | logger.debug("using config files: %s", ', '.join(files)) |
|---|
| 131 | n/a | return files |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | def _convert_metadata(self, name, value): |
|---|
| 134 | n/a | # converts a value found in setup.cfg into a valid metadata |
|---|
| 135 | n/a | # XXX |
|---|
| 136 | n/a | return value |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | def _read_setup_cfg(self, parser, cfg_filename): |
|---|
| 139 | n/a | cfg_directory = os.path.dirname(os.path.abspath(cfg_filename)) |
|---|
| 140 | n/a | content = {} |
|---|
| 141 | n/a | for section in parser.sections(): |
|---|
| 142 | n/a | content[section] = dict(parser.items(section)) |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | # global setup hooks are called first |
|---|
| 145 | n/a | if 'global' in content: |
|---|
| 146 | n/a | if 'setup_hooks' in content['global']: |
|---|
| 147 | n/a | setup_hooks = split_multiline(content['global']['setup_hooks']) |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | # add project directory to sys.path, to allow hooks to be |
|---|
| 150 | n/a | # distributed with the project |
|---|
| 151 | n/a | sys.path.insert(0, cfg_directory) |
|---|
| 152 | n/a | try: |
|---|
| 153 | n/a | for line in setup_hooks: |
|---|
| 154 | n/a | try: |
|---|
| 155 | n/a | hook = resolve_name(line) |
|---|
| 156 | n/a | except ImportError as e: |
|---|
| 157 | n/a | logger.warning('cannot find setup hook: %s', |
|---|
| 158 | n/a | e.args[0]) |
|---|
| 159 | n/a | else: |
|---|
| 160 | n/a | self.setup_hooks.append(hook) |
|---|
| 161 | n/a | self.run_hooks(content) |
|---|
| 162 | n/a | finally: |
|---|
| 163 | n/a | sys.path.pop(0) |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | metadata = self.dist.metadata |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | # setting the metadata values |
|---|
| 168 | n/a | if 'metadata' in content: |
|---|
| 169 | n/a | for key, value in content['metadata'].items(): |
|---|
| 170 | n/a | key = key.replace('_', '-') |
|---|
| 171 | n/a | if metadata.is_multi_field(key): |
|---|
| 172 | n/a | value = split_multiline(value) |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | if key == 'project-url': |
|---|
| 175 | n/a | value = [(label.strip(), url.strip()) |
|---|
| 176 | n/a | for label, url in |
|---|
| 177 | n/a | [v.split(',') for v in value]] |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | if key == 'description-file': |
|---|
| 180 | n/a | if 'description' in content['metadata']: |
|---|
| 181 | n/a | msg = ("description and description-file' are " |
|---|
| 182 | n/a | "mutually exclusive") |
|---|
| 183 | n/a | raise PackagingOptionError(msg) |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | filenames = value.split() |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | # concatenate all files |
|---|
| 188 | n/a | value = [] |
|---|
| 189 | n/a | for filename in filenames: |
|---|
| 190 | n/a | # will raise if file not found |
|---|
| 191 | n/a | with open(filename) as description_file: |
|---|
| 192 | n/a | value.append(description_file.read().strip()) |
|---|
| 193 | n/a | # add filename as a required file |
|---|
| 194 | n/a | if filename not in metadata.requires_files: |
|---|
| 195 | n/a | metadata.requires_files.append(filename) |
|---|
| 196 | n/a | value = '\n'.join(value).strip() |
|---|
| 197 | n/a | key = 'description' |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | if metadata.is_metadata_field(key): |
|---|
| 200 | n/a | metadata[key] = self._convert_metadata(key, value) |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | if 'files' in content: |
|---|
| 203 | n/a | files = content['files'] |
|---|
| 204 | n/a | self.dist.package_dir = files.pop('packages_root', None) |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | files = dict((key, split_multiline(value)) for key, value in |
|---|
| 207 | n/a | files.items()) |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | self.dist.packages = [] |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | packages = files.get('packages', []) |
|---|
| 212 | n/a | if isinstance(packages, str): |
|---|
| 213 | n/a | packages = [packages] |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | for package in packages: |
|---|
| 216 | n/a | if ':' in package: |
|---|
| 217 | n/a | dir_, package = package.split(':') |
|---|
| 218 | n/a | self.dist.package_dir[package] = dir_ |
|---|
| 219 | n/a | self.dist.packages.append(package) |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | self.dist.py_modules = files.get('modules', []) |
|---|
| 222 | n/a | if isinstance(self.dist.py_modules, str): |
|---|
| 223 | n/a | self.dist.py_modules = [self.dist.py_modules] |
|---|
| 224 | n/a | self.dist.scripts = files.get('scripts', []) |
|---|
| 225 | n/a | if isinstance(self.dist.scripts, str): |
|---|
| 226 | n/a | self.dist.scripts = [self.dist.scripts] |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | self.dist.package_data = {} |
|---|
| 229 | n/a | # bookkeeping for the loop below |
|---|
| 230 | n/a | firstline = True |
|---|
| 231 | n/a | prev = None |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | for line in files.get('package_data', []): |
|---|
| 234 | n/a | if '=' in line: |
|---|
| 235 | n/a | # package name -- file globs or specs |
|---|
| 236 | n/a | key, value = line.split('=') |
|---|
| 237 | n/a | prev = self.dist.package_data[key.strip()] = value.split() |
|---|
| 238 | n/a | elif firstline: |
|---|
| 239 | n/a | # invalid continuation on the first line |
|---|
| 240 | n/a | raise PackagingOptionError( |
|---|
| 241 | n/a | 'malformed package_data first line: %r (misses "=")' % |
|---|
| 242 | n/a | line) |
|---|
| 243 | n/a | else: |
|---|
| 244 | n/a | # continuation, add to last seen package name |
|---|
| 245 | n/a | prev.extend(line.split()) |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | firstline = False |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | self.dist.data_files = [] |
|---|
| 250 | n/a | for data in files.get('data_files', []): |
|---|
| 251 | n/a | data = data.split('=') |
|---|
| 252 | n/a | if len(data) != 2: |
|---|
| 253 | n/a | continue |
|---|
| 254 | n/a | key, value = data |
|---|
| 255 | n/a | values = [v.strip() for v in value.split(',')] |
|---|
| 256 | n/a | self.dist.data_files.append((key, values)) |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | # manifest template |
|---|
| 259 | n/a | self.dist.extra_files = files.get('extra_files', []) |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | resources = [] |
|---|
| 262 | n/a | for rule in files.get('resources', []): |
|---|
| 263 | n/a | glob, destination = rule.split('=', 1) |
|---|
| 264 | n/a | rich_glob = glob.strip().split(' ', 1) |
|---|
| 265 | n/a | if len(rich_glob) == 2: |
|---|
| 266 | n/a | prefix, suffix = rich_glob |
|---|
| 267 | n/a | else: |
|---|
| 268 | n/a | assert len(rich_glob) == 1 |
|---|
| 269 | n/a | prefix = '' |
|---|
| 270 | n/a | suffix = glob |
|---|
| 271 | n/a | if destination == '<exclude>': |
|---|
| 272 | n/a | destination = None |
|---|
| 273 | n/a | resources.append( |
|---|
| 274 | n/a | (prefix.strip(), suffix.strip(), destination.strip())) |
|---|
| 275 | n/a | self.dist.data_files = get_resources_dests( |
|---|
| 276 | n/a | cfg_directory, resources) |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | ext_modules = self.dist.ext_modules |
|---|
| 279 | n/a | for section_key in content: |
|---|
| 280 | n/a | # no str.partition in 2.4 :( |
|---|
| 281 | n/a | labels = section_key.split(':') |
|---|
| 282 | n/a | if len(labels) == 2 and labels[0] == 'extension': |
|---|
| 283 | n/a | values_dct = content[section_key] |
|---|
| 284 | n/a | if 'name' in values_dct: |
|---|
| 285 | n/a | raise PackagingOptionError( |
|---|
| 286 | n/a | 'extension name should be given as [extension: name], ' |
|---|
| 287 | n/a | 'not as key') |
|---|
| 288 | n/a | name = labels[1].strip() |
|---|
| 289 | n/a | _check_name(name, self.dist.packages) |
|---|
| 290 | n/a | ext_modules.append(Extension( |
|---|
| 291 | n/a | name, |
|---|
| 292 | n/a | _pop_values(values_dct, 'sources'), |
|---|
| 293 | n/a | _pop_values(values_dct, 'include_dirs'), |
|---|
| 294 | n/a | _pop_values(values_dct, 'define_macros'), |
|---|
| 295 | n/a | _pop_values(values_dct, 'undef_macros'), |
|---|
| 296 | n/a | _pop_values(values_dct, 'library_dirs'), |
|---|
| 297 | n/a | _pop_values(values_dct, 'libraries'), |
|---|
| 298 | n/a | _pop_values(values_dct, 'runtime_library_dirs'), |
|---|
| 299 | n/a | _pop_values(values_dct, 'extra_objects'), |
|---|
| 300 | n/a | _pop_values(values_dct, 'extra_compile_args'), |
|---|
| 301 | n/a | _pop_values(values_dct, 'extra_link_args'), |
|---|
| 302 | n/a | _pop_values(values_dct, 'export_symbols'), |
|---|
| 303 | n/a | _pop_values(values_dct, 'swig_opts'), |
|---|
| 304 | n/a | _pop_values(values_dct, 'depends'), |
|---|
| 305 | n/a | values_dct.pop('language', None), |
|---|
| 306 | n/a | values_dct.pop('optional', None), |
|---|
| 307 | n/a | **values_dct)) |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | def parse_config_files(self, filenames=None): |
|---|
| 310 | n/a | if filenames is None: |
|---|
| 311 | n/a | filenames = self.find_config_files() |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | logger.debug("Distribution.parse_config_files():") |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | parser = RawConfigParser() |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | for filename in filenames: |
|---|
| 318 | n/a | logger.debug(" reading %s", filename) |
|---|
| 319 | n/a | parser.read(filename, encoding='utf-8') |
|---|
| 320 | n/a | |
|---|
| 321 | n/a | if os.path.split(filename)[-1] == 'setup.cfg': |
|---|
| 322 | n/a | self._read_setup_cfg(parser, filename) |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | for section in parser.sections(): |
|---|
| 325 | n/a | if section == 'global': |
|---|
| 326 | n/a | if parser.has_option('global', 'compilers'): |
|---|
| 327 | n/a | self._load_compilers(parser.get('global', 'compilers')) |
|---|
| 328 | n/a | |
|---|
| 329 | n/a | if parser.has_option('global', 'commands'): |
|---|
| 330 | n/a | self._load_commands(parser.get('global', 'commands')) |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | options = parser.options(section) |
|---|
| 333 | n/a | opt_dict = self.dist.get_option_dict(section) |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | for opt in options: |
|---|
| 336 | n/a | if opt == '__name__': |
|---|
| 337 | n/a | continue |
|---|
| 338 | n/a | val = parser.get(section, opt) |
|---|
| 339 | n/a | opt = opt.replace('-', '_') |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | if opt == 'sub_commands': |
|---|
| 342 | n/a | val = split_multiline(val) |
|---|
| 343 | n/a | if isinstance(val, str): |
|---|
| 344 | n/a | val = [val] |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | # Hooks use a suffix system to prevent being overriden |
|---|
| 347 | n/a | # by a config file processed later (i.e. a hook set in |
|---|
| 348 | n/a | # the user config file cannot be replaced by a hook |
|---|
| 349 | n/a | # set in a project config file, unless they have the |
|---|
| 350 | n/a | # same suffix). |
|---|
| 351 | n/a | if (opt.startswith("pre_hook.") or |
|---|
| 352 | n/a | opt.startswith("post_hook.")): |
|---|
| 353 | n/a | hook_type, alias = opt.split(".") |
|---|
| 354 | n/a | hook_dict = opt_dict.setdefault( |
|---|
| 355 | n/a | hook_type, (filename, {}))[1] |
|---|
| 356 | n/a | hook_dict[alias] = val |
|---|
| 357 | n/a | else: |
|---|
| 358 | n/a | opt_dict[opt] = filename, val |
|---|
| 359 | n/a | |
|---|
| 360 | n/a | # Make the RawConfigParser forget everything (so we retain |
|---|
| 361 | n/a | # the original filenames that options come from) |
|---|
| 362 | n/a | parser.__init__() |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | # If there was a "global" section in the config file, use it |
|---|
| 365 | n/a | # to set Distribution options. |
|---|
| 366 | n/a | if 'global' in self.dist.command_options: |
|---|
| 367 | n/a | for opt, (src, val) in self.dist.command_options['global'].items(): |
|---|
| 368 | n/a | alias = self.dist.negative_opt.get(opt) |
|---|
| 369 | n/a | try: |
|---|
| 370 | n/a | if alias: |
|---|
| 371 | n/a | setattr(self.dist, alias, not strtobool(val)) |
|---|
| 372 | n/a | elif opt == 'dry_run': # FIXME ugh! |
|---|
| 373 | n/a | setattr(self.dist, opt, strtobool(val)) |
|---|
| 374 | n/a | else: |
|---|
| 375 | n/a | setattr(self.dist, opt, val) |
|---|
| 376 | n/a | except ValueError as msg: |
|---|
| 377 | n/a | raise PackagingOptionError(msg) |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | def _load_compilers(self, compilers): |
|---|
| 380 | n/a | compilers = split_multiline(compilers) |
|---|
| 381 | n/a | if isinstance(compilers, str): |
|---|
| 382 | n/a | compilers = [compilers] |
|---|
| 383 | n/a | for compiler in compilers: |
|---|
| 384 | n/a | set_compiler(compiler.strip()) |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | def _load_commands(self, commands): |
|---|
| 387 | n/a | commands = split_multiline(commands) |
|---|
| 388 | n/a | if isinstance(commands, str): |
|---|
| 389 | n/a | commands = [commands] |
|---|
| 390 | n/a | for command in commands: |
|---|
| 391 | n/a | set_command(command.strip()) |
|---|