def run(self, args): import re requests = {} for filepath in args: try: stream = open(filepath) except OSError as ex: raise BadCommandUsage("can't open rql log file %s: %s" % (filepath, ex)) for lineno, line in enumerate(stream): if not ' WHERE ' in line: continue try: rql, time = line.split('--') rql = re.sub(r"(\'\w+': \d*)", '', rql) if '{' in rql: rql = rql[:rql.index('{')] req = requests.setdefault(rql, []) time.strip() chunks = time.split() clocktime = float(chunks[0][1:]) cputime = float(chunks[-3]) req.append((clocktime, cputime)) except Exception as exc: sys.stderr.write('Line %s: %s (%s)\n' % (lineno, exc, line)) stat = [] for rql, times in requests.items(): stat.append((sum(time[0] for time in times), sum(time[1] for time in times), len(times), rql)) stat.sort() stat.reverse() total_time = sum(clocktime for clocktime, cputime, occ, rql in stat) * 0.01 print( 'Percentage;Cumulative Time (clock);Cumulative Time (CPU);Occurences;Query' ) for clocktime, cputime, occ, rql in stat: print('%.2f;%.2f;%.2f;%s;%s' % (clocktime / total_time, clocktime, cputime, occ, rql))
def run(self, args): import re from logilab.common.shellutils import ASK cubename = args[0] if not re.match('[_A-Za-z][_A-Za-z0-9]*$', cubename): raise BadCommandUsage( 'cube name must be a valid python module name') verbose = self.get('verbose') destdir = self.get('directory') if not destdir: destdir = getcwd() elif not osp.isdir(destdir): print("-> creating cubes directory", destdir) try: mkdir(destdir) except OSError as err: self.fail("failed to create directory %r\n(%s)" % (destdir, err)) default_name = 'cubicweb-%s' % cubename.lower().replace('_', '-') if verbose: distname = input('Debian name for your cube ? [%s]): ' % default_name).strip() if not distname: distname = default_name elif not distname.startswith('cubicweb-'): if ASK.confirm('Do you mean cubicweb-%s ?' % distname): distname = 'cubicweb-' + distname else: distname = default_name if not re.match('[a-z][-a-z0-9]*$', distname): raise BadCommandUsage( 'cube distname should be a valid debian package name') cubedir = osp.join(destdir, distname) if osp.exists(cubedir): self.fail("%s already exists!" % cubedir) skeldir = osp.join(BASEDIR, 'skeleton') longdesc = shortdesc = self['short-description'] or input( 'Enter a short description for your cube: ') if verbose: longdesc = input( 'Enter a long description (leave empty to reuse the short one): ' ) dependencies = { 'cubicweb': '>= %s' % cubicwebversion, } if verbose: dependencies.update(self._ask_for_dependencies()) context = { 'cubename': cubename, 'distname': distname, 'shortdesc': shortdesc, 'longdesc': longdesc or shortdesc, 'dependencies': dependencies, 'version': cubicwebversion, 'year': str(date.today().year), 'author': self['author'], 'author-email': self['author-email'], 'rfc2822-date': datetime.now(tz=UTC).strftime('%a, %d %b %Y %T %z'), 'author-web-site': self['author-web-site'], 'license': self['license'], 'long-license': self.LICENSES[self['license']], } exclude = SKEL_EXCLUDE if self['layout'] == 'simple': exclude += ('sobjects.py*', 'precreate.py*', 'realdb_test*', 'cubes.*', 'uiprops.py*') copy_skeleton(skeldir, cubedir, context, exclude=exclude)
def run(self, args): """run the command with its specific arguments""" if not args: mode = 'all' elif len(args) == 1: mode = args[0] else: raise BadCommandUsage('Too many arguments') from cubicweb.migration import ConfigurationProblem if mode == 'all': print('CubicWeb %s (%s mode)' % (cwcfg.cubicweb_version(), cwcfg.mode)) print() if mode in ('all', 'config', 'configurations'): cwcfg.load_available_configs() print('Available configurations:') for config in CONFIGURATIONS: print('*', config.name) for line in config.__doc__.splitlines(): line = line.strip() if not line: continue print(' ', line) print() if mode in ('all', 'cubes'): cfgpb = ConfigurationProblem(cwcfg) try: cube_names = available_cube_names(cwcfg) namesize = max(len(x) for x in cube_names) except ConfigurationError as ex: print('No cubes available:', ex) except ValueError: print('No cubes available') else: print('Available cubes:') for cube in cube_names: try: tinfo = cwcfg.cube_pkginfo(cube) tversion = tinfo.version cfgpb.add_cube(cube, tversion) except (ConfigurationError, AttributeError) as ex: tinfo = None tversion = '[missing cube information: %s]' % ex print('* %s %s' % (cube.ljust(namesize), tversion)) if self.config.verbose: if tinfo: descr = getattr(tinfo, 'description', '') if not descr: descr = tinfo.__doc__ if descr: print(' ' + ' \n'.join(descr.splitlines())) modes = detect_available_modes(cwcfg.cube_dir(cube)) print(' available modes: %s' % ', '.join(modes)) print() if mode in ('all', 'instances'): try: regdir = cwcfg.instances_dir() except ConfigurationError as ex: print('No instance available:', ex) print() return instances = list_instances(regdir) if instances: print('Available instances (%s):' % regdir) for appid in instances: modes = cwcfg.possible_configurations(appid) if not modes: print('* %s (BROKEN instance, no configuration found)' % appid) continue print('* %s (%s)' % (appid, ', '.join(modes))) try: config = cwcfg.config_for(appid, modes[0]) except Exception as exc: print(' (BROKEN instance, %s)' % exc) continue else: print('No instance available in %s' % regdir) print() if mode == 'all': # configuration management problem solving cfgpb.solve() if cfgpb.warnings: print('Warnings:\n', '\n'.join('* ' + txt for txt in cfgpb.warnings)) if cfgpb.errors: print('Errors:') for op, cube, version, src in cfgpb.errors: if op == 'add': print('* cube', cube, end=' ') if version: print(' version', version, end=' ') print('is not installed, but required by %s' % src) else: print( '* cube %s version %s is installed, but version %s is required by %s' % (cube, cfgpb.cubes[cube], version, src) )
def check_options_consistency(config): if config.automatic and config.config_level > 0: raise BadCommandUsage('--automatic and --config-level should not be ' 'used together')
def run(self, args): """run the <command>_method on each argument (a list of instance identifiers) """ if not args: if "CW_INSTANCE" in os.environ: appid = os.environ["CW_INSTANCE"] else: raise BadCommandUsage("Error: instance id is missing") else: appid = args[0] cmdmeth = getattr(self, '%s_instance' % self.name) traceback_ = None # debugmode=True is to force to have a StreamHandler used instead of # writting the logs into a file in /tmp self.cwconfig = cwcfg.config_for(appid, debugmode=True) # by default loglevel is 'error' but we keep the default value to None # because some subcommands (e.g: pyramid) can override the loglevel in # certain situations if it's not explicitly set by the user and we want # to detect that (the "None" case) if self['loglevel'] is None: # if no loglevel is set but dbglevel is here we want to set level to debug if self['dbglevel']: init_cmdline_log_threshold(self.cwconfig, 'debug') else: init_cmdline_log_threshold(self.cwconfig, 'error') else: init_cmdline_log_threshold(self.cwconfig, self['loglevel']) if self['dbglevel']: set_debug('|'.join('DBG_' + x.upper() for x in self['dbglevel'])) try: status = cmdmeth(appid) or 0 except (ExecutionError, ConfigurationError) as ex: # we need to do extract this information here for pdb since it is # now lost in python 3 once we exit the try/catch statement exception_type, exception, traceback_ = sys.exc_info() sys.stderr.write('instance %s not %s: %s\n' % ( appid, self.actionverb, ex)) status = 4 except Exception as ex: # idem exception_type, exception, traceback_ = sys.exc_info() traceback.print_exc() sys.stderr.write('instance %s not %s: %s\n' % ( appid, self.actionverb, ex)) status = 8 except (KeyboardInterrupt, SystemExit) as ex: # idem exception_type, exception, traceback_ = sys.exc_info() sys.stderr.write('%s aborted\n' % self.name) if isinstance(ex, KeyboardInterrupt): status = 2 # specific error code else: status = ex.code if status != 0 and self.config.pdb: pdb = utils.get_pdb() if traceback_ is not None: pdb.post_mortem(traceback_) else: print("WARNING: Could not access to the traceback because the command return " "code is different than 0 but the command didn't raised an exception.") # we can't use "header=" of set_trace because ipdb doesn't supports it pdb.set_trace() sys.exit(status)