def test_to_ascii(): assert utils.to_ascii(None) is None assert utils.to_ascii('test') == 'test' assert utils.to_ascii(b'test') == 'test' # Test not utf-8 characters with mock.patch('traceback.print_exc') as mock_traceback: assert utils.to_ascii(b'te\xe9st') == 'test' mock_traceback.assert_called_once_with()
def run_command_local_or_remote(context, cmd, addr): if addr == me(): out = run_command(context, cmd) return out else: try: results = parallax.parallax_call([addr], cmd) except ValueError as err: context.logger.error("\n{}\n".format(err)) context.failed = True else: return utils.to_ascii(results[0][1][1])
def run_command_local_or_remote(context, cmd, addr, err_record=False): if addr == me(): _, out = run_command(context, cmd, err_record) return out else: try: results = parallax.parallax_call(addr.split(','), cmd) except ValueError as err: if err_record: context.command_error_output = str(err) return context.logger.error("\n{}\n".format(err)) context.failed = True else: return utils.to_ascii(results[0][1][1])
def get_command_info_timeout(cmd, timeout=5): # Python 101: How to timeout a subprocess def kill(process): process.kill() proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) my_timer = Timer(timeout, kill, [proc]) try: my_timer.start() stdout, stderr = proc.communicate() finally: my_timer.cancel() if stdout and proc.returncode == 0: return crmutils.to_ascii(stdout) else: return ""
def get_process_status(s): """ Returns true if argument is the name of a running process. s: process name returns Boolean and pid """ # find pids of running processes pids = [pid for pid in os.listdir('/proc') if pid.isdigit()] for pid in pids: try: pid_file = os.path.join('/proc', pid, 'cmdline') with open(pid_file, 'rb') as f: data = f.read() procname = os.path.basename(crmshutils.to_ascii(data).replace('\x00', ' ').split(' ')[0]) if procname == s or procname == s + ':': return True, int(pid) except EnvironmentError: # a process may have died since we got the list of pids pass return False, -1
def sub_string_test(in_string, pattern=constants.SANITIZE): pattern_string = re.sub(" ", "|", pattern) for line in crmutils.to_ascii(in_string).split('\n'): if re.search('name="%s"' % pattern_string, line): return True return False
def read_from_file(infile): data = None _open = get_open_method(infile) with _open(infile, 'rt', encoding='utf-8', errors='replace') as f: data = f.read() return crmutils.to_ascii(data)