author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Fri, 15 Jan 2010 09:36:49 +0100 | |
branch | stable |
changeset 4245 | ff8e355dbc97 |
parent 4212 | ab6573088b4a |
child 4714 | fccda6dd91bf |
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 |
||
68 |
DEFAULT_MSG = 'we need a manager connection on the repository \ |
|
69 |
(the server doesn\'t have to run, even should better not)' |
|
70 |
||
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
71 |
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
|
72 |
passwdmsg='password'): |
0 | 73 |
if not user: |
3701
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
74 |
if msg: |
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
75 |
print msg |
0 | 76 |
while not user: |
77 |
user = raw_input('login: ') |
|
78 |
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
|
79 |
passwd = getpass('%s: ' % passwdmsg) |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
80 |
if confirm: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
81 |
while True: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
82 |
passwd2 = getpass('confirm password: ') |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
83 |
if passwd == passwd2: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
84 |
break |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
85 |
print 'password doesn\'t match' |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
86 |
passwd = getpass('password: ') |
0 | 87 |
# XXX decode password using stdin encoding then encode it using appl'encoding |
88 |
return user, passwd |
|
89 |
||
90 |
||
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
|
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
97 |
|
0 | 98 |
class LoopTask(object): |
99 |
"""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
|
100 |
def __init__(self, interval, func, args): |
0 | 101 |
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
|
102 |
def auto_restart_func(self=self, func=func, args=args): |
0 | 103 |
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
|
104 |
func(*args) |
0 | 105 |
finally: |
106 |
self.start() |
|
107 |
self.func = auto_restart_func |
|
108 |
self.name = func.__name__ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
109 |
|
0 | 110 |
def start(self): |
111 |
self._t = Timer(self.interval, self.func) |
|
112 |
self._t.start() |
|
113 |
||
114 |
def cancel(self): |
|
115 |
self._t.cancel() |
|
116 |
||
117 |
def join(self): |
|
118 |
self._t.join() |
|
119 |
||
120 |
||
121 |
class RepoThread(Thread): |
|
122 |
"""subclass of thread so it auto remove itself from a given list once |
|
123 |
executed |
|
124 |
""" |
|
125 |
def __init__(self, target, running_threads): |
|
126 |
def auto_remove_func(self=self, func=target): |
|
127 |
try: |
|
128 |
func() |
|
129 |
finally: |
|
130 |
self.running_threads.remove(self) |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1134
diff
changeset
|
131 |
Thread.__init__(self, target=auto_remove_func) |
0 | 132 |
self.running_threads = running_threads |
133 |
self._name = target.__name__ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
134 |
|
0 | 135 |
def start(self): |
136 |
self.running_threads.append(self) |
|
3585
cd437d24aa65
use daemon thread
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
137 |
self.daemon = True |
0 | 138 |
Thread.start(self) |
139 |
||
140 |
@property |
|
141 |
def name(self): |
|
142 |
return '%s(%s)' % (self._name, Thread.getName(self)) |