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