author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Mon, 25 May 2009 12:18:17 +0200 | |
changeset 1910 | 864aa3ea0db5 |
parent 1802 | d628defebc17 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""Some utilities for the CubicWeb server. |
2 |
||
3 |
:organization: Logilab |
|
1134 | 4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
import sys |
|
10 |
import string |
|
11 |
from threading import Timer, Thread |
|
12 |
from getpass import getpass |
|
13 |
from random import choice |
|
14 |
||
15 |
try: |
|
16 |
from crypt import crypt |
|
17 |
except ImportError: |
|
18 |
# crypt is not available (eg windows) |
|
19 |
from cubicweb.md5crypt import crypt |
|
20 |
||
21 |
||
22 |
def getsalt(chars=string.letters + string.digits): |
|
23 |
"""generate a random 2-character 'salt'""" |
|
24 |
return choice(chars) + choice(chars) |
|
25 |
||
26 |
||
27 |
def crypt_password(passwd, salt=None): |
|
28 |
"""return the encrypted password using the given salt or a generated one |
|
29 |
""" |
|
30 |
if passwd is None: |
|
31 |
return None |
|
32 |
if salt is None: |
|
33 |
salt = getsalt() |
|
34 |
return crypt(passwd, salt) |
|
35 |
||
36 |
||
37 |
def cartesian_product(seqin): |
|
38 |
"""returns a generator which returns the cartesian product of `seqin` |
|
39 |
||
40 |
for more details, see : |
|
41 |
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 |
|
42 |
""" |
|
43 |
def rloop(seqin, comb): |
|
44 |
"""recursive looping function""" |
|
45 |
if seqin: # any more sequences to process? |
|
46 |
for item in seqin[0]: |
|
47 |
newcomb = comb + [item] # add next item to current combination |
|
48 |
# 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
|
49 |
for item in rloop(seqin[1:], newcomb): |
0 | 50 |
yield item # seqs and newcomb |
51 |
else: # processing last sequence |
|
52 |
yield comb # comb finished, add to list |
|
53 |
return rloop(seqin, []) |
|
54 |
||
55 |
||
56 |
def cleanup_solutions(rqlst, solutions): |
|
57 |
for sol in solutions: |
|
58 |
for vname in sol.keys(): |
|
59 |
if not (vname in rqlst.defined_vars or vname in rqlst.aliases): |
|
60 |
del sol[vname] |
|
61 |
||
62 |
||
63 |
DEFAULT_MSG = 'we need a manager connection on the repository \ |
|
64 |
(the server doesn\'t have to run, even should better not)' |
|
65 |
||
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
66 |
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
|
67 |
passwdmsg='password'): |
0 | 68 |
if not user: |
69 |
print msg |
|
70 |
while not user: |
|
71 |
user = raw_input('login: ') |
|
72 |
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
|
73 |
passwd = getpass('%s: ' % passwdmsg) |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
74 |
if confirm: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
75 |
while True: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
76 |
passwd2 = getpass('confirm password: ') |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
77 |
if passwd == passwd2: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
78 |
break |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
79 |
print 'password doesn\'t match' |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
80 |
passwd = getpass('password: ') |
0 | 81 |
# XXX decode password using stdin encoding then encode it using appl'encoding |
82 |
return user, passwd |
|
83 |
||
84 |
||
85 |
class LoopTask(object): |
|
86 |
"""threaded task restarting itself once executed""" |
|
87 |
def __init__(self, interval, func): |
|
88 |
self.interval = interval |
|
89 |
def auto_restart_func(self=self, func=func): |
|
90 |
try: |
|
91 |
func() |
|
92 |
finally: |
|
93 |
self.start() |
|
94 |
self.func = auto_restart_func |
|
95 |
self.name = func.__name__ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
96 |
|
0 | 97 |
def start(self): |
98 |
self._t = Timer(self.interval, self.func) |
|
99 |
self._t.start() |
|
100 |
||
101 |
def cancel(self): |
|
102 |
self._t.cancel() |
|
103 |
||
104 |
def join(self): |
|
105 |
self._t.join() |
|
106 |
||
107 |
||
108 |
class RepoThread(Thread): |
|
109 |
"""subclass of thread so it auto remove itself from a given list once |
|
110 |
executed |
|
111 |
""" |
|
112 |
def __init__(self, target, running_threads): |
|
113 |
def auto_remove_func(self=self, func=target): |
|
114 |
try: |
|
115 |
func() |
|
116 |
finally: |
|
117 |
self.running_threads.remove(self) |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1134
diff
changeset
|
118 |
Thread.__init__(self, target=auto_remove_func) |
0 | 119 |
self.running_threads = running_threads |
120 |
self._name = target.__name__ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
121 |
|
0 | 122 |
def start(self): |
123 |
self.running_threads.append(self) |
|
124 |
Thread.start(self) |
|
125 |
||
126 |
@property |
|
127 |
def name(self): |
|
128 |
return '%s(%s)' % (self._name, Thread.getName(self)) |