Mercurial > libervia-backend
annotate libervia/backend/tools/utils.py @ 4351:6a0a081485b8
plugin autocrypt: Autocrypt protocol implementation:
Implementation of autocrypt: `autocrypt` header is checked, and if present and no public
key is known for the peer, the key is imported.
`autocrypt` header is also added to outgoing message (only if an email gateway is
detected).
For the moment, the JID is use as identifier, but the real email used by gateway should be
used in the future.
rel 456
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 28 Feb 2025 09:23:35 +0100 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
3368
e86b71b1aa31
core: minor typos, docstring/comments update
Goffi <goffi@goffi.org>
parents:
3239
diff
changeset
|
3 # SaT: an XMPP client |
3479 | 4 # Copyright (C) 2009-2021 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
|
5 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
6 # 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
|
7 # 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
|
8 # 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
|
9 # (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
|
10 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
11 # 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
|
12 # 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
|
13 # 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
|
14 # 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
|
15 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
16 # 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
|
17 # 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
|
18 |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 """ various useful methods """ |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
3860
e417c478b488
plugin XEP-0060, tools (utils): doc/type hints
Goffi <goffi@goffi.org>
parents:
3739
diff
changeset
|
21 from typing import Optional, Union |
601
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 |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
24 import datetime |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
25 import subprocess |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
26 import time |
2089
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1935
diff
changeset
|
27 import sys |
2181
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
28 import random |
2470
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
29 import inspect |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
30 import textwrap |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
31 import functools |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
32 import asyncio |
3215
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
33 from twisted.python import procutils, failure |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
34 from twisted.internet import defer |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
35 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
36 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
37 from libervia.backend.tools import xmpp_datetime |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
38 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
39 log = getLogger(__name__) |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
40 |
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
41 |
3028 | 42 NO_REPOS_DATA = "repository data unknown" |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
43 repos_cache_dict = None |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
44 repos_cache = None |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
45 |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
46 |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 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
|
48 """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
|
49 |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
50 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
|
51 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
52 |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 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
|
54 for char in unicode_source: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
55 if unicodedata.category(char) == "Cc" and char != "\n": |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 continue |
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 yield char |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
58 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
59 return "".join(valid_chars(ustr)) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
60 |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
62 def logError(failure_): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
63 """Genertic errback which log the error as a warning, and re-raise it""" |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
64 log.warning(failure_.value) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
65 raise failure_ |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
66 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
67 |
2470
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
68 def partial(func, *fixed_args, **fixed_kwargs): |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
69 # FIXME: temporary hack to workaround the fact that inspect.getargspec is not working with functools.partial |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4033
diff
changeset
|
70 # making partial unusable with current D-bus module (in add_method). |
2470
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
71 # Should not be needed anywore once moved to Python 3 |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
72 |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
73 ori_args = inspect.getargspec(func).args |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
74 func = functools.partial(func, *fixed_args, **fixed_kwargs) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
75 if ori_args[0] == "self": |
2470
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
76 del ori_args[0] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
77 ori_args = ori_args[len(fixed_args) :] |
2470
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
78 for kw in fixed_kwargs: |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
79 ori_args.remove(kw) |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
80 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
81 exec( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
82 textwrap.dedent( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
83 """\ |
2470
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
84 def method({args}): |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
85 return func({kw_args}) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
86 """ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
87 ).format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
88 args=", ".join(ori_args), kw_args=", ".join([a + "=" + a for a in ori_args]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
89 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
90 locals(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
91 ) |
2470
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
92 |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
93 return method |
8084066ac95b
tools (utils): new "partial" function which is a hack to workaround issue with functools.partial while using it with dbus module.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
94 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
95 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4033
diff
changeset
|
96 def as_deferred(func, *args, **kwargs): |
3215
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
97 """Call a method and return a Deferred |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
98 |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
99 the method can be a simple callable, a Deferred or a coroutine. |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
100 It is similar to defer.maybeDeferred, but also handles coroutines |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
101 """ |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
102 try: |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
103 ret = func(*args, **kwargs) |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
104 except Exception as e: |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
105 return defer.fail(failure.Failure(e)) |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
106 else: |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
107 if asyncio.iscoroutine(ret): |
3215
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
108 return defer.ensureDeferred(ret) |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
109 elif isinstance(ret, defer.Deferred): |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
110 return ret |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
111 elif isinstance(ret, failure.Failure): |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
112 return defer.fail(ret) |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
113 else: |
3239
4396bf14f5fc
core (tools/utils): fixed return value for non async callbacks
Goffi <goffi@goffi.org>
parents:
3215
diff
changeset
|
114 return defer.succeed(ret) |
3215
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
115 |
bfa1bde97f48
core (tools/utils): new `asDeferred` function:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
116 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
117 def aio(func): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
118 """Decorator to return a Deferred from asyncio coroutine |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
119 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
120 Functions with this decorator are run in asyncio context |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
121 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
122 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
123 def wrapper(*args, **kwargs): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
124 return defer.Deferred.fromFuture(asyncio.ensure_future(func(*args, **kwargs))) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
125 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
126 return wrapper |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
127 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
128 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
129 def as_future(d): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
130 return d.asFuture(asyncio.get_event_loop()) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
131 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
132 |
3540
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
133 def ensure_deferred(func): |
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
134 """Decorator to apply ensureDeferred to a function |
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
135 |
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
136 to be used when the function is called by third party library (e.g. wokkel) |
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
137 Otherwise, it's better to use ensureDeferred as early as possible. |
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
138 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
139 |
3540
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
140 def wrapper(*args, **kwargs): |
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
141 return defer.ensureDeferred(func(*args, **kwargs)) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
142 |
3540
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
143 return wrapper |
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
144 |
aa58451b77ba
tools (utils): new `ensure_deferred` decorator
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
145 |
3860
e417c478b488
plugin XEP-0060, tools (utils): doc/type hints
Goffi <goffi@goffi.org>
parents:
3739
diff
changeset
|
146 def xmpp_date( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
147 timestamp: Optional[Union[float, int]] = None, with_time: bool = True |
3860
e417c478b488
plugin XEP-0060, tools (utils): doc/type hints
Goffi <goffi@goffi.org>
parents:
3739
diff
changeset
|
148 ) -> str: |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
149 """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
|
150 |
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
151 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
|
152 the string returned by this method is valid with RFC 3339 |
3877
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
3860
diff
changeset
|
153 this function redirects to the functions in the :mod:`sat.tools.datetime` module |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
154 @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
|
155 @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
|
156 @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
|
157 """ |
3878
32087d7c25d4
tools (datetime, utils): fix incorrect use of naive object:
Goffi <goffi@goffi.org>
parents:
3877
diff
changeset
|
158 dtime = datetime.datetime.fromtimestamp( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
159 time.time() if timestamp is None else timestamp, datetime.timezone.utc |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
160 ) |
3877
00212260f659
plugin XEP-0420: Implementation of Stanza Content Encryption:
Syndace <me@syndace.dev>
parents:
3860
diff
changeset
|
161 |
3879
46930301f0c1
tools: renamed module `sat.tools.datetime` to `date.tools.xmpp_datetime` to avoid conflict with Python's standard lib
Goffi <goffi@goffi.org>
parents:
3878
diff
changeset
|
162 return ( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
163 xmpp_datetime.format_datetime(dtime) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
164 if with_time |
3879
46930301f0c1
tools: renamed module `sat.tools.datetime` to `date.tools.xmpp_datetime` to avoid conflict with Python's standard lib
Goffi <goffi@goffi.org>
parents:
3878
diff
changeset
|
165 else xmpp_datetime.format_date(dtime.date()) |
46930301f0c1
tools: renamed module `sat.tools.datetime` to `date.tools.xmpp_datetime` to avoid conflict with Python's standard lib
Goffi <goffi@goffi.org>
parents:
3878
diff
changeset
|
166 ) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
167 |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
168 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
169 def parse_xmpp_date(xmpp_date_str: str, with_time: bool = True) -> float: |
3898
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
170 """Get timestamp from XEP-0082 datetime |
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
171 |
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
172 @param xmpp_date_str: XEP-0082 formatted datetime or time |
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
173 @param with_time: if True, ``xmpp_date_str`` must be a datetime, otherwise if must be |
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
174 a time profile. |
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
175 @return: datetime converted to unix time |
4033
5a42c7842556
core (plugins): implementation of XEP-0215 "External Service Discovery":
Goffi <goffi@goffi.org>
parents:
3953
diff
changeset
|
176 @raise ValueError: the format is invalid |
3898
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
177 """ |
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
178 if with_time: |
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
179 dt = xmpp_datetime.parse_datetime(xmpp_date_str) |
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
180 else: |
3953
8179cff7ef5c
tools (utils): fix `parse_xmpp_date` timezone:
Goffi <goffi@goffi.org>
parents:
3898
diff
changeset
|
181 d = xmpp_datetime.parse_date(xmpp_date_str) |
8179cff7ef5c
tools (utils): fix `parse_xmpp_date` timezone:
Goffi <goffi@goffi.org>
parents:
3898
diff
changeset
|
182 dt = datetime.datetime.combine(d, datetime.datetime.min.time()) |
3898
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
183 |
3953
8179cff7ef5c
tools (utils): fix `parse_xmpp_date` timezone:
Goffi <goffi@goffi.org>
parents:
3898
diff
changeset
|
184 return dt.timestamp() |
3898
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
185 |
022ae35a9d82
tools (utils): helper `parse_xmpp_date` method:
Goffi <goffi@goffi.org>
parents:
3879
diff
changeset
|
186 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4033
diff
changeset
|
187 def generate_password(vocabulary=None, size=20): |
2181
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
188 """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
|
189 |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
190 @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
|
191 @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
|
192 @return (unicode): generated password |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
193 """ |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
194 random.seed() |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
2089
diff
changeset
|
195 if vocabulary is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
196 vocabulary = [ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
197 chr(i) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
198 for i in list(range(0x30, 0x3A)) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
199 + list(range(0x41, 0x5B)) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
200 + list(range(0x61, 0x7B)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
201 ] |
3028 | 202 return "".join([random.choice(vocabulary) for i in range(15)]) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
203 |
1502
566908d483f6
core (utils): added a method to generate XEP-0082 style dates
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
204 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4033
diff
changeset
|
205 def get_repository_data(module, as_string=True, is_path=False): |
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
|
206 """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
|
207 |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
208 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
|
209 - using "hg" executable |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
210 - 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
|
211 is_path is True), and parse dirstate file to get revision |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
212 - checking package version, which should have repository data when we are on a dev version |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
213 @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
|
214 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
|
215 @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
|
216 @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
|
217 absolute path to the parent of a ".hg" directory |
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
|
218 @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
|
219 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
|
220 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
|
221 - 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
|
222 - 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
|
223 - 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
|
224 - tag: latest tag used in hierarchie |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
225 - distance: number of commits since the last tag |
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
|
226 """ |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
227 global repos_cache_dict |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
228 if as_string: |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
229 global repos_cache |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
230 if repos_cache is not None: |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
231 return repos_cache |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
232 else: |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
233 if repos_cache_dict is not None: |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
234 return repos_cache_dict |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
235 |
2089
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1935
diff
changeset
|
236 if sys.platform == "android": |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
237 # FIXME: workaround to avoid trouble on android, need to be fixed properly |
3028 | 238 repos_cache = "Cagou android build" |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
239 return repos_cache |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
240 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
241 KEYS = ("node", "node_short", "branch", "date", "tag", "distance") |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
242 ori_cwd = os.getcwd() |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
243 |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
244 if is_path: |
2568
8e204f0d3193
tools(utils): fixed path handling in getRepositoryData
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
245 repos_root = os.path.abspath(module) |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
246 else: |
2568
8e204f0d3193
tools(utils): fixed path handling in getRepositoryData
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
247 repos_root = os.path.abspath(os.path.dirname(module.__file__)) |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
248 |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
249 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
250 hg_path = procutils.which("hg")[0] |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
251 except IndexError: |
3028 | 252 log.warning("Can't find hg executable") |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
253 hg_path = None |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
254 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
|
255 |
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
|
256 if hg_path is not None: |
1376
28fd9e838f8f
core: getRepositoryData now get the module in argument
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
257 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
|
258 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
259 hg_data_raw = subprocess.check_output( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
260 [ |
3739
0a87cae85746
tools (utils): fix `getRepositoryData` crash:
Goffi <goffi@goffi.org>
parents:
3540
diff
changeset
|
261 "python3", |
0a87cae85746
tools (utils): fix `getRepositoryData` crash:
Goffi <goffi@goffi.org>
parents:
3540
diff
changeset
|
262 hg_path, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
263 "log", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
264 "-r", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
265 "-1", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
266 "--template", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
267 "{node}\n" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
268 "{node|short}\n" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
269 "{branch}\n" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
270 "{date|isodate}\n" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
271 "{latesttag}\n" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
272 "{latesttagdistance}", |
3028 | 273 ], |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
274 text=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
275 ) |
3739
0a87cae85746
tools (utils): fix `getRepositoryData` crash:
Goffi <goffi@goffi.org>
parents:
3540
diff
changeset
|
276 except subprocess.CalledProcessError as e: |
0a87cae85746
tools (utils): fix `getRepositoryData` crash:
Goffi <goffi@goffi.org>
parents:
3540
diff
changeset
|
277 log.error(f"Can't get repository data: {e}") |
0a87cae85746
tools (utils): fix `getRepositoryData` crash:
Goffi <goffi@goffi.org>
parents:
3540
diff
changeset
|
278 hg_data = {} |
0a87cae85746
tools (utils): fix `getRepositoryData` crash:
Goffi <goffi@goffi.org>
parents:
3540
diff
changeset
|
279 except Exception as e: |
0a87cae85746
tools (utils): fix `getRepositoryData` crash:
Goffi <goffi@goffi.org>
parents:
3540
diff
changeset
|
280 log.error(f"Unexpected error, can't get repository data : [{type(e)}] {e}") |
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
|
281 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
|
282 else: |
3028 | 283 hg_data = dict(list(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
|
284 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
285 hg_data["modified"] = "+" in subprocess.check_output( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
286 ["python3", hg_path, "id", "-i"], text=True |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
287 ) |
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
|
288 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
|
289 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
|
290 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
|
291 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
|
292 |
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
|
293 if not hg_data: |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
294 # .hg/dirstate method |
3028 | 295 log.debug("trying dirstate method") |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
296 if is_path: |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
297 os.chdir(repos_root) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
298 else: |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
299 os.chdir(os.path.abspath(os.path.dirname(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
|
300 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
301 with open(".hg/dirstate", "rb") as hg_dirstate: |
3064
d62fceccff22
core (tools/utils): fixed hex conversion in getRepositoryData following Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
302 hg_data["node"] = hg_dirstate.read(20).hex() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
303 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
|
304 except IOError: |
3028 | 305 log.debug("Can't access repository 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
|
306 |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
307 # we restore original working dir |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
308 os.chdir(ori_cwd) |
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
309 |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
310 if not hg_data: |
3028 | 311 log.debug("Mercurial not available or working, trying package version") |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
312 try: |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
313 import pkg_resources |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
314 except ImportError: |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
315 log.warning("pkg_resources not available, can't get package data") |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
316 else: |
3420
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
317 try: |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
318 pkg_version = pkg_resources.get_distribution(C.APP_NAME_FILE).version |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
319 version, local_id = pkg_version.split("+", 1) |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
320 except pkg_resources.DistributionNotFound: |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
321 log.warning("can't retrieve package data") |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
322 except ValueError: |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
323 log.info( |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
324 "no local version id in package: {pkg_version}".format( |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
325 pkg_version=pkg_version |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
326 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
327 ) |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
328 else: |
3420
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
329 if version != C.APP_VERSION: |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
330 log.warning( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4079
diff
changeset
|
331 "Incompatible version ({version}) and pkg_version ({pkg_version})".format( |
3420
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
332 version=C.APP_VERSION, pkg_version=pkg_version |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
333 ) |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
334 ) |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
335 else: |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
336 try: |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
337 hg_node, hg_distance = local_id.split(".") |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
338 except ValueError: |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
339 log.warning("Version doesn't specify repository data") |
b526b95f11f2
tools(utils): fixed error catching when getting repository data with `pkg_resources`
Goffi <goffi@goffi.org>
parents:
3368
diff
changeset
|
340 hg_data = {"node_short": hg_node, "distance": hg_distance} |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
341 |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
342 repos_cache_dict = hg_data |
1858
06e13ae616cf
tools (utils): improved repository version detection:
Goffi <goffi@goffi.org>
parents:
1857
diff
changeset
|
343 |
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
|
344 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
|
345 if not hg_data: |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
346 repos_cache = NO_REPOS_DATA |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
347 else: |
3028 | 348 strings = ["rev", hg_data["node_short"]] |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
349 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
350 if hg_data["modified"]: |
3028 | 351 strings.append("[M]") |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
352 except KeyError: |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
353 pass |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
354 try: |
3028 | 355 strings.extend(["({branch} {date})".format(**hg_data)]) |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
356 except KeyError: |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
357 pass |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
358 try: |
3028 | 359 strings.extend(["+{distance}".format(**hg_data)]) |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
360 except KeyError: |
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
361 pass |
3028 | 362 repos_cache = " ".join(strings) |
2573
18e2ca5f798e
tools (utils): better repository version handling:
Goffi <goffi@goffi.org>
parents:
2568
diff
changeset
|
363 return repos_cache |
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
|
364 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
|
365 return hg_data |