annotate version.py @ 324:ce79bf5fa463

- the cut and paste between file and dir conditions is always a bad thing - i made one function (hg_version) to basically call and parse hg - i made a function to include the cases of what might be returned by imp.find_modules (_input_id) - the check for a .hg folder was insufficient. Lots of things could go wrong. Instead I use the return code from the Popen process. The return code catches this and any other problem that hg runs into. - its easier to offer more rcs support in future (cvs,svn,git)
author James Bergstra <bergstrj@iro.umontreal.ca>
date Thu, 12 Jun 2008 20:54:49 -0400
parents 44f94ffe28f7
children 7a734dba4cac
rev   line source
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
1 import subprocess as _subprocess
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
2 import imp as _imp
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
3 import sys
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
4 import os
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
5
324
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
6
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
7
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
8 def src_version(module_name):
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
9 """Return compact identifier of module code.
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
10
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
11 @return: compact identifier of module code.
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
12 @rtype: string
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
13
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
14 @note: This function tries to establish that the source files and the repo
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
15 are syncronized. It raises an Exception if there are un-tracked '.py'
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
16 files, or if there are un-committed modifications. This implementation uses
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
17 "hg id" to establish this. The code returned by "hg id" is not affected by
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
18 hg pull, but pulling might remove the " tip" string which might have
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
19 appeared. This implementation ignores the " tip" information, and only
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
20 uses the code.
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
21
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
22 @note: This implementation is assumes that the import directory is under
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
23 version control by mercurial.
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
24
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
25 """
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
26
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
27 if module_name not in _cache:
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
28
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
29 try :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
30 location = _imp.find_module(module_name)[1]
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
31 except ImportError:
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
32 _cache[module_name] = None
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
33 return None
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
34 #print 'location:', location
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
35 isdir = False
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
36 if os.path.isdir(location) :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
37 isdir = True
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
38 elif os.path.isfile(location) :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
39 isdir = False
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
40 else :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
41 # SEEMS THIS CASE EXIST, FOR WEIRD BUILTIN FUNCTIONS
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
42 #print location,": it's 'not a dir, it's not a file, it's superman!"
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
43 #raise Exception('Unknown location or file type')
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
44 _cache[module_name] = None
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
45 return None
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
46
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
47
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
48 # we're dealing with a dir
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
49 if isdir :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
50
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
51 # under hg?
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
52 if not os.path.exists( os.path.join( location , '.hg') ) :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
53 _cache[module_name] = None
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
54 return None
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
55
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
56 status = _subprocess.Popen(('hg','st'),cwd=location,stdout=_subprocess.PIPE).communicate()[0]
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
57 #print 'status =', status
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
58 #TODO: check that the process return code is 0 (ticket #45)
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
59
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
60 #status_codes = [line[0] for line in if line and line[0] != '?']
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
61 for line in status.split('\n'):
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
62 if not line: continue
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
63 if line[0] != '?':
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
64 raise Exception('Uncommitted modification to "%s" in %s (%s)'
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
65 %(line[2:], __name__,location))
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
66 if line[0] == '?' and line[-3:] == '.py':
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
67 raise Exception('Untracked file "%s" in %s (%s)'
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
68 %(line[2:], __name__, location))
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
69
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
70 hg_id = _subprocess.Popen(('hg','id'),cwd=location,stdout=_subprocess.PIPE).communicate()[0]
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
71
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
72 # This asserts my understanding of hg id return values
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
73 # There is mention in the doc that it might return two parent hash codes
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
74 # but I've never seen it, and I dont' know what it means or how it is
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
75 # formatted.
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
76 tokens = hg_id.split(' ')
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
77 assert len(tokens) <= 2
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
78 assert len(tokens) >= 1
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
79 assert tokens[0][-1] != '+' # the trailing + indicates uncommitted changes
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
80 if len(tokens) == 2:
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
81 assert tokens[1] == 'tip\n'
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
82
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
83 _cache[module_name] = tokens[0]
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
84
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
85 # we're dealing with a file
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
86 if not isdir :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
87
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
88 folder = os.path.split( os.path.abspath(location) )[0]
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
89 # under hg?
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
90 if not os.path.exists( os.path.join( folder , '.hg') ) :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
91 _cache[module_name] = None
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
92 return None
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
93
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
94 status = _subprocess.Popen(('hg','st',location),cwd=folder,stdout=_subprocess.PIPE).communicate()[0]
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
95 #print 'status =', status
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
96
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
97 #status_codes = [line[0] for line in if line and line[0] != '?']
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
98 for line in status.split('\n'):
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
99 if not line: continue
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
100 if line[0] != '?':
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
101 raise Exception('Uncommitted modification to "%s" in %s (%s)'
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
102 %(line[2:], location,folder))
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
103 if line[0] == '?' and line[-3:] == '.py':
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
104 raise Exception('Untracked file "%s" in %s (%s)'
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
105 %(line[2:], location, folder))
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
106
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
107 hg_id = _subprocess.Popen(('hg','id'),cwd=folder,stdout=_subprocess.PIPE).communicate()[0]
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
108
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
109 # This asserts my understanding of hg id return values
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
110 # There is mention in the doc that it might return two parent hash codes
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
111 # but I've never seen it, and I dont' know what it means or how it is
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
112 # formatted.
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
113 tokens = hg_id.split(' ')
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
114 assert len(tokens) <= 2
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
115 assert len(tokens) >= 1
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
116 if tokens[0][-1] == '+' :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
117 tokens[0] = tokens[0][:-1] # the change was not on this file
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
118 if len(tokens) == 2:
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
119 assert tokens[1] == 'tip\n'
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
120
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
121 _cache[module_name] = tokens[0]
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
122
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
123
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
124 return _cache[module_name]
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
125
324
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
126 _unknown_version = 'unknown version'
308
9ebc960260c5 init of version.py
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
127
324
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
128 def hg_version(dirname, filenames=None):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
129 """Return current changeset of directory I{dirname}.
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
130
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
131 @type filename: list of str (or default: None)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
132 @param filename: if specified, we ignore modifications to other files.
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
133
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
134 @rtype: tuple (last changeset, modified)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
135
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
136 """
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
137 if type(filenames) not in (list, tuple, type(None)):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
138 raise TypeError(filenames)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
139
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
140 #may raise exception, for example if hg is not visible via PATH
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
141 status_proc = _subprocess.Popen(('hg','st'), cwd=dirname,
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
142 stdout=_subprocess.PIPE, stderr=_subprocess.PIPE)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
143 status = status_proc.communicate()[0] #read stdout into buffer
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
144 if status_proc.returncode != 0:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
145 raise OSError('hg returned %i, maybe %s is not under hg control?',
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
146 (status_proc.returncode, dirname))
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
147
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
148 #may raise exception, for example if hg is not visible via PATH
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
149 id_proc = _subprocess.Popen(('hg','id', '-i'), cwd=dirname,
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
150 stdout=_subprocess.PIPE, stderr=_subprocess.PIPE)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
151 id_stdout = id_proc.communicate()[0]
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
152 if id_proc.returncode != 0:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
153 raise OSError('hg returned %i, maybe %s is not under hg control?',
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
154 (id_proc.returncode, dirname))
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
155
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
156 care_about = (lambda some_file : True) if filenames is None \
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
157 else (lambda some_file : some_file in filenames)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
158
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
159 # parse status codes for what we care about
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
160 care_about_mod = False
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
161 for line in status.split('\n'):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
162 if not line: #empty lines happen
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
163 continue
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
164 line_file = line[2:]
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
165 if line[0] != '?' and care_about(line_file):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
166 care_about_mod = True
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
167 #raise Exception('Uncommitted modification',
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
168 #os.path.join(dirname, line_file))
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
169 if line[0] == '?' and line[-3:] == '.py':
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
170 print >> sys.stderr, 'WARNING: untracked file', os.path.join(dirname, line_file)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
171
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
172 # id_stdout is 12 hex digits followed by '+\n' or '\n'
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
173 # return the trailing '+' character only if there were changes to files that
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
174 # the caller cares about (named in filenames)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
175 modified = (id_stdout[12] == '+')
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
176 assert len(id_stdout) in (13, 14) #sanity check
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
177 if modified and care_about_mod :
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
178 return id_stdout[:13]
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
179 else:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
180 return id_stdout[:12]
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
181
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
182 def _import_id_py_source(location):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
183 try:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
184 dirname = os.path.dirname(location[1])
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
185 basename = os.path.basename(location[1])
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
186 return hg_version(dirname, [basename])
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
187 except OSError, e:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
188 print >> sys.stderr, 'IGNORNING', e
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
189 return _unknown_version + ' PY_SOURCE'
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
190
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
191 def _import_id_py_compiled(location):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
192 #a .pyc file was found, but no corresponding .py
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
193 return _unknown_version + ' PYC_COMPILED'
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
194
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
195 def _import_id_pkg_directory(location):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
196 try:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
197 return hg_version(location[1])
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
198 except OSError, e:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
199 print >> sys.stderr, 'IGNORNING', e
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
200 return _unknown_version + ' PKG_DIRECTORY'
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
201
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
202 def _import_id(tag):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
203 try :
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
204 location = _imp.find_module(tag)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
205 except ImportError, e: #raise when tag is not found
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
206 return e #put this in the cache, import_id will raise it
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
207
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
208 #the find_module was successful, location is valid
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
209 resource_type = location[2][2]
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
210
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
211 if resource_type == _imp.PY_SOURCE:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
212 return _import_id_py_source(location)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
213 if resource_type == _imp.PY_COMPILED:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
214 return _import_id_py_compiled(location)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
215 if resource_type == _imp.C_EXTENSION:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
216 raise NoteImplementedError
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
217 if resource_type == _imp.PY_RESOURCE:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
218 raise NoteImplementedError
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
219 if resource_type == _imp.PKG_DIRECTORY:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
220 return _import_id_pkg_directory(location)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
221 if resource_type == _imp.C_BUILTIN:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
222 raise NoteImplementedError
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
223 if resource_type == _imp.PY_FROZEN:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
224 raise NoteImplementedError
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
225
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
226 assert False #the list of resource types above should be exhaustive
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
227
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
228 def import_id(tag):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
229 """Return an identifier of the code imported by 'import <tag>'.
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
230
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
231 @param tag: a module or file name
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
232 @type tag: string
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
233
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
234 @rtype: string
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
235 @return: identifier of the code imported by 'import <tag>'.
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
236
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
237 This high-level function might do different things depending on, for
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
238 example, whether I{tag} identifies a file or a directory, or whether the
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
239 named entity is under some sort of version/revision control.
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
240
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
241 Versions are sought in the following order:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
242 0. If I{tag} is 'python' then sys.version will be returned
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
243 1. If I{tag} names a file or folder under revision control, this function
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
244 will attempt to guess which one, and return a string that identifies the
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
245 running code (a revision id, not the whole file!)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
246 2. If I{tag} names a module with a __version__ attribute, then that
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
247 attribute will be returned as a string.
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
248 3. The string starting with 'unknown version' will be returned for other valid modules.
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
249 4. An exception will be raise for non-existent modules.
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
250
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
251 @note: This function may import the named entity in order to return a
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
252 __version__ module attribute.
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
253
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
254 """
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
255 if tag not in import_id.cache:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
256 import_id.cache[tag] = _import_id(tag)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
257
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
258 #in the case of bad module names, we cached the ImportError exception
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
259 rval = import_id.cache[tag]
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
260 if isinstance(rval, Exception):
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
261 raise rval
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
262 return rval
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
263 import_id.cache = {'python':sys.version}
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
264
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
265 def get_all_src_versions() :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
266 """
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
267 Get the version of all loaded module.
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
268 Calls src_version on all loaded modules. These modules are found
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
269 using sys.modules.
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
270
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
271 Returns a dictionnary: name->version.
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
272
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
273 @RETURN dict Dictionnary (module's name) -> (version)
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
274 @SEE src_version
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
275 """
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
276 allmodules = sys.modules
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
277 d = dict()
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
278 for m in allmodules :
324
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
279 try:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
280 d[m] = import_id(m)
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
281 except:
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
282 pass
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
283 return d
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
284
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
285
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
286 if __name__ == "__main__" :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
287
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
288 if len(sys.argv) == 2 :
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
289 print 'testing on', sys.argv[1]
324
ce79bf5fa463 - the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 323
diff changeset
290 print import_id(sys.argv[1])
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
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents: 308
diff changeset
291