Commits

Lenard Lindstrom committed 48347ea

Made reporting optional and added a collection stop

Comments (0)

Files changed (3)

trackmod/__init__.py

 
 """A package for tracking module use
 
-Exports start(repfilepth).
+Exports begin(repfilepth=None).
 
 """
 
     finally:
         repfile.close()
     
-def start(repfilepth):
+def begin(repfilepth=None):
     global installed
 
     if not installed:
         sys.meta_path.insert(0, importer)
         installed = True
-        atexit.register(generate_report, repfilepth)
+        if repfilepth is not None:
+            atexit.register(generate_report, repfilepth)
 
-reporter.init()
+def end():
+    reporter.end()
+    importer.end()
 
+reporter.begin()  # Keep this last.
 
 
+

trackmod/importer.py

 from trackmod import module
 
 
+collect_data = True
+
 def find_module(fullname, path=None):
-    if fullname in sys.modules:
-        return None
-    try:
+    if collect_data and fullname not in sys.modules:
         m = module.Module(fullname)
         sys.modules[fullname] = m
         try:
-            reload(m)
+            try:
+                reload(m)
+            except ImportError:
+                return None;
         finally:
             del sys.modules[fullname]
-    except ImportError:
+        m.__class__ = module.TrackerModule
+        loader.add(fullname, m)
+        return loader
+    else:
         return None
-    m.__class__ = module.TrackerModule
-    loader.add(fullname, m)
-    return loader
 
-
+def end():
+    global collect_data
+    collect_data = False

trackmod/reporter.py

+# Keep this first.
 def listmods():
     return [n for n, m in sys.modules.iteritems() if m is not None]
 
 already_loaded = listmods()
 import threading
 
+collect_data = True
 
 def print_(*args, **kwds):
     stream = kwds.get('file', sys.stdout)
         rep(name, "(%s)" % attr)
     rep("\n=== end of report ===")
 
-def init():
+def begin():
     global already_loaded, loaded, accessed, data_lock, added_by_trackmod
     added_by_trackmod = list(set(listmods()) - set(already_loaded))
     loaded = set()
     accessed = set()
     data_lock = threading.Lock()
 
+def end():
+    global collect_data
+    collect_data = False
+
 def add_loaded(name):
-    data_lock.acquire()
-    try:
-        loaded.add(name)
-    finally:
-        data_lock.release()
+    if collect_data:
+        data_lock.acquire()
+        try:
+            loaded.add(name)
+        finally:
+            data_lock.release()
 
 def add_accessed(name, attr):
-    data_lock.acquire()
-    try:
-        accessed.add((name, attr))
-    finally:
-        data_lock.release()
+    if collect_data:
+        data_lock.acquire()
+        try:
+            accessed.add((name, attr))
+        finally:
+            data_lock.release()
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.