author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Sun, 17 Oct 2010 21:10:01 +0200 | |
changeset 6527 | 2421875ab889 |
parent 6495 | 3187112bc7cc |
child 7155 | 4bab50b02927 |
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/>. |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
18 |
"""some utilities for cubicweb command line tools""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
2397
cdedc2a32b06
[shell] move toolsutils.confirm() to logilab.common.shellutils
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
22 |
# 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
|
23 |
|
0 | 24 |
import os, sys |
4554
2279ba039494
use subprocess instead of os.popen to run diff
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4212
diff
changeset
|
25 |
import subprocess |
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
26 |
from os import listdir, makedirs, environ, chmod, walk, remove |
0 | 27 |
from os.path import exists, join, abspath, normpath |
28 |
||
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
29 |
try: |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
30 |
from os import symlink |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
31 |
except ImportError: |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
32 |
def symlink(*args): |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
33 |
raise NotImplementedError |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
34 |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
35 |
from logilab.common.clcommands import Command as BaseCommand |
0 | 36 |
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
|
37 |
from logilab.common.shellutils import ASK |
0 | 38 |
|
39 |
from cubicweb import warning |
|
40 |
from cubicweb import ConfigurationError, ExecutionError |
|
41 |
||
2790
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
42 |
def underline_title(title, car='-'): |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
43 |
return title+'\n'+(car*len(title)) |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
44 |
|
0 | 45 |
def iter_dir(directory, condition_file=None, ignore=()): |
46 |
"""iterate on a directory""" |
|
47 |
for sub in listdir(directory): |
|
48 |
if sub in ('CVS', '.svn', '.hg'): |
|
49 |
continue |
|
50 |
if condition_file is not None and \ |
|
51 |
not exists(join(directory, sub, condition_file)): |
|
52 |
continue |
|
53 |
if sub in ignore: |
|
54 |
continue |
|
55 |
yield sub |
|
56 |
||
57 |
def create_dir(directory): |
|
58 |
"""create a directory if it doesn't exist yet""" |
|
59 |
try: |
|
60 |
makedirs(directory) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2388
diff
changeset
|
61 |
print '-> created directory %s.' % directory |
0 | 62 |
except OSError, ex: |
63 |
import errno |
|
64 |
if ex.errno != errno.EEXIST: |
|
65 |
raise |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2388
diff
changeset
|
66 |
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
|
67 |
|
0 | 68 |
def create_symlink(source, target): |
69 |
"""create a symbolic link""" |
|
70 |
if exists(target): |
|
71 |
remove(target) |
|
72 |
symlink(source, target) |
|
73 |
print '[symlink] %s <-- %s' % (target, source) |
|
74 |
||
75 |
def create_copy(source, target): |
|
76 |
import shutil |
|
77 |
print '[copy] %s <-- %s' % (target, source) |
|
78 |
shutil.copy2(source, target) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
79 |
|
0 | 80 |
def rm(whatever): |
81 |
import shutil |
|
82 |
shutil.rmtree(whatever) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2388
diff
changeset
|
83 |
print '-> removed %s' % whatever |
0 | 84 |
|
85 |
def show_diffs(appl_file, ref_file, askconfirm=True): |
|
86 |
"""interactivly replace the old file with the new file according to |
|
87 |
user decision |
|
88 |
""" |
|
89 |
import shutil |
|
4554
2279ba039494
use subprocess instead of os.popen to run diff
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4212
diff
changeset
|
90 |
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
|
91 |
diffs = pipe.stdout.read() |
0 | 92 |
if diffs: |
93 |
if askconfirm: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
94 |
print |
0 | 95 |
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
|
96 |
action = ASK.ask('Replace ?', ('Y', 'n', 'q'), 'Y').lower() |
0 | 97 |
else: |
98 |
action = 'y' |
|
99 |
if action == 'y': |
|
100 |
try: |
|
101 |
shutil.copyfile(ref_file, appl_file) |
|
102 |
except IOError: |
|
103 |
os.system('chmod a+w %s' % appl_file) |
|
104 |
shutil.copyfile(ref_file, appl_file) |
|
105 |
print 'replaced' |
|
106 |
elif action == 'q': |
|
107 |
sys.exit(0) |
|
108 |
else: |
|
109 |
copy_file = appl_file + '.default' |
|
110 |
copy = file(copy_file, 'w') |
|
111 |
copy.write(open(ref_file).read()) |
|
112 |
copy.close() |
|
113 |
print 'keep current version, the new file has been written to', copy_file |
|
114 |
else: |
|
115 |
print 'no diff between %s and %s' % (appl_file, ref_file) |
|
116 |
||
5184
955ee1b24756
[c-c newcube] #1192: simpler cubicweb-ctl newcube, and more
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5021
diff
changeset
|
117 |
SKEL_EXCLUDE = ('*.py[co]', '*.orig', '*~', '*_flymake.py') |
0 | 118 |
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
|
119 |
exclude=SKEL_EXCLUDE, askconfirm=False): |
0 | 120 |
import shutil |
121 |
from fnmatch import fnmatch |
|
122 |
skeldir = normpath(skeldir) |
|
123 |
targetdir = normpath(targetdir) |
|
124 |
for dirpath, dirnames, filenames in walk(skeldir): |
|
125 |
tdirpath = dirpath.replace(skeldir, targetdir) |
|
126 |
create_dir(tdirpath) |
|
127 |
for fname in filenames: |
|
128 |
if any(fnmatch(fname, pat) for pat in exclude): |
|
129 |
continue |
|
130 |
fpath = join(dirpath, fname) |
|
131 |
if 'CUBENAME' in fname: |
|
132 |
tfpath = join(tdirpath, fname.replace('CUBENAME', context['cubename'])) |
|
133 |
elif 'DISTNAME' in fname: |
|
134 |
tfpath = join(tdirpath, fname.replace('DISTNAME', context['distname'])) |
|
135 |
else: |
|
136 |
tfpath = join(tdirpath, fname) |
|
137 |
if fname.endswith('.tmpl'): |
|
138 |
tfpath = tfpath[:-5] |
|
139 |
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
|
140 |
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
|
141 |
fill_templated_file(fpath, tfpath, context) |
0 | 142 |
print '[generate] %s <-- %s' % (tfpath, fpath) |
143 |
elif exists(tfpath): |
|
144 |
show_diffs(tfpath, fpath, askconfirm) |
|
145 |
else: |
|
146 |
shutil.copyfile(fpath, tfpath) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
147 |
|
0 | 148 |
def fill_templated_file(fpath, tfpath, context): |
149 |
fobj = file(tfpath, 'w') |
|
150 |
templated = file(fpath).read() |
|
151 |
fobj.write(templated % context) |
|
152 |
fobj.close() |
|
153 |
||
154 |
def restrict_perms_to_user(filepath, log=None): |
|
155 |
"""set -rw------- permission on the given file""" |
|
156 |
if log: |
|
157 |
log('set %s permissions to 0600', filepath) |
|
158 |
else: |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2388
diff
changeset
|
159 |
print '-> set %s permissions to 0600' % filepath |
0 | 160 |
chmod(filepath, 0600) |
161 |
||
162 |
def read_config(config_file): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2397
diff
changeset
|
163 |
"""read the instance configuration from a file and return it as a |
0 | 164 |
dictionnary |
165 |
||
166 |
:type config_file: str |
|
167 |
:param config_file: path to the configuration file |
|
168 |
||
169 |
:rtype: dict |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
170 |
:return: a dictionary with specified values associated to option names |
0 | 171 |
""" |
172 |
from logilab.common.fileutils import lines |
|
173 |
config = current = {} |
|
174 |
try: |
|
175 |
for line in lines(config_file, comments='#'): |
|
176 |
try: |
|
177 |
option, value = line.split('=', 1) |
|
178 |
except ValueError: |
|
179 |
option = line.strip().lower() |
|
180 |
if option[0] == '[': |
|
181 |
# start a section |
|
182 |
section = option[1:-1] |
|
183 |
assert not config.has_key(section), \ |
|
184 |
'Section %s is defined more than once' % section |
|
185 |
config[section] = current = {} |
|
186 |
continue |
|
187 |
print >> sys.stderr, 'ignoring malformed line\n%r' % line |
|
188 |
continue |
|
189 |
option = option.strip().replace(' ', '_') |
|
190 |
value = value.strip() |
|
191 |
current[option] = value or None |
|
192 |
except IOError, ex: |
|
193 |
warning('missing or non readable configuration file %s (%s)', |
|
194 |
config_file, ex) |
|
195 |
return config |
|
196 |
||
197 |
||
198 |
_HDLRS = {} |
|
199 |
||
200 |
class metacmdhandler(type): |
|
201 |
def __new__(mcs, name, bases, classdict): |
|
202 |
cls = super(metacmdhandler, mcs).__new__(mcs, name, bases, classdict) |
|
203 |
if getattr(cls, 'cfgname', None) and getattr(cls, 'cmdname', None): |
|
204 |
_HDLRS.setdefault(cls.cmdname, []).append(cls) |
|
205 |
return cls |
|
206 |
||
207 |
||
208 |
class CommandHandler(object): |
|
209 |
"""configuration specific helper for cubicweb-ctl commands""" |
|
210 |
__metaclass__ = metacmdhandler |
|
211 |
def __init__(self, config): |
|
212 |
self.config = config |
|
213 |
||
214 |
class Command(BaseCommand): |
|
215 |
"""base class for cubicweb-ctl commands""" |
|
216 |
||
217 |
def config_helper(self, config, required=True, cmdname=None): |
|
218 |
if cmdname is None: |
|
219 |
cmdname = self.name |
|
220 |
for helpercls in _HDLRS.get(cmdname, ()): |
|
221 |
if helpercls.cfgname == config.name: |
|
222 |
return helpercls(config) |
|
223 |
if config.name == 'all-in-one': |
|
224 |
for helpercls in _HDLRS.get(cmdname, ()): |
|
225 |
if helpercls.cfgname == 'repository': |
|
226 |
return helpercls(config) |
|
227 |
if required: |
|
228 |
msg = 'No helper for command %s using %s configuration' % ( |
|
229 |
cmdname, config.name) |
|
230 |
raise ConfigurationError(msg) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
231 |
|
0 | 232 |
def fail(self, reason): |
233 |
print "command failed:", reason |
|
234 |
sys.exit(1) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
235 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
236 |
|
0 | 237 |
CONNECT_OPTIONS = ( |
238 |
("user", |
|
239 |
{'short': 'u', 'type' : 'string', 'metavar': '<user>', |
|
240 |
'help': 'connect as <user> instead of being prompted to give it.', |
|
241 |
} |
|
242 |
), |
|
243 |
("password", |
|
244 |
{'short': 'p', 'type' : 'password', 'metavar': '<password>', |
|
245 |
'help': 'automatically give <password> for authentication instead of \ |
|
246 |
being prompted to give it.', |
|
247 |
}), |
|
248 |
("host", |
|
249 |
{'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
|
250 |
'default': None, |
0 | 251 |
'help': 'specify the name server\'s host name. Will be detected by \ |
252 |
broadcast if not provided.', |
|
253 |
}), |
|
254 |
) |
|
255 |
||
256 |
def config_connect(appid, optconfig): |
|
257 |
from cubicweb.dbapi import connect |
|
258 |
from getpass import getpass |
|
259 |
user = optconfig.user |
|
260 |
if not user: |
|
261 |
user = raw_input('login: ') |
|
262 |
password = optconfig.password |
|
263 |
if not password: |
|
264 |
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
|
265 |
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
|
266 |