def start_process(component_name, executable, run_in_fg, cli_options, extra_cli_options='', on_keyboard_interrupt=None, failed_to_start_err=-100, extra_options=None, stderr_path=None, stdin_data=None, async_keyword=async_keyword): """ Starts a new process from a given Python path, either in background or foreground (run_in_fg). """ stderr_path = stderr_path or mkstemp('-zato-start-{}.txt'.format(component_name.replace(' ','')))[1] stdout_redirect = '' if run_in_fg else '1> /dev/null' stderr_redirect = '2> {}'.format(stderr_path) program = '{} {} {} {}'.format(executable, extra_cli_options, stdout_redirect, stderr_redirect) try: _stderr = _StdErr(stderr_path, stderr_sleep_fg if run_in_fg else stderr_sleep_bg) run_kwargs = { async_keyword: False if run_in_fg else True, } # Do not send input if it does not really exist because it prevents pdb from attaching to a service's stdin if stdin_data: run_kwargs['input'] = stdin_data sarge_run(program, **run_kwargs) # Wait a moment for any potential errors _err = _stderr.wait_for_error() if _err: logger.warn(_err) sys.exit(failed_to_start_err) except KeyboardInterrupt: if on_keyboard_interrupt: on_keyboard_interrupt() sys.exit(0)
def start_python_process(run_in_fg, py_path, name, program_dir, on_keyboard_interrupt=None, failed_to_start_err=-100, extra_options=None, stderr_path=None, stdin_data=None): """ Starts a new process from a given Python path, either in background or foreground (run_in_fg). """ stderr_path = stderr_path or mkstemp('-zato-start-{}.txt'.format( name.replace(' ', '')))[1] stdout_redirect = '' if run_in_fg else '1> /dev/null' stderr_redirect = '2> {}'.format(stderr_path) options = { 'fg': run_in_fg, } if extra_options: options.update(extra_options) options = CLI_ARG_SEP.join('{}={}'.format(k, v) for k, v in options.items()) program = '{} -m {} {} {} {} {}'.format(get_executable(), py_path, program_dir, options, stdout_redirect, stderr_redirect) try: _stderr = _StdErr(stderr_path, stderr_sleep_fg if run_in_fg else stderr_sleep_bg) run_kwargs = { 'async': False if run_in_fg else True, } # Do not send input if it does not really exist because it prevents pdb from attaching to a service's stdin if stdin_data: run_kwargs['input'] = stdin_data sarge_run(program, **run_kwargs) # Wait a moment for any potential errors _err = _stderr.wait_for_error() if _err: logger.warn(_err) sys.exit(failed_to_start_err) except KeyboardInterrupt: if on_keyboard_interrupt: on_keyboard_interrupt() sys.exit(0)
def push_doc(remote, branch, message, output, exclude, extra, tmp): repo_dir = os.path.join(tmp, 'repo') docs_dir = os.path.join(tmp, 'copy', output) # Try to clone the repo branch command = sarge_run('git clone {0} -b {1} repo'.format(remote, branch), cwd=tmp, stdout=DEVNULL, stderr=DEVNULL) # Some git versions fails if there is no branch, others clone master intead # If command failed, nothing was cloned, clone master in this case if command.returncode != 0: run('git clone {} repo'.format(remote), cwd=tmp) # With old git versions, if remote branch not found, use HEAD instead, # check if the branch really exists command = sarge_run( 'git show-ref --verify --quiet refs/heads/{}'.format(branch), cwd=repo_dir) if command.returncode != 0: # branch doesn't exists cprint('=== Creating new branch "{}"'.format(branch)) run('git checkout --orphan {}'.format(branch), cwd=repo_dir) run('git rm -rf .', cwd=repo_dir) for entry in extra: run('touch {}'.format(entry), cwd=repo_dir) for entry in os.listdir(docs_dir): if entry not in exclude: shutil.move(os.path.join(docs_dir, entry), repo_dir) run('git add -A', cwd=repo_dir) run('git commit -m "{}"'.format(message), cwd=repo_dir) run('git push origin {}'.format(branch), cwd=repo_dir) cprint('===') cprint('=== Documentation pushed.') cprint('===')
def run(command, get_output=False, cwd=None): """By default, run all commands at GITPATH directory. If command fails, stop program execution. """ if cwd is None: cwd = GITPATH cprint('===') cprint('=== Command: ', command) cprint('=== CWD: ', cwd) cprint('===') if get_output: proc = capture_stdout(command, cwd=cwd) out = proc.stdout.read().decode() print(out, end='') check_exit_code(proc.returncode) return out else: proc = sarge_run(command, cwd=cwd) check_exit_code(proc.returncode)