Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

91 rinda
2.7 KiB

  1. import ast
  2. import json
  3. import textwrap
  4. from pathlib import Path
  5. def iter_namespace_pkgs(namespace):
  6. parts = namespace.split(".")
  7. for i in range(len(parts)):
  8. yield ".".join(parts[: i + 1])
  9. def build_namespace_package(tmpdir, name, version="1.0", impl="pkg_resources"):
  10. src_dir = tmpdir / name
  11. src_dir.mkdir()
  12. setup_py = src_dir / 'setup.py'
  13. namespace, _, rest = name.rpartition('.')
  14. namespaces = list(iter_namespace_pkgs(namespace))
  15. setup_args = {
  16. "name": name,
  17. "version": version,
  18. "packages": namespaces,
  19. }
  20. if impl == "pkg_resources":
  21. tmpl = '__import__("pkg_resources").declare_namespace(__name__)'
  22. setup_args["namespace_packages"] = namespaces
  23. elif impl == "pkgutil":
  24. tmpl = '__path__ = __import__("pkgutil").extend_path(__path__, __name__)'
  25. else:
  26. raise ValueError(f"Cannot recognise {impl=} when creating namespaces")
  27. args = json.dumps(setup_args, indent=4)
  28. assert ast.literal_eval(args) # ensure it is valid Python
  29. script = textwrap.dedent(
  30. """\
  31. import setuptools
  32. args = {args}
  33. setuptools.setup(**args)
  34. """
  35. ).format(args=args)
  36. setup_py.write_text(script, encoding='utf-8')
  37. ns_pkg_dir = Path(src_dir, namespace.replace(".", "/"))
  38. ns_pkg_dir.mkdir(parents=True)
  39. for ns in namespaces:
  40. pkg_init = src_dir / ns.replace(".", "/") / '__init__.py'
  41. pkg_init.write_text(tmpl, encoding='utf-8')
  42. pkg_mod = ns_pkg_dir / (rest + '.py')
  43. some_functionality = 'name = {rest!r}'.format(**locals())
  44. pkg_mod.write_text(some_functionality, encoding='utf-8')
  45. return src_dir
  46. def build_pep420_namespace_package(tmpdir, name):
  47. src_dir = tmpdir / name
  48. src_dir.mkdir()
  49. pyproject = src_dir / "pyproject.toml"
  50. namespace, _, rest = name.rpartition(".")
  51. script = f"""\
  52. [build-system]
  53. requires = ["setuptools"]
  54. build-backend = "setuptools.build_meta"
  55. [project]
  56. name = "{name}"
  57. version = "3.14159"
  58. """
  59. pyproject.write_text(textwrap.dedent(script), encoding='utf-8')
  60. ns_pkg_dir = Path(src_dir, namespace.replace(".", "/"))
  61. ns_pkg_dir.mkdir(parents=True)
  62. pkg_mod = ns_pkg_dir / (rest + ".py")
  63. some_functionality = f"name = {rest!r}"
  64. pkg_mod.write_text(some_functionality, encoding='utf-8')
  65. return src_dir
  66. def make_site_dir(target):
  67. """
  68. Add a sitecustomize.py module in target to cause
  69. target to be added to site dirs such that .pth files
  70. are processed there.
  71. """
  72. sc = target / 'sitecustomize.py'
  73. target_str = str(target)
  74. tmpl = '__import__("site").addsitedir({target_str!r})'
  75. sc.write_text(tmpl.format(**locals()), encoding='utf-8')