def main(): custom_usage = '''%(prog)s [SHELLPY ARGS] file [SCRIPT ARGS] For arguments help use: %(prog)s --help ''' custom_epilog = '''github : github.com/lamerman/shellpy''' def is_shellpy_file(fil): if not re.match('.+\.spy$', fil): parser.error('Shellpy can only run *.spy files') return fil parser = ArgumentParser(description='A tool for convenient shell scripting in python', usage=custom_usage, epilog=custom_epilog) parser.add_argument('file', help='path to spy file', type=is_shellpy_file) shellpy_args, unknown_args = parser.parse_known_args() filename = shellpy_args.file filename_index = sys.argv.index(filename) # remove script arguments given before script name # comment out if want to keep them script_args = [x for x in unknown_args if x not in sys.argv[:filename_index]] processed_file = preprocess_file(filename, is_root_script=True) # include directory of the script to pythonpath new_env = os.environ.copy() new_env['PYTHONPATH'] = new_env.get("PYTHONPATH", '') + os.pathsep + os.path.dirname(filename) retcode = subprocess.call(processed_file + ' ' + ' '.join(script_args), shell=True, env=new_env) exit(retcode)
def main(python_version): custom_usage = '''%(prog)s [SHELLPY ARGS] file [SCRIPT ARGS] For arguments help use: %(prog)s --help ''' custom_epilog = '''github : github.com/lamerman/shellpy''' def is_shellpy_file(fil): if not re.match('.+\.spy$', fil): parser.error('Shellpy can only run *.spy files') return fil parser = ArgumentParser(description='A tool for convenient shell scripting in python', usage=custom_usage, epilog=custom_epilog) parser.add_argument('file', help='path to spy file', type=is_shellpy_file) parser.add_argument('-v', '--verbose', help='increase output verbosity. Always print the command being executed', action="store_true") parser.add_argument('-vv', help='even bigger output verbosity. All stdout and stderr of executed commands is ' 'printed', action="store_true") shellpy_args, unknown_args = parser.parse_known_args() if shellpy_args.verbose or shellpy_args.vv: config.PRINT_ALL_COMMANDS = True if shellpy_args.vv: config.PRINT_STDOUT_ALWAYS = True config.PRINT_STDERR_ALWAYS = True filename = shellpy_args.file filename_index = sys.argv.index(filename) # remove script arguments given before script name # comment out if want to keep them script_args = [x for x in unknown_args if x not in sys.argv[:filename_index]] processed_file = preprocess_file(filename, is_root_script=True, python_version=python_version) # include directory of the script to pythonpath new_env = os.environ.copy() new_env['PYTHONPATH'] = new_env.get("PYTHONPATH", '') + os.pathsep + os.path.dirname(filename) new_env[SHELLPY_PARAMS] = config.dumps() retcode = subprocess.call(processed_file + ' ' + ' '.join(script_args), shell=True, env=new_env) exit(retcode)
def main(): custom_usage = '''%(prog)s [SHELLPY ARGS] file [SCRIPT ARGS] For arguments help use: %(prog)s --help ''' custom_epilog = '''github : github.com/lamerman/shellpy''' def is_shellpy_file(fil): if not re.match('.+\.spy$', fil): parser.error('Shellpy can only run *.spy files') return fil parser = ArgumentParser(description='A tool for convenient shell scripting in python', usage=custom_usage, epilog=custom_epilog) parser.add_argument('file', help='path to spy file', type=is_shellpy_file) parser.add_argument('-v', '--verbose', help='increase output verbosity. Always print the command being executed', action="store_true") parser.add_argument('-vv', help='even bigger output verbosity. All stdout and stderr of executed commands is ' 'printed', action="store_true") shellpy_args, unknown_args = parser.parse_known_args() if shellpy_args.verbose or shellpy_args.vv: config.PRINT_ALL_COMMANDS = True if shellpy_args.vv: config.PRINT_STDOUT_ALWAYS = True config.PRINT_STDERR_ALWAYS = True filename = shellpy_args.file filename_index = sys.argv.index(filename) # remove script arguments given before script name # comment out if want to keep them script_args = [x for x in unknown_args if x not in sys.argv[:filename_index]] processed_file = preprocess_file(filename, is_root_script=True) # include directory of the script to pythonpath new_env = os.environ.copy() new_env['PYTHONPATH'] = new_env.get("PYTHONPATH", '') + os.pathsep + os.path.dirname(filename) new_env[SHELLPY_PARAMS] = config.dumps() retcode = subprocess.call(processed_file + ' ' + ' '.join(script_args), shell=True, env=new_env) exit(retcode)
def load_module(self, module_name): """If the module was located it then is loaded by this function. It is also a part of PEP-0302 interface Loading means first preprocessing of shell python code if it is not processed already and the addition to system path and import. :param module_name: the name of the module to load :return: the module imported. This function assumes that it will import a module anyway since find_module already found the module """ sys.meta_path.remove(self) if module_name.find('.') == -1: spy_module_path = locator.locate_spy_module(module_name) spy_file_path = locator.locate_spy_file(module_name) if spy_module_path is not None: new_module_path = preprocessor.preprocess_module(spy_module_path) new_module_pythonpath = os.path.split(new_module_path)[0] if new_module_pythonpath not in sys.path: sys.path.append(new_module_pythonpath) module = import_module(module_name) elif spy_file_path is not None: new_module_path = preprocessor.preprocess_file(spy_file_path, is_root_script=False) new_module_pythonpath = os.path.split(new_module_path)[0] if new_module_pythonpath not in sys.path: sys.path.append(new_module_pythonpath) module = import_module(module_name) else: raise ImportError("Unexpected error occured in importer. Neither shellpy module not file was found") else: module = import_module(module_name) sys.meta_path.insert(0, self) return module
def load_module(self, module_name): """If the module was located it then is loaded by this function. It is also a part of PEP-0302 interface Loading means first preprocessing of shell python code if it is not processed already and the addition to system path and import. :param module_name: the name of the module to load :return: the module imported. This function assumes that it will import a module anyway since find_module already found the module """ sys.meta_path.remove(self) if module_name.find(".") == -1: spy_module_path = locator.locate_spy_module(module_name) spy_file_path = locator.locate_spy_file(module_name) if spy_module_path is not None: new_module_path = preprocessor.preprocess_module(spy_module_path) new_module_pythonpath = os.path.split(new_module_path)[0] if new_module_pythonpath not in sys.path: sys.path.append(new_module_pythonpath) module = import_module(module_name) elif spy_file_path is not None: new_module_path = preprocessor.preprocess_file(spy_file_path, is_root_script=False) new_module_pythonpath = os.path.split(new_module_path)[0] if new_module_pythonpath not in sys.path: sys.path.append(new_module_pythonpath) module = import_module(module_name) else: raise ImportError("Unexpected error occured in importer. Neither shellpy module not file was found") else: module = import_module(module_name) sys.meta_path.insert(0, self) return module
def main(): custom_usage = '''%(prog)s [SHELLPY ARGS] file [SCRIPT ARGS] For arguments help use: %(prog)s --help ''' custom_epilog = '''github : github.com/lamerman/shellpy''' def is_shellpy_file(fil): if not re.match('.+\.spy$', fil): parser.error('Shellpy can only run *.spy files') return fil parser = ArgumentParser( description='A tool for convenient shell scripting in python', usage=custom_usage, epilog=custom_epilog) parser.add_argument('file', help='path to spy file', type=is_shellpy_file) shellpy_args, unknown_args = parser.parse_known_args() filename = shellpy_args.file filename_index = sys.argv.index(filename) # remove script arguments given before script name # comment out if want to keep them script_args = [ x for x in unknown_args if x not in sys.argv[:filename_index] ] processed_file = preprocess_file(filename, is_root_script=True) # include directory of the script to pythonpath new_env = os.environ.copy() new_env['PYTHONPATH'] = new_env.get( "PYTHONPATH", '') + os.pathsep + os.path.dirname(filename) retcode = subprocess.call(processed_file + ' ' + ' '.join(script_args), shell=True, env=new_env) exit(retcode)
def main(): if len(sys.argv) == 1 or sys.argv[1] in ['-h', '--help', 'help']: print('Shellpy, write shell scripts in Python easily') print('Usage: shellpy [*.spy script] [arguments]') exit(0) filename = sys.argv[1] if not filename.endswith('.spy'): print('Error: Shellpy can only run *.spy files') print('Usage: shellpy [*.spy script] [arguments]') exit(1) processed_file = preprocess_file(filename, is_root_script=True) # include directory of the script to pythonpath new_env = os.environ.copy() new_env['PYTHONPATH'] = new_env.get( "PYTHONPATH", '') + os.pathsep + os.path.split(filename)[0] retcode = subprocess.call(processed_file + ' ' + ' '.join(sys.argv[2:]), shell=True, env=new_env) exit(retcode)
def main(python_version): custom_usage = '''%(prog)s [SHELLPY ARGS] file [SCRIPT ARGS] For arguments help use: %(prog)s --help ''' custom_epilog = '''github : github.com/lamerman/shellpy''' try: spy_file_index = next(index for index, arg in enumerate(sys.argv) if re.match('.+\.spy$', arg)) shellpy_args = sys.argv[1:spy_file_index] script_args = sys.argv[spy_file_index + 1:] except StopIteration: shellpy_args = sys.argv[1:] spy_file_index = None parser = ArgumentParser( description='A tool for convenient shell scripting in python', usage=custom_usage, epilog=custom_epilog) parser.add_argument( '-v', '--verbose', help= 'increase output verbosity. Always print the command being executed', action="store_true") parser.add_argument( '-vv', help= 'even bigger output verbosity. All stdout and stderr of executed commands is ' 'printed', action="store_true") shellpy_args, _ = parser.parse_known_args(shellpy_args) if spy_file_index is None: exit( 'No *.spy file was specified. Only *.spy files are supported by the tool.' ) if shellpy_args.verbose or shellpy_args.vv: config.PRINT_ALL_COMMANDS = True if shellpy_args.vv: config.PRINT_STDOUT_ALWAYS = True config.PRINT_STDERR_ALWAYS = True filename = sys.argv[spy_file_index] processed_file = preprocess_file(filename, is_root_script=True, python_version=python_version) # include directory of the script to pythonpath new_env = os.environ.copy() new_env['PYTHONPATH'] = new_env.get( "PYTHONPATH", '') + os.pathsep + os.path.dirname(filename) new_env[SHELLPY_PARAMS] = config.dumps() retcode = subprocess.call(processed_file + ' ' + ' '.join(script_args), shell=True, env=new_env) exit(retcode)