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.

139 lines
5.0 KiB

  1. # -*- coding: utf8 -*-
  2. # Copyright (c) 2020 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 typing import List, Tuple, Type, TypeVar, Union
  22. from nr.databind.core import ObjectMapper, NodeCollector, SerializationError
  23. from nr.databind.json import JsonModule
  24. from nr.stream import Stream
  25. import yaml
  26. import os
  27. import sys
  28. from .monorepo import MonorepoModel
  29. from .package import PackageModel
  30. T = TypeVar('T')
  31. ExcInfo = Tuple
  32. def get_existing_file(directory: str, choices: List[str]) -> bool:
  33. for fn in choices:
  34. path = os.path.join(directory, fn)
  35. if os.path.isfile(path):
  36. return path
  37. return None
  38. class Project:
  39. """
  40. Loads package and mono repo configuration files and caches them to ensure that
  41. the same filename is never loaded into a different model object.
  42. """
  43. monorepo_filenames = ['monorepo.yml', 'monorepo.yaml']
  44. package_filenames = ['package.yml', 'package.yaml']
  45. def __init__(self, mapper: ObjectMapper = None):
  46. self._cache: Dict[str, Union[MonorepoModel, PackageModel]] = {}
  47. self.mapper = mapper or ObjectMapper(JsonModule())
  48. self.subject: Union[MonorepoModel, PackageModel] = None
  49. self.monorepo: MonorepoModel = None
  50. self.packages: List[PackageModel] = []
  51. self.invalid_packages: List[Tuple[str, ExcInfo]] = []
  52. def load(
  53. self,
  54. directory: str = '.',
  55. expect: Type[Union[MonorepoModel, PackageModel]] = None,
  56. ) -> Union[MonorepoModel, PackageModel]:
  57. """
  58. Loads all project information from *directory*. This searches in all parent directories
  59. for a package or monorepo configuration, then loads all resources that belong to the
  60. project.
  61. """
  62. monorepo_fn = None
  63. package_fn = None
  64. # TODO(NiklasRosenstein): Iterate parent dirs until match is found.
  65. for dirname in [directory]:
  66. package_fn = get_existing_file(dirname, self.package_filenames)
  67. if package_fn:
  68. break
  69. monorepo_fn = get_existing_file(dirname, self.monorepo_filenames)
  70. if monorepo_fn:
  71. break
  72. if package_fn:
  73. monorepo_fn = get_existing_file(os.path.dirname(os.path.dirname(package_fn)),
  74. self.monorepo_filenames)
  75. if monorepo_fn:
  76. self.subject = self._load_monorepo(monorepo_fn)
  77. if package_fn:
  78. self.subject = self._load_package(package_fn)
  79. if expect and not isinstance(self.subject, expect):
  80. raise TypeError('expected {!r} at {!r}, got {!r}'.format(
  81. expect.__name__, directory, type(self.subject).__name__))
  82. return self.subject
  83. def _load_object(self, filename: str, type_: Type[T]) -> T:
  84. filename = os.path.normpath(os.path.abspath(filename))
  85. if filename in self._cache:
  86. obj = self._cache[filename]
  87. assert isinstance(obj, type_), 'type mismatch: have {} but expected {}'.format(
  88. type(obj).__name__, type_.__name__)
  89. return obj
  90. with open(filename) as fp:
  91. data = yaml.safe_load(fp)
  92. node_collector = NodeCollector()
  93. obj = self._cache[filename] = self.mapper.deserialize(
  94. data, type_, filename=filename, decorations=[node_collector])
  95. obj.filename = filename
  96. obj.unknown_keys = list(Stream.concat(
  97. (x.locator.append(k) for k in x.unknowns)
  98. for x in node_collector.nodes))
  99. return obj
  100. def _load_monorepo(self, filename: str) -> MonorepoModel:
  101. self.monorepo = self._load_object(filename, MonorepoModel)
  102. # Load packages in that monorepo.
  103. directory = os.path.dirname(filename)
  104. for item_name in os.listdir(directory):
  105. package_fn = get_existing_file(os.path.join(directory, item_name), self.package_filenames)
  106. if package_fn:
  107. try:
  108. self._load_package(package_fn)
  109. except SerializationError as exc:
  110. self.invalid_packages.append((item_name, sys.exc_info()))
  111. return self.monorepo
  112. def _load_package(self, filename: str) -> PackageModel:
  113. package = self._load_object(filename, PackageModel)
  114. if package not in self.packages:
  115. self.packages.append(package)
  116. return package