Mercurial > pylearn
annotate version.py @ 357:2291a244a887
Added common.string
author | Joseph Turian <turian@iro.umontreal.ca> |
---|---|
date | Thu, 19 Jun 2008 16:27:02 -0400 |
parents | 7a734dba4cac |
children |
rev | line source |
---|---|
308 | 1 import subprocess as _subprocess |
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 | 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 |
325
7a734dba4cac
wrote back _cache
Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
parents:
324
diff
changeset
|
7 _cache = dict() |
308 | 8 |
9 def src_version(module_name): | |
10 """Return compact identifier of module code. | |
11 | |
12 @return: compact identifier of module code. | |
13 @rtype: string | |
14 | |
15 @note: This function tries to establish that the source files and the repo | |
16 are syncronized. It raises an Exception if there are un-tracked '.py' | |
17 files, or if there are un-committed modifications. This implementation uses | |
18 "hg id" to establish this. The code returned by "hg id" is not affected by | |
19 hg pull, but pulling might remove the " tip" string which might have | |
20 appeared. This implementation ignores the " tip" information, and only | |
21 uses the code. | |
22 | |
23 @note: This implementation is assumes that the import directory is under | |
24 version control by mercurial. | |
25 | |
26 """ | |
27 | |
28 if module_name not in _cache: | |
29 | |
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
|
30 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
|
31 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
|
32 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
|
33 _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
|
34 return None |
308 | 35 #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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 # 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
|
43 #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
|
44 #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
|
45 _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
|
46 return None |
308 | 47 |
48 | |
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
|
49 # 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
|
50 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
|
51 |
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 # 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
|
53 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
|
54 _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
|
55 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
|
56 |
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 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
|
58 #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
|
59 #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
|
60 |
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 #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
|
62 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
|
63 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
|
64 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
|
65 raise Exception('Uncommitted modification to "%s" in %s (%s)' |
308 | 66 %(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
|
67 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
|
68 raise Exception('Untracked file "%s" in %s (%s)' |
308 | 69 %(line[2:], __name__, location)) |
70 | |
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
|
71 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
|
72 |
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 # 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
|
74 # 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
|
75 # 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
|
76 # 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 |
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 _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
|
85 |
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 # 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
|
87 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
|
88 |
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 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
|
90 # 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
|
91 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
|
92 _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
|
93 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
|
94 |
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 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
|
96 #print 'status =', status |
308 | 97 |
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
|
98 #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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 %(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
|
104 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
|
105 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
|
106 %(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
|
107 |
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
|
108 hg_id = _subprocess.Popen(('hg','id'),cwd=folder,stdout=_subprocess.PIPE).communicate()[0] |
308 | 109 |
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
|
110 # 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
|
111 # 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
|
112 # 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
|
113 # 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 |
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 _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
|
123 |
308 | 124 |
125 return _cache[module_name] | |
126 | |
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
|
127 _unknown_version = 'unknown version' |
308 | 128 |
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
|
129 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
|
130 """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
|
131 |
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 @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
|
133 @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
|
134 |
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 @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
|
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 """ |
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 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
|
139 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
|
140 |
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 #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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 (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
|
148 |
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 #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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 (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
|
156 |
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 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
|
158 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
|
159 |
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 # 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 #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
|
169 #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
|
170 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
|
171 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
|
172 |
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 # 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
|
174 # 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
|
175 # 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 |
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 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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 |
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 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
|
193 #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
|
194 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
|
195 |
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 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 |
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 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 |
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 #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
|
210 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
|
211 |
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 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 |
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 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
|
228 |
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 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
|
230 """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
|
231 |
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 @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
|
233 @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
|
234 |
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 @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
|
236 @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
|
237 |
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 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
|
239 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
|
240 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
|
241 |
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 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
|
243 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 |
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 @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
|
253 __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
|
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 """ |
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 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
|
257 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
|
258 |
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 #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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 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
|
265 |
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 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
|
267 """ |
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 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
|
269 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
|
270 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
|
271 |
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 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
|
273 |
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 @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
|
275 @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
|
276 """ |
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 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
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 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
|
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 |
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 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
|
288 |
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 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
|
290 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
|
291 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
|
292 |