Mercurial > pylearn
annotate pylearn/version.py @ 1345:6bae1852cff4
make version detection work around a strange behavior of imp.find_module
author | Frederic Bastien <nouiz@nouiz.org> |
---|---|
date | Wed, 27 Oct 2010 10:36:55 -0400 |
parents | 2b8b991ac5fa |
children | 5e893cd6daae |
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 |
901
a88c1746b068
copy the fct record_version from LeDeepNet
Frederic Bastien <nouiz@nouiz.org>
parents:
900
diff
changeset
|
9 def record_versions(results, modules, prefix='version_'): |
902
e966d5ab8103
added comment to record_versions
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
901
diff
changeset
|
10 """Bizarre version-recording function... |
e966d5ab8103
added comment to record_versions
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
901
diff
changeset
|
11 |
e966d5ab8103
added comment to record_versions
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
901
diff
changeset
|
12 For each module in `modules` it executes |
e966d5ab8103
added comment to record_versions
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
901
diff
changeset
|
13 result.<prefix><module.__name__> = import_id(module.__name) |
e966d5ab8103
added comment to record_versions
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
901
diff
changeset
|
14 |
e966d5ab8103
added comment to record_versions
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
901
diff
changeset
|
15 :returns: None |
e966d5ab8103
added comment to record_versions
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
901
diff
changeset
|
16 """ |
901
a88c1746b068
copy the fct record_version from LeDeepNet
Frederic Bastien <nouiz@nouiz.org>
parents:
900
diff
changeset
|
17 for module in modules: |
a88c1746b068
copy the fct record_version from LeDeepNet
Frederic Bastien <nouiz@nouiz.org>
parents:
900
diff
changeset
|
18 setattr(results, prefix+module.__name__, import_id(module.__name__)) |
a88c1746b068
copy the fct record_version from LeDeepNet
Frederic Bastien <nouiz@nouiz.org>
parents:
900
diff
changeset
|
19 |
308 | 20 def src_version(module_name): |
21 """Return compact identifier of module code. | |
22 | |
23 @return: compact identifier of module code. | |
24 @rtype: string | |
25 | |
26 @note: This function tries to establish that the source files and the repo | |
27 are syncronized. It raises an Exception if there are un-tracked '.py' | |
28 files, or if there are un-committed modifications. This implementation uses | |
29 "hg id" to establish this. The code returned by "hg id" is not affected by | |
30 hg pull, but pulling might remove the " tip" string which might have | |
31 appeared. This implementation ignores the " tip" information, and only | |
32 uses the code. | |
33 | |
34 @note: This implementation is assumes that the import directory is under | |
35 version control by mercurial. | |
36 | |
37 """ | |
38 | |
39 if module_name not in _cache: | |
40 | |
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
|
41 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
|
42 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
|
43 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
|
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 | 46 #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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 # 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
|
54 #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
|
55 #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
|
56 _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
|
57 return None |
308 | 58 |
59 | |
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
|
60 # 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
|
61 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
|
62 |
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 # 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
|
64 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
|
65 _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
|
66 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
|
67 |
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 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
|
69 #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
|
70 #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
|
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 #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
|
73 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
|
74 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
|
75 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
|
76 raise Exception('Uncommitted modification to "%s" in %s (%s)' |
308 | 77 %(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
|
78 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
|
79 raise Exception('Untracked file "%s" in %s (%s)' |
308 | 80 %(line[2:], __name__, location)) |
81 | |
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
|
82 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
|
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 # 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
|
85 # 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
|
86 # 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
|
87 # 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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
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 _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
|
96 |
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 # 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
|
98 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
|
99 |
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 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
|
101 # 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
|
102 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
|
103 _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
|
104 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
|
105 |
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 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
|
107 #print 'status =', status |
308 | 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 #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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 %(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
|
115 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
|
116 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
|
117 %(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
|
118 |
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 hg_id = _subprocess.Popen(('hg','id'),cwd=folder,stdout=_subprocess.PIPE).communicate()[0] |
308 | 120 |
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
|
121 # 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
|
122 # 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
|
123 # 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
|
124 # 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
|
125 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 |
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
|
133 _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
|
134 |
308 | 135 |
136 return _cache[module_name] | |
137 | |
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
|
138 _unknown_version = 'unknown version' |
308 | 139 |
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
|
140 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
|
141 """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
|
142 |
ce79bf5fa463
- 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 @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
|
144 @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
|
145 |
ce79bf5fa463
- 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 @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
|
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 """ |
ce79bf5fa463
- 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 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
|
150 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
|
151 |
ce79bf5fa463
- 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 #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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 (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
|
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 #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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 (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
|
167 |
900 | 168 if filenames is None: |
169 care_about = (lambda some_file : True) | |
170 else: | |
171 care_about = (lambda some_file : some_file in filenames) | |
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
|
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 # 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 #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
|
182 #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
|
183 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
|
184 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
|
185 |
ce79bf5fa463
- 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 # 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
|
187 # 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
|
188 # 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
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_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
|
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 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
|
199 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
|
200 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
|
201 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
|
202 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
|
203 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
|
204 |
ce79bf5fa463
- 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 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
|
206 #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
|
207 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
|
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 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 |
ce79bf5fa463
- 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 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
|
217 try : |
1343
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
218 mod = __import__(tag) |
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
|
219 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
|
220 return e #put this in the cache, import_id will raise it |
1345
6bae1852cff4
make version detection work around a strange behavior of imp.find_module
Frederic Bastien <nouiz@nouiz.org>
parents:
1344
diff
changeset
|
221 |
6bae1852cff4
make version detection work around a strange behavior of imp.find_module
Frederic Bastien <nouiz@nouiz.org>
parents:
1344
diff
changeset
|
222 try: |
6bae1852cff4
make version detection work around a strange behavior of imp.find_module
Frederic Bastien <nouiz@nouiz.org>
parents:
1344
diff
changeset
|
223 location = _imp.find_module(tag) |
6bae1852cff4
make version detection work around a strange behavior of imp.find_module
Frederic Bastien <nouiz@nouiz.org>
parents:
1344
diff
changeset
|
224 except ImportError, e: #raise when tag is not found |
6bae1852cff4
make version detection work around a strange behavior of imp.find_module
Frederic Bastien <nouiz@nouiz.org>
parents:
1344
diff
changeset
|
225 #sometimes fin_module raise an ImportError even when we can import it |
6bae1852cff4
make version detection work around a strange behavior of imp.find_module
Frederic Bastien <nouiz@nouiz.org>
parents:
1344
diff
changeset
|
226 if hasattr(mod,'__version__'): |
6bae1852cff4
make version detection work around a strange behavior of imp.find_module
Frederic Bastien <nouiz@nouiz.org>
parents:
1344
diff
changeset
|
227 return mod.__version__ |
6bae1852cff4
make version detection work around a strange behavior of imp.find_module
Frederic Bastien <nouiz@nouiz.org>
parents:
1344
diff
changeset
|
228 return e |
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
|
229 #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
|
230 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
|
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 if resource_type == _imp.PY_SOURCE: |
1343
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
233 ret = _import_id_py_source(location) |
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
|
234 if resource_type == _imp.PY_COMPILED: |
1343
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
235 ret = _import_id_py_compiled(location) |
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
|
236 if resource_type == _imp.C_EXTENSION: |
1344
2b8b991ac5fa
give the NotImplementedError a string message
Frederic Bastien <nouiz@nouiz.org>
parents:
1343
diff
changeset
|
237 raise NotImplementedError('Not implemented the version recording of C_EXTENSION module') |
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
|
238 if resource_type == _imp.PY_RESOURCE: |
1344
2b8b991ac5fa
give the NotImplementedError a string message
Frederic Bastien <nouiz@nouiz.org>
parents:
1343
diff
changeset
|
239 raise NotImplementedError('Not implemented the version recording of PY_RESOURCE module') |
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
|
240 if resource_type == _imp.PKG_DIRECTORY: |
1343
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
241 ret = _import_id_pkg_directory(location) |
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
|
242 if resource_type == _imp.C_BUILTIN: |
1344
2b8b991ac5fa
give the NotImplementedError a string message
Frederic Bastien <nouiz@nouiz.org>
parents:
1343
diff
changeset
|
243 raise NotImplementedError('Not implemented the version recording of C_BUILTIN module') |
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
|
244 if resource_type == _imp.PY_FROZEN: |
1344
2b8b991ac5fa
give the NotImplementedError a string message
Frederic Bastien <nouiz@nouiz.org>
parents:
1343
diff
changeset
|
245 raise NotImplementedError('Not implemented the version recording of PY_FROZEN module') |
1343
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
246 #the list of resource types above should be exhaustive |
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
|
247 |
1343
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
248 if ret.startswith(_unknown_version): |
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
249 if hasattr(mod,'__version__'): |
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
250 return mod.__version__ |
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
251 else: |
cf0fc12a50f7
record_version work with module that are not checkout and have __version__ defined(as numpy).
Frederic Bastien <nouiz@nouiz.org>
parents:
922
diff
changeset
|
252 return ret |
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
|
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 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
|
255 """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
|
256 |
ce79bf5fa463
- 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 @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
|
258 @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
|
259 |
ce79bf5fa463
- 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 @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
|
261 @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
|
262 |
ce79bf5fa463
- 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 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
|
264 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
|
265 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
|
266 |
ce79bf5fa463
- the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
323
diff
changeset
|
267 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
|
268 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
|
269 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
|
270 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
|
271 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
|
272 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
|
273 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
|
274 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
|
275 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
|
276 |
ce79bf5fa463
- the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
323
diff
changeset
|
277 @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
|
278 __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
|
279 |
ce79bf5fa463
- 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 """ |
ce79bf5fa463
- 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 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
|
282 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
|
283 |
ce79bf5fa463
- the cut and paste between file and dir conditions is always a bad thing
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
323
diff
changeset
|
284 #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
|
285 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
|
286 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
|
287 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
|
288 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
|
289 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
|
290 |
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 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
|
292 """ |
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
|
293 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
|
294 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
|
295 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
|
296 |
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
|
297 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
|
298 |
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
|
299 @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
|
300 @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
|
301 """ |
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
|
302 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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 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
|
308 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
|
309 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
|
310 |
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
|
311 |
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
|
312 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
|
313 |
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
|
314 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
|
315 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
|
316 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
|
317 |