def runTests(options): global GROUND_QUERY, SINGLE_TEST, DEBUG SINGLE_TEST = options.singleTest DEBUG = options.debug GROUND_QUERY = options.groundQuery suite = unittest.makeSuite(OwlTestSuite) if options.profile: #from profile import Profile from hotshot import Profile, stats p = Profile('fuxi.profile') #p = Profile() for i in range(options.runs): p.runcall(unittest.TextTestRunner(verbosity=5).run, suite) p.close() s = stats.load('fuxi.profile') # s=p.create_stats() s.strip_dirs() s.sort_stats('time', 'cumulative', 'pcalls') s.print_stats(.1) s.print_callers(.05) s.print_callees(.05) else: for i in range(options.runs): unittest.TextTestRunner(verbosity=5).run(suite)
def runTests(options): if options is None: options = defaultOptions() global REASONING_STRATEGY, GROUND_QUERY, SINGLE_TEST, DEBUG SINGLE_TEST = options.singleTest DEBUG = options.debug GROUND_QUERY = options.groundQuery REASONING_STRATEGY = options.strategy suite = unittest.makeSuite(OwlTestSuite) if options.profile: # from profile import Profile from hotshot import Profile, stats p = Profile('fuxi.profile') # p = Profile() for i in range(options.runs): p.runcall(unittest.TextTestRunner(verbosity=5).run,suite) p.close() s = stats.load('fuxi.profile') # s = p.create_stats() s.strip_dirs() s.sort_stats('time','cumulative','pcalls') s.print_stats(.1) s.print_callers(.05) s.print_callees(.05) else: for i in range(options.runs): unittest.TextTestRunner(verbosity=5).run(suite)
def runTests(options): if options is None: options = defaultOptions() global REASONING_STRATEGY, GROUND_QUERY, SINGLE_TEST, DEBUG SINGLE_TEST = options.singleTest DEBUG = True # options.debug GROUND_QUERY = options.groundQuery REASONING_STRATEGY = options.strategy suite = unittest.makeSuite(OwlTestSuite) print("NTests: {}".format(suite.countTestCases())) if options.profile: # from profile import Profile from hotshot import Profile, stats p = Profile('fuxi.profile') # p = Profile() for i in range(options.runs): p.runcall(unittest.TextTestRunner(verbosity=5).run, suite) p.close() s = stats.load('fuxi.profile') # s = p.create_stats() s.strip_dirs() s.sort_stats('time', 'cumulative', 'pcalls') s.print_stats(.1) s.print_callers(.05) s.print_callees(.05) else: for i in range(options.runs): unittest.TextTestRunner(verbosity=5).run(suite)
def runProfiler(func, args=tuple(), kw={}, verbose=True, nb_func=25, sort_by=('cumulative', 'calls')): profile_filename = "/tmp/profiler" prof = Profile(profile_filename) try: if verbose: print "[+] Run profiler" result = prof.runcall(func, *args, **kw) prof.close() if verbose: print "[+] Stop profiler" print "[+] Process data..." stat = loadStats(profile_filename) if verbose: print "[+] Strip..." stat.strip_dirs() if verbose: print "[+] Sort data..." stat.sort_stats(*sort_by) if verbose: print print "[+] Display statistics" print stat.print_stats(nb_func) return result finally: unlink(profile_filename)
def runProfiler(logger, func, args=tuple(), kw={}, verbose=True, nb_func=25, sort_by=('time', )): """ Run a function in a profiler and then display the functions sorted by time. """ profile_filename = "/tmp/profiler" prof = Profile(profile_filename) try: logger.warning("Run profiler") result = prof.runcall(func, *args, **kw) prof.close() logger.error("Profiler: Process data...") stat = loadStats(profile_filename) stat.strip_dirs() stat.sort_stats(*sort_by) logger.error("Profiler: Result:") log = StringIO() stat.stream = log stat.print_stats(nb_func) log.seek(0) for line in log: logger.error(line.rstrip()) return result finally: unlink(profile_filename)
def runProfiler(logger, func, args=tuple(), kw={}, verbose=True, nb_func=25, sort_by=('time',)): """ Run a function in a profiler and then display the functions sorted by time. """ profile_filename = "/tmp/profiler" prof = Profile(profile_filename) try: logger.warning("Run profiler") result = prof.runcall(func, *args, **kw) prof.close() logger.error("Profiler: Process data...") stat = loadStats(profile_filename) stat.strip_dirs() stat.sort_stats(*sort_by) logger.error("Profiler: Result:") log = StringIO() stat.stream = log stat.print_stats(nb_func) log.seek(0) for line in log: logger.error(line.rstrip()) return result finally: unlink(profile_filename)
def profile(): from hotshot import Profile prof = Profile('widget.prof') prof.runcall(main) prof.close() import hotshot.stats stats = hotshot.stats.load('widget.prof') stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(30)
def profileTests(): from hotshot import Profile, stats p = Profile('rdflib-mysql.profile') p.runcall(testRun) p.close() s = stats.load('rdflib-mysql.profile') s.strip_dirs() s.sort_stats('time','cumulative','pcalls') #s.sort_stats('time','pcalls') s.print_stats(.1) s.print_callers(.1) s.print_callees(.1)
def profileTests(): from hotshot import Profile, stats p = Profile('rdflib-mysql.profile') p.runcall(testRun) p.close() s = stats.load('rdflib-mysql.profile') s.strip_dirs() s.sort_stats('time', 'cumulative', 'pcalls') #s.sort_stats('time','pcalls') s.print_stats(.1) s.print_callers(.1) s.print_callees(.1)
def profile(func, name='main', runKCacheGrind=False): prof = Profile('/tmp/' + name + '.prof', lineevents=1, linetimings=1) try: prof.runcall(func) finally: prof.close() # For running KCacheGrind you need to install (aptitude): # - kcachegrind # - kcachegrind-convertors if runKCacheGrind: path = '/'.join(__file__.split('/')[:-1]) system( 'hotshot2calltree -o /tmp/%(name)s.out /tmp/%(name)s.prof; kcachegrind /tmp/%(name)s.out' % locals())
def __call__(self, environ, start_response): """Invoke the Controller""" # WSGIController.__call__ dispatches to the Controller method # the request is routed to. This routing information is # available in environ['pylons.routes_dict'] try: if g.OPT.requestProfiling: pf = Profile(g.OPT.profileDumpFile) response = pf.runcall(WSGIController.__call__, self, environ, start_response) pf.close() return response else: return WSGIController.__call__(self, environ, start_response) finally: meta.Session.remove()
def runTests(profile=False): suite = unittest.makeSuite(OwlTestSuite) if profile: #from profile import Profile from hotshot import Profile, stats p = Profile('fuxi.profile') #p = Profile() p.runcall(unittest.TextTestRunner(verbosity=5).run,suite) p.close() s = stats.load('fuxi.profile') # s=p.create_stats() s.strip_dirs() s.sort_stats('time','cumulative','pcalls') s.print_stats(.1) s.print_callers(.05) s.print_callees(.05) else: unittest.TextTestRunner(verbosity=5).run(suite)
def __init__(self, args, reporter=None): self._rcfile = None self._plugins = [] preprocess_options( args, { # option: (callback, takearg) "rcfile": (self.cb_set_rcfile, True), "load-plugins": (self.cb_add_plugins, True), }, ) self.linter = linter = self.LinterClass( ( ( "rcfile", { "action": "callback", "callback": lambda *args: 1, "type": "string", "metavar": "<file>", "help": "Specify a configuration file.", }, ), ( "init-hook", { "action": "callback", "type": "string", "metavar": "<code>", "callback": cb_init_hook, "help": "Python code to execute, usually for sys.path \ manipulation such as pygtk.require().", }, ), ( "help-msg", { "action": "callback", "type": "string", "metavar": "<msg-id>", "callback": self.cb_help_message, "group": "Commands", "help": """Display a help message for the given message id and \ exit. The value may be a comma separated list of message ids.""", }, ), ( "list-msgs", { "action": "callback", "metavar": "<msg-id>", "callback": self.cb_list_messages, "group": "Commands", "help": "Generate pylint's full documentation.", }, ), ( "generate-rcfile", { "action": "callback", "callback": self.cb_generate_config, "group": "Commands", "help": """Generate a sample configuration file according to \ the current configuration. You can put other options before this one to get \ them in the generated configuration.""", }, ), ( "generate-man", { "action": "callback", "callback": self.cb_generate_manpage, "group": "Commands", "help": "Generate pylint's man page.", "hide": "True", }, ), ( "errors-only", { "action": "callback", "callback": self.cb_error_mode, "short": "e", "help": """In error mode, checkers without error messages are \ disabled and for others, only the ERROR messages are displayed, and no reports \ are done by default""", }, ), ("profile", {"type": "yn", "metavar": "<y_or_n>", "default": False, "help": "Profiled execution."}), ), option_groups=self.option_groups, reporter=reporter, pylintrc=self._rcfile, ) # register standard checkers checkers.initialize(linter) # load command line plugins linter.load_plugin_modules(self._plugins) # read configuration linter.disable_message("W0704") linter.read_config_file() # is there some additional plugins in the file configuration, in config_parser = linter._config_parser if config_parser.has_option("MASTER", "load-plugins"): plugins = splitstrip(config_parser.get("MASTER", "load-plugins")) linter.load_plugin_modules(plugins) # now we can load file config and command line, plugins (which can # provide options) have been registered linter.load_config_file() if reporter: # if a custom reporter is provided as argument, it may be overriden # by file parameters, so re-set it here, but before command line # parsing so it's still overrideable by command line option linter.set_reporter(reporter) args = linter.load_command_line_configuration(args) # insert current working directory to the python path to have a correct # behaviour sys.path.insert(0, os.getcwd()) if self.linter.config.profile: print >> sys.stderr, "** profiled run" from hotshot import Profile, stats prof = Profile("stones.prof") prof.runcall(linter.check, args) prof.close() data = stats.load("stones.prof") data.strip_dirs() data.sort_stats("time", "calls") data.print_stats(30) sys.path.pop(0)
network.inferredFacts = network.filteredFacts if options.closure \ and options.output in RDF_SERIALIZATION_FORMATS: cGraph = network.closureGraph(factGraph) cGraph.namespace_manager = namespace_manager print(cGraph.serialize(destination=None, format=options.output, base=None)) elif options.output and options.output in RDF_SERIALIZATION_FORMATS: print(network.inferredFacts.serialize(destination=None, format=options.output, base=None)) if __name__ == '__main__': from hotshot import Profile, stats # import pycallgraph # pycallgraph.start_trace() # main() # pycallgraph.make_dot_graph('FuXi-timing.png') # sys.exit(1) p = Profile('fuxi.profile') p.runcall(main) p.close() s = stats.load('fuxi.profile') s.strip_dirs() s.sort_stats('time', 'cumulative', 'pcalls') s.print_stats(.05) s.print_callers(.01) s.print_callees(.01)
if operations == None: sys.exit(-1) if output_op: for op, pkg in operations: print op, pkg.source sys.exit(0) if __name__ == '__main__': hotshot = 0 if hotshot: import tempfile from hotshot import Profile import hotshot.stats filename = tempfile.mktemp() prof = Profile(filename) try: prof = prof.runcall(main) except SystemExit: pass prof.close() del prof s = hotshot.stats.load(filename) s.strip_dirs().sort_stats('time').print_stats(20) s.strip_dirs().sort_stats('cumulative').print_stats(20) os.unlink(filename) else: main()
def __init__(self, args, reporter=None, exit=True): self._rcfile = None self._plugins = [] preprocess_options( args, { # option: (callback, takearg) "rcfile": (self.cb_set_rcfile, True), "load-plugins": (self.cb_add_plugins, True), }, ) self.linter = linter = self.LinterClass( ( ( "rcfile", { "action": "callback", "callback": lambda *args: 1, "type": "string", "metavar": "<file>", "help": "Specify a configuration file.", }, ), ( "init-hook", { "action": "callback", "type": "string", "metavar": "<code>", "callback": cb_init_hook, "level": 1, "help": "Python code to execute, usually for sys.path \ manipulation such as pygtk.require().", }, ), ( "help-msg", { "action": "callback", "type": "string", "metavar": "<msg-id>", "callback": self.cb_help_message, "group": "Commands", "help": """Display a help message for the given message id and \ exit. The value may be a comma separated list of message ids.""", }, ), ( "list-msgs", { "action": "callback", "metavar": "<msg-id>", "callback": self.cb_list_messages, "group": "Commands", "level": 1, "help": "Generate pylint's messages.", }, ), ( "full-documentation", { "action": "callback", "metavar": "<msg-id>", "callback": self.cb_full_documentation, "group": "Commands", "level": 1, "help": "Generate pylint's full documentation.", }, ), ( "generate-rcfile", { "action": "callback", "callback": self.cb_generate_config, "group": "Commands", "help": """Generate a sample configuration file according to \ the current configuration. You can put other options before this one to get \ them in the generated configuration.""", }, ), ( "generate-man", { "action": "callback", "callback": self.cb_generate_manpage, "group": "Commands", "help": "Generate pylint's man page.", "hide": True, }, ), ( "errors-only", { "action": "callback", "callback": self.cb_error_mode, "short": "E", "help": """In error mode, checkers without error messages are \ disabled and for others, only the ERROR messages are displayed, and no reports \ are done by default""", }, ), ( "profile", { "type": "yn", "metavar": "<y_or_n>", "default": False, "hide": True, "help": "Profiled execution.", }, ), ), option_groups=self.option_groups, reporter=reporter, pylintrc=self._rcfile, ) # register standard checkers from pylint import checkers checkers.initialize(linter) # load command line plugins linter.load_plugin_modules(self._plugins) # add some help section linter.add_help_section("Environment variables", config.ENV_HELP, level=1) linter.add_help_section( "Output", """ Using the default text output, the message format is : MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE There are 5 kind of message types : * (C) convention, for programming standard violation * (R) refactor, for bad code smell * (W) warning, for python specific problems * (E) error, for probable bugs in the code * (F) fatal, if an error occurred which prevented pylint from doing further processing. """, level=1, ) linter.add_help_section( "Output status code", """ Pylint should leave with following status code: * 0 if everything went fine * 1 if a fatal message was issued * 2 if an error message was issued * 4 if a warning message was issued * 8 if a refactor message was issued * 16 if a convention message was issued * 32 on usage error status 1 to 16 will be bit-ORed so you can know which different categories has been issued by analysing pylint output status code """, level=1, ) # read configuration linter.disable("W0704") linter.read_config_file() # is there some additional plugins in the file configuration, in config_parser = linter.cfgfile_parser if config_parser.has_option("MASTER", "load-plugins"): plugins = splitstrip(config_parser.get("MASTER", "load-plugins")) linter.load_plugin_modules(plugins) # now we can load file config and command line, plugins (which can # provide options) have been registered linter.load_config_file() if reporter: # if a custom reporter is provided as argument, it may be overridden # by file parameters, so re-set it here, but before command line # parsing so it's still overrideable by command line option linter.set_reporter(reporter) args = linter.load_command_line_configuration(args) if not args: print linter.help() sys.exit(32) # insert current working directory to the python path to have a correct # behaviour sys.path.insert(0, os.getcwd()) if self.linter.config.profile: print >> sys.stderr, "** profiled run" from hotshot import Profile, stats prof = Profile("stones.prof") prof.runcall(linter.check, args) prof.close() data = stats.load("stones.prof") data.strip_dirs() data.sort_stats("time", "calls") data.print_stats(30) else: linter.check(args) sys.path.pop(0) if exit: sys.exit(self.linter.msg_status)
if operations == None: sys.exit(-1) if output_op: for op,pkg in operations: print op, pkg.source sys.exit(0) if __name__ == '__main__': hotshot = 0 if hotshot: import tempfile from hotshot import Profile import hotshot.stats filename = tempfile.mktemp() prof = Profile(filename) try: prof = prof.runcall(main) except SystemExit: pass prof.close() del prof s = hotshot.stats.load(filename) s.strip_dirs().sort_stats('time').print_stats(20) s.strip_dirs().sort_stats('cumulative').print_stats(20) os.unlink(filename) else: main()
if options.closure and options.output in RDF_SERIALIZATION_FORMATS: cGraph = network.closureGraph(factGraph) cGraph.namespace_manager = namespace_manager print( cGraph.serialize(destination=None, format=options.output, base=None)) elif options.output and options.output in RDF_SERIALIZATION_FORMATS: print( network.inferredFacts.serialize(destination=None, format=options.output, base=None)) if __name__ == '__main__': from hotshot import Profile, stats # import pycallgraph # pycallgraph.start_trace() # main() # pycallgraph.make_dot_graph('FuXi-timing.png') # sys.exit(1) p = Profile('fuxi.profile') p.runcall(main) p.close() s = stats.load('fuxi.profile') s.strip_dirs() s.sort_stats('time', 'cumulative', 'pcalls') s.print_stats(.05) s.print_callers(.01) s.print_callees(.01)
def __init__(self, args, reporter=None): self._rcfile = None self._plugins = [] preprocess_options( args, { # option: (callback, takearg) 'rcfile': (self.cb_set_rcfile, True), 'load-plugins': (self.cb_add_plugins, True), }) self.linter = linter = self.LinterClass(( ('rcfile', { 'action': 'callback', 'callback': lambda *args: 1, 'type': 'string', 'metavar': '<file>', 'help': 'Specify a configuration file.' }), ('init-hook', { 'action': 'callback', 'type': 'string', 'metavar': '<code>', 'callback': cb_init_hook, 'help': 'Python code to execute, usually for sys.path \ manipulation such as pygtk.require().' }), ('help-msg', { 'action': 'callback', 'type': 'string', 'metavar': '<msg-id>', 'callback': self.cb_help_message, 'group': 'Commands', 'help': '''Display a help message for the given message id and \ exit. The value may be a comma separated list of message ids.''' }), ('list-msgs', { 'action': 'callback', 'metavar': '<msg-id>', 'callback': self.cb_list_messages, 'group': 'Commands', 'help': "Generate pylint's full documentation." }), ('generate-rcfile', { 'action': 'callback', 'callback': self.cb_generate_config, 'group': 'Commands', 'help': '''Generate a sample configuration file according to \ the current configuration. You can put other options before this one to get \ them in the generated configuration.''' }), ('generate-man', { 'action': 'callback', 'callback': self.cb_generate_manpage, 'group': 'Commands', 'help': "Generate pylint's man page.", 'hide': 'True' }), ('errors-only', { 'action': 'callback', 'callback': self.cb_error_mode, 'short': 'e', 'help': '''In error mode, checkers without error messages are \ disabled and for others, only the ERROR messages are displayed, and no reports \ are done by default''' }), ('profile', { 'type': 'yn', 'metavar': '<y_or_n>', 'default': False, 'help': 'Profiled execution.' }), ), option_groups=self. option_groups, reporter=reporter, pylintrc=self._rcfile) # register standard checkers checkers.initialize(linter) # load command line plugins linter.load_plugin_modules(self._plugins) # read configuration linter.disable_message('W0704') linter.read_config_file() # is there some additional plugins in the file configuration, in config_parser = linter._config_parser if config_parser.has_option('MASTER', 'load-plugins'): plugins = splitstrip( config_parser.get('MASTER', 'load-plugins')) linter.load_plugin_modules(plugins) # now we can load file config and command line, plugins (which can # provide options) have been registered linter.load_config_file() if reporter: # if a custom reporter is provided as argument, it may be overriden # by file parameters, so re-set it here, but before command line # parsing so it's still overrideable by command line option linter.set_reporter(reporter) args = linter.load_command_line_configuration(args) # insert current working directory to the python path to have a correct # behaviour sys.path.insert(0, os.getcwd()) if self.linter.config.profile: print('** profiled run', file=sys.stderr) from hotshot import Profile, stats prof = Profile('stones.prof') prof.runcall(linter.check, args) prof.close() data = stats.load('stones.prof') data.strip_dirs() data.sort_stats('time', 'calls') data.print_stats(30) sys.path.pop(0)
def __init__(self, args, reporter=None): self._rcfile = None self._plugins = [] preprocess_options(args, { # option: (callback, takearg) 'rcfile': (self.cb_set_rcfile, True), 'load-plugins': (self.cb_add_plugins, True), }) self.linter = linter = self.LinterClass(( ('rcfile', {'action' : 'callback', 'callback' : lambda *args: 1, 'type': 'string', 'metavar': '<file>', 'help' : 'Specify a configuration file.'}), ('init-hook', {'action' : 'callback', 'type' : 'string', 'metavar': '<code>', 'callback' : cb_init_hook, 'help' : 'Python code to execute, usually for sys.path \ manipulation such as pygtk.require().'}), ('help-msg', {'action' : 'callback', 'type' : 'string', 'metavar': '<msg-id>', 'callback' : self.cb_help_message, 'group': 'Commands', 'help' : '''Display a help message for the given message id and \ exit. The value may be a comma separated list of message ids.'''}), ('list-msgs', {'action' : 'callback', 'metavar': '<msg-id>', 'callback' : self.cb_list_messages, 'group': 'Commands', 'help' : "Generate pylint's full documentation."}), ('generate-rcfile', {'action' : 'callback', 'callback' : self.cb_generate_config, 'group': 'Commands', 'help' : '''Generate a sample configuration file according to \ the current configuration. You can put other options before this one to get \ them in the generated configuration.'''}), ('generate-man', {'action' : 'callback', 'callback' : self.cb_generate_manpage, 'group': 'Commands', 'help' : "Generate pylint's man page.",'hide': 'True'}), ('errors-only', {'action' : 'callback', 'callback' : self.cb_error_mode, 'short': 'e', 'help' : '''In error mode, checkers without error messages are \ disabled and for others, only the ERROR messages are displayed, and no reports \ are done by default'''}), ('profile', {'type' : 'yn', 'metavar' : '<y_or_n>', 'default': False, 'help' : 'Profiled execution.'}), ), option_groups=self.option_groups, reporter=reporter, pylintrc=self._rcfile) # register standard checkers from pylint import checkers checkers.initialize(linter) # load command line plugins linter.load_plugin_modules(self._plugins) # add some help section linter.add_help_section('Environment variables', config.ENV_HELP) linter.add_help_section('Output', ''' Using the default text output, the message format is : MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE There are 5 kind of message types : * (C) convention, for programming standard violation * (R) refactor, for bad code smell * (W) warning, for python specific problems * (E) error, for probable bugs in the code * (F) fatal, if an error occured which prevented pylint from doing further \ processing. ''') linter.add_help_section('Output status code', ''' Pylint should leave with following status code: * 0 if everything went fine * 1 if some fatal message issued * 2 if some error message issued * 4 if some warning message issued * 8 if some refactor message issued * 16 if some convention message issued * 32 on usage error status 1 to 16 will be bit-ORed so you can know which different categories has been issued by analysing pylint output status code ''') # read configuration linter.disable_message('W0704') linter.read_config_file() # is there some additional plugins in the file configuration, in config_parser = linter._config_parser if config_parser.has_option('MASTER', 'load-plugins'): plugins = get_csv(config_parser.get('MASTER', 'load-plugins')) linter.load_plugin_modules(plugins) # now we can load file config and command line, plugins (which can # provide options) have been registered linter.load_config_file() if reporter: # if a custom reporter is provided as argument, it may be overriden # by file parameters, so re-set it here, but before command line # parsing so it's still overrideable by command line option linter.set_reporter(reporter) args = linter.load_command_line_configuration(args) if not args: print linter.help() sys.exit(32) # insert current working directory to the python path to have a correct # behaviour sys.path.insert(0, os.getcwd()) if self.linter.config.profile: print >> sys.stderr, '** profiled run' from hotshot import Profile, stats prof = Profile('stones.prof') prof.runcall(linter.check, args) prof.close() data = stats.load('stones.prof') data.strip_dirs() data.sort_stats('time', 'calls') data.print_stats(30) else: linter.check(args) sys.path.pop(0) sys.exit(self.linter.msg_status)
def __init__(self, args, reporter=None): self._rcfile = None self._plugins = [] preprocess_options(args, { # option: (callback, takearg) 'rcfile': (self.cb_set_rcfile, True), 'load-plugins': (self.cb_add_plugins, True), }) self.linter = linter = self.LinterClass(( ('rcfile', {'action' : 'callback', 'callback' : lambda *args: 1, 'type': 'string', 'metavar': '<file>', 'help' : 'Specify a configuration file.'}), ('init-hook', {'action' : 'callback', 'type' : 'string', 'metavar': '<code>', 'callback' : cb_init_hook, 'help' : 'Python code to execute, usually for sys.path \ manipulation such as pygtk.require().'}), ('help-msg', {'action' : 'callback', 'type' : 'string', 'metavar': '<msg-id>', 'callback' : self.cb_help_message, 'group': 'Commands', 'help' : '''Display a help message for the given message id and \ exit. The value may be a comma separated list of message ids.'''}), ('list-msgs', {'action' : 'callback', 'metavar': '<msg-id>', 'callback' : self.cb_list_messages, 'group': 'Commands', 'help' : "Generate pylint's full documentation."}), ('generate-rcfile', {'action' : 'callback', 'callback' : self.cb_generate_config, 'group': 'Commands', 'help' : '''Generate a sample configuration file according to \ the current configuration. You can put other options before this one to get \ them in the generated configuration.'''}), ('generate-man', {'action' : 'callback', 'callback' : self.cb_generate_manpage, 'group': 'Commands', 'help' : "Generate pylint's man page.",'hide': 'True'}), ('errors-only', {'action' : 'callback', 'callback' : self.cb_error_mode, 'short': 'e', 'help' : '''In error mode, checkers without error messages are \ disabled and for others, only the ERROR messages are displayed, and no reports \ are done by default'''}), ('profile', {'type' : 'yn', 'metavar' : '<y_or_n>', 'default': False, 'help' : 'Profiled execution.'}), ), option_groups=self.option_groups, reporter=reporter, pylintrc=self._rcfile) # register standard checkers checkers.initialize(linter) # load command line plugins linter.load_plugin_modules(self._plugins) # read configuration linter.disable_message('W0704') linter.read_config_file() # is there some additional plugins in the file configuration, in config_parser = linter._config_parser if config_parser.has_option('MASTER', 'load-plugins'): plugins = splitstrip(config_parser.get('MASTER', 'load-plugins')) linter.load_plugin_modules(plugins) # now we can load file config and command line, plugins (which can # provide options) have been registered linter.load_config_file() if reporter: # if a custom reporter is provided as argument, it may be overriden # by file parameters, so re-set it here, but before command line # parsing so it's still overrideable by command line option linter.set_reporter(reporter) args = linter.load_command_line_configuration(args) # insert current working directory to the python path to have a correct # behaviour sys.path.insert(0, os.getcwd()) if self.linter.config.profile: print >> sys.stderr, '** profiled run' from hotshot import Profile, stats prof = Profile('stones.prof') prof.runcall(linter.check, args) prof.close() data = stats.load('stones.prof') data.strip_dirs() data.sort_stats('time', 'calls') data.print_stats(30) sys.path.pop(0)
def __init__(self, args, reporter=None): self._rcfile = None self._plugins = [] preprocess_options( args, { # option: (callback, takearg) 'rcfile': (self.cb_set_rcfile, True), 'load-plugins': (self.cb_add_plugins, True), }) self.linter = linter = self.LinterClass( ( ('rcfile', { 'action': 'callback', 'callback': lambda *args: 1, 'type': 'string', 'metavar': '<file>', 'help': 'Specify a configuration file.' }), ('init-hook', { 'action': 'callback', 'type': 'string', 'metavar': '<code>', 'callback': cb_init_hook, 'help': 'Python code to execute, usually for sys.path \ manipulation such as pygtk.require().' }), ('help-msg', { 'action': 'callback', 'type': 'string', 'metavar': '<msg-id>', 'callback': self.cb_help_message, 'group': 'Commands', 'help': '''Display a help message for the given message id and \ exit. The value may be a comma separated list of message ids.''' }), ('list-msgs', { 'action': 'callback', 'metavar': '<msg-id>', 'callback': self.cb_list_messages, 'group': 'Commands', 'help': "Generate pylint's messages." }), ('full-documentation', { 'action': 'callback', 'metavar': '<msg-id>', 'callback': self.cb_full_documentation, 'group': 'Commands', 'help': "Generate pylint's full documentation." }), ('generate-rcfile', { 'action': 'callback', 'callback': self.cb_generate_config, 'group': 'Commands', 'help': '''Generate a sample configuration file according to \ the current configuration. You can put other options before this one to get \ them in the generated configuration.''' }), ('generate-man', { 'action': 'callback', 'callback': self.cb_generate_manpage, 'group': 'Commands', 'help': "Generate pylint's man page.", 'hide': 'True' }), ('errors-only', { 'action': 'callback', 'callback': self.cb_error_mode, 'short': 'e', 'help': '''In error mode, checkers without error messages are \ disabled and for others, only the ERROR messages are displayed, and no reports \ are done by default''' }), ('profile', { 'type': 'yn', 'metavar': '<y_or_n>', 'default': False, 'help': 'Profiled execution.' }), ), option_groups=self.option_groups, reporter=reporter, pylintrc=self._rcfile) # register standard checkers from pylint import checkers checkers.initialize(linter) # load command line plugins linter.load_plugin_modules(self._plugins) # add some help section linter.add_help_section('Environment variables', config.ENV_HELP) linter.add_help_section( 'Output', ''' Using the default text output, the message format is : MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE There are 5 kind of message types : * (C) convention, for programming standard violation * (R) refactor, for bad code smell * (W) warning, for python specific problems * (E) error, for probable bugs in the code * (F) fatal, if an error occurred which prevented pylint from doing further processing. ''') linter.add_help_section( 'Output status code', ''' Pylint should leave with following status code: * 0 if everything went fine * 1 if a fatal message was issued * 2 if an error message was issued * 4 if a warning message was issued * 8 if a refactor message was issued * 16 if a convention message was issued * 32 on usage error status 1 to 16 will be bit-ORed so you can know which different categories has been issued by analysing pylint output status code ''') # read configuration linter.disable_message('W0704') linter.read_config_file() # is there some additional plugins in the file configuration, in config_parser = linter._config_parser if config_parser.has_option('MASTER', 'load-plugins'): plugins = splitstrip(config_parser.get('MASTER', 'load-plugins')) linter.load_plugin_modules(plugins) # now we can load file config and command line, plugins (which can # provide options) have been registered linter.load_config_file() if reporter: # if a custom reporter is provided as argument, it may be overridden # by file parameters, so re-set it here, but before command line # parsing so it's still overrideable by command line option linter.set_reporter(reporter) args = linter.load_command_line_configuration(args) if not args: print linter.help() sys.exit(32) # insert current working directory to the python path to have a correct # behaviour sys.path.insert(0, os.getcwd()) if self.linter.config.profile: print >> sys.stderr, '** profiled run' from hotshot import Profile, stats prof = Profile('stones.prof') prof.runcall(linter.check, args) prof.close() data = stats.load('stones.prof') data.strip_dirs() data.sort_stats('time', 'calls') data.print_stats(30) else: linter.check(args) sys.path.pop(0) sys.exit(self.linter.msg_status)
class ProfileMiddleware(object): """ Displays hotshot profiling for any view. http://yoursite.com/yourview/?prof Add the "prof" key to query string by appending ?prof (or &prof=) and you'll see the profiling results in your browser. It's set up to only be available in django's debug mode, is available for superuser otherwise, but you really shouldn't add this middleware to any production configuration. WARNING: It uses hotshot profiler which is not thread safe. """ def __init__(self): self.tmpfile = None self.prof = None def process_request(self, request): if (settings.DEBUG or request.user.is_superuser) \ and request.method == 'GET' and request.GET.get('prof', False): self.tmpfile = mktemp() self.prof = Profile(self.tmpfile) def process_view(self, request, callback, callback_args, callback_kwargs): if (settings.DEBUG or request.user.is_superuser) \ and request.method == 'GET' and request.GET.get('prof', False): return self.prof.runcall(callback, request, *callback_args, **callback_kwargs) @staticmethod def get_group(f): for g in group_prefix_re: name = g.findall(f) if name: return name[0] @staticmethod def get_summary(results_dict, sum_obj): group = [(item[1], item[0]) for item in results_dict.items()] group.sort(reverse=True) group = list[:40] res = " tottime\n" for item in group: res += "%4.1f%% %7.3f %s\n" % \ (100 * item[0] / sum_obj if sum_obj else 0, item[0], item[1]) return res def summary_for_files(self, stats_str): stats_str = stats_str.split("\n")[5:] mystats = {} mygroups = {} sum_obj = 0 for s in stats_str: fields = words_re.split(s) if len(fields) == 7: time = float(fields[2]) sum_obj += time file_name = fields[6].split(":")[0] if file_name not in mystats: mystats[file_name] = 0 mystats[file_name] += time group = self.get_group(file_name) if group not in mygroups: mygroups[group] = 0 mygroups[group] += time return '<pre> ---- By file ----\n\n{}\n ' \ '---- By group ---\n\n{}</pre>'.\ format(self.get_summary(mystats, sum_obj), self.get_summary(mygroups, sum_obj)) def process_response(self, request, response): if (settings.DEBUG or request.user.is_superuser) \ and request.method == 'GET' and request.GET.get('prof', False): self.prof.close() out = StringIO.StringIO() old_stdout = sys.stdout sys.stdout = out obj_stats = stats.load(self.tmpfile) obj_stats.sort_stats('time', 'calls') obj_stats.print_stats() sys.stdout = old_stdout stats_str = out.getvalue() if response and response.content and stats_str: response.content = "<pre>" + stats_str + "</pre>" response.content = "\n".join(response.content.split("\n")[:40]) response.content += self.summary_for_files(stats_str) unlink(self.tmpfile) return response