def pull_git(url, target=None, branch=None, settings=DEFAULT_SETTINGS): console = JsonStreams(settings.log_to_console) console.input(f'# Pull repository from {url}') if not target: target = '.' if target and path.isdir(f'{target}/.git'): pull_cmds = ['git fetch', f'git checkout -q origin/{branch or "HEAD"}'] else: branch_option = f'--branch {branch}' if branch else '' pull_cmds = [f'git clone -q {branch_option} {url} {target}'] r = run_script([ 'git --version', *pull_cmds, 'git rev-parse HEAD', ], settings, cwd=target) if target == '.': commit = r.get('console_data')[-1].get('text') r['vars'] = dict(pullnrun_git_commit=commit) return r
def log_plan_statistics(plan_return_value, settings=DEFAULT_SETTINGS): console = JsonStreams(settings.log_to_console) data = Data(plan_return_value) console.input(text=f'# Execution statistics{detail(data.name)}') console.log(f'{"Started:":10} {data.started}') console.log(f'{"Elapsed:":10} {data.elapsed} s') console.log(Statistics(data.statistics).as_str(10)) return dict( success=True, console_data=console.data, )
def generate_report(plan_return_value, output_dir, settings=DEFAULT_SETTINGS): console = JsonStreams(settings.log_to_console) env = Environment(loader=PackageLoader('pullnrun')) env.filters['prefix_if'] = _prefix_if template = env.get_template('report.html.j2') stream = template.stream(**plan_return_value) report_filename = 'pullnrun_report.html' with open(f'{output_dir}/{report_filename}', 'w') as f: stream.dump(f) console.log(f'Log available in {os.path.realpath(report_filename)}') return dict( success=True, console_data=[], )
def echo(settings=DEFAULT_SETTINGS, **kwargs): console = JsonStreams(settings.log_to_console) console.input(f'# Echo values') for key, value in kwargs.items(): console.log(f'{key}: {value}') return dict( success=True, console_data=console.data, )
def run_command(command, settings=DEFAULT_SETTINGS, **kwargs): if isinstance(command, str): command = shlex.split(command) with JsonStreams(log_to_console=settings.log_to_console) as (stdout, stderr, streams): streams.push('stdin', text=command_as_str(command)) process = run(command, stderr=stderr, stdout=stdout, bufsize=0, **kwargs) return dict( success=process.returncode == 0, console_data=streams.read(wait=True), )
def push_http( url, method='PUT', filename=None, settings=DEFAULT_SETTINGS, **kwargs): console = JsonStreams(settings.log_to_console) console.input(f'# Push {filename or "data"} to {url}') try: if filename: with open(filename, 'rb') as f: r = request(method, url, files=dict(file=f), **kwargs) else: r = request(method, url, **kwargs) console.log( f'{method.title()} {filename or "data"} returned {r.status_code}.') r.raise_for_status() except Exception as e: console.error(f'Pushing {filename or "data"} failed: {str(e)}') return dict(success=False, console_data=console.data, ) return dict(success=True, console_data=console.data, )
def pull_http(url, method='GET', filename=None, extract=False, settings=DEFAULT_SETTINGS, **kwargs): console = JsonStreams(settings.log_to_console) console.input(f'# Pull {filename or "file"} from {url}') if not filename: filename = url.split('/')[-1] console.log(f'No filename defined, using {filename}.') try: with request(method, url, stream=True, **kwargs) as r: status_code = r.status_code console.log(f'{method.title()} {filename} returned {status_code}.') r.raise_for_status() _write_to_file(r, filename) console.log(f'Writing {filename} completed.') except Exception as e: console.error(f'Pulling {filename} failed: {str(e)}') return dict( success=False, console_data=console.data, ) if extract: try: console.log('Unpacking archive.') unpack_archive(filename) console.log('Unpacking succeeded.') except Exception as e: console.error(f'Unpacking failed: {str(e)}') return dict( success=False, console_data=console.data, ) return dict( success=True, console_data=console.data, )
def log_versions(settings=DEFAULT_SETTINGS): console = JsonStreams(settings.log_to_console) console.input(f'# Log versions') console.log(f'pullnrun {__version__}') console.log(f'jinja2 {_jinja2_version}') console.log(f'python {sys.version}') console.log(sys.executable) console.log(platform.platform()) console.log(inspect.getfile(pullnrun)) return dict( success=True, console_data=console.data, )