def download(url, pos, lock): import run_commands try: run_commands.run(['wget', '-c', url], False, cwd=pos) except Exception: logging.error('failed (%s)' % url) with lock: with open('failed_scenes.txt', 'a') as _fo: _fo.write('%s\n' % url)
def find_and_kill() -> None: search_criteria = input('WHAT TYPE OF PROCESS SHOULD I SEARCH FOR? ') results = run(f'ps aux | grep {search_criteria}') parsed_results: List[PSLineData] = [] if results.stdout is not None: parsed_results = parse_ps_data(results.stdout) print('================') if len(parsed_results) > 0: for num, parsed_line_data in enumerate(parsed_results): print(f'{num}) {parsed_line_data}') to_kill = int(input('WHO DO YOU WANT TO KILL? ')) run(f'kill {parsed_results[to_kill].pid}') else: print('No Results')
def _convert(self, f_in, f_ot, d_from, d_to, rec): _cmd = 'gdal_translate -of %s %s %s' % (d_to, f_in, f_ot) logging.info('run gdal %s' % _cmd) import run_commands _ps = run_commands.run(_cmd) if _ps[0] != 0: raise Exception('failed to process the data')
def capture_pic(f_ot): import config _cfg = config.cfg _cmd = _cfg.get('conf', 'cmd') % (f_ot, ) logging.info('cmd: %s' % _cmd) import run_commands return run_commands.run(_cmd)[0]
def is_it_up(host: str) -> None: results = run(f'ping -c 5 {host}') #print(results) data = parse_ping_data(results.stdout, results.stderr) #print(data) if results.return_code == 0: output = f'{data.original_host} is UP ({data.packets_received}/{data.packets_transmitted}' output += f' | average rtt: {data.rtt_avg})' print(output) else: output = f'{data.original_host}: {data.error_message}' print(output)
def test_command_fail(self): config = { 'Command_options': { 'Timeout': 10 }, 'Info': { 'lhs': ['false'] } } returned = run_commands.run(config) expected = run_commands.CommandOutput( **{'err': [('E', "Command 'lhs' returned a non-success code")]}) self.assertEqual(returned, expected)
def run(cmd, retry, delay): cmd = cmd.strip() if not cmd: logging.warning('no cmd provided') return _vs = cmd.split(' ') import run_commands import os for i in xrange(retry): _f = os.path.abspath(_vs[0]) if not os.path.exists(_f): import time time.sleep(delay) logging.info('waiting for %ss' % delay) continue _cmd = ' '.join(['python', '-u', _f] + list(_vs[1:])) logging.info('start cmd: %s' % _cmd) import sys _p = os.path.dirname(_f) _f_log = os.path.join(sys.path[0], 'log', 'cmd_std.log') with open(_f_log, 'w') as _f_opo: import config if config.cfg.getboolean('conf', 'background'): _out_log = _f_opo _err_log = _f_opo else: import sys _out_log = sys.stdout _err_log = sys.stderr run_commands.run(_cmd, True, cwd=_p, stdout=_out_log, stderr=_err_log) break
def test_command_timeout(self): config = { 'Command_options': { 'Timeout': 0.1 }, 'Info': { 'lhs': ['sleep', '2'] } } returned = run_commands.run(config) expected = run_commands.CommandOutput( **{'err': [('E', "Command 'lhs' not done after timeout")]}) self.assertEqual(returned, expected)
def test_command_not_found(self): config = { 'Command_options': { 'Timeout': 10 }, 'Info': { 'lhs': ['this_command_should_not_exist'] } } returned = run_commands.run(config) expected = run_commands.CommandOutput( **{'err': [('E', "Command 'lhs' not found")]}) self.assertEqual(returned, expected)
def test_centered_output(self): config = { 'Command_options': { 'Timeout': 10 }, 'Info': { '_centered_output_': ['echo', 'rhs'] } } returned = run_commands.run(config) expected = run_commands.CommandOutput(**{ 'out': [('_centered_', 'rhs', 3)], 'max_rhs': 3 }) self.assertEqual(returned, expected)
def test_string_command(self): config = { 'Command_options': { 'Timeout': 10 }, 'Info': { 'lhs': 'echo rhs' } } returned = run_commands.run(config) expected = run_commands.CommandOutput(**{ 'out': [('lhs', 'rhs', 3)], 'max_lhs': 3, 'max_rhs': 3 }) self.assertEqual(returned, expected)
def test_underscore_to_space(self): config = { 'Command_options': { 'Timeout': 10 }, 'Info': { 'Replace_underscore': ['echo', 'rhs'] } } returned = run_commands.run(config) expected = run_commands.CommandOutput( **{ 'out': [('Replace underscore', 'rhs', 3)], 'max_lhs': 18, 'max_rhs': 3 }) self.assertEqual(returned, expected)
def test_multi_line_command(self): config = { 'Command_options': { 'Timeout': 10 }, 'Info': { 'lhs': ['printf', 'foo\nbar'] } } returned = run_commands.run(config) expected = run_commands.CommandOutput( **{ 'out': [('lhs', 'foo', 3), ('', 'bar', 3)], 'max_lhs': 3, 'max_rhs': 3 }) self.assertEqual(returned, expected)
def test_two_commands(self): config = { 'Command_options': { 'Timeout': 10 }, 'Info': { 'zFirst': 'echo rhs1', 'aLast': ['echo', 'rhs2'] } } returned = run_commands.run(config) expected = run_commands.CommandOutput( **{ 'out': [('zFirst', 'rhs1', 4), ('aLast', 'rhs2', 4)], 'max_lhs': 6, 'max_rhs': 4 }) self.assertEqual(returned, expected)
def test_int_command(self): config = {'Command_options': {'Timeout': 10}, 'Info': {'lhs': 2}} returned = run_commands.run(config) expected = run_commands.CommandOutput( **{'out': [('', '', 0), ('', '', 0)]}) self.assertEqual(returned, expected)
from run_commands import run, CommandResults if __name__ == '__main__': print(run('ls')) print(run('ls -la')) print(run('ping -c 1 google.com')) print(run('ps aux | grep "python"'))
#!/usr/bin/python3 # wraith.py # Entry point # # Parses command-line arguments and hooks up the other modules. import os import load_conf import run_commands import image import output os.environ ['WRAITH_PATH'] = os.path.dirname (__file__) conf_file = os.path.join (os.path.expandvars ('${WRAITH_PATH}'), 'config.toml') #conf_file = os.path.join (os.path.expandvars ('${WRAITH_PATH}'), 'terroo.toml') config = load_conf.load (conf_file) img = image.draw (config) info = run_commands.run (config) output.show (config, img, info) # TODO: # Handle errors
from run_commands import run, cd, exists, isdir, _expand print('moving to your ~/Downloads directory') cd('~/Downloads') raspbian_dir = 'raspbian_download_dir' print(f"Checking on {raspbian_dir} directory.") if not exists(raspbian_dir): print(f"It dosn't exist. Creating {raspbian_dir} directory.") run(f'mkdir {raspbian_dir}') elif exists(raspbian_dir) and not isdir(raspbian_dir): print( f'"~/Downloads/{raspbian_dir}" exists but is not a directory. I\'m confused. exiting' ) exit(1) print(f"The {raspbian_dir} exists. Moving into that directory.") cd(raspbian_dir) download_filename = "raspbian_lite_latest" print(f"Checking on download file: {download_filename}") if not exists(download_filename): print("It doesn't exist. Downloading ...") run('wget https://downloads.raspberrypi.org/raspbian_lite_latest', capture_output=False) else: print("It exists.") print("Checking on the extracted file.") extracted_file = "*raspbian*.img"