From 776c979c8744c3d47275436377d9ea258bb78576 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Tue, 19 May 2020 21:22:10 -0700 Subject: [PATCH 1/8] sage.graphs.graph_decompositions: Make it a namespace package by removing __init__.py (which had unnecessary reimports only) --- src/sage/graphs/graph_decompositions/__init__.py | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 src/sage/graphs/graph_decompositions/__init__.py diff --git a/src/sage/graphs/graph_decompositions/__init__.py b/src/sage/graphs/graph_decompositions/__init__.py deleted file mode 100644 index 79890f6c47f..00000000000 --- a/src/sage/graphs/graph_decompositions/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -# This file is not empty ! -import sage.graphs.graph_decompositions.vertex_separation -import sage.graphs.graph_decompositions.rankwidth -import sage.graphs.graph_decompositions.bandwidth -import sage.graphs.graph_decompositions.cutwidth From 2e61adaf7bca46adc04ebf8cde025415c764ec69 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Tue, 19 May 2020 22:11:31 -0700 Subject: [PATCH 2/8] sage.numerical.backends: Make it a namespace package by removing __init__.py (which was empty) --- src/sage/numerical/backends/__init__.py | 1 - 1 file changed, 1 deletion(-) delete mode 100644 src/sage/numerical/backends/__init__.py diff --git a/src/sage/numerical/backends/__init__.py b/src/sage/numerical/backends/__init__.py deleted file mode 100644 index ffeb2db3606..00000000000 --- a/src/sage/numerical/backends/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# not an empty file From 26b1ca03aa1bc4fa250d69f4bb73fa6237883d08 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Tue, 19 May 2020 21:45:41 -0700 Subject: [PATCH 3/8] Mark src and src/sage/ext_data as directories that are not namespace packages --- src/nonamespace | 0 src/sage/ext_data/nonamespace | 0 src/sage/tests/books/nonamespace | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/nonamespace create mode 100644 src/sage/ext_data/nonamespace create mode 100644 src/sage/tests/books/nonamespace diff --git a/src/nonamespace b/src/nonamespace new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/sage/ext_data/nonamespace b/src/sage/ext_data/nonamespace new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/sage/tests/books/nonamespace b/src/sage/tests/books/nonamespace new file mode 100644 index 00000000000..e69de29bb2d From a733af51eb86abfe98f09786c628190127610b43 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Tue, 19 May 2020 21:46:45 -0700 Subject: [PATCH 4/8] sage_setup.find.find_python_sources: Handle native namespace packages, 'nonamespace' files --- src/sage_setup/find.py | 28 ++++++++++++++++++++++++++-- src/setup.py | 1 + 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/sage_setup/find.py b/src/sage_setup/find.py index 3630ac7b79a..707600e3052 100644 --- a/src/sage_setup/find.py +++ b/src/sage_setup/find.py @@ -80,7 +80,8 @@ def find_python_sources(src_dir, modules=['sage'], distributions=None): - the list of package names (corresponding to directories with ``__init__.py``), - - Python module names (corresponding to other ``*.py`` files). + - Python module names (corresponding to other ``*.py`` files except for + those below directories with a file named ``nonamespace`` in it). - Cython extensions (corresponding to ``*.pyx`` files). @@ -112,6 +113,26 @@ def find_python_sources(src_dir, modules=['sage'], distributions=None): sage: ['sage.doctest.tests' in L for L in (py_packages, py_modules)] [False, False] + Native namespace package (no ``__init__.py``, PEP 420):: + + sage: ['sage.graphs.graph_decompositions' in L for L in (py_packages, py_modules)] + [False, False] + + Python module in a native namespace package:: + + sage: ['sage.graphs.graph_decompositions.modular_decomposition' in L for L in (py_packages, py_modules)] + [False, True] + + Subdirectory marked with a ``nonamespace`` file:: + + sage: ['sage.extdata' in L for L in (py_packages, py_modules)] + [False, False] + + Python file (not module) below a directory with a ``nonamespace`` file:: + + sage: ['sage.ext_data.nbconvert.postprocess' in L for L in (py_packages, py_modules)] + [False, False] + Filtering by distribution (distutils package):: sage: find_python_sources(SAGE_SRC, distributions=['sage-tdlib']) @@ -149,7 +170,10 @@ def find_python_sources(src_dir, modules=['sage'], distributions=None): # Ordinary package. if distributions is None or '' in distributions: python_packages.append(package) - else: + if os.path.exists(os.path.join(dirpath, 'nonamespace')): + # Marked as "not a namespace package" + # (similar to nodoctest in sage.doctest.control) + dirnames.clear() continue def is_in_distributions(filename): diff --git a/src/setup.py b/src/setup.py index 01dd6c90205..dcf8e3ff3f8 100755 --- a/src/setup.py +++ b/src/setup.py @@ -93,6 +93,7 @@ author_email= 'https://groups.google.com/group/sage-support', url = 'https://www.sagemath.org', packages = python_packages, + py_modules = python_modules, package_data = { 'sage.libs.gap': ['sage.gaprc'], 'sage.interfaces': ['sage-maxima.lisp'], From c08c511d6ad344f0e2e98914af4246e9fe6812ec Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Tue, 2 Jun 2020 18:14:41 -0700 Subject: [PATCH 5/8] is_package_or_namespace_package_dir: New, recognize namespace packages by file 'namespace', monkeypatch into Cython --- .../graphs/graph_decompositions/namespace | 0 src/sage/numerical/backends/namespace | 0 src/sage_setup/find.py | 29 +++++++++++++++++++ src/setup.py | 6 +++- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/sage/graphs/graph_decompositions/namespace create mode 100644 src/sage/numerical/backends/namespace diff --git a/src/sage/graphs/graph_decompositions/namespace b/src/sage/graphs/graph_decompositions/namespace new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/sage/numerical/backends/namespace b/src/sage/numerical/backends/namespace new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/sage_setup/find.py b/src/sage_setup/find.py index 707600e3052..e6766a7db95 100644 --- a/src/sage_setup/find.py +++ b/src/sage_setup/find.py @@ -196,6 +196,35 @@ def is_in_distributions(filename): os.chdir(cwd) return python_packages, python_modules, cython_modules +def is_package_or_namespace_package_dir(dirpath): + """ + True when ``dirpath`` is a regular or namespace package. + + EXAMPLES:: + + sage: from sage.env import SAGE_SRC + sage: from sage_setup.find import is_package_or_namespace_package_dir + sage: is_package_or_namespace_package_dir(SAGE_SRC) + False + + An ordinary package:: + + sage: is_package_or_namespace_package_dir(os.path.join(SAGE_SRC, 'sage', 'structure')) + True + + A namespace package:: + + sage: is_package_or_namespace_package_dir(os.path.join(SAGE_SRC, 'sage', 'numerical', 'backends') + True + + """ + PACKAGE_FILES = ("__init__.py", "__init__.pyc", "__init__.pyx", "__init__.pxd") + for filename in PACKAGE_FILES: + path = os.path.join(dirpath, filename) + if os.path.exists(path): + return True + return os.path.exists(os.path.join(dirpath, 'namespace')) + def find_extra_files(src_dir, modules, cythonized_dir, special_filenames=[]): """ Find all extra files which should be installed. diff --git a/src/setup.py b/src/setup.py index dcf8e3ff3f8..3e1349c9c08 100755 --- a/src/setup.py +++ b/src/setup.py @@ -70,7 +70,7 @@ log.warn('distributions = {0}'.format(distributions)) -from sage_setup.find import find_python_sources +from sage_setup.find import find_python_sources, is_package_or_namespace_package_dir python_packages, python_modules, cython_modules = find_python_sources( SAGE_SRC, ['sage', 'sage_setup'], distributions=distributions) @@ -78,6 +78,10 @@ print("Discovered Python/Cython sources, time: %.2f seconds." % (time.time() - t)) +import Cython.Build.Dependencies +import Cython.Build.Cythonize +import Cython.Utils +Cython.Utils.is_package_dir = Cython.Build.Cythonize.is_package_dir = Cython.Build.Dependencies.is_package_dir = is_package_or_namespace_package_dir from sage_setup.command.sage_install import sage_install From 4407e5ea4748f926739fb0735cb094485d7f0ea8 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Thu, 16 Jul 2020 18:43:43 -0700 Subject: [PATCH 6/8] sage_setup.command.sage_install.sage_install: Rename to sage_install_and_clean, update use it in src/setup.py; refactor through new class sage_install --- src/sage_setup/command/sage_install.py | 9 +++++++++ src/setup.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/sage_setup/command/sage_install.py b/src/sage_setup/command/sage_install.py index 726049bf14b..f9ed30cc6c5 100644 --- a/src/sage_setup/command/sage_install.py +++ b/src/sage_setup/command/sage_install.py @@ -8,6 +8,7 @@ from distutils.command.install import install class sage_install(install): + def run(self): install.run(self) self.install_kernel_spec() @@ -31,6 +32,14 @@ def install_kernel_spec(self): # the install_data directory for installing our Jupyter files. SageKernelSpec.update(prefix=self.install_data) +class sage_install_and_clean(sage_install): + + def run(self): + sage_install.run(self) + t = time.time() + self.clean_stale_files() + log.info('Finished cleaning, time: %.2f seconds.' % (time.time() - t)) + def clean_stale_files(self): """ Remove stale installed files. diff --git a/src/setup.py b/src/setup.py index 3e1349c9c08..f670b84fbef 100755 --- a/src/setup.py +++ b/src/setup.py @@ -83,7 +83,7 @@ import Cython.Utils Cython.Utils.is_package_dir = Cython.Build.Cythonize.is_package_dir = Cython.Build.Dependencies.is_package_dir = is_package_or_namespace_package_dir -from sage_setup.command.sage_install import sage_install +from sage_setup.command.sage_install import sage_install_and_clean ######################################################### ### Distutils @@ -182,5 +182,5 @@ cmdclass = dict(build=sage_build, build_cython=sage_build_cython, build_ext=sage_build_ext, - install=sage_install), + install=sage_install_and_clean), ext_modules = cython_modules) From a13c126de7f89a2838ad0ec7a3a6e0bb0f882f48 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Mon, 17 Aug 2020 21:40:19 -0700 Subject: [PATCH 7/8] Make sage.graphs, sage.matrix, sage.libs, sage.interfaces namespace packages --- src/sage/graphs/__init__.py | 3 --- src/sage/{interfaces/__init__.py => graphs/namespace} | 0 src/sage/{libs/__init__.py => interfaces/namespace} | 0 src/sage/libs/namespace | 0 src/sage/matrix/__init__.py | 2 -- src/sage/matrix/namespace | 0 6 files changed, 5 deletions(-) delete mode 100644 src/sage/graphs/__init__.py rename src/sage/{interfaces/__init__.py => graphs/namespace} (100%) rename src/sage/{libs/__init__.py => interfaces/namespace} (100%) create mode 100644 src/sage/libs/namespace delete mode 100644 src/sage/matrix/__init__.py create mode 100644 src/sage/matrix/namespace diff --git a/src/sage/graphs/__init__.py b/src/sage/graphs/__init__.py deleted file mode 100644 index 89e7c1280cd..00000000000 --- a/src/sage/graphs/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from __future__ import absolute_import - -from . import dot2tex_utils diff --git a/src/sage/interfaces/__init__.py b/src/sage/graphs/namespace similarity index 100% rename from src/sage/interfaces/__init__.py rename to src/sage/graphs/namespace diff --git a/src/sage/libs/__init__.py b/src/sage/interfaces/namespace similarity index 100% rename from src/sage/libs/__init__.py rename to src/sage/interfaces/namespace diff --git a/src/sage/libs/namespace b/src/sage/libs/namespace new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/sage/matrix/__init__.py b/src/sage/matrix/__init__.py deleted file mode 100644 index 58b5ef12b66..00000000000 --- a/src/sage/matrix/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# Resolve a cyclic import -import sage.matrix.args diff --git a/src/sage/matrix/namespace b/src/sage/matrix/namespace new file mode 100644 index 00000000000..e69de29bb2d From a967aca4491de98118612f30cbdf8bd75ab4efa9 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Thu, 16 Jul 2020 23:35:54 -0700 Subject: [PATCH 8/8] WIP --- src/sage_setup/find.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/sage_setup/find.py b/src/sage_setup/find.py index e6766a7db95..c1cd986cb91 100644 --- a/src/sage_setup/find.py +++ b/src/sage_setup/find.py @@ -333,7 +333,11 @@ def installed_files_by_module(site_packages, modules=('sage',)): sage: f2 'sage/structure/....pyc' - This takes about 30ms with warm cache: + Namespace packages:: + + sage: files_by_module['sage.graphs.graph_decompositions'] + + This takes about 30ms with warm cache:: sage: timeit('installed_files_by_module(site_packages)', # random output ....: number=1, repeat=1)