]> git.phdru.name Git - ppu.git/commitdiff
Add cmp.py
authorOleg Broytman <phd@phdru.name>
Sun, 30 Apr 2017 17:03:17 +0000 (20:03 +0300)
committerOleg Broytman <phd@phdru.name>
Sun, 30 Apr 2017 17:28:27 +0000 (20:28 +0300)
cmp.py [new file with mode: 0755]
docs/index.rst
setup.py
tests/test_cmp.py [new file with mode: 0755]

diff --git a/cmp.py b/cmp.py
new file mode 100755 (executable)
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)
index 3f27dd25c84e9c4ddfb44c0f677b5616ba13db09..2da73c2b951713ac8a5836ef3998d5af44c872c5 100644 (file)
@@ -18,6 +18,15 @@ Portable Python Utilities
 Command line
 ------------
 
+cmp.py
+~~~~~~
+
+Compare two files.
+
+Usage::
+
+    cmp.py file1 file2
+
 remove-old-files.py
 ~~~~~~~~~~~~~~~~~~~
 
index 22a230d3ee4104eefd72003db43743f884476c3d..88b510d56693574cd9bc5f991fc9fab168798628 100755 (executable)
--- 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 (executable)
index 0000000..49b48ac
--- /dev/null
@@ -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