comparison version.py @ 308:9ebc960260c5

init of version.py
author James Bergstra <bergstrj@iro.umontreal.ca>
date Tue, 10 Jun 2008 20:02:25 -0400
parents
children 44f94ffe28f7
comparison
equal deleted inserted replaced
307:29c5ad01e9ce 308:9ebc960260c5
1 import subprocess as _subprocess
2 import imp as _imp
3
4 _cache = {}
5
6 def src_version(module_name):
7 """Return compact identifier of module code.
8
9 @return: compact identifier of module code.
10 @rtype: string
11
12 @note: This function tries to establish that the source files and the repo
13 are syncronized. It raises an Exception if there are un-tracked '.py'
14 files, or if there are un-committed modifications. This implementation uses
15 "hg id" to establish this. The code returned by "hg id" is not affected by
16 hg pull, but pulling might remove the " tip" string which might have
17 appeared. This implementation ignores the " tip" information, and only
18 uses the code.
19
20 @note: This implementation is assumes that the import directory is under
21 version control by mercurial.
22
23 """
24 #
25 # NOTE
26 #
27 # If you find bugs in this function, please update the __src_version__
28 # function in theano, pylearn, and email either theano-dev or pylearn-dev so
29 # that people can update their experiment dirs (the output of this function
30 # is meant to be hard-coded in external files).
31 #
32
33 if module_name not in _cache:
34
35 location = _imp.find_module(module_name)[1]
36 #print 'location:', location
37
38 status = _subprocess.Popen(('hg','st'),cwd=location,stdout=_subprocess.PIPE).communicate()[0]
39 #TODO: check that the process return code is 0 (ticket #45)
40
41 #status_codes = [line[0] for line in if line and line[0] != '?']
42 for line in status.split('\n'):
43 if not line: continue
44 if line[0] != '?':
45 raise Exception('Uncommitted modification to "%s" in %s (%s)'
46 %(line[2:], __name__,location))
47 if line[0] == '?' and line[-3:] == '.py':
48 raise Exception('Untracked file "%s" in %s (%s)'
49 %(line[2:], __name__, location))
50
51 hg_id = _subprocess.Popen(('hg','id'),cwd=location,stdout=_subprocess.PIPE).communicate()[0]
52
53 #This asserts my understanding of hg id return values
54 # There is mention in the doc that it might return two parent hash codes
55 # but I've never seen it, and I dont' know what it means or how it is
56 # formatted.
57 tokens = hg_id.split(' ')
58 assert len(tokens) <= 2
59 assert len(tokens) >= 1
60 assert tokens[0][-1] != '+' # the trailing + indicates uncommitted changes
61 if len(tokens) == 2:
62 assert tokens[1] == 'tip\n'
63
64 _cache[module_name] = tokens[0]
65
66 return _cache[module_name]
67
68