author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 11 Feb 2011 11:28:46 +0100 | |
branch | stable |
changeset 6977 | cb78108bf603 |
parent 6765 | b922e3a817e9 |
child 7573 | c8f8762c986d |
permissions | -rw-r--r-- |
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
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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.""" |
0 | 19 |
__docformat__ = "restructuredtext en" |
20 |
||
21 |
import sys |
|
22 |
import string |
|
6765
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
23 |
import logging |
0 | 24 |
from threading import Timer, Thread |
25 |
from getpass import getpass |
|
26 |
from random import choice |
|
27 |
||
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 |
from cubicweb.server import SOURCE_TYPES |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
29 |
|
0 | 30 |
try: |
31 |
from crypt import crypt |
|
32 |
except ImportError: |
|
33 |
# crypt is not available (eg windows) |
|
34 |
from cubicweb.md5crypt import crypt |
|
35 |
||
36 |
||
37 |
def getsalt(chars=string.letters + string.digits): |
|
38 |
"""generate a random 2-character 'salt'""" |
|
39 |
return choice(chars) + choice(chars) |
|
40 |
||
41 |
||
42 |
def crypt_password(passwd, salt=None): |
|
43 |
"""return the encrypted password using the given salt or a generated one |
|
44 |
""" |
|
45 |
if passwd is None: |
|
46 |
return None |
|
47 |
if salt is None: |
|
48 |
salt = getsalt() |
|
49 |
return crypt(passwd, salt) |
|
50 |
||
51 |
||
52 |
def cartesian_product(seqin): |
|
53 |
"""returns a generator which returns the cartesian product of `seqin` |
|
54 |
||
55 |
for more details, see : |
|
56 |
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 |
|
57 |
""" |
|
58 |
def rloop(seqin, comb): |
|
59 |
"""recursive looping function""" |
|
60 |
if seqin: # any more sequences to process? |
|
61 |
for item in seqin[0]: |
|
6128 | 62 |
newcomb = comb + [item] # add next item to current combination |
0 | 63 |
# call rloop w/ remaining seqs, newcomb |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
64 |
for item in rloop(seqin[1:], newcomb): |
0 | 65 |
yield item # seqs and newcomb |
66 |
else: # processing last sequence |
|
67 |
yield comb # comb finished, add to list |
|
68 |
return rloop(seqin, []) |
|
69 |
||
70 |
||
71 |
def cleanup_solutions(rqlst, solutions): |
|
72 |
for sol in solutions: |
|
73 |
for vname in sol.keys(): |
|
74 |
if not (vname in rqlst.defined_vars or vname in rqlst.aliases): |
|
75 |
del sol[vname] |
|
76 |
||
77 |
||
5066
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
78 |
def eschema_eid(session, eschema): |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
79 |
"""get eid of the CWEType entity for the given yams type. You should use |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
80 |
this because when schema has been loaded from the file-system, not from the |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
81 |
database, (e.g. during tests), eschema.eid is not set. |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
82 |
""" |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
83 |
if eschema.eid is None: |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
84 |
eschema.eid = session.execute( |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
85 |
'Any X WHERE X is CWEType, X name %(name)s', |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
86 |
{'name': str(eschema)})[0][0] |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
87 |
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
|
88 |
|
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
89 |
|
0 | 90 |
DEFAULT_MSG = 'we need a manager connection on the repository \ |
91 |
(the server doesn\'t have to run, even should better not)' |
|
92 |
||
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
93 |
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
|
94 |
passwdmsg='password'): |
0 | 95 |
if not user: |
3701
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
96 |
if msg: |
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
97 |
print msg |
0 | 98 |
while not user: |
99 |
user = raw_input('login: ') |
|
100 |
user = unicode(user, sys.stdin.encoding) |
|
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
101 |
passwd = getpass('%s: ' % passwdmsg) |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
102 |
if confirm: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
103 |
while True: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
104 |
passwd2 = getpass('confirm password: ') |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
105 |
if passwd == passwd2: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
106 |
break |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
107 |
print 'password doesn\'t match' |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
108 |
passwd = getpass('password: ') |
0 | 109 |
# XXX decode password using stdin encoding then encode it using appl'encoding |
110 |
return user, passwd |
|
111 |
||
112 |
||
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
|
113 |
_MARKER=object() |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
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
|
120 |
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
|
121 |
|
0 | 122 |
class LoopTask(object): |
123 |
"""threaded task restarting itself once executed""" |
|
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2105
diff
changeset
|
124 |
def __init__(self, interval, func, args): |
5602
277b15d6d3ed
forbid looping tasks with an interval of 0
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5601
diff
changeset
|
125 |
if interval <= 0: |
277b15d6d3ed
forbid looping tasks with an interval of 0
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5601
diff
changeset
|
126 |
raise ValueError('Loop task interval must be > 0 ' |
277b15d6d3ed
forbid looping tasks with an interval of 0
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5601
diff
changeset
|
127 |
'(current value: %f for %s)' % \ |
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
|
128 |
(interval, func_name(func))) |
0 | 129 |
self.interval = interval |
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2105
diff
changeset
|
130 |
def auto_restart_func(self=self, func=func, args=args): |
0 | 131 |
try: |
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2105
diff
changeset
|
132 |
func(*args) |
6765
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
133 |
except: |
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
134 |
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
|
135 |
logger.exception('Unhandled exception in LoopTask %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
|
136 |
raise |
0 | 137 |
finally: |
138 |
self.start() |
|
139 |
self.func = auto_restart_func |
|
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
|
140 |
self.name = func_name(func) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
141 |
|
4714
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
142 |
def __str__(self): |
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
143 |
return '%s (%s seconds)' % (self.name, self.interval) |
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
144 |
|
0 | 145 |
def start(self): |
146 |
self._t = Timer(self.interval, self.func) |
|
5601
92cf309672ca
/siteinfo page: display information about the names of the running threads
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
147 |
self._t.setName('%s-%s[%d]' % (self._t.getName(), self.name, self.interval)) |
0 | 148 |
self._t.start() |
149 |
||
150 |
def cancel(self): |
|
151 |
self._t.cancel() |
|
152 |
||
153 |
def join(self): |
|
5581
0aae5216f99e
[repo] ensure thread is alive before calling .join. Closes #963580
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
154 |
if self._t.isAlive(): |
0aae5216f99e
[repo] ensure thread is alive before calling .join. Closes #963580
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
155 |
self._t.join() |
0 | 156 |
|
157 |
||
158 |
class RepoThread(Thread): |
|
159 |
"""subclass of thread so it auto remove itself from a given list once |
|
160 |
executed |
|
161 |
""" |
|
162 |
def __init__(self, target, running_threads): |
|
163 |
def auto_remove_func(self=self, func=target): |
|
164 |
try: |
|
165 |
func() |
|
6765
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
166 |
except: |
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
167 |
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
|
168 |
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
|
169 |
raise |
0 | 170 |
finally: |
171 |
self.running_threads.remove(self) |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1134
diff
changeset
|
172 |
Thread.__init__(self, target=auto_remove_func) |
0 | 173 |
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
|
174 |
self._name = func_name(target) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
175 |
|
0 | 176 |
def start(self): |
177 |
self.running_threads.append(self) |
|
3585
cd437d24aa65
use daemon thread
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
178 |
self.daemon = True |
0 | 179 |
Thread.start(self) |
180 |
||
5376
2c3f14bc2590
[python2.6] don't add a name property on Thread
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5066
diff
changeset
|
181 |
def getName(self): |
0 | 182 |
return '%s(%s)' % (self._name, Thread.getName(self)) |