def cli_lines_with_dir(input_): try: lines = None tmp_dir = None tmp_dir = tempfile.mkdtemp() main([tmp_dir, input_]) filename = cdx_filename(os.path.basename(input_)) print(filename) with open(os.path.join(tmp_dir, filename), 'rb') as fh: lines = fh.read(8192).rstrip().split(b'\n') finally: try: if tmp_dir: shutil.rmtree(tmp_dir) except OSError as exc: if exc.errno != 2: raise if not lines: return # print first, last, num lines print(lines[1].decode('utf-8')) print(lines[-1].decode('utf-8')) print('Total: ' + str(len(lines)))
def cli_lines(cmds): buff = BytesIO() orig = sys.stdout.buffer if hasattr(sys.stdout, 'buffer') else None sys.stdout.buffer = buff main(cmds) sys.stdout.buffer = orig lines = buff.getvalue().rstrip().split(b'\n') # print first, last, num lines print(lines[1].decode('utf-8')) print(lines[-1].decode('utf-8')) print('Total: ' + str(len(lines)))