Skip to content

Commit 3c7fefe

Browse files
Release Managervbraun
Release Manager
authored andcommitted
Trac #21604: Cleaning up stale installed files in setup()
This part of `src/setup.py` should be moved inside some distutils command: {{{ ######################################################### ### Clean ######################################################### print('Cleaning up stale installed files....') t = time.time() from sage_setup.clean import clean_install_dir output_dirs = SITE_PACKAGES + glob.glob(os.path.join(SAGE_SRC, 'build', 'lib*')) for output_dir in output_dirs: print('- cleaning {0}'.format(output_dir)) clean_install_dir(output_dir, python_packages, python_modules, ext_modules, python_data_files) print('Finished cleaning, time: %.2f seconds.' % (time.time() - t)) }}} URL: https://trac.sagemath.org/21604 Reported by: jdemeyer Ticket author(s): Jeroen Demeyer Reviewer(s): Matthias Koeppe
2 parents 7490b40 + 5ba95ed commit 3c7fefe

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

src/setup.py

+47-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
from __future__ import print_function
33

4-
import os, sys, time, errno, platform, subprocess, glob
4+
import os, sys, time, errno, platform, subprocess
55
from distutils.core import setup, DistutilsSetupError
66

77
def excepthook(*exc):
@@ -641,35 +641,63 @@ def run_cythonize():
641641

642642

643643
#########################################################
644-
### Clean
644+
### Install Jupyter kernel spec and clean stale files
645645
#########################################################
646646

647-
print('Cleaning up stale installed files....')
648-
t = time.time()
649-
from sage_setup.clean import clean_install_dir
650-
output_dirs = SITE_PACKAGES + glob.glob(os.path.join(build_base, 'lib*'))
651-
for output_dir in output_dirs:
652-
print('- cleaning {0}'.format(output_dir))
653-
clean_install_dir(output_dir, python_packages, python_modules,
654-
ext_modules, python_data_files)
655-
print('Finished cleaning, time: %.2f seconds.' % (time.time() - t))
656-
657-
658-
#########################################################
659-
### Install also Jupyter kernel spec
660-
#########################################################
661-
662-
# We cannot just add the installation of the kernel spec to data_files
663-
# since the file is generated, not copied.
664647
class sage_install(install):
665648
def run(self):
666649
install.run(self)
667650
self.install_kernel_spec()
651+
log.warn('Cleaning up stale installed files....')
652+
t = time.time()
653+
self.clean_stale_files()
654+
log.warn('Finished cleaning, time: %.2f seconds.' % (time.time() - t))
668655

669656
def install_kernel_spec(self):
657+
"""
658+
Install the Jupyter kernel spec.
659+
660+
.. NOTE::
661+
662+
The files are generated, not copied. Therefore, we cannot
663+
use ``data_files`` for this.
664+
"""
670665
from sage.repl.ipython_kernel.install import SageKernelSpec
671666
SageKernelSpec.update()
672667

668+
def clean_stale_files(self):
669+
"""
670+
Remove stale installed files.
671+
672+
This removes files which are built/installed but which do not
673+
exist in the Sage sources (typically because some source file
674+
has been deleted). Files are removed from the build directory
675+
``build/lib-*`` and from the install directory ``site-packages``.
676+
"""
677+
dist = self.distribution
678+
cmd_build_py = dist.command_obj["build_py"]
679+
680+
# Determine all Python modules inside all packages
681+
py_modules = []
682+
for package in dist.packages:
683+
package_dir = cmd_build_py.get_package_dir(package)
684+
py_modules += cmd_build_py.find_package_modules(package, package_dir)
685+
# modules is a list of triples (package, module, module_file).
686+
# Construct the complete module name from this.
687+
py_modules = ["{0}.{1}".format(*m) for m in py_modules]
688+
689+
# Clean install directory (usually, purelib and platlib are the same)
690+
# and build directory.
691+
output_dirs = [self.install_purelib, self.install_platlib, self.build_lib]
692+
from sage_setup.clean import clean_install_dir
693+
for output_dir in set(output_dirs):
694+
log.warn('- cleaning {0}'.format(output_dir))
695+
clean_install_dir(output_dir,
696+
dist.packages,
697+
py_modules,
698+
dist.ext_modules,
699+
dist.data_files)
700+
673701

674702
#########################################################
675703
### Distutils

0 commit comments

Comments
 (0)