git » abk » commit 4e95669

Add multiple hashes support, and rearrange configuration.

author
2005-03-03 01:00:22 UTC
committer
2005-03-03 01:00:22 UTC
parent 2303a4475bb23c2dbdc4810fa7a834282c2b7aea

Add multiple hashes support, and rearrange configuration.

abk +49 -13

diff --git a/abk b/abk
index cd70312..5781772 100644
--- a/abk
+++ b/abk
@@ -11,10 +11,10 @@ from stat import *
 #
 class config:
 	# copy mode; can be "raw", "gzip", "bzip2"
-	copy_mode = 'bzip2'
+	copy_mode = 'gzip'
 
-	# use_hash; true or false
-	use_hash = 1
+	# hash mode; can be "none", "md5", "sha"
+	hash_mode = "sha"
 
 
 #
@@ -44,13 +44,7 @@ class file_info:
 		self.hash = None
 		self.stat = None
 		
-		if config.copy_mode == 'raw':
-			self.copy_file_reg = self.copy_file_reg_raw
-		elif config.copy_mode == 'gzip':
-			self.copy_file_reg = self.copy_file_reg_gzip
-		elif config.copy_mode == 'bzip2':
-			self.copy_file_reg = self.copy_file_reg_bzip2
-
+	
 	def __repr__(self):
 		return "<%s: %s %d>" % (self.name, self.type, self.size)
 
@@ -83,7 +77,7 @@ class file_info:
 		self.uid = s.st_uid
 		self.gid = s.st_gid
 
-		if config.use_hash and self.type == 'r':
+		if self.type == 'r':
 			self.hash = self.hash_file()
 
 	def cmp_mdata(self, other):
@@ -147,7 +141,7 @@ class file_info:
 		sfile.close()
 		dfile.close()
 
-	def copy_file_reg_gz(self, dst):
+	def copy_file_reg_gzip(self, dst):
 		"Copy a regular file, destination is gzip compressed."
 		import gzip
 		sfile = open(self.name)
@@ -161,6 +155,7 @@ class file_info:
 		sfile.close()
 		dfile.close()
 	
+	copy_file_reg = copy_file_reg_gzip
 
 	def copy_file_link(self, dst):
 		"Copy a symbolic link."
@@ -211,8 +206,9 @@ class file_info:
 		else:
 			raise 'Unk type: 0x%x %d' % (self.mode, self.name)
 
-	def hash_file(self):
+	def hash_file_sha(self):
 		"Returns the sha1sum of a file."
+		import sha
 		hash = sha.new()
 		f = open(self.name)
 		data = f.read(PSIZE)
@@ -222,6 +218,24 @@ class file_info:
 		f.close()
 		return hash.hexdigest()
 
+	def hash_file_md5(self):
+		"Returns the md5sum of a file."
+		import md5
+		hash = md5.new()
+		f = open(self.name)
+		data = f.read(PSIZE)
+		while data:
+			hash.update(data)
+			data = f.read(PSIZE)
+		f.close()
+		return hash.hexdigest()
+	
+	def hash_file_none(self):
+		"Empty hash."
+		return '-'
+	
+	hash_file = hash_file_sha
+
 
 class index_file:
 	"Represents the index file."
@@ -405,6 +419,28 @@ Commands:
 		Synchronizes src with dst, using the given index files.
 """
 
+# configuration
+if config.copy_mode == 'raw':
+	file_info.copy_file_reg = file_info.copy_file_reg_raw
+elif config.copy_mode == 'gzip':
+	file_info.copy_file_reg = file_info.copy_file_reg_gzip
+elif config.copy_mode == 'bzip2':
+	file_info.copy_file_reg = file_info.copy_file_reg_bzip2
+else:
+	print "Configuration error"
+	sys.exit(1)
+
+if config.hash_mode == 'none':
+	file_info.hash_file = file_info.hash_file_none
+elif config.hash_mode == 'md5':
+	file_info.hash_file = file_info.hash_file_md5
+elif config.hash_mode == 'sha':
+	file_info.hash_file = file_info.hash_file_sha
+else:
+	print "Configuration error"
+	sys.exit(1)
+
+# main command
 try:
 	cmd = sys.argv[1]
 except: