Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
namespaces.py91 linesDownload Raw Back to tests
1import ast2import json3import textwrap4from pathlib import Path5 6 7def iter_namespace_pkgs(namespace):8    parts = namespace.split(".")9    for i in range(len(parts)):10        yield ".".join(parts[: i + 1])11 12 13def build_namespace_package(tmpdir, name, version="1.0", impl="pkg_resources"):14    src_dir = tmpdir / name15    src_dir.mkdir()16    setup_py = src_dir / 'setup.py'17    namespace, _, rest = name.rpartition('.')18    namespaces = list(iter_namespace_pkgs(namespace))19    setup_args = {20        "name": name,21        "version": version,22        "packages": namespaces,23    }24 25    if impl == "pkg_resources":26        tmpl = '__import__("pkg_resources").declare_namespace(__name__)'27        setup_args["namespace_packages"] = namespaces28    elif impl == "pkgutil":29        tmpl = '__path__ = __import__("pkgutil").extend_path(__path__, __name__)'30    else:31        raise ValueError(f"Cannot recognise {impl=} when creating namespaces")32 33    args = json.dumps(setup_args, indent=4)34    assert ast.literal_eval(args)  # ensure it is valid Python35 36    script = textwrap.dedent(37        """\38        import setuptools39        args = {args}40        setuptools.setup(**args)41        """42    ).format(args=args)43    setup_py.write_text(script, encoding='utf-8')44 45    ns_pkg_dir = Path(src_dir, namespace.replace(".", "/"))46    ns_pkg_dir.mkdir(parents=True)47 48    for ns in namespaces:49        pkg_init = src_dir / ns.replace(".", "/") / '__init__.py'50        pkg_init.write_text(tmpl, encoding='utf-8')51 52    pkg_mod = ns_pkg_dir / (rest + '.py')53    some_functionality = 'name = {rest!r}'.format(**locals())54    pkg_mod.write_text(some_functionality, encoding='utf-8')55    return src_dir56 57 58def build_pep420_namespace_package(tmpdir, name):59    src_dir = tmpdir / name60    src_dir.mkdir()61    pyproject = src_dir / "pyproject.toml"62    namespace, _, rest = name.rpartition(".")63    script = f"""\64        [build-system]65        requires = ["setuptools"]66        build-backend = "setuptools.build_meta"67 68        [project]69        name = "{name}"70        version = "3.14159"71        """72    pyproject.write_text(textwrap.dedent(script), encoding='utf-8')73    ns_pkg_dir = Path(src_dir, namespace.replace(".", "/"))74    ns_pkg_dir.mkdir(parents=True)75    pkg_mod = ns_pkg_dir / (rest + ".py")76    some_functionality = f"name = {rest!r}"77    pkg_mod.write_text(some_functionality, encoding='utf-8')78    return src_dir79 80 81def make_site_dir(target):82    """83    Add a sitecustomize.py module in target to cause84    target to be added to site dirs such that .pth files85    are processed there.86    """87    sc = target / 'sitecustomize.py'88    target_str = str(target)89    tmpl = '__import__("site").addsitedir({target_str!r})'90    sc.write_text(tmpl.format(**locals()), encoding='utf-8')91 
codekingpro/portable-devtools · Team Ai