From c6e98694274a9c3e09fa3192a5f247607081c1ae Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 30 Apr 2017 20:03:17 +0300 Subject: [PATCH] Add cmp.py --- cmp.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++ docs/index.rst | 9 +++++ setup.py | 2 +- tests/test_cmp.py | 43 +++++++++++++++++++++++ 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100755 cmp.py create mode 100755 tests/test_cmp.py diff --git a/cmp.py b/cmp.py new file mode 100755 index 0000000..0168bec --- /dev/null +++ b/cmp.py @@ -0,0 +1,89 @@ +#! /usr/bin/env python +"""cmp.py: compare two files. Portable replacement for cmp.""" + +import os +import sys + +if sys.argv[1] in ("-h", "--help"): + print("Broytman cmp.py 1.0, Copyright (C) 2003-2017 PhiloSoft Design") + print("Usage: cmp.py [-h|--help|-V|--version] [-i] file1 file2") + sys.exit() +elif sys.argv[1] in ("-V", "--version"): + print("Broytman cmp.py 1.0, Copyright (C) 2003-2017 PhiloSoft Design") + sys.exit() +elif sys.argv[1] == "-i": + show_pbar = False + fname1 = sys.argv[2] + fname2 = sys.argv[3] +else: + show_pbar = sys.stderr.isatty() + fname1 = sys.argv[1] + fname2 = sys.argv[2] + +if show_pbar: + try: + from m_lib.pbar.tty_pbar import ttyProgressBar + except ImportError: + show_pbar = 0 + +if show_pbar: + try: + size = os.path.getsize(fname1) + except: + print(fname1, ": no such file") + sys.exit(1) + +if show_pbar: + pbar = ttyProgressBar(0, size) + +file1 = open(fname1, 'rb') +file2 = open(fname2, 'rb') + +M = 1024*1024 +diff = False +count = 0 + + +def report(): + if show_pbar: + global pbar + del pbar + sys.stderr.write("Files differ at %d megabayte block\n" % count) + global diff + diff = True + + +while True: + block1 = file1.read(M) + block2 = file2.read(M) + + if show_pbar: + pbar.display(file1.tell()) + + if block1 and block2: + if len(block1) != len(block2): + report() + break + elif block1: + report() + break + elif block2: + report() + break + else: + break + + if block1 != block2: + report() + break + + count += 1 + +if show_pbar and not diff: + del pbar + +file1.close() +file2.close() + +if diff: + sys.exit(1) diff --git a/docs/index.rst b/docs/index.rst index 3f27dd2..2da73c2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -18,6 +18,15 @@ Portable Python Utilities Command line ------------ +cmp.py +~~~~~~ + +Compare two files. + +Usage:: + + cmp.py file1 file2 + remove-old-files.py ~~~~~~~~~~~~~~~~~~~ diff --git a/setup.py b/setup.py index 22a230d..88b510d 100755 --- a/setup.py +++ b/setup.py @@ -31,5 +31,5 @@ setup(name='ppu', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', ], - scripts=['remove-old-files.py'], + scripts=['cmp.py', 'remove-old-files.py'], ) diff --git a/tests/test_cmp.py b/tests/test_cmp.py new file mode 100755 index 0000000..49b48ac --- /dev/null +++ b/tests/test_cmp.py @@ -0,0 +1,43 @@ +#! /usr/bin/env python + +import os +import shutil +import subprocess +import sys +from tempfile import mkdtemp +from find_in_path import find_in_path + + +tmp_dir = None + +test_prog_path = find_in_path('cmp.py') +if not test_prog_path: + sys.exit("Cannot find cmp.py in %s" % os.environ["PATH"]) + + +def setup(): + global tmp_dir + tmp_dir = mkdtemp() + os.chdir(tmp_dir) + + +def teardown(): + os.chdir(os.sep) # To the root of the FS + shutil.rmtree(tmp_dir) + + +def create_file(name, content): + with open(name, 'w') as fp: + fp.write(content) + + +def test_cmp_equal(): + create_file('test1', 'test') + create_file('test2', 'test') + assert subprocess.call( + [sys.executable, test_prog_path, "-i", "test1", "test2"]) == 0 + + create_file('test3', 'test3') + create_file('test4', 'test4') + assert subprocess.call( + [sys.executable, test_prog_path, "-i", "test3", "test4"]) == 1 -- 2.39.2