1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """A user friendly command line interface to access all MadGraph features.
16 Uses the cmd package for command interpretation and tab completion.
17 """
18
19
20 import atexit
21 import logging
22 import optparse
23 import os
24 import pydoc
25 import re
26 import subprocess
27 import sys
28 import traceback
29 import time
30
31 root_path = os.path.split(os.path.dirname(os.path.realpath( __file__ )))[0]
32 root_path = os.path.split(root_path)[0]
33 sys.path.insert(0, root_path)
34
35
36 pjoin = os.path.join
37
38 import madgraph
39 import madgraph.interface.extended_cmd as cmd
40 import madgraph.interface.madgraph_interface as MGcmd
41 import madgraph.iolibs.files as files
42
43 from madgraph import MG4DIR, MG5DIR, MadGraph5Error
44
45 logger = logging.getLogger('cmdprint')
46
47
49 """ Helping class containing all the switching routine """
50
51 - def __init__(self, main='MadGraph', *args, **opt):
56
57
58
59
61 """redefine all the command to call directly the appropriate child"""
62
63 correct = True
64
65
66 overwritable = []
67
68 self.to_preserve = [key for key,method in Switcher.__dict__.items() if
69 hasattr(method, '__call__') ]
70 self.to_preserve += ['do_shell', 'help_shell', 'complete_shell']
71
72 ff = open(pjoin(MG5DIR, 'additional_command'), 'w')
73
74 for key in dir(self):
75
76 if key in self.to_preserve:
77 continue
78 if not (key.startswith('do_') or key.startswith('complete_') or \
79 key.startswith('help_') or key.startswith('check_') or \
80 key in overwritable):
81 continue
82 text = """\
83 def %(key)s(self, *args, **opts):
84 return self.cmd.%(key)s(self, *args, **opts)
85
86 """ % {'key': key}
87 logger.warning("""Command %s not define in the Master.
88 The line to add to the master_interface.py are written in 'additional_command' file""" % key)
89 ff.write(text)
90 correct = False
91
92
93
94
95 define = {}
96 for mother in MasterCmd.__mro__:
97 if mother.__name__ in ['Cmd', 'BasicCmd', 'ExtendedCmd']:
98 continue
99
100
101 for data in mother.__dict__:
102
103 if data in Switcher.__dict__ or data.startswith('__'):
104 continue
105 if data in MasterCmd.__dict__:
106
107 continue
108 if data not in define:
109 define[data] = mother.__name__
110 else:
111 logger.warning('%s define in %s and in %s but not in Switcher.' % (data, define[data], mother.__name__))
112 correct = False
113
114
115 define = {}
116 for mother in MasterCmdWeb.__mro__:
117 if mother.__name__ in ['Cmd', 'BasicCmd', 'ExtendedCmd']:
118 continue
119
120 for data in mother.__dict__:
121
122 if data in Switcher.__dict__ or data.startswith('__'):
123 continue
124 if data in MasterCmdWeb.__dict__:
125
126 continue
127 if data not in define:
128 define[data] = mother.__name__
129 else:
130 logger.warning('%s define in %s and in %s but not in Switcher.' % (data, define[data], mother.__name__))
131 correct = False
132
133 if not correct:
134 raise Exception, 'The Cmd interface has dangerous features. Please see previous warnings and correct those.'
135
136
137
138
139
141 return self.cmd.check_add(self, *args, **opts)
142
145
148
151
154
156 return self.cmd.check_draw(self, *args, **opts)
157
160
163
164 - def check_history(self, *args, **opts):
165 return self.cmd.check_history(self, *args, **opts)
166
169
172
175
177 return self.cmd.check_load(self, *args, **opts)
178
180 return self.cmd.check_open(self, *args, **opts)
181
184
187
189 return self.cmd.check_save(self, *args, **opts)
190
192 return self.cmd.check_set(self, *args, **opts)
193
196
199
202
205
208
211
214
217
218 - def complete_history(self, *args, **opts):
219 return self.cmd.complete_history(self, *args, **opts)
220
223
226
229
232
235
238
241
244
247
248 - def do_EOF(self, *args, **opts):
249 return self.cmd.do_EOF(self, *args, **opts)
250
251 - def do_add(self, *args, **opts):
252 return self.cmd.do_add(self, *args, **opts)
253
255 return self.cmd.do_check(self, *args, **opts)
256
258 return self.cmd.do_define(self, *args, **opts)
259
261 return self.cmd.do_display(self, *args, **opts)
262
264 return self.cmd.do_exit(self, *args, **opts)
265
268
270 return self.cmd.do_help(self, *args, **opts)
271
272 - def do_history(self, *args, **opts):
273 return self.cmd.do_history(self, *args, **opts)
274
276 return self.cmd.do_import(self, *args, **opts)
277
279 return self.cmd.do_install(self, *args, **opts)
280
282 return self.cmd.do_launch(self, *args, **opts)
283
285 return self.cmd.do_load(self, *args, **opts)
286
288 return self.cmd.do_open(self, *args, **opts)
289
291 return self.cmd.do_output(self, *args, **opts)
292
294 return self.cmd.do_quit(self, *args, **opts)
295
297 return self.cmd.do_save(self, *args, **opts)
298
299 - def do_set(self, *args, **opts):
300 return self.cmd.do_set(self, *args, **opts)
301
304
306 return self.cmd.help_EOF(self, *args, **opts)
307
309 return self.cmd.help_add(self, *args, **opts)
310
312 return self.cmd.help_check(self, *args, **opts)
313
316
319
322
324 return self.cmd.help_help(self, *args, **opts)
325
326 - def help_history(self, *args, **opts):
327 return self.cmd.help_history(self, *args, **opts)
328
331
334
337
339 return self.cmd.help_load(self, *args, **opts)
340
342 return self.cmd.help_open(self, *args, **opts)
343
346
348 return self.cmd.help_quit(self, *args, **opts)
349
351 return self.cmd.help_save(self, *args, **opts)
352
354 return self.cmd.help_set(self, *args, **opts)
355
358
361
364
365
366 -class MasterCmd(Switcher, MGcmd.MadGraphCmd, cmd.CmdShell):
377
379
381
382 if os.environ.has_key('_CONDOR_SCRATCH_DIR'):
383 self.writing_dir = pjoin(os.environ['_CONDOR_SCRATCH_DIR'], \
384 os.path.pardir)
385 else:
386 self.writing_dir = pjoin(os.environ['MADGRAPH_DATA'],
387 os.environ['REMOTE_USER'])
388
389
390
391 Switcher.__init__(self, mgme_dir = '', *arg, **opt)
392
393 self.options['timeout'] = 1
394
405
407 """Finalize web generation"""
408
409 self.cmd.finalize(self, nojpeg, online = True)
410
411
413 """Generate an amplitude for a given process"""
414
415 try:
416 Switcher.do_generate(self, line)
417 except:
418
419 files.cp(self._export_dir+'/HTML/stop.jpg',self._export_dir+'/HTML/card.jpg')
420 raise
421
422
424 """Generate an amplitude for a given process and add to
425 existing amplitudes
426 syntax:
427 """
428 try:
429 Switcher.do_add(self, line)
430 except:
431
432 files.cp(self._export_dir+'/HTML/stop.jpg',self._export_dir+'/HTML/card.jpg')
433 raise
434
435
437
438 """Force to use the web configuration file only"""
439 config_path = pjoin(os.environ['MADGRAPH_BASE'], 'mg5_configuration.txt')
440 return Switcher.set_configuration(self, config_path=config_path)
441
442
443 - def do_save(self, line, check=True):
444 """Save information to file"""
445
446 if check:
447 self.check_save([])
448 raise
449
450 args = self.split_arg(line)
451 if args[0] != 'options':
452 Switcher.do_save(self, line,check)
453 else:
454
455
456
457 files.cp(pjoin(MG5DIR,'input','mg5_configuration.txt'), args[1])
458