changeset 323:44f94ffe28f7

version works with file and folder, returns null if not in hg. Also a function to get all modules, but seems to get junk at the same time
author Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
date Thu, 12 Jun 2008 17:12:45 -0400
parents ad8be93b3c55
children ce79bf5fa463
files version.py
diffstat 1 files changed, 113 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/version.py	Thu Jun 12 12:45:42 2008 -0400
+++ b/version.py	Thu Jun 12 17:12:45 2008 -0400
@@ -1,5 +1,7 @@
 import subprocess as _subprocess
 import imp as _imp
+import sys
+import os
 
 _cache = {}
 
@@ -21,48 +23,129 @@
     version control by mercurial.
 
     """
-    #
-    # NOTE
-    #
-    # If you find bugs in this function, please update the __src_version__
-    # function in theano, pylearn, and email either theano-dev or pylearn-dev so
-    # that people can update their experiment dirs (the output of this function
-    # is meant to be hard-coded in external files).
-    #
 
     if module_name not in _cache:
 
-        location = _imp.find_module(module_name)[1]
+        try :
+            location = _imp.find_module(module_name)[1]
+        except ImportError:
+            _cache[module_name] = None
+            return None
         #print 'location:', location
+        isdir = False
+        if os.path.isdir(location) :
+            isdir = True
+        elif os.path.isfile(location) :
+            isdir = False
+        else :
+            # SEEMS THIS CASE EXIST, FOR WEIRD BUILTIN FUNCTIONS
+            #print location,": it's 'not a dir, it's not a file, it's superman!"
+            #raise Exception('Unknown location or file type')
+            _cache[module_name] = None
+            return None
 
-        status = _subprocess.Popen(('hg','st'),cwd=location,stdout=_subprocess.PIPE).communicate()[0]
-        #TODO: check that the process return code is 0 (ticket #45)
 
-        #status_codes = [line[0] for line in  if line and line[0] != '?']
-        for line in status.split('\n'):
-            if not line: continue
-            if line[0] != '?':
-                raise Exception('Uncommitted modification to "%s" in %s (%s)'
+        # we're dealing with a dir
+        if isdir :
+
+            # under hg?
+            if not os.path.exists( os.path.join( location , '.hg') ) :
+                _cache[module_name] = None
+                return None
+
+            status = _subprocess.Popen(('hg','st'),cwd=location,stdout=_subprocess.PIPE).communicate()[0]
+            #print 'status =', status
+            #TODO: check that the process return code is 0 (ticket #45)
+
+            #status_codes = [line[0] for line in  if line and line[0] != '?']
+            for line in status.split('\n'):
+                if not line: continue
+                if line[0] != '?':
+                    raise Exception('Uncommitted modification to "%s" in %s (%s)'
                         %(line[2:], __name__,location))
-            if line[0] == '?' and line[-3:] == '.py':
-                raise Exception('Untracked file "%s" in %s (%s)'
+                if line[0] == '?' and line[-3:] == '.py':
+                    raise Exception('Untracked file "%s" in %s (%s)'
                         %(line[2:], __name__, location))
 
-        hg_id = _subprocess.Popen(('hg','id'),cwd=location,stdout=_subprocess.PIPE).communicate()[0]
+            hg_id = _subprocess.Popen(('hg','id'),cwd=location,stdout=_subprocess.PIPE).communicate()[0]
+
+            # This asserts my understanding of hg id return values
+            # There is mention in the doc that it might return two parent hash codes
+            # but I've never seen it, and I dont' know what it means or how it is
+            # formatted.
+            tokens = hg_id.split(' ')
+            assert len(tokens) <= 2
+            assert len(tokens) >= 1
+            assert tokens[0][-1] != '+' # the trailing + indicates uncommitted changes
+            if len(tokens) == 2:
+                assert tokens[1] == 'tip\n'
+
+            _cache[module_name] = tokens[0]
+
+        # we're dealing with a file
+        if not isdir :
+
+            folder = os.path.split( os.path.abspath(location) )[0]
+            # under hg?
+            if not os.path.exists( os.path.join( folder , '.hg') ) :
+                _cache[module_name] = None
+                return None
+
+            status = _subprocess.Popen(('hg','st',location),cwd=folder,stdout=_subprocess.PIPE).communicate()[0]
+            #print 'status =', status
 
-        #This asserts my understanding of hg id return values
-        # There is mention in the doc that it might return two parent hash codes
-        # but I've never seen it, and I dont' know what it means or how it is
-        # formatted.
-        tokens = hg_id.split(' ')
-        assert len(tokens) <= 2
-        assert len(tokens) >= 1
-        assert tokens[0][-1] != '+' # the trailing + indicates uncommitted changes
-        if len(tokens) == 2:
-            assert tokens[1] == 'tip\n'
+            #status_codes = [line[0] for line in  if line and line[0] != '?']
+            for line in status.split('\n'):
+                if not line: continue
+                if line[0] != '?':
+                    raise Exception('Uncommitted modification to "%s" in %s (%s)'
+                        %(line[2:], location,folder))
+                if line[0] == '?' and line[-3:] == '.py':
+                    raise Exception('Untracked file "%s" in %s (%s)'
+                        %(line[2:], location, folder))
+
+            hg_id = _subprocess.Popen(('hg','id'),cwd=folder,stdout=_subprocess.PIPE).communicate()[0]
 
-        _cache[module_name] = tokens[0]
+            # This asserts my understanding of hg id return values
+            # There is mention in the doc that it might return two parent hash codes
+            # but I've never seen it, and I dont' know what it means or how it is
+            # formatted.
+            tokens = hg_id.split(' ')
+            assert len(tokens) <= 2
+            assert len(tokens) >= 1
+            if tokens[0][-1] == '+' :
+                tokens[0] = tokens[0][:-1] # the change was not on this file
+            if len(tokens) == 2:
+                assert tokens[1] == 'tip\n'
+
+            _cache[module_name] = tokens[0]
+
 
     return _cache[module_name]
 
 
+
+def get_all_src_versions() :
+    """
+    Get the version of all loaded module.
+    Calls src_version on all loaded modules. These modules are found
+    using sys.modules.
+
+    Returns a dictionnary: name->version.
+    
+    @RETURN dict Dictionnary (module's name) -> (version)
+    @SEE src_version
+    """
+    allmodules = sys.modules
+    d = dict()
+    for m in allmodules :
+        d[m] = src_version(m)
+    return d
+
+
+if __name__ == "__main__" :
+
+    if len(sys.argv) == 2 :
+        print 'testing on', sys.argv[1]
+        print src_version(sys.argv[1])
+