author | Julien Jehannet <julien.jehannet@logilab.fr> |
Thu, 29 Jul 2010 15:18:31 +0200 | |
branch | stable |
changeset 6035 | f8c7aa251782 |
parent 5426 | 0d4853a6e5ee |
child 6138 | 65f5e488f983 |
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""some utilities for cubicweb tools |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
2397
cdedc2a32b06
[shell] move toolsutils.confirm() to logilab.common.shellutils
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
23 |
# XXX move most of this in logilab.common (shellutils ?) |
cdedc2a32b06
[shell] move toolsutils.confirm() to logilab.common.shellutils
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
24 |
|
0 | 25 |
import os, sys |
4554
2279ba039494
use subprocess instead of os.popen to run diff
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4212
diff
changeset
|
26 |
import subprocess |
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
27 |
from os import listdir, makedirs, environ, chmod, walk, remove |
0 | 28 |
from os.path import exists, join, abspath, normpath |
29 |
||
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
30 |
try: |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
31 |
from os import symlink |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
32 |
except ImportError: |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
33 |
def symlink(*args): |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
34 |
raise NotImplementedError |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
35 |
|
0 | 36 |
from logilab.common.clcommands import Command as BaseCommand, \ |
1132 | 37 |
main_run as base_main_run |
0 | 38 |
from logilab.common.compat import any |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
39 |
from logilab.common.shellutils import ASK |
0 | 40 |
|
41 |
from cubicweb import warning |
|
42 |
from cubicweb import ConfigurationError, ExecutionError |
|
43 |
||
2790
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
44 |
def underline_title(title, car='-'): |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
45 |
return title+'\n'+(car*len(title)) |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
46 |
|
0 | 47 |
def iter_dir(directory, condition_file=None, ignore=()): |
48 |
"""iterate on a directory""" |
|
49 |
for sub in listdir(directory): |
|
50 |
if sub in ('CVS', '.svn', '.hg'): |
|
51 |
continue |
|
52 |
if condition_file is not None and \ |
|
53 |
not exists(join(directory, sub, condition_file)): |
|
54 |
continue |
|
55 |
if sub in ignore: |
|
56 |
continue |
|
57 |
yield sub |
|
58 |
||
59 |
def create_dir(directory): |
|
60 |
"""create a directory if it doesn't exist yet""" |
|
61 |
try: |
|
62 |
makedirs(directory) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2388
diff
changeset
|
63 |
print '-> created directory %s.' % directory |
0 | 64 |
except OSError, ex: |
65 |
import errno |
|
66 |
if ex.errno != errno.EEXIST: |
|
67 |
raise |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2388
diff
changeset
|
68 |
print '-> directory %s already exists, no need to create it.' % directory |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
69 |
|
0 | 70 |
def create_symlink(source, target): |
71 |
"""create a symbolic link""" |
|
72 |
if exists(target): |
|
73 |
remove(target) |
|
74 |
symlink(source, target) |
|
75 |
print '[symlink] %s <-- %s' % (target, source) |
|
76 |
||
77 |
def create_copy(source, target): |
|
78 |
import shutil |
|
79 |
print '[copy] %s <-- %s' % (target, source) |
|
80 |
shutil.copy2(source, target) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
81 |
|
0 | 82 |
def rm(whatever): |
83 |
import shutil |
|
84 |
shutil.rmtree(whatever) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2388
diff
changeset
|
85 |
print '-> removed %s' % whatever |
0 | 86 |
|
87 |
def show_diffs(appl_file, ref_file, askconfirm=True): |
|
88 |
"""interactivly replace the old file with the new file according to |
|
89 |
user decision |
|
90 |
""" |
|
91 |
import shutil |
|
4554
2279ba039494
use subprocess instead of os.popen to run diff
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4212
diff
changeset
|
92 |
pipe = subprocess.Popen(['diff', '-u', appl_file, ref_file], stdout=subprocess.PIPE) |
2279ba039494
use subprocess instead of os.popen to run diff
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4212
diff
changeset
|
93 |
diffs = pipe.stdout.read() |
0 | 94 |
if diffs: |
95 |
if askconfirm: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
96 |
print |
0 | 97 |
print diffs |
5324
449cc4fa9c42
[migration] makes Yes the default answer to replace configuration file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
98 |
action = ASK.ask('Replace ?', ('Y', 'n', 'q'), 'Y').lower() |
0 | 99 |
else: |
100 |
action = 'y' |
|
101 |
if action == 'y': |
|
102 |
try: |
|
103 |
shutil.copyfile(ref_file, appl_file) |
|
104 |
except IOError: |
|
105 |
os.system('chmod a+w %s' % appl_file) |
|
106 |
shutil.copyfile(ref_file, appl_file) |
|
107 |
print 'replaced' |
|
108 |
elif action == 'q': |
|
109 |
sys.exit(0) |
|
110 |
else: |
|
111 |
copy_file = appl_file + '.default' |
|
112 |
copy = file(copy_file, 'w') |
|
113 |
copy.write(open(ref_file).read()) |
|
114 |
copy.close() |
|
115 |
print 'keep current version, the new file has been written to', copy_file |
|
116 |
else: |
|
117 |
print 'no diff between %s and %s' % (appl_file, ref_file) |
|
118 |
||
5184
955ee1b24756
[c-c newcube] #1192: simpler cubicweb-ctl newcube, and more
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5021
diff
changeset
|
119 |
SKEL_EXCLUDE = ('*.py[co]', '*.orig', '*~', '*_flymake.py') |
0 | 120 |
def copy_skeleton(skeldir, targetdir, context, |
5184
955ee1b24756
[c-c newcube] #1192: simpler cubicweb-ctl newcube, and more
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5021
diff
changeset
|
121 |
exclude=SKEL_EXCLUDE, askconfirm=False): |
0 | 122 |
import shutil |
123 |
from fnmatch import fnmatch |
|
124 |
skeldir = normpath(skeldir) |
|
125 |
targetdir = normpath(targetdir) |
|
126 |
for dirpath, dirnames, filenames in walk(skeldir): |
|
127 |
tdirpath = dirpath.replace(skeldir, targetdir) |
|
128 |
create_dir(tdirpath) |
|
129 |
for fname in filenames: |
|
130 |
if any(fnmatch(fname, pat) for pat in exclude): |
|
131 |
continue |
|
132 |
fpath = join(dirpath, fname) |
|
133 |
if 'CUBENAME' in fname: |
|
134 |
tfpath = join(tdirpath, fname.replace('CUBENAME', context['cubename'])) |
|
135 |
elif 'DISTNAME' in fname: |
|
136 |
tfpath = join(tdirpath, fname.replace('DISTNAME', context['distname'])) |
|
137 |
else: |
|
138 |
tfpath = join(tdirpath, fname) |
|
139 |
if fname.endswith('.tmpl'): |
|
140 |
tfpath = tfpath[:-5] |
|
141 |
if not askconfirm or not exists(tfpath) or \ |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
142 |
ASK.confirm('%s exists, overwrite?' % tfpath): |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
143 |
fill_templated_file(fpath, tfpath, context) |
0 | 144 |
print '[generate] %s <-- %s' % (tfpath, fpath) |
145 |
elif exists(tfpath): |
|
146 |
show_diffs(tfpath, fpath, askconfirm) |
|
147 |
else: |
|
148 |
shutil.copyfile(fpath, tfpath) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
149 |
|
0 | 150 |
def fill_templated_file(fpath, tfpath, context): |
151 |
fobj = file(tfpath, 'w') |
|
152 |
templated = file(fpath).read() |
|
153 |
fobj.write(templated % context) |
|
154 |
fobj.close() |
|
155 |
||
156 |
def restrict_perms_to_user(filepath, log=None): |
|
157 |
"""set -rw------- permission on the given file""" |
|
158 |
if log: |
|
159 |
log('set %s permissions to 0600', filepath) |
|
160 |
else: |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2388
diff
changeset
|
161 |
print '-> set %s permissions to 0600' % filepath |
0 | 162 |
chmod(filepath, 0600) |
163 |
||
164 |
def read_config(config_file): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2397
diff
changeset
|
165 |
"""read the instance configuration from a file and return it as a |
0 | 166 |
dictionnary |
167 |
||
168 |
:type config_file: str |
|
169 |
:param config_file: path to the configuration file |
|
170 |
||
171 |
:rtype: dict |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
172 |
:return: a dictionary with specified values associated to option names |
0 | 173 |
""" |
174 |
from logilab.common.fileutils import lines |
|
175 |
config = current = {} |
|
176 |
try: |
|
177 |
for line in lines(config_file, comments='#'): |
|
178 |
try: |
|
179 |
option, value = line.split('=', 1) |
|
180 |
except ValueError: |
|
181 |
option = line.strip().lower() |
|
182 |
if option[0] == '[': |
|
183 |
# start a section |
|
184 |
section = option[1:-1] |
|
185 |
assert not config.has_key(section), \ |
|
186 |
'Section %s is defined more than once' % section |
|
187 |
config[section] = current = {} |
|
188 |
continue |
|
189 |
print >> sys.stderr, 'ignoring malformed line\n%r' % line |
|
190 |
continue |
|
191 |
option = option.strip().replace(' ', '_') |
|
192 |
value = value.strip() |
|
193 |
current[option] = value or None |
|
194 |
except IOError, ex: |
|
195 |
warning('missing or non readable configuration file %s (%s)', |
|
196 |
config_file, ex) |
|
197 |
return config |
|
198 |
||
5021
58e89f3dfbae
handle nicely typical installation other than debian package / mercurial forest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
199 |
def env_path(env_var, default, name, checkexists=True): |
0 | 200 |
"""get a path specified in a variable or using the default value and return |
201 |
it. |
|
202 |
||
203 |
:type env_var: str |
|
204 |
:param env_var: name of an environment variable |
|
205 |
||
206 |
:type default: str |
|
207 |
:param default: default value if the environment variable is not defined |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
208 |
|
0 | 209 |
:type name: str |
210 |
:param name: the informal name of the path, used for error message |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
211 |
|
0 | 212 |
:rtype: str |
213 |
:return: the value of the environment variable or the default value |
|
214 |
||
215 |
:raise `ConfigurationError`: if the returned path does not exist |
|
216 |
""" |
|
217 |
path = environ.get(env_var, default) |
|
5021
58e89f3dfbae
handle nicely typical installation other than debian package / mercurial forest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
218 |
if checkexists and not exists(path): |
58e89f3dfbae
handle nicely typical installation other than debian package / mercurial forest
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
219 |
raise ConfigurationError('%s directory %s doesn\'t exist' % (name, path)) |
0 | 220 |
return abspath(path) |
221 |
||
222 |
||
223 |
||
224 |
_HDLRS = {} |
|
225 |
||
226 |
class metacmdhandler(type): |
|
227 |
def __new__(mcs, name, bases, classdict): |
|
228 |
cls = super(metacmdhandler, mcs).__new__(mcs, name, bases, classdict) |
|
229 |
if getattr(cls, 'cfgname', None) and getattr(cls, 'cmdname', None): |
|
230 |
_HDLRS.setdefault(cls.cmdname, []).append(cls) |
|
231 |
return cls |
|
232 |
||
233 |
||
234 |
class CommandHandler(object): |
|
235 |
"""configuration specific helper for cubicweb-ctl commands""" |
|
236 |
__metaclass__ = metacmdhandler |
|
237 |
def __init__(self, config): |
|
238 |
self.config = config |
|
239 |
||
240 |
class Command(BaseCommand): |
|
241 |
"""base class for cubicweb-ctl commands""" |
|
242 |
||
243 |
def config_helper(self, config, required=True, cmdname=None): |
|
244 |
if cmdname is None: |
|
245 |
cmdname = self.name |
|
246 |
for helpercls in _HDLRS.get(cmdname, ()): |
|
247 |
if helpercls.cfgname == config.name: |
|
248 |
return helpercls(config) |
|
249 |
if config.name == 'all-in-one': |
|
250 |
for helpercls in _HDLRS.get(cmdname, ()): |
|
251 |
if helpercls.cfgname == 'repository': |
|
252 |
return helpercls(config) |
|
253 |
if required: |
|
254 |
msg = 'No helper for command %s using %s configuration' % ( |
|
255 |
cmdname, config.name) |
|
256 |
raise ConfigurationError(msg) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
257 |
|
0 | 258 |
def fail(self, reason): |
259 |
print "command failed:", reason |
|
260 |
sys.exit(1) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
261 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
262 |
|
0 | 263 |
def main_run(args, doc): |
264 |
"""command line tool""" |
|
265 |
try: |
|
4352
afe1f9bc308a
nicer usage for cubicweb-ctl
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
266 |
base_main_run(args, doc, copyright=None) |
0 | 267 |
except ConfigurationError, err: |
268 |
print 'ERROR: ', err |
|
269 |
sys.exit(1) |
|
270 |
except ExecutionError, err: |
|
271 |
print err |
|
272 |
sys.exit(2) |
|
273 |
||
274 |
CONNECT_OPTIONS = ( |
|
275 |
("user", |
|
276 |
{'short': 'u', 'type' : 'string', 'metavar': '<user>', |
|
277 |
'help': 'connect as <user> instead of being prompted to give it.', |
|
278 |
} |
|
279 |
), |
|
280 |
("password", |
|
281 |
{'short': 'p', 'type' : 'password', 'metavar': '<password>', |
|
282 |
'help': 'automatically give <password> for authentication instead of \ |
|
283 |
being prompted to give it.', |
|
284 |
}), |
|
285 |
("host", |
|
286 |
{'short': 'H', 'type' : 'string', 'metavar': '<hostname>', |
|
541
0d75cfe50f83
fix default value of pyro ns host
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
287 |
'default': None, |
0 | 288 |
'help': 'specify the name server\'s host name. Will be detected by \ |
289 |
broadcast if not provided.', |
|
290 |
}), |
|
291 |
) |
|
292 |
||
293 |
def config_connect(appid, optconfig): |
|
294 |
from cubicweb.dbapi import connect |
|
295 |
from getpass import getpass |
|
296 |
user = optconfig.user |
|
297 |
if not user: |
|
298 |
user = raw_input('login: ') |
|
299 |
password = optconfig.password |
|
300 |
if not password: |
|
301 |
password = getpass('password: ') |
|
2388
fddb0fd11321
[api] update dbapi.connect() calls to match new prototype (user parameter is now named login)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
302 |
return connect(login=user, password=password, host=optconfig.host, database=appid) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
303 |