Mercurial > libervia-backend
annotate src/tools/utils.py @ 2320:f4a0723042ee
jp (pubsub/search): fixed encoding for stdin in external commands
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 09 Jul 2017 15:20:31 +0200 |
parents | 228d208fb5db |
children | 8b37a62336c3 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1858
diff
changeset
|
1 #!/usr/bin/env python2 |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
4 # SAT: a jabber client |
1766 | 5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
10 # (at your option) any later version. |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
15 # GNU Affero General Public License for more details. |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ various useful methods """ |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 import unicodedata |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
23 import os.path |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
24 from sat.core.log import getLogger |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
25 log = getLogger(__name__) |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
26 import datetime |
2238
228d208fb5db
core (tools/utils): added method to parse XMPP dates
Goffi <goffi@goffi.org>
parents:
2181
diff
changeset
|
27 import dateutil |
228d208fb5db
core (tools/utils): added method to parse XMPP dates
Goffi <goffi@goffi.org>
parents:
2181
diff
changeset
|
28 import calendar |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
29 import time |
2089
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1935
diff
changeset
|
30 import sys |
2181
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
31 import random |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
32 |
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
33 |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 def clean_ustr(ustr): |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 """Clean unicode string |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
36 |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
37 remove special characters from unicode string |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
38 """ |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 def valid_chars(unicode_source): |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 for char in unicode_source: |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 if unicodedata.category(char) == 'Cc' and char!='\n': |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 continue |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 yield char |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 return ''.join(valid_chars(ustr)) |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
46 def xmpp_date(timestamp=None, with_time=True): |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
47 """Return date according to XEP-0082 specification |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
48 |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
49 to avoid reveling the timezone, we always return UTC dates |
1935
1128feb54180
core: removed pyfeed and xe dependencies:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
50 the string returned by this method is valid with RFC 3339 |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
51 @param timestamp(None, float): posix timestamp. If None current time will be used |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
52 @param with_time(bool): if True include the time |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
53 @return(unicode): XEP-0082 formatted date and time |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
54 """ |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
55 template_date = u"%Y-%m-%d" |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
56 template_time = u"%H:%M:%SZ" |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
57 template = u"{}T{}".format(template_date, template_time) if with_time else template_date |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
58 return datetime.datetime.utcfromtimestamp(time.time() if timestamp is None else timestamp).strftime(template) |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
59 |
2238
228d208fb5db
core (tools/utils): added method to parse XMPP dates
Goffi <goffi@goffi.org>
parents:
2181
diff
changeset
|
60 def date_parse(value): |
228d208fb5db
core (tools/utils): added method to parse XMPP dates
Goffi <goffi@goffi.org>
parents:
2181
diff
changeset
|
61 """Parse a date and return corresponding unix timestamp |
228d208fb5db
core (tools/utils): added method to parse XMPP dates
Goffi <goffi@goffi.org>
parents:
2181
diff
changeset
|
62 |
228d208fb5db
core (tools/utils): added method to parse XMPP dates
Goffi <goffi@goffi.org>
parents:
2181
diff
changeset
|
63 @param value(unicode): date to parse, in any format supported by dateutil.parser |
228d208fb5db
core (tools/utils): added method to parse XMPP dates
Goffi <goffi@goffi.org>
parents:
2181
diff
changeset
|
64 """ |
228d208fb5db
core (tools/utils): added method to parse XMPP dates
Goffi <goffi@goffi.org>
parents:
2181
diff
changeset
|
65 return calendar.timegm(dateutil.parser.parse(unicode(value)).utctimetuple()) |
228d208fb5db
core (tools/utils): added method to parse XMPP dates
Goffi <goffi@goffi.org>
parents:
2181
diff
changeset
|
66 |
2181
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
67 def generatePassword(vocabulary=None, size=20): |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
68 """Generate a password with random characters. |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
69 |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
70 @param vocabulary(iterable): characters to use to create password |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
71 @param size(int): number of characters in the password to generate |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
72 @return (unicode): generated password |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
73 """ |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
74 random.seed() |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
75 if vocabulary is None: |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
76 vocabulary = [chr(i) for i in range(0x30,0x3A) + range(0x41,0x5B) + range (0x61,0x7B)] |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
77 return u''.join([random.choice(vocabulary) for i in range(15)]) |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
78 |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
79 def getRepositoryData(module, as_string=True, is_path=False, save_dir_path=None): |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
80 """Retrieve info on current mecurial repository |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
81 |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
82 Data is gotten by using the following methods, in order: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
83 - using "hg" executable |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
84 - looking for a ".hg_data" file in the root of the module |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
85 this file must contain the data dictionnary serialized with pickle |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
86 - looking for a .hg/dirstate in parent directory of module (or in module/.hg if |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
87 is_path is True), and parse dirstate file to get revision |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
88 @param module(unicode): module to look for (e.g. sat, libervia) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
89 module can be a path if is_path is True (see below) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
90 @param as_string(bool): if True return a string, else return a dictionary |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
91 @param is_path(bool): if True "module" is not handled as a module name, but as an |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
92 absolute path to the parent of a ".hg" directory |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
93 @param save_path(str, None): if not None, the value will be saved to given path as a pickled dict |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
94 /!\\ the .hg_data file in the given directory will be overwritten |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
95 @return (unicode, dictionary): retrieved info in a nice string, |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
96 or a dictionary with retrieved data (key is not present if data is not found), |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
97 key can be: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
98 - node: full revision number (40 bits) |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
99 - branch: branch name |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
100 - date: ISO 8601 format date |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
101 - tag: latest tag used in hierarchie |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
102 """ |
2089
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1935
diff
changeset
|
103 if sys.platform == "android": |
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1935
diff
changeset
|
104 # FIXME: workaround to avoid trouble on android, need to be fixed properly |
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1935
diff
changeset
|
105 return u"Cagou android build" |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
106 from distutils.spawn import find_executable |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
107 import subprocess |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
108 KEYS=("node", "node_short", "branch", "date", "tag") |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
109 ori_cwd = os.getcwd() |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
110 |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
111 if is_path: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
112 repos_root = module |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
113 else: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
114 repos_root = os.path.dirname(module.__file__) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
115 |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
116 hg_path = find_executable('hg') |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
117 |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
118 if hg_path is not None: |
1376
28fd9e838f8f
core: getRepositoryData now get the module in argument
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
119 os.chdir(repos_root) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
120 try: |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
121 hg_data_raw = subprocess.check_output(["hg","log", "-r", "-1", "--template","{node}\n{node|short}\n{branch}\n{date|isodate}\n{latesttag}"]) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
122 except subprocess.CalledProcessError: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
123 hg_data = {} |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
124 else: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
125 hg_data = dict(zip(KEYS, hg_data_raw.split('\n'))) |
1857
2d2617930f97
core (tools): fixed bad call to "hg id" when hg is not working to guest revision, which was the cause of an Exception
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
126 try: |
2d2617930f97
core (tools): fixed bad call to "hg id" when hg is not working to guest revision, which was the cause of an Exception
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
127 hg_data['modified'] = '+' in subprocess.check_output(["hg","id","-i"]) |
2d2617930f97
core (tools): fixed bad call to "hg id" when hg is not working to guest revision, which was the cause of an Exception
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
128 except subprocess.CalledProcessError: |
2d2617930f97
core (tools): fixed bad call to "hg id" when hg is not working to guest revision, which was the cause of an Exception
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
129 pass |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
130 else: |
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
131 hg_data = {} |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
132 |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
133 if not hg_data: |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
134 # .hg_data pickle method |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
135 log.debug(u"Mercurial not available or working, trying other methods") |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
136 if save_dir_path is None: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
137 log.debug(u"trying .hg_data method") |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
138 |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
139 try: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
140 with open(os.path.join(repos_root, '.hg_data')) as f: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
141 import cPickle as pickle |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
142 hg_data = pickle.load(f) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
143 except IOError as e: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
144 log.debug(u"Can't access .hg_data file: {}".format(e)) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
145 except pickle.UnpicklingError: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
146 log.warning(u"Error while reading {}, can't get repos data".format(f.name)) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
147 |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
148 if not hg_data: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
149 # .hg/dirstate method |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
150 log.debug(u"trying dirstate method") |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
151 if is_path: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
152 os.chdir(repos_root) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
153 else: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
154 os.chdir(os.path.relpath('..', repos_root)) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
155 try: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
156 with open('.hg/dirstate') as hg_dirstate: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
157 hg_data['node'] = hg_dirstate.read(20).encode('hex') |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
158 hg_data['node_short'] = hg_data['node'][:12] |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
159 except IOError: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
160 log.warning(u"Can't access repository data") |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
161 |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
162 # we restore original working dir |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
163 os.chdir(ori_cwd) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
164 |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
165 # data saving |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
166 if save_dir_path is not None and hg_data: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
167 if not os.path.isdir(save_dir_path): |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
168 log.warning(u"Given path is not a directory, can't save data") |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
169 else: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
170 import cPickle as pickle |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
171 dest_path = os.path.join(save_dir_path, ".hg_data") |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
172 try: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
173 with open(dest_path, 'w') as f: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
174 pickle.dump(hg_data, f, 2) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
175 except IOError as e: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
176 log.warning(u"Can't save file to {path}: {reason}".format( |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
177 path=dest_path, reason=e)) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
178 else: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
179 log.debug(u"repository data saved to {}".format(dest_path)) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
180 |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
181 if as_string: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
182 if not hg_data: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
183 return u'repository data unknown' |
1471
934e402c90bf
core: tools.utils.getRepositoryData now return "hg log -r -1" and short form of node + fixed crash if mercurial is not present:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
184 strings = [u'rev', hg_data['node_short']] |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
185 try: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
186 if hg_data['modified']: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
187 strings.append(u"[M]") |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
188 except KeyError: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
189 pass |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
190 try: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
191 strings.extend([u'({branch} {date})'.format(**hg_data)]) |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
192 except KeyError: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
193 pass |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
194 |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
195 return u' '.join(strings) |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
196 else: |
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
197 return hg_data |