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