changeset 79:08d5f6023998

Replace checksum calculator with python version.
author Edho Prima Arief <me@myconan.net>
date Sat, 09 Jul 2011 15:33:37 +0700
parents 06fd72a78cc1
children 59db9fa5d28b
files bin/cek bin/cek.pl bin/cek.py bin/putcrc
diffstat 4 files changed, 24 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/bin/cek	Sun Jun 19 12:53:42 2011 +0700
+++ b/bin/cek	Sat Jul 09 15:33:37 2011 +0700
@@ -2,7 +2,7 @@
 
 bn() { basename "/$*"; }
 #cs() { cksfv -- "$@"; }
-cs() { cek.pl "$@"; }
+cs() { cek.py "$@"; }
 if [ "$#" -lt 1 ]; then
 	cat <<EOF
 Usage: $(bn "$0") file1 file2 ... fileN
--- a/bin/cek.pl	Sun Jun 19 12:53:42 2011 +0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-#!/usr/bin/env perl
-
-use warnings;
-use strict;
-use Compress::Zlib;
-
-sub get_file_crc32 {
-  my $block_size = 1*2**20;
-  my $crc32_digest = 0;
-  my $return_code = 0;
-  my $file_name = $_[0];
-  if (!$file_name) {
-    #internal error, no file name passed to the function
-    $return_code = 10;
-  } elsif (!-f $file_name) {
-    #input file name is not a file
-    $return_code = 11;
-  } elsif (!-r $file_name) {
-    #input file is not readable
-    $return_code = 12;
-  }
-  if ($return_code == 0) {
-   open(my $file_handle, '<', $file_name);
-   binmode($file_handle);
-    while(sysread($file_handle, $_, $block_size)) {
-      $crc32_digest = crc32($_, $crc32_digest);
-    }
-    close($file_handle);
-  }
-  return ($return_code, sprintf('%08X', $crc32_digest));
-}
-
-foreach (@ARGV) {
-  my $file_name = $_;
-  my @crc32 = get_file_crc32($file_name);
-  printf("%s %s", $file_name, $crc32[1]);
-  if ($crc32[0] > 0) {
-    printf(" [ERR%02d]", $crc32[0]);
-  }
-  printf("\n");
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/cek.py	Sat Jul 09 15:33:37 2011 +0700
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+
+import zlib, os, sys
+
+def get_file_crc32(filename):
+  block_size = 1*2**20
+  return_code = 0
+  file_crc32 = 0
+  file_digest = 0
+  file_handle = open(filename, "rb")
+  if return_code == 0:
+    file_block = file_handle.read(block_size)
+    while file_block:
+      file_digest = zlib.crc32(file_block, file_digest)
+      file_block = file_handle.read(block_size)
+    file_handle.close()
+    file_digest = file_digest & 0xffffffff
+  return "%08X" % file_digest
+
+if __name__ == "__main__":
+  for file in sys.argv[1:]:
+    print "%s %s" % (file, get_file_crc32(file))
--- a/bin/putcrc	Sun Jun 19 12:53:42 2011 +0700
+++ b/bin/putcrc	Sat Jul 09 15:33:37 2011 +0700
@@ -24,7 +24,7 @@
 		dirname="$(dirname "$(safe_path "${file}")")"
 		myfile="$(printf "%s\n" "${filename%.*}" | sed -e 's/\([^]]\)$/\1 /')"
 		myext="${filename##*.}"; if [ "${myext}" = "${filename}" ]; then myext=""; else myext=".${myext}"; fi
-		crc=$(cek.rb "$(safe_path "${file}")" | tail -1 | sed -e 's/.*\([A-F0-9]\{8\}\)$/\1/')
+		crc=$(cek.py "$(safe_path "${file}")" | tail -1 | sed -e 's/.*\([A-F0-9]\{8\}\)$/\1/')
 		mv -- "${file}" "${dirname}/${myfile}[${crc}]${myext}" && printf "%s => %s\n" "${file}" "${myfile}[${crc}]${myext}"
 	else
 		printf "%s\n" "${file} is not a file or unreadable"