annotate version.py @ 314:105b54ac8260

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