def main(argv=None): """Simulate 'python -m <modname>'. This is like the ``runpy`` standard library module, but this allows chained -m invocations. argv: The argv of the program, default sys.argv argv Some notes: 'python -m somemodule' => argv[0]=the module calling this 'python -m somemodule' arg => argv[0]=module calling this argv[1]=arg """ if argv is None: import sys argv = sys.argv print argv del argv[0] # The script calling this. if len(argv) > 0 and argv[0] == '-m': modname = argv[1] del argv[0:1] runpy.run_module(modname, run_name='__main__', alter_sys=True) elif len(argv) > 0: runpy.run_path(argv[0], run_name='__main__') else: from code import interact interact(local={'__name__':'__main__'})
def run_cli(): from multiprocessing import freeze_support freeze_support() setup_logging() # Use default matplotlib backend on mac/linux, but wx on windows. # The problem on mac is that the wx backend requires pythonw. On windows # we are sure to wx since it is the shipped with the app. setup_mpl(backend='WXAgg' if os.name == 'nt' else None) setup_sasmodels() if len(sys.argv) == 1 or sys.argv[1] == '-i': # Run sasview as an interactive python interpreter try: from IPython import start_ipython sys.argv = ["ipython", "--pylab"] sys.exit(start_ipython()) except ImportError: import code code.interact(local={'exit': sys.exit}) elif sys.argv[1] == '-c': exec(sys.argv[2]) else: thing_to_run = sys.argv[1] sys.argv = sys.argv[1:] import runpy if os.path.exists(thing_to_run): runpy.run_path(thing_to_run, run_name="__main__") else: runpy.run_module(thing_to_run, run_name="__main__")
def main(args=None): if args is None: args = sys.argv[1:] # TODO Add help output, --help, etc. # TODO Handle library-wide options. Eg.: # --unicodedata # --verbose / other logging stuff # TODO Allow a way to run arbitrary modules? Useful for setting # library-wide options and calling another library. Eg.: # # $ fonttools --unicodedata=... fontmake ... # # This allows for a git-like command where thirdparty commands # can be added. Should we just try importing the fonttools # module first and try without if it fails? mod = 'fontTools.'+sys.argv[1] sys.argv[1] = sys.argv[0] + ' ' + sys.argv[1] del sys.argv[0] import runpy runpy.run_module(mod, run_name='__main__')
def exec_module(module, global_variables): '''Executes the provided module as if it were provided as '-m module'. The functionality is implemented using `runpy.run_module`, which was added in Python 2.5. ''' import runpy runpy.run_module(module, global_variables, alter_sys = True)
def run_apython(args=None): if args is None: args = sys.argv[1:] namespace = parse_args(args) try: sys._argv = sys.argv sys._path = sys.path if namespace.module: sys.argv = [None] + namespace.args sys.path.insert(0, '') events.set_interactive_policy(namespace.serve) runpy.run_module(namespace.filename, run_name='__main__', alter_sys=True) elif namespace.filename: sys.argv = [None] + namespace.args path = os.path.dirname(os.path.abspath(namespace.filename)) sys.path.insert(0, path) events.set_interactive_policy(namespace.serve) runpy.run_path(namespace.filename, run_name='__main__') else: events.run_console(serve=namespace.serve) finally: sys.argv = sys._argv sys.path = sys._path
def main(): '''Run the given script under the profiler, when invoked as a module (python -m statprof ...), and display the profile report once done. ''' if not sys.argv[1:] or sys.argv[1] in ('--help', '-h'): print('usage: python -m statprof <script> [<args>]') sys.exit(2) scriptfile = sys.argv[1] if scriptfile.startswith("-m"): if scriptfile == "-m": scriptfile = sys.argv[2] del sys.argv[1:3] else: scriptfile = scriptfile[2:] del sys.argv[1] with profile(): runpy.run_module(scriptfile, run_name="__main__", alter_sys=True) else: if not os.path.exists(scriptfile): print('Error: %s does not exist' % (scriptfile,)) sys.exit(1) del sys.argv[0] # Hide 'statprof' from argument list # Replace statprof's dir with script's dir in front of module search path sys.path[0] = os.path.dirname(os.path.realpath(scriptfile)) with profile(): _runscript(scriptfile)
def _check_relative_imports(self, depth, run_name=None): contents = r"""\ from __future__ import absolute_import from . import sibling from ..uncle.cousin import nephew """ pkg_dir, mod_fname, mod_name = ( self._make_pkg(contents, depth)) try: self._add_relative_modules(pkg_dir, contents, depth) pkg_name = mod_name.rpartition('.')[0] if verbose: print("Running from source:", mod_name) d1 = run_module(mod_name, run_name=run_name) # Read from source self.assertIn("__package__", d1) self.assertTrue(d1["__package__"] == pkg_name) self.assertIn("sibling", d1) self.assertIn("nephew", d1) del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) make_legacy_pyc(mod_fname) unload(mod_name) # In case the loader caches paths if verbose: print("Running from compiled:", mod_name) d2 = run_module(mod_name, run_name=run_name) # Read from bytecode self.assertIn("__package__", d2) self.assertTrue(d2["__package__"] == pkg_name) self.assertIn("sibling", d2) self.assertIn("nephew", d2) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) if verbose: print("Module executed successfully")
def expect_import_error(self, mod_name): try: run_module(mod_name) except ImportError: pass else: self.fail("Expected import error for " + mod_name)
def test_tictactoe(): with patch( "__builtin__.raw_input", MagicMock(side_effect=["0 0", "0 1", "1 1", "0 2", "2 2", "1 0"]) ) as mock_raw_input: tictactoe.tictactoe() with patch( "__builtin__.raw_input", MagicMock(side_effect=["2 0", "0 1", "1 1", "1 0", "0 2", "1 0"]) ) as mock_raw_input: tictactoe.tictactoe() with patch( "__builtin__.raw_input", MagicMock(side_effect=["2 0", "1 1", "2 1", "1 2", "2 2", "1 0"]) ) as mock_raw_input: tictactoe.tictactoe() with patch( "__builtin__.raw_input", MagicMock( side_effect=["0 2", "0 1", "1 2", "xxxx", "ads233 j33j3j", "-1 -1", "500 2", "0 0", "0 0", "2 2", "1 0"] ), ) as mock_raw_input: with patch("__builtin__.exit", MagicMock()): # tictactoe.tictactoe() runpy.run_module("tictactoe", run_name="__main__") try: tictactoe.TicTacToeBoard(1, 0, "_") except Exception as e: pass try: tictactoe.HumanPlayer("Josh", tictactoe.TicTacToeBoard.EMPTY_SPOT) except Exception as e: pass
def main(): parser = argparse.ArgumentParser() parser.add_argument('--package', action='append') parser.add_argument('--dir', action='append') parser.add_argument('-m', action='store', metavar='MODULE') args, rest = parser.parse_known_args() if args.package: PACKAGES.extend(args.package) if args.dir: DIRS.extend(os.path.abspath(d) for d in args.dir) if not PACKAGES and not DIRS: DIRS.append(os.getcwd()) if args.m: sys.argv[1:] = rest runpy.run_module(args.m, run_name='__main__', alter_sys=True) elif rest: sys.argv = rest converted = maybe_2to3(rest[0]) with open(converted) as f: new_globals = dict(__name__='__main__', __file__=rest[0]) exec(f.read(), new_globals) else: import code code.interact()
def test_snake(): random.seed(0) mockturtle.events[:] = ( [('timer',), ('key Left',), ('timer',), ('key Up',)] + [('timer', True)] * 300 ) runpy.run_module('freegames.snake')
def test_run_package_init_exceptions(self): # These were previously wrapped in an ImportError; see Issue 14285 result = self._make_pkg("", 1, "__main__") pkg_dir, _, mod_name, _ = result mod_name = mod_name.replace(".__main__", "") self.addCleanup(self._del_pkg, pkg_dir) init = os.path.join(pkg_dir, "__runpy_pkg__", "__init__.py") exceptions = (ImportError, AttributeError, TypeError, ValueError) for exception in exceptions: name = exception.__name__ with self.subTest(name): source = "raise {0}('{0} in __init__.py.')".format(name) with open(init, "wt", encoding="ascii") as mod_file: mod_file.write(source) try: run_module(mod_name) except exception as err: self.assertNotIn("finding spec", format(err)) else: self.fail("Nothing raised; expected {}".format(name)) try: run_module(mod_name + ".submodule") except exception as err: self.assertNotIn("finding spec", format(err)) else: self.fail("Nothing raised; expected {}".format(name))
def test_tron_1(): random.seed(0) mockturtle.events[:] = ( [('key a',), ('key a',)] + [('timer', True)] * 600 ) runpy.run_module('freegames.tron')
def runTest(): savedStderr = sys.stderr totalTests = 0 hadError = False for modName in braille.__all__: myIO = StringIO() sys.stderr = myIO run_module('music21.braille.' + modName, run_name='__main__') errOutput = myIO.getvalue() myIO.close() for thisLine in errOutput.splitlines(): if re.match(r'^\.*$', thisLine): # all dots or blank line continue if re.match(r'^\-+$', thisLine): # separator continue if thisLine == 'OK': continue numTests = re.match(r'^Ran (\d+) tests? in ', thisLine) if numTests: totalTests += int(numTests.group(1)) continue hadError = True print(thisLine) print("Total tests: ", totalTests) if hadError: print("ERRORS FOUND") else: print("All good!") sys.stderr = savedStderr
def test_pacman(): random.seed(0) mockturtle.events[:] = ( [('timer', True), ('key Up',)] * 600 + [('timer', True)] * 3000 ) runpy.run_module('freegames.pacman')
def main(): global __file__ if len(sys.argv) >= 3 and sys.argv[1] == '-m': mode = 'module' module = sys.argv[2] del sys.argv[1:3] elif len(sys.argv) >= 2: mode = 'script' script = sys.argv[1] sys.argv = sys.argv[1:] else: print 'usage: python -m plop.collector -m module_to_run' sys.exit(1) collector = Collector() collector.start(duration=3600) exit_code = 0 try: if mode == 'module': import runpy runpy.run_module(module, run_name='__main__', alter_sys=True) elif mode == 'script': with open(script) as f: __file__ = script exec f.read() in globals(), globals() except SystemExit as e: exit_code = e.code collector.stop() collector.filter(50) with open('/tmp/plop.out', 'w') as f: f.write(repr(dict(collector.stack_counts))) print 'profile output saved to /tmp/plop.out' overhead = float(collector.sample_time) / collector.samples_taken print 'overhead was %s per sample (%s%%)' % (overhead, overhead / collector.interval) sys.exit(exit_code)
def load_process(process_path): ''' load process PEP 338 - Executing modules as scripts http://www.python.org/dev/peps/pep-0338 ''' if '.' not in process_path: raise RuntimeError("Invalid process path: {}".format(process_path)) module_name, process_name = process_path.rsplit('.', 1) try: try: module = runpy.run_module(module_name) except ImportError: module = runpy.run_module(module_name + ".__init__") except ImportError, e: import traceback, pkgutil tb_tups = traceback.extract_tb(sys.exc_info()[2]) if pkgutil.__file__.startswith(tb_tups[-1][0]): # If the bottommost frame in our stack was in pkgutil, # then we can safely say that this ImportError occurred # because the top level class path was not found. raise RuntimeError("Unable to load process path: {}:\n{}".format(process_path, e)) else: # If the ImportError occurred further down, # raise original exception. raise
def legacy_app_for_script(script): """Returns the CGI app specified in the input string, or None on failure. Args: script: A script specification from a python27 app.yaml. e.g. my.module.app Returns: The application as extracted from the script, or None. """ with MockedWsgiHandler.python_25_app_lock: modname = script.rsplit('.', 1)[0] app_holder = [] revert_func = lambda: None try: revert_func = stub_wsgi_utils(app_holder) runpy.run_module(modname, run_name='__main__') except ImportError as e: logging.exception('Error loading %s', script) finally: revert_func() if app_holder: return app_holder[0] else: logging.error('Cannot load an app from %s', script) return None
def run_module(module, function=None): if function is not None: # run it as a module module = os.sep.join(module.split('.')) pwd = os.getcwd() module_path = os.path.join(pwd, module) if os.path.isdir(module_path): module_path = os.path.join(module_path, '__init__.py') else: module_path = "{}.py".format(module_path) module = imp.load_source('module', module_path) if hasattr(module, function): getattr(module, function)() else: try: # Change the sys.path # https://docs.python.org/3/using/cmdline.html#cmdoption-m sys.path.insert(0, '.') runpy.run_module(module, run_name="__main__", alter_sys=True) # Reference # https://docs.python.org/2/library/runpy.html except Exception, e: exec_info = sys.exc_info() traceback.print_exception(*exec_info)
def _check_relative_imports(self, depth, run_name=None): contents = r"""\ from __future__ import absolute_import from . import sibling from ..uncle.cousin import nephew """ pkg_dir, mod_fname, mod_name = ( self._make_pkg(contents, depth)) try: self._add_relative_modules(pkg_dir, contents, depth) pkg_name = mod_name.rpartition('.')[0] if verbose: print "Running from source:", mod_name d1 = run_module(mod_name, run_name=run_name) # Read from source self.assertIn("__package__", d1) self.assertTrue(d1["__package__"] == pkg_name) self.assertIn("sibling", d1) self.assertIn("nephew", d1) del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) if not due_to_ironpython_incompatibility("IPy can't load modules from bytecode"): if verbose: print "Running from compiled:", mod_name d2 = run_module(mod_name, run_name=run_name) # Read from bytecode self.assertIn("__package__", d2) self.assertTrue(d2["__package__"] == pkg_name) self.assertIn("sibling", d2) self.assertIn("nephew", d2) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) if verbose: print "Module executed successfully"
def main(): # TODO: more options, refactor this into somewhere shared # between tornado.autoreload and auto2to3 if len(sys.argv) >= 3 and sys.argv[1] == '-m': mode = 'module' module = sys.argv[2] del sys.argv[1:3] elif len(sys.argv) >= 2: mode = "script" script = sys.argv[1] sys.argv = sys.argv[1:] else: print "usage: python -m plop.collector -m module_to_run" sys.exit(1) collector = Collector() collector.start(duration=3600) exit_code = 0 try: if mode == "module": import runpy runpy.run_module(module, run_name="__main__", alter_sys=True) elif mode == "script": with open(script) as f: global __file__ __file__ = script # Use globals as our "locals" dictionary so that # something that tries to import __main__ (e.g. the unittest # module) will see the right things. exec f.read() in globals(), globals() except SystemExit, e: exit_code = e.code
def execute(self): entry_point = self.entry() saved_sys_path = sys.path[:] # TODO(John Sirois): plumb this through all the way to the BUILD # files so that "thin" targets may specify this by default. if 'PEX_INHERIT_PATH' in os.environ: sys.path.extend(self.path()) else: sys.path = self.path() if 'PEX_COVERAGE' in os.environ: start_coverage() PythonLauncher.debug('Initialized sys.path to %s' % os.path.pathsep.join(sys.path)) force_interpreter = 'PEX_INTERPRETER' in os.environ if entry_point and not force_interpreter: PythonLauncher.debug('Detected entry_point: %s' % entry_point) runpy.run_module(entry_point, run_name='__main__') else: PythonLauncher.debug('%s, dropping into interpreter' % ( 'PEX_INTERPRETER specified' if force_interpreter else 'No entry point specified.')) if sys.argv[1:]: self.run(args=sys.argv[1:]) else: import code code.interact() sys.path = saved_sys_path
def main(modelname=None): if modelname is None: print "Starting scipysim GUI" run_module('scipysim.gui.gui', run_name="__main__") else: # TODO: Check if modelname is valid run_module('scipysim.models.' + modelname, run_name="__main__")
def import_main(mod_name, mod_path, init_globals, run_name): # pylint: disable=protected-access import types import runpy module = types.ModuleType(run_name) if init_globals is not None: module.__dict__.update(init_globals) module.__name__ = run_name class TempModulePatch(runpy._TempModule): # pylint: disable=too-few-public-methods def __init__(self, mod_name): # pylint: disable=no-member super(TempModulePatch, self).__init__(mod_name) assert self.module.__name__ == run_name self.module = module TempModule = runpy._TempModule # pylint: disable=invalid-name runpy._TempModule = TempModulePatch import_main.sentinel = (mod_name, mod_path) try: main_module = sys.modules['__main__'] sys.modules['__main__'] = sys.modules[run_name] = module if mod_name: # pragma: no cover runpy.run_module(mod_name, run_name=run_name, alter_sys=True) elif mod_path: # pragma: no branch runpy.run_path(mod_path, run_name=run_name) sys.modules['__main__'] = sys.modules[run_name] = module except: # pragma: no cover sys.modules['__main__'] = main_module raise finally: del import_main.sentinel runpy._TempModule = TempModule
def main(): """ Main function """ try: name = indexfile.__name__ version = indexfile.__version__ log = indexfile.getLogger(__name__) # local variables index = None # load commands commands = load_commands() helpstr = __doc__ % (name, name) + get_commands_help(commands) # create validation schema sch = Schema({ 'index': Or(None, And(Or('-', 'stdin'), Use(lambda x: sys.stdin)), open), Optional('format'): open, Optional('loglevel'): And(str, Use(str.lower), Or('error', 'warn', 'info', 'debug')), '<command>': Command(commands=commands), str: object }) # parse args and remove dashes args = docopt(helpstr, version="%s v%s" % (name, version), options_first=True) args = dict([(k.replace('-', ''), v) for k, v in args.iteritems()]) # validate args args = sch.validate(args) # deal with 'help' command if args.get('<command>') == 'help': docopt(helpstr, version="%s v%s" % (name, version), argv=['--help']) # load the index and delegate command config = load_config(os.getcwd(), args) indexfile.setLogLevel(config.get('loglevel')) index = open_index(config) command_ = get_command(args.get('<command>'), commands) argv = [name, command_] + args['<args>'] sys.argv = argv module_ = "indexfile.cli.indexfile_%s" % command_ runpy.run_module(module_, run_name="__main__", init_globals={'index': index, 'command': '{0} {1}'.format(name, command_)}) except KeyboardInterrupt, e: sys.exit(1)
def test_that_functional_tests_can_be_run_as_a_script(self): module = "aitc.tests.functional.test_aitc" orig_argv = sys.argv orig_stdout = sys.stdout orig_stderr = sys.stderr try: sys.argv = [sys.argv[0]] sys.stdout = StringIO.StringIO() sys.stderr = StringIO.StringIO() # With no args, that's a usage error. try: run_module(module, run_name="__main__") except (Exception, SystemExit): pass self.assertTrue("Usage" in sys.stdout.getvalue()) # With args, it will run (and fail, but run nonetheless) sys.argv.append("http://nonexistant.example.com") try: run_module(module, run_name="__main__") except (Exception, SystemExit): pass finally: sys.stderr = orig_stderr sys.stdout = orig_stdout sys.argv = orig_argv
def _check_package(self, depth): pkg_dir, mod_fname, mod_name = self._make_pkg("x=1\n", depth, "__main__") pkg_name, _, _ = mod_name.rpartition(".") forget(mod_name) try: if verbose: print("Running from source:", pkg_name) d1 = run_module(pkg_name) # Read from source self.assertIn("x", d1) self.assertTrue(d1["x"] == 1) del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) make_legacy_pyc(mod_fname) unload(mod_name) # In case loader caches paths if verbose: print("Running from compiled:", pkg_name) d2 = run_module(pkg_name) # Read from bytecode self.assertIn("x", d2) self.assertTrue(d2["x"] == 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, pkg_name) if verbose: print("Package executed successfully")
def test_cmdline(self): """Utilisation du script CLI vigilo-config.""" # Normally called from the command line, this is just for test coverage # See the memcached runit service for command-line use. self.load_conf_from_string( """[test-section]\nTEST_KEY = test_value\n""") oldout, sys.stdout = sys.stdout, StringIO() try: # pylint: disable-msg=E1103 sys.argv[1:] = ['--get', 'TEST_KEY', "--section", "test-section"] try: runpy.run_module('vigilo.common.conf', run_name='__main__', alter_sys=True) except SystemExit: pass self.assertEqual(sys.stdout.getvalue().strip(), "test_value") sys.stdout.seek(0) sys.stdout.truncate() sys.argv[1:] = [] try: runpy.run_module('vigilo.common.conf', run_name='__main__', alter_sys=True) except SystemExit: pass self.assertEqual(sys.stdout.getvalue(), '') sys.stdout.seek(0) sys.stdout.truncate() finally: sys.stdout = oldout
def run_m(args): """Run a module, just like python -m <module_name>""" sys.argv = sys.argv[sys.argv.index("-m"):] sys.argv.remove(args.m) if not len(sys.argv): sys.argv.append("") runpy.run_module(args.m, run_name="__main__", alter_sys=True) sys.exit(0)
def run(working_dir): sys.path.insert(0, working_dir) manage_file = os.getenv('PYCHARM_DJANGO_MANAGE_MODULE') if not manage_file: manage_file = 'manage' def execute_manager(settings_mod, argv = None): management.setup_environ(settings_mod) management.execute_manager = execute_manager def execute_from_command_line(argv=None): pass management.execute_from_command_line = execute_from_command_line fixGetpass() try: #import settings to prevent circular dependencies later on import django.db from django.conf import settings apps=settings.INSTALLED_APPS # From django.core.management.shell # XXX: (Temporary) workaround for ticket #1796: force early loading of all # models from installed apps. from django.db.models.loading import get_models get_models() except: pass run_module(manage_file, None, '__main__', True)
import sys import os import runpy ''' Standard usage is one of these: python -m rwb.editor <args> python -m rwb.runner <args> python -m rwb.kwbrowser <args> You can also run the "rwb" module directly, which will simply run the editor ''' if len(sys.argv) > 1 and sys.argv[1] in ("editor", "runner", "kwbrowser"): PKG = "rwb." + sys.argv.pop(1) else: # from the command line, let "rwb" be an alias for "rwb editor" PKG = "rwb.editor" # This code came from this page: # http://code.activestate.com/recipes/577088-generic-execution-script-for-a-packageapplication/ try: run_globals = runpy.run_module(PKG, run_name='__main__', alter_sys=True) executed = os.path.splitext(os.path.basename(run_globals['__file__']))[0] if executed != '__main__': # For Python 2.5 compatibility raise ImportError('Incorrectly executed %s instead of __main__' % executed) except ImportError: # For Python 2.6 compatibility runpy.run_module('%s.__main__' % PKG, run_name='__main__', alter_sys=True)
def test_main(): import runpy x = runpy.run_module('dbassembly') assert x['main'] assert x['__package__'] == 'dbassembly' assert x['__name__'] == 'dbassembly.__main__'
def test_interp_1d_example(self): runpy.run_module('sdf.examples.interp_1d')
import asyncio import socket import urllib.parse import urllib.request import collections import time import hashlib import random import binascii import sys import re import runpy import signal if len(sys.argv) < 2: config = runpy.run_module("config") elif len(sys.argv) == 2: # launch with own config config = runpy.run_path(sys.argv[1]) else: # undocumented way of launching config = {} config["PORT"] = int(sys.argv[1]) secrets = sys.argv[2].split(",") config["USERS"] = { "user%d" % i: secrets[i].zfill(32) for i in range(len(secrets)) } if len(sys.argv) > 3: config["AD_TAG"] = sys.argv[3]
def main(): """Command-line wrapper to re-run a script whenever its source changes. Scripts may be specified by filename or module name:: python -m tornado.autoreload -m tornado.test.runtests python -m tornado.autoreload tornado/test/runtests.py Running a script with this wrapper is similar to calling `tornado.autoreload.wait` at the end of the script, but this wrapper can catch import-time problems like syntax errors that would otherwise prevent the script from reaching its call to `wait`. """ original_argv = sys.argv sys.argv = sys.argv[:] if len(sys.argv) >= 3 and sys.argv[1] == "-m": mode = "module" module = sys.argv[2] del sys.argv[1:3] elif len(sys.argv) >= 2: mode = "script" script = sys.argv[1] sys.argv = sys.argv[1:] else: print(_USAGE, file=sys.stderr) sys.exit(1) try: if mode == "module": import runpy runpy.run_module(module, run_name="__main__", alter_sys=True) elif mode == "script": with open(script) as f: global __file__ __file__ = script # Use globals as our "locals" dictionary so that # something that tries to import __main__ (e.g. the unittest # module) will see the right things. exec_in(f.read(), globals(), globals()) except SystemExit as e: logging.basicConfig() gen_log.info("Script exited with status %s", e.code) except Exception as e: logging.basicConfig() gen_log.warning("Script exited with uncaught exception", exc_info=True) # If an exception occurred at import time, the file with the error # never made it into sys.modules and so we won't know to watch it. # Just to make sure we've covered everything, walk the stack trace # from the exception and watch every file. for (filename, lineno, name, line) in traceback.extract_tb(sys.exc_info()[2]): watch(filename) if isinstance(e, SyntaxError): # SyntaxErrors are special: their innermost stack frame is fake # so extract_tb won't see it and we have to get the filename # from the exception object. watch(e.filename) else: logging.basicConfig() gen_log.info("Script exited normally") # restore sys.argv so subsequent executions will include autoreload sys.argv = original_argv if mode == 'module': # runpy did a fake import of the module as __main__, but now it's # no longer in sys.modules. Figure out where it is and watch it. loader = pkgutil.get_loader(module) if loader is not None: watch(loader.get_filename()) wait()
def main(): '''make command line interface, read in analysis type, execute correct analysis pipeline script''' parser = argparse.ArgumentParser(prog='omics_pipe', description='Run omics_pipe') parser.add_argument( 'analysis_type', action="store", choices=[ 'RNAseq', 'RNAseq_sanford', 'miRNAseq', 'ChIPseq', 'GWAS', 'modular', 'custom' ], help='type of analysis to run: RNAseq, ChIPseq, GWAS, modular, custom') parser.add_argument('parameter_file', action="store", help='specify parameter file to use for analysis') parser.add_argument( '--custom_script_path', action="store", help= 'specify custom script file with full path (/example/script.py) to use for analysis if you specify analysis type as custom' ) parser.add_argument( '--custom_script_name', action="store", help= 'specify custom script file with full path (/example/script.py) to use for analysis if you specify analysis type as custom' ) args = parser.parse_args() print args print args.analysis_type print args.custom_script_path print args.custom_script_name print args.parameter_file analysis = args.analysis_type parameters = args.parameter_file path = args.custom_script_path script = args.custom_script_name stream = file(parameters, 'r') params = yaml.load(stream) default_parameters.update( params) #Update default parameters to user defined parameter file p = Bunch(default_parameters) check_create_dir(p.LOG_PATH) check_create_dir(p.FLAG_PATH) # check if sumatra project is present, if not, create one if args.analysis_type == 'RNAseq': runpy.run_module('omics_pipe.RNAseq', run_name="__main__", alter_sys=True) sys.exit(0) elif args.analysis_type == 'RNAseq_sanford': runpy.run_module('omics_pipe.RNAseq_sanford', run_name="__main__", alter_sys=True) sys.exit(0) elif args.analysis_type == 'miRNAseq': runpy.run_module('omics_pipe.miRNAseq', run_name="__main__", alter_sys=True) sys.exit(0) elif args.analysis_type == 'ChIPseq': runpy.run_module('omics_pipe.ChIPseq', run_name="__main__", alter_sys=True) sys.exit(0) elif args.analysis_type == 'GWAS': runpy.run_module('omics_pipe.GWAS', run_name="__main__", alter_sys=True) sys.exit(0) elif args.analysis_type == 'custom': os.chdir(path) runpy.run_module(script, run_name="__main__", alter_sys=True) sys.exit(0) else: print 'Error: unsupported analysis type' return
def test_library_module(self): run_module("runpy")
#!/usr/bin/python3 -OEs # -*- coding: utf-8 import sys sys.path[:] = [p for p in sys.path if 'site-packages' not in p.split('/')] import runpy runpy.run_module('aptsources_cleanup', run_name='__main__')
def launch(): import gettext import runpy if sys.executable.endswith("thonny.exe"): # otherwise some library may try to run its subprocess with thonny.exe # NB! Must be pythonw.exe not python.exe, otherwise Runner thinks console # is already allocated. sys.executable = sys.executable[:-len("thonny.exe")] + "pythonw.exe" set_dpi_aware() try: runpy.run_module("thonny.customize", run_name="__main__") except ImportError: pass gettext.install("thonny", "locale") _prepare_thonny_user_dir() if not _check_welcome(): return if _should_delegate(): try: _delegate_to_existing_instance(sys.argv[1:]) print("Delegated to an existing Thonny instance. Exiting now.") return 0 except Exception: traceback.print_exc() # Did not or could not delegate try: from thonny import workbench bench = workbench.Workbench() try: bench.mainloop() except SystemExit: bench.destroy() return 0 except SystemExit as e: from tkinter import messagebox messagebox.showerror("System exit", str(e)) return -1 except Exception: from logging import exception exception("Internal launch or mainloop error") from thonny import ui_utils dlg = ui_utils.LongTextDialog("Internal error", traceback.format_exc()) ui_utils.show_dialog(dlg, get_workbench()) return -1 finally: runner = get_runner() if runner is not None: runner.destroy_backend() return 0
import wat import runpy x = runpy.run_module("wat.translator", None, "__main__")
def main(): """Command-line wrapper to re-run a script whenever its source changes. Scripts may be specified by filename or module name:: python -m tornado.autoreload -m tornado.test.runtests python -m tornado.autoreload tornado/test/runtests.py Running a script with this wrapper is similar to calling `tornado.autoreload.wait` at the end of the script, but this wrapper can catch import-time problems like syntax errors that would otherwise prevent the script from reaching its call to `wait`. """ # Remember that we were launched with autoreload as main. # The main module can be tricky; set the variables both in our globals # (which may be __main__) and the real importable version. import tornado.autoreload global _autoreload_is_main global _original_argv, _original_spec tornado.autoreload._autoreload_is_main = _autoreload_is_main = True original_argv = sys.argv tornado.autoreload._original_argv = _original_argv = original_argv original_spec = getattr(sys.modules['__main__'], '__spec__', None) tornado.autoreload._original_spec = _original_spec = original_spec sys.argv = sys.argv[:] if len(sys.argv) >= 3 and sys.argv[1] == "-m": mode = "module" module = sys.argv[2] del sys.argv[1:3] elif len(sys.argv) >= 2: mode = "script" script = sys.argv[1] sys.argv = sys.argv[1:] else: print(_USAGE, file=sys.stderr) sys.exit(1) try: if mode == "module": import runpy runpy.run_module(module, run_name="__main__", alter_sys=True) elif mode == "script": with open(script) as f: # Execute the script in our namespace instead of creating # a new one so that something that tries to import __main__ # (e.g. the unittest module) will see names defined in the # script instead of just those defined in this module. global __file__ __file__ = script # If __package__ is defined, imports may be incorrectly # interpreted as relative to this module. global __package__ del __package__ exec_in(f.read(), globals(), globals()) except SystemExit as e: logging.basicConfig() gen_log.info("Script exited with status %s", e.code) except Exception as e: logging.basicConfig() gen_log.warning("Script exited with uncaught exception", exc_info=True) # If an exception occurred at import time, the file with the error # never made it into sys.modules and so we won't know to watch it. # Just to make sure we've covered everything, walk the stack trace # from the exception and watch every file. for (filename, lineno, name, line) in traceback.extract_tb(sys.exc_info()[2]): watch(filename) if isinstance(e, SyntaxError): # SyntaxErrors are special: their innermost stack frame is fake # so extract_tb won't see it and we have to get the filename # from the exception object. watch(e.filename) else: logging.basicConfig() gen_log.info("Script exited normally") # restore sys.argv so subsequent executions will include autoreload sys.argv = original_argv if mode == 'module': # runpy did a fake import of the module as __main__, but now it's # no longer in sys.modules. Figure out where it is and watch it. loader = pkgutil.get_loader(module) if loader is not None: watch(loader.get_filename()) wait()
def test_bounce(): random.seed(0) mockturtle.events[:] = [('timer', )] * 600 runpy.run_module('freegames.bounce')
def run_(): from runpy import run_module print(message) run_module(sys.argv[1], init_globals=local)
def run(sampler, kind): fn = sys.argv[1] if fn == '-m': module = sys.argv[2] args = sys.argv[3:] else: args = sys.argv[2:] sys.argv = [sys.argv[0]] + args sys.path[0] = os.path.abspath(os.path.dirname(fn)) sampler.start() # del sys.modules["__main__"] # do we need this? try: if fn == '-m': runpy.run_module(module, run_name="__main__") else: runpy.run_path(fn, run_name="__main__") except KeyboardInterrupt: print("Interrupted!") traceback.print_exc() except SystemExit: pass except: print("ERROR!") traceback.print_exc() print("Stopping timer and tallying statistics...") times = sampler.stop() times.sort(key=lambda p: p[1], reverse=True) with open("measure_loc.pkl", "wb") as f: pickle.dump(times, f) total = 0.0 for l, t in times: total += t if kind == "time": print("Found %d unique lines for a total of %.2fs" % (len(times), total)) else: print("Found %d unique lines with %d samples" % (len(times), total)) FRACTIONS = [0.5, 0.75, 0.9, 0.99, 1] frac_counts = [] frac_fracs = [] frac_idx = 0 DISPLAY_THRESH = 20 sofar = 0.0 total_lines = 0 for (l, t) in times: if not l: continue fn, lineno = l total_lines += 1 sofar += t if total_lines <= DISPLAY_THRESH: if kind == "time": print(("%s:%s" % (fn, lineno)).ljust(50), "%.4fs %4.1f%% % 3d %4.1f%%" % (t, t / total * 100, total_lines, sofar / total * 100.0)) else: print(("%s:%s" % (fn, lineno)).ljust(50), "% 3d %4.1f%% % 3d %4.1f%%" % (t, t / total * 100, total_lines, sofar / total * 100.0)) if sofar >= total * FRACTIONS[frac_idx]: if FRACTIONS[frac_idx] == 1: break frac_counts.append(total_lines) frac_fracs.append(sofar) frac_idx += 1 if len(times) > DISPLAY_THRESH: print("(and %d more -- see measure_loc.pkl)" % (len(times) - DISPLAY_THRESH)) assert len(frac_counts) == len(FRACTIONS) -1 for i in range(len(frac_counts)): print("Picked %d lines out of %d to reach %.2f%%" % (frac_counts[i], len(times), frac_fracs[i] / total * 100.0))
def test_python_module(path, name, base_dir, messages): """Test the given python module by importing it. :type path: str :type name: str :type base_dir: str :type messages: set[str] """ if name in sys.modules: return # cannot be tested because it has already been loaded is_ansible_module = (path.startswith('lib/ansible/modules/') or path.startswith('plugins/modules/')) and os.path.basename(path) != '__init__.py' run_main = is_ansible_module if path == 'lib/ansible/modules/utilities/logic/async_wrapper.py': # async_wrapper is a non-standard Ansible module (does not use AnsibleModule) so we cannot test the main function run_main = False capture_normal = Capture() capture_main = Capture() try: with monitor_sys_modules(path, messages): with blacklist_imports(path, name, messages): with capture_output(capture_normal): import_module(name) if run_main: with monitor_sys_modules(path, messages): with blacklist_imports(path, name, messages): with capture_output(capture_main): runpy.run_module(name, run_name='__main__') except ImporterAnsibleModuleException: # module instantiated AnsibleModule without raising an exception pass except BaseException as ex: # pylint: disable=locally-disabled, broad-except # intentionally catch all exceptions, including calls to sys.exit exc_type, _exc, exc_tb = sys.exc_info() message = str(ex) results = list(reversed(traceback.extract_tb(exc_tb))) line = 0 offset = 0 full_path = os.path.join(base_dir, path) base_path = base_dir + os.path.sep source = None # avoid line wraps in messages message = re.sub(r'\n *', ': ', message) for result in results: if result[0] == full_path: # save the line number for the file under test line = result[1] or 0 if not source and result[0].startswith(base_path) and not result[0].startswith(temp_path): # save the first path and line number in the traceback which is in our source tree source = (os.path.relpath(result[0], base_path), result[1] or 0, 0) if isinstance(ex, SyntaxError): # SyntaxError has better information than the traceback if ex.filename == full_path: # pylint: disable=locally-disabled, no-member # syntax error was reported in the file under test line = ex.lineno or 0 # pylint: disable=locally-disabled, no-member offset = ex.offset or 0 # pylint: disable=locally-disabled, no-member elif ex.filename.startswith(base_path) and not ex.filename.startswith(temp_path): # pylint: disable=locally-disabled, no-member # syntax error was reported in our source tree source = (os.path.relpath(ex.filename, base_path), ex.lineno or 0, ex.offset or 0) # pylint: disable=locally-disabled, no-member # remove the filename and line number from the message # either it was extracted above, or it's not really useful information message = re.sub(r' \(.*?, line [0-9]+\)$', '', message) if source and source[0] != path: message += ' (at %s:%d:%d)' % (source[0], source[1], source[2]) report_message(path, line, offset, 'traceback', '%s: %s' % (exc_type.__name__, message), messages) finally: capture_report(path, capture_normal, messages) capture_report(path, capture_main, messages)
# For multiproc attach, we need to use a helper stub to import debug_me before running # Flask; otherwise, we will get the connection only from the subprocess, not from the # Flask server process. import debug_me # noqa import runpy runpy.run_module("flask", run_name="__main__", alter_sys=True)
#! /usr/bin/env python import sys import runpy sys.path.insert(0, "/Users/jblackford/Development/agent") if __name__ == '__main__': runpy.run_module("agent.main", run_name="__main__")
def main(): # TODO: more options, refactor this into somewhere shared # between tornado.autoreload and auto2to3 parser = argparse.ArgumentParser( description="Plop: Python Low-Overhead Profiler", prog="python -m plop.collector", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--format", "-f", help="Output format", choices=["plop", "flamegraph"], default="plop") parser.add_argument("--module", "-m", help="Execute target as a module", action="store_const", const=True, default=False) parser.add_argument( "--mode", help="Interval timer mode to use, see `man 2 setitimer`", choices=["prof", "real", "virtual"], default="prof") parser.add_argument("--interval", help="Timer interval in seconds", default=0.01, type=float) parser.add_argument("--duration", help="Profiling duration in seconds", default=3600, type=int) parser.add_argument("--max-stacks", help="Number of most frequent stacks to store." " Ignored for Flamegraph output.", type=int, default=50) parser.add_argument("--output", "-o", help="Save output into FILE", metavar="FILE", type=str) parser.add_argument("target", help="Module or script to run") parser.add_argument( "arguments", nargs=argparse.REMAINDER, help="Pass-through arguments for the profiled application") args = parser.parse_args() sys.argv = [args.target] + args.arguments if args.format == "flamegraph": extension = "flame" formatter = FlamegraphFormatter() elif args.format == "plop": extension = "plop" formatter = PlopFormatter(max_stacks=args.max_stacks) else: sys.stderr.write("Unhandled output format: %s" % args.format) sys.stderr.flush() sys.exit(1) if args.output: filename = args.output else: try: # This is equivalent to os.makedirs('profiles', exist_ok=True) os.mkdir('profiles') except OSError as exc: if exc.errno != errno.EEXIST: raise filename = os.path.join( 'profiles', '%s-%s.%s' % (os.path.basename( args.target), time.strftime('%Y%m%d-%H%M-%S'), extension)) collector = Collector(mode=args.mode, interval=args.interval) collector.start(duration=args.duration) exit_code = 0 try: if args.module: import runpy runpy.run_module(args.target, run_name="__main__", alter_sys=True) else: with open(args.target) as f: # Execute the script in our namespace instead of creating # a new one so that something that tries to import __main__ # (e.g. the unittest module) will see names defined in the # script instead of just those defined in this module. global __file__ __file__ = args.target # If __package__ is defined, imports may be incorrectly # interpreted as relative to this module. global __package__ del __package__ six.exec_(f.read(), globals(), globals()) except SystemExit as e: exit_code = e.code collector.stop() if collector.samples_taken: formatter.store(collector, filename) print("profile output saved to %s" % filename) overhead = float(collector.sample_time) / collector.samples_taken print("overhead was %s per sample (%s%%)" % (overhead, overhead / collector.interval)) else: print("no samples collected; program was too fast") sys.exit(exit_code)
IPykernel module must be run in the main thread, hence the blocking mechanism to ready stdin to figure out when ipykernel needs to be started. Running the module ipykernel effectively means we're starting the python kernel. """ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. if __name__ != "__main__": raise Exception("{} cannot be imported".format(__name__)) import json import runpy import sys # Assumption is ipykernel is available. # Preload module (speed up, so when we really need it, it has already been loaded). from ipykernel import kernelapp as app # Block till we read somethign from `stdin`. # As soon as we get something this is a trigger to start. # The value passed into stdin would be the arguments we need to place into sys.argv. input_json = sys.stdin.readline().strip() sys.argv = json.loads(input_json) module = sys.argv[0] # Note, we must launch ipykenel in the main thread for kernel interrupt to work on windows. # Start kernel in current process. runpy.run_module(module, run_name="__main__", alter_sys=False)
def execute_module(cls, module_name): cls.demote_bootstrap() import runpy runpy.run_module(module_name, run_name='__main__')
def main(): runpy.run_module( 'wfa_cardinality_estimation_evaluation_framework' '.evaluations.run_evaluation', run_name='__main__', alter_sys=True)
def exec_module(): self.log.info("execute module %s", module_name) runpy.run_module(module_name, globals(), run_name="__main__")
def test_snake(): random.seed(0) mockturtle.events[:] = ([('timer', ), ('key Left', ), ('timer', ), ('key Up', )] + [('timer', True)] * 300) runpy.run_module('freegames.snake')
def uninstall(packages, yes): """Uninstall plugins and Python packages from the Datasette environment""" sys.argv = ["pip", "uninstall"] + list(packages) + (["-y"] if yes else []) run_module("pip", run_name="__main__")
def test___main__(main): main.return_value = 42 with pytest.raises(SystemExit) as pytest_wrapped_e: runpy.run_module("pynguin", run_name="__main__") assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 42
def _run_module(module, args): sys.argv = [module] + [str(a) for a in args] return runpy.run_module(module, init_globals=None, run_name='__main__', alter_sys=True)
#!/usr/bin/env python3 import runpy runpy.run_module('deployee.deployee', run_name='__main__')
schema_revision_metric, missing_handler, current_consumer_offset, ) from thoth.common import OpenShift, init_logging from thoth.storages.graph import GraphDatabase from prometheus_client import generate_latest from confluent_kafka import KafkaError from confluent_kafka import KafkaException from confluent_kafka import Consumer from confluent_kafka import TopicPartition # We run all the modules so that their metrics and handlers get registered run_module("thoth.investigator.advise_justification.__init__") run_module("thoth.investigator.adviser_trigger.__init__") run_module("thoth.investigator.build_analysis_trigger.__init__") run_module("thoth.investigator.cve_provided.__init__") run_module("thoth.investigator.hash_mismatch.__init__") run_module("thoth.investigator.inspection_completed.__init__") run_module("thoth.investigator.kebechet_run_url_trigger.__init__") run_module("thoth.investigator.kebechet_trigger.__init__") run_module("thoth.investigator.missing_package.__init__") run_module("thoth.investigator.missing_version.__init__") run_module("thoth.investigator.package_extract_trigger.__init__") run_module("thoth.investigator.package_released.__init__") run_module("thoth.investigator.provenance_checker_trigger.__init__") run_module("thoth.investigator.si_unanalyzed_package.__init__") run_module("thoth.investigator.solved_package.__init__") run_module("thoth.investigator.thoth_repo_init.__init__")
def run_pymobiledevice3(): del sys.argv[1] runpy.run_module("pymobiledevice3", run_name='__main__')
def test_main(mocker): """test as3ninja calls cli() on __main__""" mocked_cli = mocker.patch("as3ninja.cli.cli") runpy.run_module("as3ninja", run_name="__main__") assert mocked_cli.called is True