DEPRECATED -- Rewritten and moved to https://github.com/NiklasRosenstein/shut/. 🌊 Shore is a distribution and release management tool for pure Python packages.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

838 lines
29 KiB

  1. # -*- coding: utf8 -*-
  2. # Copyright (c) 2019 Niklas Rosenstein
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to
  6. # deal in the Software without restriction, including without limitation the
  7. # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. # sell copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. from nr.databind.core import Field, Struct, FieldName, Collect
  22. from nr.databind.json import JsonDefault, JsonSerializer, JsonMixin
  23. from nr.interface import implements
  24. from nr.pylang.utils import classdef
  25. from nr.stream import Stream
  26. from shore.core.plugins import (
  27. IBasePlugin,
  28. IBuildTarget,
  29. IPackagePlugin,
  30. IPublishTarget,
  31. IMonorepoPlugin,
  32. load_plugin,
  33. PluginNotFound)
  34. from shore.mapper import mapper
  35. from shore.util.ast import load_module_members
  36. from shore.util.version import bump_version, Version
  37. from typing import Any, Callable, Dict, Iterable, Optional, List, Type, Union
  38. import ast
  39. import collections
  40. import copy
  41. import logging
  42. import os
  43. import re
  44. import shlex
  45. import yaml
  46. __all__ = ['Package', 'Monorepo']
  47. logger = logging.getLogger(__name__)
  48. class VersionSelector(object):
  49. """ A version selector string. """
  50. def __init__(self, selector):
  51. if isinstance(selector, VersionSelector):
  52. selector = selector._string
  53. self._string = selector.strip()
  54. def __str__(self):
  55. return str(self._string)
  56. def __repr__(self):
  57. return 'VersionSelector({!r})'.format(self._string)
  58. def __eq__(self, other):
  59. if type(self) == type(other):
  60. return self._string == other._string
  61. return False
  62. def __ne__(self, other):
  63. return not (self == other)
  64. def to_setuptools(self): # type: () -> str
  65. """ Converts the version selector to a string that Setuptools/Pip can
  66. understand by expanding the `~` and `^` range selectors.
  67. Given a version number X.Y.Z, the selectors will be expanded as follows:
  68. - `^X.Y.Z` -> `>=X.Y.Z,<X+1.0.0`
  69. - `~X.Y.Z` -> `>=X.Y.Z,<X.Y+1.0`
  70. - `X.Y.Z -> ==X.Y.Z`
  71. """
  72. # Poor-mans test if this looks like the form 'X.Y.Z' without anything around it.
  73. if not ',' in self._string and self._string[0].isdigit():
  74. return '==' + self._string
  75. regex = r'[~^](\d+\.\d+(\.\d+)?[.\-\w]*)'
  76. def sub(match):
  77. index = {'^': 0, '~': 1}[match.group(0)[0]]
  78. max_version = match.group(1).split('.')[:3]
  79. if len(max_version) == 2:
  80. max_version.append('0')
  81. if '-' in max_version[-1]:
  82. max_version[-1] = max_version[-1].partition('-')[0]
  83. max_version[index] = str(int(max_version[index]) + 1)
  84. for i in range(index+1, 3):
  85. max_version[i] = '0'
  86. return '>={},<{}'.format(match.group(1), '.'.join(max_version))
  87. return re.sub(regex, sub, self._string)
  88. def is_semver_selector(self) -> bool:
  89. return self._string and self._string[0] in '^~' and ',' not in self._string
  90. def matches(self, version: Union[Version, str]) -> bool:
  91. if not self.is_semver_selector():
  92. # TODO (@NiklasRosenstein): Match setuptools version selectors.
  93. return False
  94. min_version = Version(self._string[1:])
  95. if self._string[0] == '^':
  96. max_version = bump_version(min_version, 'major')
  97. elif self._string[0] == '~':
  98. max_version = bump_version(min_version, 'minor')
  99. else:
  100. raise RuntimeError('invalid semver selector string {!r}'.format(self._string))
  101. return min_version <= Version(version) < max_version
  102. VersionSelector.ANY = VersionSelector('*')
  103. @JsonSerializer(deserialize='_deserialize')
  104. class Requirement(object):
  105. """ A requirement is a combination of a package name and a version selector.
  106. """
  107. def __init__(self, package, version): # type: (str, VersionSelector)
  108. if not isinstance(package, str):
  109. raise TypeError('expected str for package_name')
  110. if not isinstance(version, VersionSelector):
  111. raise TypeError('expected VersionSelector for version')
  112. self.package = package
  113. self.version = version
  114. def __str__(self):
  115. if self.version == VersionSelector.ANY:
  116. return self.package
  117. return '{} {}'.format(self.package, self.version)
  118. def __repr__(self):
  119. return repr(str(self))#'Requirement({!r})'.format(str(self))
  120. @classmethod
  121. def parse(cls, requirement_string):
  122. match = re.match(r'^\s*([^\s]+)(?:\s+(.+))?$', requirement_string)
  123. if not match:
  124. raise ValueError('invalid requirement: {!r}'.format(requirement_string))
  125. package, version = match.groups()
  126. return cls(package, VersionSelector(version or VersionSelector.ANY))
  127. def to_setuptools(self): # type: () -> str
  128. if self.version == VersionSelector.ANY:
  129. return self.package
  130. return '{} {}'.format(self.package, self.version.to_setuptools())
  131. @classmethod
  132. def _deserialize(cls, mapper, node):
  133. if not isinstance(node.value, str):
  134. raise node.type_error()
  135. return Requirement.parse(node.value)
  136. @JsonSerializer(deserialize='_deserialize')
  137. class Requirements(object):
  138. """ Represents package requirements, consisting of a #RequirementsList *any*
  139. that is comprised of requirements that always need to be present, and
  140. additional #RequirementsList#s in *platforms* that depend on the platform
  141. or environment (eg. `linux`, `win32` or `test` may be the platform keys).
  142. Additionally, the dependency on `python` is stored as the extra *python*
  143. attribute.
  144. This class is deserialized the same that it is represented in memory.
  145. Example:
  146. ```yaml
  147. - python ^2.7|^3.4
  148. - nr.interface ^1.0.0
  149. - test:
  150. - pytest
  151. - PyYAML
  152. ```
  153. Results in a #Requirements object like
  154. ```
  155. Requirements(python=VersionSelector('^2.7|^3.4'), required=[
  156. Requirement('nr.interface ^1.0.0')], test=[Requirement('pytest'),
  157. Requirement('PyYAML')], platforms={})
  158. ```
  159. Attributes:
  160. python (Optional[VersionSelector]): A selector for the Python version.
  161. required (List[Requirement]): A list of requirements that always need
  162. to be installed for a package, no matter the environment.
  163. test (List[Requirement]): A list of requirements that need to be installed
  164. for testing.
  165. platforms (Dict[str, Requirements]): A mapping of platform names to
  166. the requirements that need to be installed in that environment.
  167. Environments are tested against `sys.platform` in the rendered setup
  168. file.
  169. """
  170. classdef.repr('python,required,platforms')
  171. def __init__(self):
  172. self.python = None
  173. self.required = []
  174. self.platforms = {}
  175. def __bool__(self):
  176. return bool(self.python or self.required or self.platforms)
  177. @classmethod
  178. def _deserialize(cls, mapper, node):
  179. deserialize_type = [(Requirement, dict)]
  180. items = mapper.deserialize_node(node.replace(datatype=deserialize_type))
  181. self = node.datatype.cls()
  182. for index, item in enumerate(items):
  183. self._extract_from_item(mapper, node.make_child(index, None, item))
  184. return self
  185. def _extract_from_item(self, mapper, node):
  186. item = node.value
  187. if isinstance(item, Requirement):
  188. if item.package == 'python':
  189. self.python = item.version
  190. else:
  191. self.required.append(item)
  192. elif isinstance(item, dict):
  193. if len(item) != 1:
  194. raise ValueError('expected only a single key in requirements list')
  195. for key, value in item.items():
  196. value = mapper.deserialize_node(node.make_child(key, [Requirement], value))
  197. if key in self.platforms:
  198. self.platforms[key].extend(value)
  199. else:
  200. self.platforms[key] = value
  201. class RootRequirements(Requirements):
  202. classdef.repr(Requirements.__repr_properties__ + ['test', 'extra',])
  203. def __init__(self):
  204. super(RootRequirements, self).__init__()
  205. self.test = None
  206. self.extra = {}
  207. def _extract_from_item(self, mapper, node):
  208. item = node.value
  209. if isinstance(item, dict) and len(item) == 1 and 'extra' in item:
  210. values_node = node.make_child('extra', None, item['extra'])
  211. for key, value in item['extra'].items():
  212. self.extra[key] = mapper.deserialize_node(values_node.make_child(key, Requirements, value))
  213. elif isinstance(item, dict) and len(item) == 1 and 'test' in item:
  214. test_node = node.make_child('test', Requirements, item['test'])
  215. self.test = mapper.deserialize_node(test_node)
  216. else:
  217. super(RootRequirements, self)._extract_from_item(mapper, node)
  218. @JsonSerializer(deserialize='_deserialize')
  219. class Author(Struct):
  220. name = Field(str)
  221. email = Field(str)
  222. AUTHOR_EMAIL_REGEX = re.compile(r'([^<]+)<([^>]+)>')
  223. def __str__(self):
  224. return '{} <{}>'.format(self.name, self.email)
  225. @classmethod
  226. def _deserialize(cls, mapper, node):
  227. if isinstance(node.value, str):
  228. match = Author.AUTHOR_EMAIL_REGEX.match(node.value)
  229. if match:
  230. author = match.group(1).strip()
  231. email = match.group(2).strip()
  232. return Author(author, email)
  233. raise NotImplementedError
  234. @JsonSerializer(deserialize='_deserialize')
  235. class Datafile(Struct):
  236. """ Represents an entry in the #Package.datafiles configuration. Can be
  237. deserialized from a JSON-like object or a string formatted as
  238. `source:target,includepattern,!excludepattern`. """
  239. source = Field(str)
  240. target = Field(str, default='.')
  241. include = Field([str])
  242. exclude = Field([str])
  243. @classmethod
  244. def _deserialize(cls, mapper, node):
  245. if isinstance(node.value, str):
  246. left, patterns = node.value.partition(',')[::2]
  247. if ':' in left:
  248. source, target = left.partition(':')[::2]
  249. else:
  250. source, target = left, '.'
  251. if not source or not target:
  252. raise ValueError('invalid DataFile spec: {!r}'.format(node.value))
  253. include = []
  254. exclude = []
  255. for pattern in patterns.split(','):
  256. (exclude if pattern.startswith('!') else include).append(pattern.lstrip('!'))
  257. return Datafile(source, target, include, exclude)
  258. raise NotImplementedError
  259. @JsonSerializer(deserialize='_deserialize')
  260. class PluginConfig(Struct):
  261. name = Field(str)
  262. plugin = Field(IBasePlugin)
  263. @property
  264. def is_package_plugin(self) -> bool:
  265. return IPackagePlugin.provided_by(self.plugin)
  266. @property
  267. def is_monorepo_plugin(self) -> bool:
  268. return IMonorepoPlugin.provided_by(self.plugin)
  269. def get_checks(self, subject: 'BaseObject'):
  270. if isinstance(subject, Package) and self.is_package_plugin:
  271. logger.debug('getting package checks for plugin {}'.format(self.name))
  272. return self.plugin.check_package(subject)
  273. if isinstance(subject, Monorepo) and self.is_monorepo_plugin:
  274. logger.debug('getting monorepo checks for plugin {}'.format(self.name))
  275. return self.plugin.check_monorepo(subject)
  276. logger.debug('skipping plugin {}'.format(self.name))
  277. return ()
  278. def get_files(self, subject: 'BaseObject'):
  279. if isinstance(subject, Package) and self.is_package_plugin:
  280. logger.debug('getting package files for plugin {}'.format(self.name))
  281. return self.plugin.get_package_files(subject)
  282. if isinstance(subject, Monorepo) and self.is_monorepo_plugin:
  283. logger.debug('getting monorepo files for plugin {}'.format(self.name))
  284. return self.plugin.get_monorepo_files(subject)
  285. logger.debug('skipping plugin {}'.format(self.name))
  286. return ()
  287. def get_version_refs(self, subject: 'BaseObject'):
  288. if isinstance(subject, Package) and self.is_package_plugin:
  289. logger.debug('getting package version refs for plugin {}'.format(self.name))
  290. return self.plugin.get_package_version_refs(subject)
  291. if isinstance(subject, Monorepo) and self.is_monorepo_plugin:
  292. logger.debug('getting monorepo version refs for plugin {}'.format(self.name))
  293. return self.plugin.get_monorepo_version_refs(subject)
  294. logger.debug('skipping plugin {}'.format(self.name))
  295. return ()
  296. def get_build_targets(self, subject: 'BaseObject'):
  297. if isinstance(subject, Package) and self.is_package_plugin:
  298. logger.debug('getting package builders for plugin {}'.format(self.name))
  299. return self.plugin.get_package_build_targets(subject)
  300. logger.debug('skipping plugin {}'.format(self.name))
  301. return ()
  302. def get_publish_targets(self, subject: 'BaseObject'):
  303. if isinstance(subject, Package) and self.is_package_plugin:
  304. logger.debug('getting package publishers for plugin {}'.format(self.name))
  305. return self.plugin.get_package_publish_targets(subject)
  306. logger.debug('skipping plugin {}'.format(self.name))
  307. return ()
  308. @classmethod
  309. def _deserialize(cls, mapper, node):
  310. if isinstance(node.value, str):
  311. plugin_name = node.value
  312. config = None
  313. elif isinstance(node.value, dict):
  314. if 'type' not in node.value:
  315. node.value_error('missing "type" key')
  316. config = node.value.copy()
  317. plugin_name = config.pop('type')
  318. else:
  319. raise TypeError('expected str or dict')
  320. try:
  321. plugin_cls = load_plugin(plugin_name)
  322. except PluginNotFound as exc:
  323. raise ValueError('plugin "{}" not found'.format(exc))
  324. if plugin_cls.Config is not None and config is not None:
  325. config = mapper.deserialize_node(node.make_child(plugin_name, plugin_cls.Config, config))
  326. elif plugin_cls.Config is None and config:
  327. raise TypeError('plugin {} expects no configuration'.format(plugin_name))
  328. else:
  329. config = None
  330. return PluginConfig(plugin_name, plugin_cls.new_instance(config))
  331. @JsonSerializer(deserialize='_deserialize')
  332. class InstallHook(JsonMixin, Struct):
  333. event = Field(str, default=None)
  334. command = Field((str, [str])) #: Can be a string or list of strings.
  335. def normalize(self) -> 'InstallHook':
  336. if isinstance(self.command, str):
  337. return InstallHook(self.event, shlex.split(self.command))
  338. return self
  339. @classmethod
  340. def _deserialize(cls, mapper, node):
  341. if isinstance(node.value, str):
  342. return InstallHook(None, node.value)
  343. elif isinstance(node.value, dict):
  344. if len(node.value) != 1:
  345. raise ValueError('expected only one key')
  346. event, command = next(iter(node.value.items()))
  347. command = mapper.deserialize_node(node.make_child('command', InstallHook.command.datatype, command))
  348. return InstallHook(event, command)
  349. else:
  350. raise NotImplementedError # Default deserialization
  351. class ObjectCache(object):
  352. """ Helper class for loading #Package or #Monorepo objects from files. It
  353. caches the loaded object so that the same is not loaded multiple times into
  354. separate instances. """
  355. def __init__(self):
  356. self._cache = {}
  357. def clear(self):
  358. self._cache.clear()
  359. def get_or_load(self, filename: str, load_func: Callable[[str], Any]) -> Any:
  360. filename = os.path.normpath(os.path.abspath(filename))
  361. if filename not in self._cache:
  362. self._cache[filename] = load_func(filename)
  363. return self._cache[filename]
  364. class BaseObject(Struct):
  365. #: The name of the object (ie. package or repository name).
  366. name = Field(str)
  367. #: The version of the object.
  368. version = Field(Version)
  369. #: Plugins for this object.
  370. use = Field([PluginConfig])
  371. #: Directory where the "shore changelog" command stores the changelog
  372. #: YAML files.
  373. changelog_directory = Field(str, default='.changelog')
  374. #: A hidden attribute that is not deserialized but set during
  375. #: deserialization from file to know the file that the data was
  376. #: loaded from.
  377. filename = Field(str, default=None, hidden=True)
  378. #: Contains all the unhandled keys from the deserialization.
  379. unhandled_keys = Field([str], default=None, hidden=True)
  380. #: The cache that this object is stored in.
  381. cache = Field(ObjectCache, default=None, hidden=True)
  382. @property
  383. def directory(self) -> str:
  384. return os.path.dirname(self.filename)
  385. def has_plugin(self, plugin_name: str) -> bool:
  386. return any(x.name == plugin_name for x in self.use)
  387. def get_plugins(self) -> List[PluginConfig]:
  388. plugins = list(self.use)
  389. if not self.has_plugin('core'):
  390. core_plugin = load_plugin('core')
  391. plugins.insert(0, PluginConfig('core', core_plugin.new_instance(None)))
  392. return plugins
  393. def get_build_targets(self) -> Dict[str, IBuildTarget]:
  394. targets = {}
  395. for plugin in self.get_plugins():
  396. for target in plugin.get_build_targets(self):
  397. target_id = plugin.name + ':' + target.get_name()
  398. if target_id in targets:
  399. raise RuntimeError('build target ID {} is not unique'.format(target_id))
  400. targets[target_id] = target
  401. return targets
  402. def get_publish_targets(self) -> Dict[str, IPublishTarget]:
  403. targets = {}
  404. for plugin in self.get_plugins():
  405. for target in plugin.get_publish_targets(self):
  406. target_id = plugin.name + ':' + target.get_name()
  407. if target_id in targets:
  408. raise RuntimeError('publish target ID {} is not unique'.format(target_id))
  409. targets[target_id] = target
  410. return targets
  411. @classmethod
  412. def load(cls, filename: str, cache: ObjectCache) -> '_DeserializableFromFile':
  413. """ Deserializes *cls* from a YAML file specified by *filename*. """
  414. def _load(filename):
  415. collect = Collect()
  416. with open(filename) as fp:
  417. obj = mapper.deserialize(yaml.safe_load(fp), cls, filename=filename, decorations=[collect])
  418. obj.filename = filename
  419. obj.unhandled_keys = Stream.concat(
  420. (x.locator.append(k) for k in x.unknowns)
  421. for x in collect.nodes).collect()
  422. obj.cache = cache
  423. obj.on_load_hook()
  424. return obj
  425. return cache.get_or_load(filename, _load)
  426. def on_load_hook(self):
  427. """ Called after the object was loaded with #load(). """
  428. pass
  429. class Monorepo(BaseObject):
  430. private = Field(bool, default=False)
  431. #: Overrides the version field as it's optional for monorepos.
  432. version = Field(Version, default=None)
  433. #: If this option is enabled, individual packages in the monorepo have
  434. #: no individual version number. The "version" field in the package.yaml
  435. #: must be consistent with the version of the monorepo. Bumping the version
  436. #: of the monorepo will automatically bump the version in all packages.
  437. #: Bumping the version of individual packages will fail.
  438. mono_versioning = Field(bool, FieldName('mono-versioning'), default=False)
  439. #: The use field is optional on monorepos.
  440. use = Field([PluginConfig], default=list)
  441. #: Plugins to be used for all packages in the monorepo (unless explicitly
  442. #: overwritten in the package.yaml file).
  443. packages_use = Field([PluginConfig], default=list)
  444. #: Fields that can be inherited by the package.
  445. author = Field(Author, default=None)
  446. license = Field(str, default=None)
  447. url = Field(str, default=None)
  448. tag_format = Field(str, FieldName('tag-format'), default='{version}')
  449. @property
  450. def local_name(self) -> str:
  451. return self.name
  452. def get_packages(self) -> Iterable['Package']:
  453. """ Loads the packages for this mono repository. """
  454. for name in os.listdir(self.directory):
  455. path = os.path.join(self.directory, name, 'package.yaml')
  456. if os.path.isfile(path):
  457. package = Package.load(path, self.cache)
  458. assert package.monorepo is self, "woah hold up"
  459. yield package
  460. def get_private(self) -> bool:
  461. return self.private
  462. def get_tag_format(self) -> str:
  463. return self.tag_format
  464. def get_tag(self, version: str) -> str:
  465. tag_format = self.get_tag_format()
  466. return tag_format.format(name=self.name, version=version)
  467. def get_build_targets(self) -> Dict[str, IBuildTarget]:
  468. """
  469. Returns the publish targets for the monorepo. This includes the targets for
  470. packages in the monorepo.
  471. """
  472. targets = super().get_build_targets()
  473. for package in self.get_packages():
  474. for key, publisher in package.get_build_targets().items():
  475. targets[package.name + ':' + key] = self._BuildTargetWrapper(package.name, publisher)
  476. return targets
  477. def get_publish_targets(self) -> Dict[str, IPublishTarget]:
  478. """
  479. Returns the publish targets for the monorepo. If #mono_versioning is enabled,
  480. this includes the targets of child packages.
  481. """
  482. targets = super().get_publish_targets()
  483. for package in self.get_packages():
  484. for key, publisher in package.get_publish_targets().items():
  485. targets[package.name + ':' + key] = self._PublishTargetWrapper(package.name, publisher)
  486. return targets
  487. @implements(IBuildTarget)
  488. class _BuildTargetWrapper:
  489. def __init__(self, prefix, target):
  490. self.prefix = prefix
  491. self.target = target
  492. def get_name(self):
  493. return self.prefix + ':' + self.target.get_name()
  494. def get_build_artifacts(self):
  495. return self.target.get_build_artifacts()
  496. def build(self, build_directory):
  497. return self.target.build(build_directory)
  498. @implements(IPublishTarget)
  499. class _PublishTargetWrapper:
  500. def __init__(self, prefix, target):
  501. self.prefix = prefix
  502. self.target = target
  503. def get_name(self):
  504. return self.prefix + ':' + self.target.get_name()
  505. def get_build_selectors(self):
  506. return [self.prefix + ':' + k for k in self.target.get_build_selectors()]
  507. def publish(self, builds, test, build_directory, skip_existing):
  508. return self.target.publish(builds, test, build_directory, skip_existing)
  509. class Package(BaseObject):
  510. #: Filled with the Monorepo if the package is associated with one. A package
  511. #: is associated with a monorepo if the parent directory of it's own
  512. #: directory contains a `monorepo.yaml` file.
  513. monorepo = Field(Monorepo, default=None, hidden=True)
  514. #: A private package will be prevented from being published with the
  515. #: "shore publish" command.
  516. private = Field(bool, default=None)
  517. #: The version number of the package.
  518. version = Field(Version, default=None)
  519. #: The author of the package.
  520. author = Field(Author, default=None)
  521. #: The license of the package. If #private is set to True, this can be None
  522. #: without a check complaining about it.
  523. license = Field(str, default=None)
  524. #: The URL of the package (eg. the GitHub repository).
  525. url = Field(str, default=None)
  526. #: A format specified when tagging a version of the package. This defaults
  527. #: to `"{version}"`. If the package is a member of a monorepo, #get_tag_format()
  528. #: adds the package name as a prefix.
  529. tag_format = Field(str, FieldName('tag-format'), default='{version}')
  530. #: The package description.
  531. description = Field(str)
  532. #: The default "use" field is populated with setuptools and pypi.
  533. use = Field([PluginConfig], default=list)
  534. #: The long description of the package. If this is not defined, the
  535. #: setuptools plugin will load the README file.
  536. long_description = Field(str, FieldName('long-description'), default=None)
  537. #: The content type for the long description. If not specified, the
  538. #: setuptools plugin will base that on the suffix of the README file.
  539. long_description_content_type = Field(str,
  540. FieldName('long-description-content-type'), default=None)
  541. #: The name of the module (potentially as a dottet path for namespaced
  542. #: modules). This is used to find the entry file in #get_entry_file().
  543. #: If not specified, the package #name is used.
  544. modulename = Field(str, default=None)
  545. #: The directory for the source files.
  546. source_directory = Field(str, FieldName('source-directory'), default='src')
  547. #: The names of packages that should be excluded when installing the
  548. #: package. The setuptools plugin will automatically expand the names
  549. #: here to conform with what the #setuptools.find_packages() function
  550. #: expects (eg. 'test' is converted into 'test' and 'test.*').
  551. exclude_packages = Field([str], FieldName('exclude-packages'),
  552. default=lambda: ['test', 'docs'])
  553. #: The requirements for the package.
  554. requirements = Field(RootRequirements, default=RootRequirements)
  555. #: The entrypoints for the package. The structure here is the same as
  556. #: for #setuptools.setup().
  557. entrypoints = Field({"value_type": [str]}, default=dict)
  558. #: A list of datafile definitions.
  559. datafiles = Field([Datafile], default=list)
  560. #: A list of instructions to render in the MANIFEST.in file.
  561. manifest = Field([str], default=list)
  562. #: Hooks that will be executed on install events (install/develop).
  563. install_hooks = Field([InstallHook], FieldName('install-hooks'), default=list)
  564. #: List of classifiers for the package.
  565. classifiers = Field([str], default=list)
  566. #: List of keywords.
  567. keywords = Field([str], default=list)
  568. #: Set to true to indicate that the package is typed. This will render a
  569. #: "py.typed" file in the source directory and include it in the package
  570. #: data.
  571. typed = Field(bool, default=False)
  572. @property
  573. def local_name(self) -> str:
  574. if self.monorepo:
  575. relpath = os.path.relpath(self.directory, self.monorepo.directory)
  576. return os.path.normpath(relpath)
  577. return self.name
  578. def _get_inherited_field(self, field_name: str) -> Any:
  579. value = getattr(self, field_name)
  580. if value is None and self.monorepo and hasattr(self.monorepo, field_name):
  581. value = getattr(self.monorepo, field_name)
  582. return value
  583. def get_private(self) -> bool:
  584. return self._get_inherited_field('private')
  585. def get_modulename(self) -> str:
  586. return self.modulename or self.name
  587. def get_version(self) -> str:
  588. version: str = self._get_inherited_field('version')
  589. if version is None:
  590. raise RuntimeError('version is not set')
  591. return version
  592. def get_author(self) -> Optional[Author]:
  593. return self._get_inherited_field('author')
  594. def get_license(self) -> str:
  595. return self._get_inherited_field('license')
  596. def get_url(self) -> str:
  597. return self._get_inherited_field('url')
  598. def get_plugins(self) -> List[PluginConfig]:
  599. plugins = super().get_plugins()
  600. # Inherit only plugins from the monorepo that are not defined in the
  601. # package itself.
  602. if self.monorepo and self.monorepo.packages_use:
  603. plugins.extend(x for x in self.monorepo.packages_use
  604. if not self.has_plugin(x.name))
  605. # Make sure there exists a setuptools and pypi target.
  606. if not any(x.name == 'setuptools' for x in plugins):
  607. plugins.append(mapper.deserialize('setuptools', PluginConfig))
  608. if not self.get_private():
  609. if not any(x.name == 'pypi' for x in plugins):
  610. plugins.append(mapper.deserialize('pypi', PluginConfig))
  611. return plugins
  612. def get_tag_format(self) -> str:
  613. tag_format = self._get_inherited_field('tag_format')
  614. if self.monorepo and '{name}' not in tag_format:
  615. tag_format = '{name}@' + tag_format
  616. return tag_format
  617. def get_tag(self, version: str) -> str:
  618. tag_format = self.get_tag_format()
  619. return tag_format.format(name=self.name, version=version)
  620. def on_load_hook(self):
  621. """ Called when the package is loaded. Attempts to find the Monorepo that
  622. belongs to this package and load it. """
  623. monorepo_fn = os.path.join(os.path.dirname(self.directory), 'monorepo.yaml')
  624. if os.path.isfile(monorepo_fn):
  625. self.monorepo = Monorepo.load(monorepo_fn, self.cache)
  626. def get_entry_file(self) -> str:
  627. """ Returns the filename of the entry file that contains package metadata
  628. such as `__version__` and `__author__`. """
  629. name = self.get_modulename().replace('-', '_')
  630. parts = name.split('.')
  631. prefix = os.sep.join(parts[:-1])
  632. for filename in [parts[-1] + '.py', os.path.join(parts[-1], '__init__.py')]:
  633. filename = os.path.join(self.source_directory, prefix, filename)
  634. if os.path.isfile(os.path.join(self.directory, filename)):
  635. return filename
  636. raise ValueError('Entry file for package "{}" could not be determined'
  637. .format(self.name))
  638. def is_single_module(self) -> bool:
  639. return (
  640. self.get_modulename().count('.') == 0 and
  641. os.path.basename(self.get_entry_file()) != '__init__.py')
  642. def get_entry_file_abs(self) -> str:
  643. return os.path.normpath(os.path.join(self.directory, self.get_entry_file()))
  644. def get_entry_directory(self) -> str:
  645. """
  646. Returns the package directory. If this package is distributed in module-only
  647. form, a #ValueError is raised.
  648. """
  649. entry_file = self.get_entry_file()
  650. dirname, basename = os.path.split(entry_file)
  651. if basename != '__init__.py':
  652. raise ValueError('this package is in module-only form')
  653. return dirname
  654. EntryMetadata = collections.namedtuple('EntryFileData', 'author,version')
  655. def get_entry_metadata(self) -> EntryMetadata:
  656. """ Loads the entry file (see #get_entry_file()) and parses it with the
  657. #abc module to retrieve the value of the `__author__` and `__version__`
  658. variables defined in the file. """
  659. # Load the package/version data from the entry file.
  660. entry_file = self.get_entry_file()
  661. members = load_module_members(os.path.join(self.directory, entry_file))
  662. author = None
  663. version = None
  664. if '__version__' in members:
  665. try:
  666. version = ast.literal_eval(members['__version__'])
  667. except ValueError as exc:
  668. version = '<Non-literal expression>'
  669. if '__author__' in members:
  670. try:
  671. author = ast.literal_eval(members['__author__'])
  672. except ValueError as exc:
  673. author = '<Non-literal expression>'
  674. return self.EntryMetadata(author, version)