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