| 1 | n/a | """Interactive helper used to create a setup.cfg file. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | This script will generate a packaging configuration file by looking at |
|---|
| 4 | n/a | the current directory and asking the user questions. It is intended to |
|---|
| 5 | n/a | be called as *pysetup create*. |
|---|
| 6 | n/a | """ |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # Original code by Sean Reifschneider <jafo@tummy.com> |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | # Original TODO list: |
|---|
| 11 | n/a | # Look for a license file and automatically add the category. |
|---|
| 12 | n/a | # When a .c file is found during the walk, can we add it as an extension? |
|---|
| 13 | n/a | # Ask if there is a maintainer different that the author |
|---|
| 14 | n/a | # Ask for the platform (can we detect this via "import win32" or something?) |
|---|
| 15 | n/a | # Ask for the dependencies. |
|---|
| 16 | n/a | # Ask for the Requires-Dist |
|---|
| 17 | n/a | # Ask for the Provides-Dist |
|---|
| 18 | n/a | # Ask for a description |
|---|
| 19 | n/a | # Detect scripts (not sure how. #! outside of package?) |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | import os |
|---|
| 22 | n/a | import re |
|---|
| 23 | n/a | import imp |
|---|
| 24 | n/a | import sys |
|---|
| 25 | n/a | import glob |
|---|
| 26 | n/a | import shutil |
|---|
| 27 | n/a | import sysconfig |
|---|
| 28 | n/a | from hashlib import md5 |
|---|
| 29 | n/a | from textwrap import dedent |
|---|
| 30 | n/a | from tokenize import detect_encoding |
|---|
| 31 | n/a | from configparser import RawConfigParser |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | from packaging import logger |
|---|
| 34 | n/a | # importing this with an underscore as it should be replaced by the |
|---|
| 35 | n/a | # dict form or another structures for all purposes |
|---|
| 36 | n/a | from packaging._trove import all_classifiers as _CLASSIFIERS_LIST |
|---|
| 37 | n/a | from packaging.version import is_valid_version |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | _FILENAME = 'setup.cfg' |
|---|
| 40 | n/a | _DEFAULT_CFG = '.pypkgcreate' # FIXME use a section in user .pydistutils.cfg |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | _helptext = { |
|---|
| 43 | n/a | 'name': ''' |
|---|
| 44 | n/a | The name of the project to be packaged, usually a single word composed |
|---|
| 45 | n/a | of lower-case characters such as "zope.interface", "sqlalchemy" or |
|---|
| 46 | n/a | "CherryPy". |
|---|
| 47 | n/a | ''', |
|---|
| 48 | n/a | 'version': ''' |
|---|
| 49 | n/a | Version number of the software, typically 2 or 3 numbers separated by |
|---|
| 50 | n/a | dots such as "1.0", "0.6b3", or "3.2.1". "0.1.0" is recommended for |
|---|
| 51 | n/a | initial development. |
|---|
| 52 | n/a | ''', |
|---|
| 53 | n/a | 'summary': ''' |
|---|
| 54 | n/a | A one-line summary of what this project is or does, typically a sentence |
|---|
| 55 | n/a | 80 characters or less in length. |
|---|
| 56 | n/a | ''', |
|---|
| 57 | n/a | 'author': ''' |
|---|
| 58 | n/a | The full name of the author (typically you). |
|---|
| 59 | n/a | ''', |
|---|
| 60 | n/a | 'author_email': ''' |
|---|
| 61 | n/a | Email address of the project author. |
|---|
| 62 | n/a | ''', |
|---|
| 63 | n/a | 'do_classifier': ''' |
|---|
| 64 | n/a | Trove classifiers are optional identifiers that allow you to specify the |
|---|
| 65 | n/a | intended audience by saying things like "Beta software with a text UI |
|---|
| 66 | n/a | for Linux under the PSF license". However, this can be a somewhat |
|---|
| 67 | n/a | involved process. |
|---|
| 68 | n/a | ''', |
|---|
| 69 | n/a | 'packages': ''' |
|---|
| 70 | n/a | Python packages included in the project. |
|---|
| 71 | n/a | ''', |
|---|
| 72 | n/a | 'modules': ''' |
|---|
| 73 | n/a | Pure Python modules included in the project. |
|---|
| 74 | n/a | ''', |
|---|
| 75 | n/a | 'extra_files': ''' |
|---|
| 76 | n/a | You can provide extra files/dirs contained in your project. |
|---|
| 77 | n/a | It has to follow the template syntax. XXX add help here. |
|---|
| 78 | n/a | ''', |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | 'home_page': ''' |
|---|
| 81 | n/a | The home page for the project, typically a public Web page. |
|---|
| 82 | n/a | ''', |
|---|
| 83 | n/a | 'trove_license': ''' |
|---|
| 84 | n/a | Optionally you can specify a license. Type a string that identifies a |
|---|
| 85 | n/a | common license, and then you can select a list of license specifiers. |
|---|
| 86 | n/a | ''', |
|---|
| 87 | n/a | 'trove_generic': ''' |
|---|
| 88 | n/a | Optionally, you can set other trove identifiers for things such as the |
|---|
| 89 | n/a | human language, programming language, user interface, etc. |
|---|
| 90 | n/a | ''', |
|---|
| 91 | n/a | 'setup.py found': ''' |
|---|
| 92 | n/a | The setup.py script will be executed to retrieve the metadata. |
|---|
| 93 | n/a | An interactive helper will be run if you answer "n", |
|---|
| 94 | n/a | ''', |
|---|
| 95 | n/a | } |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | PROJECT_MATURITY = ['Development Status :: 1 - Planning', |
|---|
| 98 | n/a | 'Development Status :: 2 - Pre-Alpha', |
|---|
| 99 | n/a | 'Development Status :: 3 - Alpha', |
|---|
| 100 | n/a | 'Development Status :: 4 - Beta', |
|---|
| 101 | n/a | 'Development Status :: 5 - Production/Stable', |
|---|
| 102 | n/a | 'Development Status :: 6 - Mature', |
|---|
| 103 | n/a | 'Development Status :: 7 - Inactive'] |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | # XXX everything needs docstrings and tests (both low-level tests of various |
|---|
| 106 | n/a | # methods and functional tests of running the script) |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | def load_setup(): |
|---|
| 110 | n/a | """run the setup script (i.e the setup.py file) |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | This function load the setup file in all cases (even if it have already |
|---|
| 113 | n/a | been loaded before, because we are monkey patching its setup function with |
|---|
| 114 | n/a | a particular one""" |
|---|
| 115 | n/a | with open("setup.py", "rb") as f: |
|---|
| 116 | n/a | encoding, lines = detect_encoding(f.readline) |
|---|
| 117 | n/a | with open("setup.py", encoding=encoding) as f: |
|---|
| 118 | n/a | imp.load_module("setup", f, "setup.py", (".py", "r", imp.PY_SOURCE)) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def ask_yn(question, default=None, helptext=None): |
|---|
| 122 | n/a | question += ' (y/n)' |
|---|
| 123 | n/a | while True: |
|---|
| 124 | n/a | answer = ask(question, default, helptext, required=True) |
|---|
| 125 | n/a | if answer and answer[0].lower() in ('y', 'n'): |
|---|
| 126 | n/a | return answer[0].lower() |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | logger.error('You must select "Y" or "N".') |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | # XXX use util.ask |
|---|
| 132 | n/a | # FIXME: if prompt ends with '?', don't add ':' |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | def ask(question, default=None, helptext=None, required=True, |
|---|
| 136 | n/a | lengthy=False, multiline=False): |
|---|
| 137 | n/a | prompt = '%s: ' % (question,) |
|---|
| 138 | n/a | if default: |
|---|
| 139 | n/a | prompt = '%s [%s]: ' % (question, default) |
|---|
| 140 | n/a | if default and len(question) + len(default) > 70: |
|---|
| 141 | n/a | prompt = '%s\n [%s]: ' % (question, default) |
|---|
| 142 | n/a | if lengthy or multiline: |
|---|
| 143 | n/a | prompt += '\n > ' |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | if not helptext: |
|---|
| 146 | n/a | helptext = 'No additional help available.' |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | helptext = helptext.strip("\n") |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | while True: |
|---|
| 151 | n/a | line = input(prompt).strip() |
|---|
| 152 | n/a | if line == '?': |
|---|
| 153 | n/a | print('=' * 70) |
|---|
| 154 | n/a | print(helptext) |
|---|
| 155 | n/a | print('=' * 70) |
|---|
| 156 | n/a | continue |
|---|
| 157 | n/a | if default and not line: |
|---|
| 158 | n/a | return default |
|---|
| 159 | n/a | if not line and required: |
|---|
| 160 | n/a | print('*' * 70) |
|---|
| 161 | n/a | print('This value cannot be empty.') |
|---|
| 162 | n/a | print('===========================') |
|---|
| 163 | n/a | if helptext: |
|---|
| 164 | n/a | print(helptext) |
|---|
| 165 | n/a | print('*' * 70) |
|---|
| 166 | n/a | continue |
|---|
| 167 | n/a | return line |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | def convert_yn_to_bool(yn, yes=True, no=False): |
|---|
| 171 | n/a | """Convert a y/yes or n/no to a boolean value.""" |
|---|
| 172 | n/a | if yn.lower().startswith('y'): |
|---|
| 173 | n/a | return yes |
|---|
| 174 | n/a | else: |
|---|
| 175 | n/a | return no |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | def _build_classifiers_dict(classifiers): |
|---|
| 179 | n/a | d = {} |
|---|
| 180 | n/a | for key in classifiers: |
|---|
| 181 | n/a | subdict = d |
|---|
| 182 | n/a | for subkey in key.split(' :: '): |
|---|
| 183 | n/a | if subkey not in subdict: |
|---|
| 184 | n/a | subdict[subkey] = {} |
|---|
| 185 | n/a | subdict = subdict[subkey] |
|---|
| 186 | n/a | return d |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | CLASSIFIERS = _build_classifiers_dict(_CLASSIFIERS_LIST) |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | def _build_licences(classifiers): |
|---|
| 192 | n/a | res = [] |
|---|
| 193 | n/a | for index, item in enumerate(classifiers): |
|---|
| 194 | n/a | if not item.startswith('License :: '): |
|---|
| 195 | n/a | continue |
|---|
| 196 | n/a | res.append((index, item.split(' :: ')[-1].lower())) |
|---|
| 197 | n/a | return res |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | LICENCES = _build_licences(_CLASSIFIERS_LIST) |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | class MainProgram: |
|---|
| 203 | n/a | """Make a project setup configuration file (setup.cfg).""" |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | def __init__(self): |
|---|
| 206 | n/a | self.configparser = None |
|---|
| 207 | n/a | self.classifiers = set() |
|---|
| 208 | n/a | self.data = {'name': '', |
|---|
| 209 | n/a | 'version': '1.0.0', |
|---|
| 210 | n/a | 'classifier': self.classifiers, |
|---|
| 211 | n/a | 'packages': [], |
|---|
| 212 | n/a | 'modules': [], |
|---|
| 213 | n/a | 'platform': [], |
|---|
| 214 | n/a | 'resources': [], |
|---|
| 215 | n/a | 'extra_files': [], |
|---|
| 216 | n/a | 'scripts': [], |
|---|
| 217 | n/a | } |
|---|
| 218 | n/a | self._load_defaults() |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | def __call__(self): |
|---|
| 221 | n/a | setupcfg_defined = False |
|---|
| 222 | n/a | if self.has_setup_py() and self._prompt_user_for_conversion(): |
|---|
| 223 | n/a | setupcfg_defined = self.convert_py_to_cfg() |
|---|
| 224 | n/a | if not setupcfg_defined: |
|---|
| 225 | n/a | self.define_cfg_values() |
|---|
| 226 | n/a | self._write_cfg() |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | def has_setup_py(self): |
|---|
| 229 | n/a | """Test for the existence of a setup.py file.""" |
|---|
| 230 | n/a | return os.path.exists('setup.py') |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | def define_cfg_values(self): |
|---|
| 233 | n/a | self.inspect() |
|---|
| 234 | n/a | self.query_user() |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | def _lookup_option(self, key): |
|---|
| 237 | n/a | if not self.configparser.has_option('DEFAULT', key): |
|---|
| 238 | n/a | return None |
|---|
| 239 | n/a | return self.configparser.get('DEFAULT', key) |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | def _load_defaults(self): |
|---|
| 242 | n/a | # Load default values from a user configuration file |
|---|
| 243 | n/a | self.configparser = RawConfigParser() |
|---|
| 244 | n/a | # TODO replace with section in distutils config file |
|---|
| 245 | n/a | default_cfg = os.path.expanduser(os.path.join('~', _DEFAULT_CFG)) |
|---|
| 246 | n/a | self.configparser.read(default_cfg) |
|---|
| 247 | n/a | self.data['author'] = self._lookup_option('author') |
|---|
| 248 | n/a | self.data['author_email'] = self._lookup_option('author_email') |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | def _prompt_user_for_conversion(self): |
|---|
| 251 | n/a | # Prompt the user about whether they would like to use the setup.py |
|---|
| 252 | n/a | # conversion utility to generate a setup.cfg or generate the setup.cfg |
|---|
| 253 | n/a | # from scratch |
|---|
| 254 | n/a | answer = ask_yn(('A legacy setup.py has been found.\n' |
|---|
| 255 | n/a | 'Would you like to convert it to a setup.cfg?'), |
|---|
| 256 | n/a | default="y", |
|---|
| 257 | n/a | helptext=_helptext['setup.py found']) |
|---|
| 258 | n/a | return convert_yn_to_bool(answer) |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | def _dotted_packages(self, data): |
|---|
| 261 | n/a | packages = sorted(data) |
|---|
| 262 | n/a | modified_pkgs = [] |
|---|
| 263 | n/a | for pkg in packages: |
|---|
| 264 | n/a | pkg = pkg.lstrip('./') |
|---|
| 265 | n/a | pkg = pkg.replace('/', '.') |
|---|
| 266 | n/a | modified_pkgs.append(pkg) |
|---|
| 267 | n/a | return modified_pkgs |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | def _write_cfg(self): |
|---|
| 270 | n/a | if os.path.exists(_FILENAME): |
|---|
| 271 | n/a | if os.path.exists('%s.old' % _FILENAME): |
|---|
| 272 | n/a | message = ("ERROR: %(name)s.old backup exists, please check " |
|---|
| 273 | n/a | "that current %(name)s is correct and remove " |
|---|
| 274 | n/a | "%(name)s.old" % {'name': _FILENAME}) |
|---|
| 275 | n/a | logger.error(message) |
|---|
| 276 | n/a | return |
|---|
| 277 | n/a | shutil.move(_FILENAME, '%s.old' % _FILENAME) |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | with open(_FILENAME, 'w', encoding='utf-8') as fp: |
|---|
| 280 | n/a | fp.write('[metadata]\n') |
|---|
| 281 | n/a | # TODO use metadata module instead of hard-coding field-specific |
|---|
| 282 | n/a | # behavior here |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | # simple string entries |
|---|
| 285 | n/a | for name in ('name', 'version', 'summary', 'download_url'): |
|---|
| 286 | n/a | fp.write('%s = %s\n' % (name, self.data.get(name, 'UNKNOWN'))) |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | # optional string entries |
|---|
| 289 | n/a | if 'keywords' in self.data and self.data['keywords']: |
|---|
| 290 | n/a | # XXX shoud use comma to separate, not space |
|---|
| 291 | n/a | fp.write('keywords = %s\n' % ' '.join(self.data['keywords'])) |
|---|
| 292 | n/a | for name in ('home_page', 'author', 'author_email', |
|---|
| 293 | n/a | 'maintainer', 'maintainer_email', 'description-file'): |
|---|
| 294 | n/a | if name in self.data and self.data[name]: |
|---|
| 295 | n/a | fp.write('%s = %s\n' % (name, self.data[name])) |
|---|
| 296 | n/a | if 'description' in self.data: |
|---|
| 297 | n/a | fp.write( |
|---|
| 298 | n/a | 'description = %s\n' |
|---|
| 299 | n/a | % '\n |'.join(self.data['description'].split('\n'))) |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | # multiple use string entries |
|---|
| 302 | n/a | for name in ('platform', 'supported-platform', 'classifier', |
|---|
| 303 | n/a | 'requires-dist', 'provides-dist', 'obsoletes-dist', |
|---|
| 304 | n/a | 'requires-external'): |
|---|
| 305 | n/a | if not(name in self.data and self.data[name]): |
|---|
| 306 | n/a | continue |
|---|
| 307 | n/a | fp.write('%s = ' % name) |
|---|
| 308 | n/a | fp.write(''.join(' %s\n' % val |
|---|
| 309 | n/a | for val in self.data[name]).lstrip()) |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | fp.write('\n[files]\n') |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | for name in ('packages', 'modules', 'scripts', 'extra_files'): |
|---|
| 314 | n/a | if not(name in self.data and self.data[name]): |
|---|
| 315 | n/a | continue |
|---|
| 316 | n/a | fp.write('%s = %s\n' |
|---|
| 317 | n/a | % (name, '\n '.join(self.data[name]).strip())) |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | if self.data.get('package_data'): |
|---|
| 320 | n/a | fp.write('package_data =\n') |
|---|
| 321 | n/a | for pkg, spec in sorted(self.data['package_data'].items()): |
|---|
| 322 | n/a | # put one spec per line, indented under the package name |
|---|
| 323 | n/a | indent = ' ' * (len(pkg) + 7) |
|---|
| 324 | n/a | spec = ('\n' + indent).join(spec) |
|---|
| 325 | n/a | fp.write(' %s = %s\n' % (pkg, spec)) |
|---|
| 326 | n/a | fp.write('\n') |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | if self.data.get('resources'): |
|---|
| 329 | n/a | fp.write('resources =\n') |
|---|
| 330 | n/a | for src, dest in self.data['resources']: |
|---|
| 331 | n/a | fp.write(' %s = %s\n' % (src, dest)) |
|---|
| 332 | n/a | fp.write('\n') |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | os.chmod(_FILENAME, 0o644) |
|---|
| 335 | n/a | logger.info('Wrote "%s".' % _FILENAME) |
|---|
| 336 | n/a | |
|---|
| 337 | n/a | def convert_py_to_cfg(self): |
|---|
| 338 | n/a | """Generate a setup.cfg from an existing setup.py. |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | It only exports the distutils metadata (setuptools specific metadata |
|---|
| 341 | n/a | is not currently supported). |
|---|
| 342 | n/a | """ |
|---|
| 343 | n/a | data = self.data |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | def setup_mock(**attrs): |
|---|
| 346 | n/a | """Mock the setup(**attrs) in order to retrieve metadata.""" |
|---|
| 347 | n/a | |
|---|
| 348 | n/a | # TODO use config and metadata instead of Distribution |
|---|
| 349 | n/a | from distutils.dist import Distribution |
|---|
| 350 | n/a | dist = Distribution(attrs) |
|---|
| 351 | n/a | dist.parse_config_files() |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | # 1. retrieve metadata fields that are quite similar in |
|---|
| 354 | n/a | # PEP 314 and PEP 345 |
|---|
| 355 | n/a | labels = (('name',) * 2, |
|---|
| 356 | n/a | ('version',) * 2, |
|---|
| 357 | n/a | ('author',) * 2, |
|---|
| 358 | n/a | ('author_email',) * 2, |
|---|
| 359 | n/a | ('maintainer',) * 2, |
|---|
| 360 | n/a | ('maintainer_email',) * 2, |
|---|
| 361 | n/a | ('description', 'summary'), |
|---|
| 362 | n/a | ('long_description', 'description'), |
|---|
| 363 | n/a | ('url', 'home_page'), |
|---|
| 364 | n/a | ('platforms', 'platform'), |
|---|
| 365 | n/a | ('provides', 'provides-dist'), |
|---|
| 366 | n/a | ('obsoletes', 'obsoletes-dist'), |
|---|
| 367 | n/a | ('requires', 'requires-dist')) |
|---|
| 368 | n/a | |
|---|
| 369 | n/a | get = lambda lab: getattr(dist.metadata, lab.replace('-', '_')) |
|---|
| 370 | n/a | data.update((new, get(old)) for old, new in labels if get(old)) |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | # 2. retrieve data that requires special processing |
|---|
| 373 | n/a | data['classifier'].update(dist.get_classifiers() or []) |
|---|
| 374 | n/a | data['scripts'].extend(dist.scripts or []) |
|---|
| 375 | n/a | data['packages'].extend(dist.packages or []) |
|---|
| 376 | n/a | data['modules'].extend(dist.py_modules or []) |
|---|
| 377 | n/a | # 2.1 data_files -> resources |
|---|
| 378 | n/a | if dist.data_files: |
|---|
| 379 | n/a | if (len(dist.data_files) < 2 or |
|---|
| 380 | n/a | isinstance(dist.data_files[1], str)): |
|---|
| 381 | n/a | dist.data_files = [('', dist.data_files)] |
|---|
| 382 | n/a | # add tokens in the destination paths |
|---|
| 383 | n/a | vars = {'distribution.name': data['name']} |
|---|
| 384 | n/a | path_tokens = sysconfig.get_paths(vars=vars).items() |
|---|
| 385 | n/a | # sort tokens to use the longest one first |
|---|
| 386 | n/a | path_tokens = sorted(path_tokens, key=lambda x: len(x[1])) |
|---|
| 387 | n/a | for dest, srcs in (dist.data_files or []): |
|---|
| 388 | n/a | dest = os.path.join(sys.prefix, dest) |
|---|
| 389 | n/a | dest = dest.replace(os.path.sep, '/') |
|---|
| 390 | n/a | for tok, path in path_tokens: |
|---|
| 391 | n/a | path = path.replace(os.path.sep, '/') |
|---|
| 392 | n/a | if not dest.startswith(path): |
|---|
| 393 | n/a | continue |
|---|
| 394 | n/a | |
|---|
| 395 | n/a | dest = ('{%s}' % tok) + dest[len(path):] |
|---|
| 396 | n/a | files = [('/ '.join(src.rsplit('/', 1)), dest) |
|---|
| 397 | n/a | for src in srcs] |
|---|
| 398 | n/a | data['resources'].extend(files) |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | # 2.2 package_data |
|---|
| 401 | n/a | data['package_data'] = dist.package_data.copy() |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | # Use README file if its content is the desciption |
|---|
| 404 | n/a | if "description" in data: |
|---|
| 405 | n/a | ref = md5(re.sub('\s', '', |
|---|
| 406 | n/a | self.data['description']).lower().encode()) |
|---|
| 407 | n/a | ref = ref.digest() |
|---|
| 408 | n/a | for readme in glob.glob('README*'): |
|---|
| 409 | n/a | with open(readme, encoding='utf-8') as fp: |
|---|
| 410 | n/a | contents = fp.read() |
|---|
| 411 | n/a | contents = re.sub('\s', '', contents.lower()).encode() |
|---|
| 412 | n/a | val = md5(contents).digest() |
|---|
| 413 | n/a | if val == ref: |
|---|
| 414 | n/a | del data['description'] |
|---|
| 415 | n/a | data['description-file'] = readme |
|---|
| 416 | n/a | break |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | # apply monkey patch to distutils (v1) and setuptools (if needed) |
|---|
| 419 | n/a | # (abort the feature if distutils v1 has been killed) |
|---|
| 420 | n/a | try: |
|---|
| 421 | n/a | from distutils import core |
|---|
| 422 | n/a | core.setup # make sure it's not d2 maskerading as d1 |
|---|
| 423 | n/a | except (ImportError, AttributeError): |
|---|
| 424 | n/a | return |
|---|
| 425 | n/a | saved_setups = [(core, core.setup)] |
|---|
| 426 | n/a | core.setup = setup_mock |
|---|
| 427 | n/a | try: |
|---|
| 428 | n/a | import setuptools |
|---|
| 429 | n/a | except ImportError: |
|---|
| 430 | n/a | pass |
|---|
| 431 | n/a | else: |
|---|
| 432 | n/a | saved_setups.append((setuptools, setuptools.setup)) |
|---|
| 433 | n/a | setuptools.setup = setup_mock |
|---|
| 434 | n/a | # get metadata by executing the setup.py with the patched setup(...) |
|---|
| 435 | n/a | success = False # for python < 2.4 |
|---|
| 436 | n/a | try: |
|---|
| 437 | n/a | load_setup() |
|---|
| 438 | n/a | success = True |
|---|
| 439 | n/a | finally: # revert monkey patches |
|---|
| 440 | n/a | for patched_module, original_setup in saved_setups: |
|---|
| 441 | n/a | patched_module.setup = original_setup |
|---|
| 442 | n/a | if not self.data: |
|---|
| 443 | n/a | raise ValueError('Unable to load metadata from setup.py') |
|---|
| 444 | n/a | return success |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | def inspect(self): |
|---|
| 447 | n/a | """Inspect the current working diretory for a name and version. |
|---|
| 448 | n/a | |
|---|
| 449 | n/a | This information is harvested in where the directory is named |
|---|
| 450 | n/a | like [name]-[version]. |
|---|
| 451 | n/a | """ |
|---|
| 452 | n/a | dir_name = os.path.basename(os.getcwd()) |
|---|
| 453 | n/a | self.data['name'] = dir_name |
|---|
| 454 | n/a | match = re.match(r'(.*)-(\d.+)', dir_name) |
|---|
| 455 | n/a | if match: |
|---|
| 456 | n/a | self.data['name'] = match.group(1) |
|---|
| 457 | n/a | self.data['version'] = match.group(2) |
|---|
| 458 | n/a | # TODO needs testing! |
|---|
| 459 | n/a | if not is_valid_version(self.data['version']): |
|---|
| 460 | n/a | msg = "Invalid version discovered: %s" % self.data['version'] |
|---|
| 461 | n/a | raise ValueError(msg) |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | def query_user(self): |
|---|
| 464 | n/a | self.data['name'] = ask('Project name', self.data['name'], |
|---|
| 465 | n/a | _helptext['name']) |
|---|
| 466 | n/a | |
|---|
| 467 | n/a | self.data['version'] = ask('Current version number', |
|---|
| 468 | n/a | self.data.get('version'), _helptext['version']) |
|---|
| 469 | n/a | self.data['summary'] = ask('Project description summary', |
|---|
| 470 | n/a | self.data.get('summary'), _helptext['summary'], |
|---|
| 471 | n/a | lengthy=True) |
|---|
| 472 | n/a | self.data['author'] = ask('Author name', |
|---|
| 473 | n/a | self.data.get('author'), _helptext['author']) |
|---|
| 474 | n/a | self.data['author_email'] = ask('Author email address', |
|---|
| 475 | n/a | self.data.get('author_email'), _helptext['author_email']) |
|---|
| 476 | n/a | self.data['home_page'] = ask('Project home page', |
|---|
| 477 | n/a | self.data.get('home_page'), _helptext['home_page'], |
|---|
| 478 | n/a | required=False) |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | if ask_yn('Do you want me to automatically build the file list ' |
|---|
| 481 | n/a | 'with everything I can find in the current directory? ' |
|---|
| 482 | n/a | 'If you say no, you will have to define them manually.') == 'y': |
|---|
| 483 | n/a | self._find_files() |
|---|
| 484 | n/a | else: |
|---|
| 485 | n/a | while ask_yn('Do you want to add a single module?' |
|---|
| 486 | n/a | ' (you will be able to add full packages next)', |
|---|
| 487 | n/a | helptext=_helptext['modules']) == 'y': |
|---|
| 488 | n/a | self._set_multi('Module name', 'modules') |
|---|
| 489 | n/a | |
|---|
| 490 | n/a | while ask_yn('Do you want to add a package?', |
|---|
| 491 | n/a | helptext=_helptext['packages']) == 'y': |
|---|
| 492 | n/a | self._set_multi('Package name', 'packages') |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | while ask_yn('Do you want to add an extra file?', |
|---|
| 495 | n/a | helptext=_helptext['extra_files']) == 'y': |
|---|
| 496 | n/a | self._set_multi('Extra file/dir name', 'extra_files') |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | if ask_yn('Do you want to set Trove classifiers?', |
|---|
| 499 | n/a | helptext=_helptext['do_classifier']) == 'y': |
|---|
| 500 | n/a | self.set_classifier() |
|---|
| 501 | n/a | |
|---|
| 502 | n/a | def _find_files(self): |
|---|
| 503 | n/a | # we are looking for python modules and packages, |
|---|
| 504 | n/a | # other stuff are added as regular files |
|---|
| 505 | n/a | pkgs = self.data['packages'] |
|---|
| 506 | n/a | modules = self.data['modules'] |
|---|
| 507 | n/a | extra_files = self.data['extra_files'] |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | def is_package(path): |
|---|
| 510 | n/a | return os.path.exists(os.path.join(path, '__init__.py')) |
|---|
| 511 | n/a | |
|---|
| 512 | n/a | curdir = os.getcwd() |
|---|
| 513 | n/a | scanned = [] |
|---|
| 514 | n/a | _pref = ['lib', 'include', 'dist', 'build', '.', '~'] |
|---|
| 515 | n/a | _suf = ['.pyc'] |
|---|
| 516 | n/a | |
|---|
| 517 | n/a | def to_skip(path): |
|---|
| 518 | n/a | path = relative(path) |
|---|
| 519 | n/a | |
|---|
| 520 | n/a | for pref in _pref: |
|---|
| 521 | n/a | if path.startswith(pref): |
|---|
| 522 | n/a | return True |
|---|
| 523 | n/a | |
|---|
| 524 | n/a | for suf in _suf: |
|---|
| 525 | n/a | if path.endswith(suf): |
|---|
| 526 | n/a | return True |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | return False |
|---|
| 529 | n/a | |
|---|
| 530 | n/a | def relative(path): |
|---|
| 531 | n/a | return path[len(curdir) + 1:] |
|---|
| 532 | n/a | |
|---|
| 533 | n/a | def dotted(path): |
|---|
| 534 | n/a | res = relative(path).replace(os.path.sep, '.') |
|---|
| 535 | n/a | if res.endswith('.py'): |
|---|
| 536 | n/a | res = res[:-len('.py')] |
|---|
| 537 | n/a | return res |
|---|
| 538 | n/a | |
|---|
| 539 | n/a | # first pass: packages |
|---|
| 540 | n/a | for root, dirs, files in os.walk(curdir): |
|---|
| 541 | n/a | if to_skip(root): |
|---|
| 542 | n/a | continue |
|---|
| 543 | n/a | for dir_ in sorted(dirs): |
|---|
| 544 | n/a | if to_skip(dir_): |
|---|
| 545 | n/a | continue |
|---|
| 546 | n/a | fullpath = os.path.join(root, dir_) |
|---|
| 547 | n/a | dotted_name = dotted(fullpath) |
|---|
| 548 | n/a | if is_package(fullpath) and dotted_name not in pkgs: |
|---|
| 549 | n/a | pkgs.append(dotted_name) |
|---|
| 550 | n/a | scanned.append(fullpath) |
|---|
| 551 | n/a | |
|---|
| 552 | n/a | # modules and extra files |
|---|
| 553 | n/a | for root, dirs, files in os.walk(curdir): |
|---|
| 554 | n/a | if to_skip(root): |
|---|
| 555 | n/a | continue |
|---|
| 556 | n/a | |
|---|
| 557 | n/a | if any(root.startswith(path) for path in scanned): |
|---|
| 558 | n/a | continue |
|---|
| 559 | n/a | |
|---|
| 560 | n/a | for file in sorted(files): |
|---|
| 561 | n/a | fullpath = os.path.join(root, file) |
|---|
| 562 | n/a | if to_skip(fullpath): |
|---|
| 563 | n/a | continue |
|---|
| 564 | n/a | # single module? |
|---|
| 565 | n/a | if os.path.splitext(file)[-1] == '.py': |
|---|
| 566 | n/a | modules.append(dotted(fullpath)) |
|---|
| 567 | n/a | else: |
|---|
| 568 | n/a | extra_files.append(relative(fullpath)) |
|---|
| 569 | n/a | |
|---|
| 570 | n/a | def _set_multi(self, question, name): |
|---|
| 571 | n/a | existing_values = self.data[name] |
|---|
| 572 | n/a | value = ask(question, helptext=_helptext[name]).strip() |
|---|
| 573 | n/a | if value not in existing_values: |
|---|
| 574 | n/a | existing_values.append(value) |
|---|
| 575 | n/a | |
|---|
| 576 | n/a | def set_classifier(self): |
|---|
| 577 | n/a | self.set_maturity_status(self.classifiers) |
|---|
| 578 | n/a | self.set_license(self.classifiers) |
|---|
| 579 | n/a | self.set_other_classifier(self.classifiers) |
|---|
| 580 | n/a | |
|---|
| 581 | n/a | def set_other_classifier(self, classifiers): |
|---|
| 582 | n/a | if ask_yn('Do you want to set other trove identifiers?', 'n', |
|---|
| 583 | n/a | _helptext['trove_generic']) != 'y': |
|---|
| 584 | n/a | return |
|---|
| 585 | n/a | self.walk_classifiers(classifiers, [CLASSIFIERS], '') |
|---|
| 586 | n/a | |
|---|
| 587 | n/a | def walk_classifiers(self, classifiers, trovepath, desc): |
|---|
| 588 | n/a | trove = trovepath[-1] |
|---|
| 589 | n/a | |
|---|
| 590 | n/a | if not trove: |
|---|
| 591 | n/a | return |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | for key in sorted(trove): |
|---|
| 594 | n/a | if len(trove[key]) == 0: |
|---|
| 595 | n/a | if ask_yn('Add "%s"' % desc[4:] + ' :: ' + key, 'n') == 'y': |
|---|
| 596 | n/a | classifiers.add(desc[4:] + ' :: ' + key) |
|---|
| 597 | n/a | continue |
|---|
| 598 | n/a | |
|---|
| 599 | n/a | if ask_yn('Do you want to set items under\n "%s" (%d sub-items)?' |
|---|
| 600 | n/a | % (key, len(trove[key])), 'n', |
|---|
| 601 | n/a | _helptext['trove_generic']) == 'y': |
|---|
| 602 | n/a | self.walk_classifiers(classifiers, trovepath + [trove[key]], |
|---|
| 603 | n/a | desc + ' :: ' + key) |
|---|
| 604 | n/a | |
|---|
| 605 | n/a | def set_license(self, classifiers): |
|---|
| 606 | n/a | while True: |
|---|
| 607 | n/a | license = ask('What license do you use?', |
|---|
| 608 | n/a | helptext=_helptext['trove_license'], required=False) |
|---|
| 609 | n/a | if not license: |
|---|
| 610 | n/a | return |
|---|
| 611 | n/a | |
|---|
| 612 | n/a | license_words = license.lower().split(' ') |
|---|
| 613 | n/a | found_list = [] |
|---|
| 614 | n/a | |
|---|
| 615 | n/a | for index, licence in LICENCES: |
|---|
| 616 | n/a | for word in license_words: |
|---|
| 617 | n/a | if word in licence: |
|---|
| 618 | n/a | found_list.append(index) |
|---|
| 619 | n/a | break |
|---|
| 620 | n/a | |
|---|
| 621 | n/a | if len(found_list) == 0: |
|---|
| 622 | n/a | logger.error('Could not find a matching license for "%s"' % |
|---|
| 623 | n/a | license) |
|---|
| 624 | n/a | continue |
|---|
| 625 | n/a | |
|---|
| 626 | n/a | question = 'Matching licenses:\n\n' |
|---|
| 627 | n/a | |
|---|
| 628 | n/a | for index, list_index in enumerate(found_list): |
|---|
| 629 | n/a | question += ' %s) %s\n' % (index + 1, |
|---|
| 630 | n/a | _CLASSIFIERS_LIST[list_index]) |
|---|
| 631 | n/a | |
|---|
| 632 | n/a | question += ('\nType the number of the license you wish to use or ' |
|---|
| 633 | n/a | '? to try again:') |
|---|
| 634 | n/a | choice = ask(question, required=False) |
|---|
| 635 | n/a | |
|---|
| 636 | n/a | if choice == '?': |
|---|
| 637 | n/a | continue |
|---|
| 638 | n/a | if choice == '': |
|---|
| 639 | n/a | return |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | try: |
|---|
| 642 | n/a | index = found_list[int(choice) - 1] |
|---|
| 643 | n/a | except ValueError: |
|---|
| 644 | n/a | logger.error( |
|---|
| 645 | n/a | "Invalid selection, type a number from the list above.") |
|---|
| 646 | n/a | |
|---|
| 647 | n/a | classifiers.add(_CLASSIFIERS_LIST[index]) |
|---|
| 648 | n/a | |
|---|
| 649 | n/a | def set_maturity_status(self, classifiers): |
|---|
| 650 | n/a | maturity_name = lambda mat: mat.split('- ')[-1] |
|---|
| 651 | n/a | maturity_question = '''\ |
|---|
| 652 | n/a | Please select the project status: |
|---|
| 653 | n/a | |
|---|
| 654 | n/a | %s |
|---|
| 655 | n/a | |
|---|
| 656 | n/a | Status''' % '\n'.join('%s - %s' % (i, maturity_name(n)) |
|---|
| 657 | n/a | for i, n in enumerate(PROJECT_MATURITY)) |
|---|
| 658 | n/a | while True: |
|---|
| 659 | n/a | choice = ask(dedent(maturity_question), required=False) |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | if choice: |
|---|
| 662 | n/a | try: |
|---|
| 663 | n/a | choice = int(choice) - 1 |
|---|
| 664 | n/a | key = PROJECT_MATURITY[choice] |
|---|
| 665 | n/a | classifiers.add(key) |
|---|
| 666 | n/a | return |
|---|
| 667 | n/a | except (IndexError, ValueError): |
|---|
| 668 | n/a | logger.error( |
|---|
| 669 | n/a | "Invalid selection, type a single digit number.") |
|---|
| 670 | n/a | |
|---|
| 671 | n/a | |
|---|
| 672 | n/a | def main(): |
|---|
| 673 | n/a | """Main entry point.""" |
|---|
| 674 | n/a | program = MainProgram() |
|---|
| 675 | n/a | # # uncomment when implemented |
|---|
| 676 | n/a | # if not program.load_existing_setup_script(): |
|---|
| 677 | n/a | # program.inspect_directory() |
|---|
| 678 | n/a | # program.query_user() |
|---|
| 679 | n/a | # program.update_config_file() |
|---|
| 680 | n/a | # program.write_setup_script() |
|---|
| 681 | n/a | # packaging.util.cfg_to_args() |
|---|
| 682 | n/a | program() |
|---|