author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 12 Oct 2010 23:36:54 +0200 | |
changeset 6466 | 43d71dd8a8ec |
parent 6427 | c8a5ac2d1eaa |
child 6765 | b922e3a817e9 |
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 |
|
23 |
from threading import Timer, Thread |
|
24 |
from getpass import getpass |
|
25 |
from random import choice |
|
26 |
||
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
|
27 |
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
|
28 |
|
0 | 29 |
try: |
30 |
from crypt import crypt |
|
31 |
except ImportError: |
|
32 |
# crypt is not available (eg windows) |
|
33 |
from cubicweb.md5crypt import crypt |
|
34 |
||
35 |
||
36 |
def getsalt(chars=string.letters + string.digits): |
|
37 |
"""generate a random 2-character 'salt'""" |
|
38 |
return choice(chars) + choice(chars) |
|
39 |
||
40 |
||
41 |
def crypt_password(passwd, salt=None): |
|
42 |
"""return the encrypted password using the given salt or a generated one |
|
43 |
""" |
|
44 |
if passwd is None: |
|
45 |
return None |
|
46 |
if salt is None: |
|
47 |
salt = getsalt() |
|
48 |
return crypt(passwd, salt) |
|
49 |
||
50 |
||
51 |
def cartesian_product(seqin): |
|
52 |
"""returns a generator which returns the cartesian product of `seqin` |
|
53 |
||
54 |
for more details, see : |
|
55 |
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 |
|
56 |
""" |
|
57 |
def rloop(seqin, comb): |
|
58 |
"""recursive looping function""" |
|
59 |
if seqin: # any more sequences to process? |
|
60 |
for item in seqin[0]: |
|
6128 | 61 |
newcomb = comb + [item] # add next item to current combination |
0 | 62 |
# 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
|
63 |
for item in rloop(seqin[1:], newcomb): |
0 | 64 |
yield item # seqs and newcomb |
65 |
else: # processing last sequence |
|
66 |
yield comb # comb finished, add to list |
|
67 |
return rloop(seqin, []) |
|
68 |
||
69 |
||
70 |
def cleanup_solutions(rqlst, solutions): |
|
71 |
for sol in solutions: |
|
72 |
for vname in sol.keys(): |
|
73 |
if not (vname in rqlst.defined_vars or vname in rqlst.aliases): |
|
74 |
del sol[vname] |
|
75 |
||
76 |
||
5066
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
77 |
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
|
78 |
"""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
|
79 |
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
|
80 |
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
|
81 |
""" |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
82 |
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
|
83 |
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
|
84 |
'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
|
85 |
{'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
|
86 |
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
|
87 |
|
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
88 |
|
0 | 89 |
DEFAULT_MSG = 'we need a manager connection on the repository \ |
90 |
(the server doesn\'t have to run, even should better not)' |
|
91 |
||
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
92 |
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
|
93 |
passwdmsg='password'): |
0 | 94 |
if not user: |
3701
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
95 |
if msg: |
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
96 |
print msg |
0 | 97 |
while not user: |
98 |
user = raw_input('login: ') |
|
99 |
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
|
100 |
passwd = getpass('%s: ' % passwdmsg) |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
101 |
if confirm: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
102 |
while True: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
103 |
passwd2 = getpass('confirm password: ') |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
104 |
if passwd == passwd2: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
105 |
break |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
106 |
print 'password doesn\'t match' |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
107 |
passwd = getpass('password: ') |
0 | 108 |
# XXX decode password using stdin encoding then encode it using appl'encoding |
109 |
return user, passwd |
|
110 |
||
111 |
||
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
|
112 |
_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
|
113 |
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
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
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
|
120 |
|
0 | 121 |
class LoopTask(object): |
122 |
"""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
|
123 |
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
|
124 |
if interval <= 0: |
277b15d6d3ed
forbid looping tasks with an interval of 0
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5601
diff
changeset
|
125 |
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
|
126 |
'(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
|
127 |
(interval, func_name(func))) |
0 | 128 |
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
|
129 |
def auto_restart_func(self=self, func=func, args=args): |
0 | 130 |
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
|
131 |
func(*args) |
0 | 132 |
finally: |
133 |
self.start() |
|
134 |
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
|
135 |
self.name = func_name(func) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
136 |
|
4714
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
137 |
def __str__(self): |
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
138 |
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
|
139 |
|
0 | 140 |
def start(self): |
141 |
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
|
142 |
self._t.setName('%s-%s[%d]' % (self._t.getName(), self.name, self.interval)) |
0 | 143 |
self._t.start() |
144 |
||
145 |
def cancel(self): |
|
146 |
self._t.cancel() |
|
147 |
||
148 |
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
|
149 |
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
|
150 |
self._t.join() |
0 | 151 |
|
152 |
||
153 |
class RepoThread(Thread): |
|
154 |
"""subclass of thread so it auto remove itself from a given list once |
|
155 |
executed |
|
156 |
""" |
|
157 |
def __init__(self, target, running_threads): |
|
158 |
def auto_remove_func(self=self, func=target): |
|
159 |
try: |
|
160 |
func() |
|
161 |
finally: |
|
162 |
self.running_threads.remove(self) |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1134
diff
changeset
|
163 |
Thread.__init__(self, target=auto_remove_func) |
0 | 164 |
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
|
165 |
self._name = func_name(target) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
166 |
|
0 | 167 |
def start(self): |
168 |
self.running_threads.append(self) |
|
3585
cd437d24aa65
use daemon thread
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
169 |
self.daemon = True |
0 | 170 |
Thread.start(self) |
171 |
||
5376
2c3f14bc2590
[python2.6] don't add a name property on Thread
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5066
diff
changeset
|
172 |
def getName(self): |
0 | 173 |
return '%s(%s)' % (self._name, Thread.getName(self)) |