comparison src/tools/utils.py @ 1375:3a20312d4012

core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
author Goffi <goffi@goffi.org>
date Thu, 19 Mar 2015 19:47:01 +0100
parents 1a759096ccbd
children 28fd9e838f8f
comparison
equal deleted inserted replaced
1374:0befb14ecf62 1375:3a20312d4012
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """ various useful methods """ 20 """ various useful methods """
21 21
22 import unicodedata 22 import unicodedata
23 import os.path
24 from sat.core.log import getLogger
25 log = getLogger(__name__)
23 26
24 27
25 def clean_ustr(ustr): 28 def clean_ustr(ustr):
26 """Clean unicode string 29 """Clean unicode string
30
27 remove special characters from unicode string""" 31 remove special characters from unicode string"""
28 def valid_chars(unicode_source): 32 def valid_chars(unicode_source):
29 for char in unicode_source: 33 for char in unicode_source:
30 if unicodedata.category(char) == 'Cc' and char!='\n': 34 if unicodedata.category(char) == 'Cc' and char!='\n':
31 continue 35 continue
32 yield char 36 yield char
33 return ''.join(valid_chars(ustr)) 37 return ''.join(valid_chars(ustr))
34 38
39 def getRepositoryData(as_string=True):
40 """Retrieve info on current mecurial repository
41
42 @param as_string(bool): if True return a string, else return a dictionary
43 @return (unicode, dictionary): retrieved info in a nice string,
44 or a dictionary with retrieved data (key is not present if data is not found),
45 key can be:
46 - node: full revision number (40 bits)
47 - branch: branch name
48 - date: ISO 8601 format date
49 - tag: latest tag used in hierarchie
50 """
51 from distutils.spawn import find_executable
52 import subprocess
53 import sat
54 KEYS=("node", "branch", "date", "tag")
55 sat_root = os.path.dirname(sat.__file__)
56 hg_path = find_executable('hg')
57
58 if hg_path is not None:
59 os.chdir(sat_root)
60 try:
61 hg_data_raw = subprocess.check_output(["hg","parents","--template","{node}\n{branch}\n{date|isodate}\n{latesttag}"])
62 except subprocess.CalledProcessError:
63 hg_data = {}
64 else:
65 hg_data = dict(zip(KEYS, hg_data_raw.split('\n')))
66 try:
67 hg_data['modified'] = '+' in subprocess.check_output(["hg","id","-i"])
68 except subprocess.CalledProcessError:
69 pass
70
71 if not hg_data:
72 log.debug(u"Mercurial not available or working, trying to get data from dirstate")
73 os.chdir(os.path.relpath('..', sat_root))
74 try:
75 with open('.hg/dirstate') as hg_dirstate:
76 hg_data['node'] = hg_dirstate.read(20).encode('hex')
77 except IOError:
78 log.warning(u"Can't access repository data")
79
80 if as_string:
81 if not hg_data:
82 return u'repository data unknown'
83 strings = [u'rev', hg_data['node']]
84 try:
85 if hg_data['modified']:
86 strings.append(u"[M]")
87 except KeyError:
88 pass
89 try:
90 strings.extend([u'({branch} {date})'.format(**hg_data)])
91 except KeyError:
92 pass
93
94 return u' '.join(strings)
95 else:
96 return hg_data