Repository: spack/spack

Issue:
spack clean --python-cache deletes .pyc files in installed packages
I just noticed that `spack clean --python-cache` deletes `.pyc` files in Spack-installed packages. If you install Python or Python-packages with Spack and then run `spack --debug clean --python-cache`, you can reproduce this.

We should probably ignore the `opt/` directory when traversing the filesystem. I don't think it will hurt anything, but it does slow down the Python installation temporarily and cause `spack clean` to take longer than it should.

Also, the `help` message for `--all` needs to be updated with the `-p` flag.

P.S. Can we finally delete https://github.com/spack/spack/blob/develop/bin/spack#L59,L81? These files haven't existed for years, and now we have a simple solution to clean them if users run into trouble.

@healther 


Discussion:
I can do the clean up and the documentation, but I'd still remove the `.pyc` files in the `opt` folder. The background is that I had people use packages with the system python, and I'm not certain how python reacts to mismatched `.pyc` stuff.

In my mind `spack clean -a` should be the solution for "I screwed up by having something wrong in my cache and just want to have spack clean itself up". The temporary speed loss shouldn't matter, as you are not supposed to screw up that often ;)

Resolving patch:
diff --git a/lib/spack/spack/cmd/clean.py b/lib/spack/spack/cmd/clean.py
--- a/lib/spack/spack/cmd/clean.py
+++ b/lib/spack/spack/cmd/clean.py
@@ -32,7 +32,8 @@
 import spack.cmd
 import spack.repo
 import spack.stage
-from spack.paths import spack_root
+from spack.paths import lib_path, var_path
+
 
 description = "remove temporary build files and/or downloaded archives"
 section = "build"
@@ -59,7 +60,7 @@ def setup_parser(subparser):
         '-p', '--python-cache', action='store_true',
         help="remove .pyc, .pyo files and __pycache__ folders")
     subparser.add_argument(
-        '-a', '--all', action=AllClean, help="equivalent to -sdm", nargs=0
+        '-a', '--all', action=AllClean, help="equivalent to -sdmp", nargs=0
     )
     subparser.add_argument(
         'specs',
@@ -97,14 +98,15 @@ def clean(parser, args):
 
     if args.python_cache:
         tty.msg('Removing python cache files')
-        for root, dirs, files in os.walk(spack_root):
-            for f in files:
-                if f.endswith('.pyc') or f.endswith('.pyo'):
-                    fname = os.path.join(root, f)
-                    tty.debug('Removing {0}'.format(fname))
-                    os.remove(fname)
-            for d in dirs:
-                if d == '__pycache__':
-                    dname = os.path.join(root, d)
-                    tty.debug('Removing {0}'.format(dname))
-                    shutil.rmtree(dname)
+        for directory in [lib_path, var_path]:
+            for root, dirs, files in os.walk(directory):
+                for f in files:
+                    if f.endswith('.pyc') or f.endswith('.pyo'):
+                        fname = os.path.join(root, f)
+                        tty.debug('Removing {0}'.format(fname))
+                        os.remove(fname)
+                for d in dirs:
+                    if d == '__pycache__':
+                        dname = os.path.join(root, d)
+                        tty.debug('Removing {0}'.format(dname))
+                        shutil.rmtree(dname)


Test patch: