On Tue, 18/09/2012 11:05 -0400, Barry Warsaw wrote:
> So I guess if no one else has any other suggestions, then the following
> changes should be made to the patch, after which it could be applied:
> 
> * compare against site.getsitepackages() for directories that should not be
>   removed.
> * update the docstring to indicate that site package directories are not
>   removed even if magic_tag is None

Done, the updated patch attached. Thanks for the suggestions!

Also, I've updated my branch at lp:~mitya57/+junk/python3-defaults so
that it can be easily merged.

--
Dmitry Shachnev
Author: Dmitry Shachnev <mity...@gmail.com>
Description: Don't remove *.py[co] files of foreign packages
Bug: http://bugs.debian.org/685167
=== modified file 'py3clean'
--- py3clean	2012-07-13 04:52:23 +0000
+++ py3clean	2012-09-18 15:29:29 +0000
@@ -25,8 +25,9 @@
 import optparse
 import sys
 from glob import glob1
+from site import getsitepackages
 from os import environ, remove, rmdir
-from os.path import dirname, exists, join
+from os.path import dirname, basename, exists, join
 sys.path.insert(1, '/usr/share/python3/')
 from debpython import files as dpf
 from debpython.version import SUPPORTED, getver, vrepr
@@ -72,16 +73,22 @@
 def destroyer(magic_tag=None):  # ;-)
     """Remove every .py[co] file associated to received .py file.
 
-    :param magic_tag: if None, removes __pycache__ directories,
+    :param magic_tag: if None, removes all associated .py[co] files
+        from __pycache__ directory,
         if False, removes python3.1's .pyc files only,
         otherwise removes given magic tag from __pycache__ directory
     :type magic_tag: None or False or str"""
     if magic_tag is None:
 
-        # remove all files in __pycache__ directory
+        # remove compiled files in __pycache__ directory
         def find_files_to_remove(pyfile):
-            directory = "%s/__pycache__/" % dirname(pyfile)
-            for fn in glob1(directory, '*'):
+            directory = dirname(pyfile)
+            fnames = "*"
+            if directory in getsitepackages():
+                # remove the .py extension
+                fnames = basename(pyfile)[:-3] + ".*"
+            directory += "/__pycache__/"
+            for fn in glob1(directory, fnames):
                 yield join(directory, fn)
             # remove "classic" .pyc files as well
             for filename in ("%sc" % pyfile, "%so" % pyfile):
@@ -98,8 +105,12 @@
 
         # remove .pyc files for no longer needed magic tags
         def find_files_to_remove(pyfile):
-            directory = "%s/__pycache__/" % dirname(pyfile)
-            for fn in glob1(directory, "*.%s.py[co]" % magic_tag):
+            directory = dirname(pyfile)
+            fnames = "*"
+            if directory in getsitepackages():
+                fnames = basename(pyfile)[:-3]
+            directory += "/__pycache__/"
+            for fn in glob1(directory, "%s.%s.py[co]" % (fnames, magic_tag)):
                 yield join(directory, fn)
 
     def myremove(fname):

Reply via email to