changeset 29:98f5ce7e8326

Handle the HgWeb configuration file using a configuration parser. IN: -
author John Schneiderman <JohnMS@CodeGNU.com>
date Thu, 07 Aug 2014 20:08:06 -0500
parents 13f4dad3c9ad
children 3ae2aa2d3ea9
files doc/ChangeLog doc/TODO src/manager.py
diffstat 3 files changed, 37 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/doc/ChangeLog	Wed Aug 06 19:23:37 2014 -0500
+++ b/doc/ChangeLog	Thu Aug 07 20:08:06 2014 -0500
@@ -22,6 +22,7 @@
   storage name.
 - Generate default ignore file when creating a new managed repository.
 - Write an ignore file to an existing managed repository.
+- HgWeb configuration now managed cleanly, i.e. no longer a raw read/write.
 2014-07-29 John Schneiderman <Licensing _AT_ CodeGNU _DOT_ com> 0.2.1
 - Fixed issue where the configuration file copy started before it closed.
 - Fixed issue in Windows locking temporary file and preventing copying.
--- a/doc/TODO	Wed Aug 06 19:23:37 2014 -0500
+++ b/doc/TODO	Thu Aug 07 20:08:06 2014 -0500
@@ -30,7 +30,6 @@
 - Allow plug-ins addition and removal from repositories.
 - Installation tutorial.
 - User tutorials.
-- Actually parse the hgweb.config file to work with it.
 - Manager functions should take both an output and error device instead of defaulting to std.
 - Manager should handle the HgWeb configuration file settings.
 - Manager should provide a REST API for repository management.
--- a/src/manager.py	Wed Aug 06 19:23:37 2014 -0500
+++ b/src/manager.py	Thu Aug 07 20:08:06 2014 -0500
@@ -378,32 +378,28 @@
 
 	 @return When the process is successful gives true, else-wise false.
 	"""
-	import tempfile
+	from ConfigParser import SafeConfigParser
 	import os
-	import shutil
+	import sys
+
+	parser = SafeConfigParser()
+	if not parser.read(settings.HgWebPath + os.sep + 'hgweb.config'):
+		print >>sys.stderr, "Failed to located HgWeb configuration file."
+		return False
 
-	foundRepo=False
-	tempFile=None
-	with tempfile.NamedTemporaryFile(delete=False) as target:
-		tempFile = target.name
-		with open(settings.HgWebPath + os.sep + 'hgweb.config', 'r') as source:
-			for line in source:
-				if repo.StorageName + ' = ' in line:
-					foundRepo=True
-				else:
-					target.write(line)
-		target.flush()
-		if not source.closed:
-			while not source.closed:
-				print "Waiting three seconds for configuration to close."
-				time.sleep(3)
-	try:
-		shutil.copy2(tempFile, settings.HgWebPath + os.sep + 'hgweb.config')
-	except IOError as e:
-		print >>sys.stderr, "Unregister error({0}): {1}".format(e.errno, e.strerror)
-		foundRepo = False
-	os.remove(tempFile)
-	return foundRepo
+	if parser.has_section('paths'):
+		if parser.has_option('paths', repo.StorageName):
+			print "Unregistering repository ..."
+			parser.remove_option('paths', repo.StorageName)
+			with open(settings.HgWebPath + os.sep + 'hgweb.config', 'wb') as configfile:
+				parser.write(configfile)
+			return True
+		else:
+			print "The repository already is unregistered."
+			return True
+	else:
+		print >>sys.stderr, "The HgWeb configuration file does not have a paths section!"
+		return False
 
 def __removeRepository(repo):
 	""" Removes an existing Mercurial repository.
@@ -522,13 +518,23 @@
 
 	 @return When the process is successful gives true, else-wise false.
 	"""
+	from ConfigParser import SafeConfigParser
 	import os
 	import sys
 
-	try:
-		with open(settings.HgWebPath + os.sep + 'hgweb.config', 'a') as hgweb:
-			hgweb.write('\n' + repo.StorageName + " = " + settings.RepositoryPath + os.sep + repo.StorageName)
-		return True
-	except IOError as e:
-		print >>sys.stderr, "Web registration error({0}): {1}".format(e.errno, e.strerror)
+	parser = SafeConfigParser()
+	if not parser.read(settings.HgWebPath + os.sep + 'hgweb.config'):
+		print >>sys.stderr, "Failed to located HgWeb configuration file."
 		return False
+
+	if parser.has_section('paths'):
+		if parser.has_option('paths', repo.StorageName):
+			print >>sys.stderr, "The repository already is registered."
+			return False
+	else:
+		parser.add_section('paths')
+	print >>sys.stderr, "Registering repository ..."
+	parser.set('paths', repo.StorageName, settings.RepositoryPath + os.sep + repo.StorageName)
+	with open(settings.HgWebPath + os.sep + 'hgweb.config', 'wb') as configfile:
+		parser.write(configfile)
+	return True