author | Philippe Pepiot <philippe.pepiot@logilab.fr> |
Tue, 16 Apr 2019 15:49:03 +0200 | |
branch | 3.26 |
changeset 12577 | 2508ba96fad2 |
parent 12280 | 577e8d3896b4 |
child 12508 | a8c1ea390400 |
permissions | -rw-r--r-- |
11091
29aebc1edd29
[repository] drop usage of no more necessary eschema_eid function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10907
diff
changeset
|
1 |
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6128 | 18 |
"""Some utilities for the CubicWeb server.""" |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10095
diff
changeset
|
19 |
from __future__ import print_function |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7573
diff
changeset
|
20 |
|
11767
432f87a63057
flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11129
diff
changeset
|
21 |
|
12011
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
22 |
from functools import wraps |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
23 |
import sched |
0 | 24 |
import sys |
6765
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
25 |
import logging |
12013
7b975655d0ae
[server] Drop utils's LoopTask and TasksManager classes not used anymore
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12011
diff
changeset
|
26 |
from threading import Thread |
0 | 27 |
from getpass import getpass |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
28 |
|
12280
577e8d3896b4
[server] unicode → text_type in utils
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12193
diff
changeset
|
29 |
from six import PY2, text_type |
10678
77333ec71fab
[server/utils] convert to py3k using six
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10589
diff
changeset
|
30 |
from six.moves import input |
77333ec71fab
[server/utils] convert to py3k using six
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10589
diff
changeset
|
31 |
|
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
32 |
from passlib.utils import handlers as uh, to_hash_str |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
33 |
from passlib.context import CryptContext |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
34 |
|
11091
29aebc1edd29
[repository] drop usage of no more necessary eschema_eid function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10907
diff
changeset
|
35 |
from logilab.common.deprecation import deprecated |
29aebc1edd29
[repository] drop usage of no more necessary eschema_eid function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10907
diff
changeset
|
36 |
|
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
37 |
from cubicweb.md5crypt import crypt as md5crypt |
0 | 38 |
|
39 |
||
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
40 |
class CustomMD5Crypt(uh.HasSalt, uh.GenericHandler): |
8398
a9fe30c953be
[server/utils] passlib 1.6 is now less tolerant wrt handler names (closes #2349330)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8317
diff
changeset
|
41 |
name = 'cubicwebmd5crypt' |
a9fe30c953be
[server/utils] passlib 1.6 is now less tolerant wrt handler names (closes #2349330)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8317
diff
changeset
|
42 |
setting_kwds = ('salt',) |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
43 |
min_salt_size = 0 |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
44 |
max_salt_size = 8 |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
45 |
salt_chars = uh.H64_CHARS |
0 | 46 |
|
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
47 |
@classmethod |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
48 |
def from_string(cls, hash): |
8414
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
49 |
salt, chk = uh.parse_mc2(hash, u'') |
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
50 |
if chk is None: |
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
51 |
raise ValueError('missing checksum') |
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
52 |
return cls(salt=salt, checksum=chk) |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
53 |
|
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
54 |
def to_string(self): |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
55 |
return to_hash_str(u'%s$%s' % (self.salt, self.checksum or u'')) |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
56 |
|
12193
e4ee04d442be
[server/utils] update passlib API to 1.7 (closes #17054805)
David Douard <david.douard@logilab.fr>
parents:
12014
diff
changeset
|
57 |
def _calc_checksum(self, secret): |
8414
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
58 |
return md5crypt(secret, self.salt.encode('ascii')).decode('utf-8') |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
59 |
|
12014
116005b5fce9
[server] Make cubicweb/server/utils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12013
diff
changeset
|
60 |
|
8546
3d2038d6f20d
[sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents:
8446
diff
changeset
|
61 |
_CRYPTO_CTX = CryptContext(['sha512_crypt', CustomMD5Crypt, 'des_crypt', 'ldap_salted_sha1'], |
3d2038d6f20d
[sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents:
8446
diff
changeset
|
62 |
deprecated=['cubicwebmd5crypt', 'des_crypt']) |
12193
e4ee04d442be
[server/utils] update passlib API to 1.7 (closes #17054805)
David Douard <david.douard@logilab.fr>
parents:
12014
diff
changeset
|
63 |
# for bw compat with passlib < 1.7 |
e4ee04d442be
[server/utils] update passlib API to 1.7 (closes #17054805)
David Douard <david.douard@logilab.fr>
parents:
12014
diff
changeset
|
64 |
if not hasattr(_CRYPTO_CTX, 'hash'): |
e4ee04d442be
[server/utils] update passlib API to 1.7 (closes #17054805)
David Douard <david.douard@logilab.fr>
parents:
12014
diff
changeset
|
65 |
_CRYPTO_CTX.hash = _CRYPTO_CTX.encrypt |
8546
3d2038d6f20d
[sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents:
8446
diff
changeset
|
66 |
verify_and_update = _CRYPTO_CTX.verify_and_update |
0 | 67 |
|
12014
116005b5fce9
[server] Make cubicweb/server/utils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12013
diff
changeset
|
68 |
|
0 | 69 |
def crypt_password(passwd, salt=None): |
70 |
"""return the encrypted password using the given salt or a generated one |
|
71 |
""" |
|
72 |
if salt is None: |
|
11880
bc9d901cb9e6
[server] Use CryptContext's hash method instead of deprecated encrypt method
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11767
diff
changeset
|
73 |
return _CRYPTO_CTX.hash(passwd).encode('ascii') |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
74 |
# empty hash, accept any password for backwards compat |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
75 |
if salt == '': |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
76 |
return salt |
8550
63260486de89
[server/utils] catch ValueError from password verification
Julien Cristau <julien.cristau@logilab.fr>
parents:
8546
diff
changeset
|
77 |
try: |
63260486de89
[server/utils] catch ValueError from password verification
Julien Cristau <julien.cristau@logilab.fr>
parents:
8546
diff
changeset
|
78 |
if _CRYPTO_CTX.verify(passwd, salt): |
63260486de89
[server/utils] catch ValueError from password verification
Julien Cristau <julien.cristau@logilab.fr>
parents:
8546
diff
changeset
|
79 |
return salt |
12014
116005b5fce9
[server] Make cubicweb/server/utils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12013
diff
changeset
|
80 |
except ValueError: # e.g. couldn't identify hash |
8550
63260486de89
[server/utils] catch ValueError from password verification
Julien Cristau <julien.cristau@logilab.fr>
parents:
8546
diff
changeset
|
81 |
pass |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
82 |
# wrong password |
10681
4383f5a30504
[server/utils] crypt_password should always return bytes
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10678
diff
changeset
|
83 |
return b'' |
0 | 84 |
|
12014
116005b5fce9
[server] Make cubicweb/server/utils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12013
diff
changeset
|
85 |
|
11091
29aebc1edd29
[repository] drop usage of no more necessary eschema_eid function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10907
diff
changeset
|
86 |
@deprecated('[3.22] no more necessary, directly get eschema.eid') |
9610
29450466273a
[server] eschema_eid needs a connection, not a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9468
diff
changeset
|
87 |
def eschema_eid(cnx, eschema): |
11091
29aebc1edd29
[repository] drop usage of no more necessary eschema_eid function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10907
diff
changeset
|
88 |
"""get eid of the CWEType entity for the given yams type. |
29aebc1edd29
[repository] drop usage of no more necessary eschema_eid function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10907
diff
changeset
|
89 |
|
29aebc1edd29
[repository] drop usage of no more necessary eschema_eid function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10907
diff
changeset
|
90 |
This used to be necessary because when the schema has been loaded from the |
29aebc1edd29
[repository] drop usage of no more necessary eschema_eid function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10907
diff
changeset
|
91 |
file-system, not from the database, (e.g. during tests), eschema.eid was |
29aebc1edd29
[repository] drop usage of no more necessary eschema_eid function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10907
diff
changeset
|
92 |
not set. |
5066
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
93 |
""" |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
94 |
return eschema.eid |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
95 |
|
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
96 |
|
0 | 97 |
DEFAULT_MSG = 'we need a manager connection on the repository \ |
98 |
(the server doesn\'t have to run, even should better not)' |
|
99 |
||
12014
116005b5fce9
[server] Make cubicweb/server/utils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12013
diff
changeset
|
100 |
|
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
101 |
def manager_userpasswd(user=None, msg=DEFAULT_MSG, confirm=False, |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
102 |
passwdmsg='password'): |
0 | 103 |
if not user: |
3701
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
104 |
if msg: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10095
diff
changeset
|
105 |
print(msg) |
0 | 106 |
while not user: |
10678
77333ec71fab
[server/utils] convert to py3k using six
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10589
diff
changeset
|
107 |
user = input('login: ') |
77333ec71fab
[server/utils] convert to py3k using six
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10589
diff
changeset
|
108 |
if PY2: |
12280
577e8d3896b4
[server] unicode → text_type in utils
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12193
diff
changeset
|
109 |
user = text_type(user, sys.stdin.encoding) |
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
110 |
passwd = getpass('%s: ' % passwdmsg) |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
111 |
if confirm: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
112 |
while True: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
113 |
passwd2 = getpass('confirm password: ') |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
114 |
if passwd == passwd2: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
115 |
break |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10095
diff
changeset
|
116 |
print('password doesn\'t match') |
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
117 |
passwd = getpass('password: ') |
0 | 118 |
# XXX decode password using stdin encoding then encode it using appl'encoding |
119 |
return user, passwd |
|
120 |
||
121 |
||
12011
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
122 |
if PY2: |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
123 |
import time # noqa |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
124 |
|
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
125 |
class scheduler(sched.scheduler): |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
126 |
"""Python2 version of sched.scheduler that matches Python3 API.""" |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
127 |
|
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
128 |
def __init__(self, **kwargs): |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
129 |
kwargs.setdefault('timefunc', time.time) |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
130 |
kwargs.setdefault('delayfunc', time.sleep) |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
131 |
# sched.scheduler is an old-style class. |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
132 |
sched.scheduler.__init__(self, **kwargs) |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
133 |
|
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
134 |
else: |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
135 |
scheduler = sched.scheduler |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
136 |
|
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
137 |
|
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
138 |
def schedule_periodic_task(scheduler, interval, func, *args): |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
139 |
"""Enter a task with `func(*args)` as a periodic event in `scheduler` |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
140 |
executing at `interval` seconds. Once executed, the task would re-schedule |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
141 |
itself unless a BaseException got raised. |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
142 |
""" |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
143 |
@wraps(func) |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
144 |
def task(*args): |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
145 |
restart = True |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
146 |
try: |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
147 |
func(*args) |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
148 |
except Exception: |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
149 |
logger = logging.getLogger('cubicweb.scheduler') |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
150 |
logger.exception('Unhandled exception in periodic task "%s"', |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
151 |
func.__name__) |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
152 |
except BaseException as exc: |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
153 |
logger = logging.getLogger('cubicweb.scheduler') |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
154 |
logger.error('periodic task "%s" not re-scheduled due to %r', |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
155 |
func.__name__, exc) |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
156 |
restart = False |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
157 |
finally: |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
158 |
if restart: |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
159 |
scheduler.enter(interval, 1, task, argument=args) |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
160 |
|
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
161 |
return scheduler.enter(interval, 1, task, argument=args) |
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
162 |
|
d2888fee6031
[server] introduce a scheduler class to run repository "looping tasks"
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11880
diff
changeset
|
163 |
|
9468
39b7a91a3f4c
[repo] pylint cleanup, mainly of imports, with a bit of style
Julien Cristau <julien.cristau@logilab.fr>
parents:
9467
diff
changeset
|
164 |
_MARKER = object() |
12014
116005b5fce9
[server] Make cubicweb/server/utils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12013
diff
changeset
|
165 |
|
116005b5fce9
[server] Make cubicweb/server/utils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
12013
diff
changeset
|
166 |
|
6381
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
167 |
def func_name(func): |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
168 |
name = getattr(func, '__name__', _MARKER) |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
169 |
if name is _MARKER: |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
170 |
name = getattr(func, 'func_name', _MARKER) |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
171 |
if name is _MARKER: |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
172 |
name = repr(func) |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
173 |
return name |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
174 |
|
0 | 175 |
|
176 |
class RepoThread(Thread): |
|
177 |
"""subclass of thread so it auto remove itself from a given list once |
|
178 |
executed |
|
179 |
""" |
|
180 |
def __init__(self, target, running_threads): |
|
181 |
def auto_remove_func(self=self, func=target): |
|
182 |
try: |
|
183 |
func() |
|
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7573
diff
changeset
|
184 |
except Exception: |
6765
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
185 |
logger = logging.getLogger('cubicweb.repository') |
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
186 |
logger.exception('Unhandled exception in RepoThread %s', self._name) |
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
187 |
raise |
0 | 188 |
finally: |
189 |
self.running_threads.remove(self) |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1134
diff
changeset
|
190 |
Thread.__init__(self, target=auto_remove_func) |
0 | 191 |
self.running_threads = running_threads |
6381
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
192 |
self._name = func_name(target) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
193 |
|
0 | 194 |
def start(self): |
195 |
self.running_threads.append(self) |
|
3585
cd437d24aa65
use daemon thread
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
196 |
self.daemon = True |
0 | 197 |
Thread.start(self) |
198 |
||
5376
2c3f14bc2590
[python2.6] don't add a name property on Thread
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5066
diff
changeset
|
199 |
def getName(self): |
0 | 200 |
return '%s(%s)' % (self._name, Thread.getName(self)) |