author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 31 Mar 2010 15:59:32 +0200 | |
branch | stable |
changeset 5122 | c06078d59e87 |
parent 5066 | bf5cbc351e99 |
child 5376 | 2c3f14bc2590 |
permissions | -rw-r--r-- |
0 | 1 |
"""Some utilities for the CubicWeb server. |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3701
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1910
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import sys |
|
11 |
import string |
|
12 |
from threading import Timer, Thread |
|
13 |
from getpass import getpass |
|
14 |
from random import choice |
|
15 |
||
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
|
16 |
from logilab.common.configuration import Configuration |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
17 |
|
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
18 |
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
|
19 |
|
0 | 20 |
try: |
21 |
from crypt import crypt |
|
22 |
except ImportError: |
|
23 |
# crypt is not available (eg windows) |
|
24 |
from cubicweb.md5crypt import crypt |
|
25 |
||
26 |
||
27 |
def getsalt(chars=string.letters + string.digits): |
|
28 |
"""generate a random 2-character 'salt'""" |
|
29 |
return choice(chars) + choice(chars) |
|
30 |
||
31 |
||
32 |
def crypt_password(passwd, salt=None): |
|
33 |
"""return the encrypted password using the given salt or a generated one |
|
34 |
""" |
|
35 |
if passwd is None: |
|
36 |
return None |
|
37 |
if salt is None: |
|
38 |
salt = getsalt() |
|
39 |
return crypt(passwd, salt) |
|
40 |
||
41 |
||
42 |
def cartesian_product(seqin): |
|
43 |
"""returns a generator which returns the cartesian product of `seqin` |
|
44 |
||
45 |
for more details, see : |
|
46 |
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 |
|
47 |
""" |
|
48 |
def rloop(seqin, comb): |
|
49 |
"""recursive looping function""" |
|
50 |
if seqin: # any more sequences to process? |
|
51 |
for item in seqin[0]: |
|
52 |
newcomb = comb + [item] # add next item to current combination |
|
53 |
# 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
|
54 |
for item in rloop(seqin[1:], newcomb): |
0 | 55 |
yield item # seqs and newcomb |
56 |
else: # processing last sequence |
|
57 |
yield comb # comb finished, add to list |
|
58 |
return rloop(seqin, []) |
|
59 |
||
60 |
||
61 |
def cleanup_solutions(rqlst, solutions): |
|
62 |
for sol in solutions: |
|
63 |
for vname in sol.keys(): |
|
64 |
if not (vname in rqlst.defined_vars or vname in rqlst.aliases): |
|
65 |
del sol[vname] |
|
66 |
||
67 |
||
5066
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
68 |
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
|
69 |
"""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
|
70 |
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
|
71 |
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
|
72 |
""" |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
73 |
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
|
74 |
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
|
75 |
'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
|
76 |
{'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
|
77 |
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
|
78 |
|
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
79 |
|
0 | 80 |
DEFAULT_MSG = 'we need a manager connection on the repository \ |
81 |
(the server doesn\'t have to run, even should better not)' |
|
82 |
||
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
83 |
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
|
84 |
passwdmsg='password'): |
0 | 85 |
if not user: |
3701
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
86 |
if msg: |
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
87 |
print msg |
0 | 88 |
while not user: |
89 |
user = raw_input('login: ') |
|
90 |
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
|
91 |
passwd = getpass('%s: ' % passwdmsg) |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
92 |
if confirm: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
93 |
while True: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
94 |
passwd2 = getpass('confirm password: ') |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
95 |
if passwd == passwd2: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
96 |
break |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
97 |
print 'password doesn\'t match' |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
98 |
passwd = getpass('password: ') |
0 | 99 |
# XXX decode password using stdin encoding then encode it using appl'encoding |
100 |
return user, passwd |
|
101 |
||
102 |
||
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
|
103 |
def ask_source_config(sourcetype, inputlevel=0): |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
104 |
sconfig = Configuration(options=SOURCE_TYPES[sourcetype].options) |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
105 |
sconfig.adapter = sourcetype |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
106 |
sconfig.input_config(inputlevel=inputlevel) |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
107 |
return sconfig |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
108 |
|
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
109 |
|
0 | 110 |
class LoopTask(object): |
111 |
"""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
|
112 |
def __init__(self, interval, func, args): |
0 | 113 |
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
|
114 |
def auto_restart_func(self=self, func=func, args=args): |
0 | 115 |
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
|
116 |
func(*args) |
0 | 117 |
finally: |
118 |
self.start() |
|
119 |
self.func = auto_restart_func |
|
120 |
self.name = func.__name__ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
121 |
|
4714
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
122 |
def __str__(self): |
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
123 |
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
|
124 |
|
0 | 125 |
def start(self): |
126 |
self._t = Timer(self.interval, self.func) |
|
127 |
self._t.start() |
|
128 |
||
129 |
def cancel(self): |
|
130 |
self._t.cancel() |
|
131 |
||
132 |
def join(self): |
|
133 |
self._t.join() |
|
134 |
||
135 |
||
136 |
class RepoThread(Thread): |
|
137 |
"""subclass of thread so it auto remove itself from a given list once |
|
138 |
executed |
|
139 |
""" |
|
140 |
def __init__(self, target, running_threads): |
|
141 |
def auto_remove_func(self=self, func=target): |
|
142 |
try: |
|
143 |
func() |
|
144 |
finally: |
|
145 |
self.running_threads.remove(self) |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1134
diff
changeset
|
146 |
Thread.__init__(self, target=auto_remove_func) |
0 | 147 |
self.running_threads = running_threads |
148 |
self._name = target.__name__ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
149 |
|
0 | 150 |
def start(self): |
151 |
self.running_threads.append(self) |
|
3585
cd437d24aa65
use daemon thread
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
152 |
self.daemon = True |
0 | 153 |
Thread.start(self) |
154 |
||
155 |
@property |
|
156 |
def name(self): |
|
157 |
return '%s(%s)' % (self._name, Thread.getName(self)) |