author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Sun, 27 Sep 2009 14:37:55 +0200 | |
branch | stable |
changeset 3488 | ab9e7bcfc764 |
parent 2708 | 60d728bdcba5 |
child 3585 | cd437d24aa65 |
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 |
||
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: |
74 |
print msg |
|
75 |
while not user: |
|
76 |
user = raw_input('login: ') |
|
77 |
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
|
78 |
passwd = getpass('%s: ' % passwdmsg) |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
79 |
if confirm: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
80 |
while True: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
81 |
passwd2 = getpass('confirm password: ') |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
82 |
if passwd == passwd2: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
83 |
break |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
84 |
print 'password doesn\'t match' |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
85 |
passwd = getpass('password: ') |
0 | 86 |
# XXX decode password using stdin encoding then encode it using appl'encoding |
87 |
return user, passwd |
|
88 |
||
89 |
||
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
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
|
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 |
|
0 | 97 |
class LoopTask(object): |
98 |
"""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
|
99 |
def __init__(self, interval, func, args): |
0 | 100 |
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
|
101 |
def auto_restart_func(self=self, func=func, args=args): |
0 | 102 |
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
|
103 |
func(*args) |
0 | 104 |
finally: |
105 |
self.start() |
|
106 |
self.func = auto_restart_func |
|
107 |
self.name = func.__name__ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
108 |
|
0 | 109 |
def start(self): |
110 |
self._t = Timer(self.interval, self.func) |
|
111 |
self._t.start() |
|
112 |
||
113 |
def cancel(self): |
|
114 |
self._t.cancel() |
|
115 |
||
116 |
def join(self): |
|
117 |
self._t.join() |
|
118 |
||
119 |
||
120 |
class RepoThread(Thread): |
|
121 |
"""subclass of thread so it auto remove itself from a given list once |
|
122 |
executed |
|
123 |
""" |
|
124 |
def __init__(self, target, running_threads): |
|
125 |
def auto_remove_func(self=self, func=target): |
|
126 |
try: |
|
127 |
func() |
|
128 |
finally: |
|
129 |
self.running_threads.remove(self) |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1134
diff
changeset
|
130 |
Thread.__init__(self, target=auto_remove_func) |
0 | 131 |
self.running_threads = running_threads |
132 |
self._name = target.__name__ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
133 |
|
0 | 134 |
def start(self): |
135 |
self.running_threads.append(self) |
|
136 |
Thread.start(self) |
|
137 |
||
138 |
@property |
|
139 |
def name(self): |
|
140 |
return '%s(%s)' % (self._name, Thread.getName(self)) |