changeset 6:75b05ad6b26d

Delete a repository under the control of HgWeb. IN: -
author John Schneiderman <JohnMS@CodeGNU.com>
date Tue, 22 Jul 2014 00:21:49 -0500
parents d5991e08acb9
children a21bb6b95a9a
files doc/TODO src/hwm.py src/manager.py
diffstat 3 files changed, 109 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/doc/TODO	Mon Jul 21 23:35:28 2014 -0500
+++ b/doc/TODO	Tue Jul 22 00:21:49 2014 -0500
@@ -38,3 +38,4 @@
 - Generate default ignore file using XML.
 - 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.
--- a/src/hwm.py	Mon Jul 21 23:35:28 2014 -0500
+++ b/src/hwm.py	Tue Jul 22 00:21:49 2014 -0500
@@ -84,7 +84,7 @@
 
 	parser = argparse.ArgumentParser(description='Manages HgWeb Repositories')
 	parser.add_argument("repository", action="store", help='The storage-name of the repository.')
-	parser.add_argument("-a", "--action", action="store", nargs=1, type=str, choices=["create"], help='Performs an action upon a managed HgWeb repository.')
+	parser.add_argument("-a", "--action", action="store", nargs=1, type=str, choices=["create","delete"], help='Performs an action upon a managed HgWeb repository.')
 	parser.add_argument("-n", "--name", action="store", nargs='?', default="", help='The display name of the repository.')
 	parser.add_argument("-d", "--description", action="store", nargs='?', default="", help='The description of the repository.')
 	parser.add_argument("-c", "--contact", action="store", nargs='?', default="", help='The contact point of a repository.')
@@ -94,11 +94,15 @@
 
 	if 'create' == args.action[0]:
 		if manager.create(repo):
-			print "Success creating repository %s." % repo.StorageName
 			exit(0)
 		else:
-			print >>sys.stderr, "Failure creating repository %s." % repo.StorageName
 			exit(1)
+	elif 'delete' == args.action[0]:
+		if manager.delete(repo):
+			exit(0)
+		else:
+			exit(1)
+
 	print >>sys.stderr, "Failure determine action request %s." % args.action
 	exit(2)
 
--- a/src/manager.py	Mon Jul 21 23:35:28 2014 -0500
+++ b/src/manager.py	Tue Jul 22 00:21:49 2014 -0500
@@ -61,16 +61,111 @@
 
 	# Add the new repository to the web register.
 	if __registerRepository(repository):
+		print "Registered repository with HgWeb."
+		return True
+	else:
+		print >>sys.stderr, "Failed to register repository with HgWeb."
+		return False
+
+def delete(repository):
+	""" Removes an existing repository from HgWeb.
+
+	 @pre The repository to remove must already exist.
+
+	 @param[in] repository	The targeted repository to delete.
+
+	 @post The file system no longer contains a directory in the repository
+	  path that is the supplied repository. The repository is no longer
+	  registered in HgWeb.
+
+	 @return When the deletion is successful gives true, else-wise false.
+	"""
+	if not __doesRepositoryExist(repository):
+		print >>sys.stderr, "The repository %s is not valid." % repository.StorageName
+		return False
+
+	print "Removing repository: " + repository.StorageName
+	if __unregisterRepository(repository):
+		print "Unregistered from HgWeb."
+	else:
+		print >>sys.stderr, "Failed to unregister repository from HgWeb."
+		return False
+
+	if __deleteRepository(repository):
+		print "Deleted repository."
+		return True
+	else:
+		print >>sys.stderr, "Failed to delete the repository."
+		return False
+
+def __doesRepositoryExist(repo):
+	""" Checks to see if a repository exists.
+
+	 @param[in] repo	The targeted repository for which to check.
+
+	 @return Gives true when the repository exists, false else-wise.
+	"""
+	import os
+
+	if os.path.exists(settings.RepositoryPath + os.sep + repo.StorageName) \
+			and os.path.isdir(settings.RepositoryPath + os.sep + repo.StorageName) \
+			and os.path.isfile(settings.RepositoryPath + os.sep + repo.StorageName + os.sep + ".hg" + os.sep + "hgrc"):
 		return True
 	else:
 		return False
 
+def __unregisterRepository(repo):
+	""" Unregisters the repository with HgWeb.
+
+	 @param[in] repo	The targeted repository for which to unregister.
+
+	 @post The HgWeb configuration file does not contain the repository details
+	  and is returned to a read-only state. Upon any error the standard error
+	  buffer contains an error message.
+
+	 @return When the process is successful gives true, else-wise false.
+	"""
+	import tempfile
+	import os
+	import shutil
+
+	foundRepo=False
+	with tempfile.NamedTemporaryFile() as target:
+		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()
+		shutil.copyfile(target.name, settings.HgWebPath + os.sep + 'hgweb.config')
+	return foundRepo
+
+def __deleteRepository(repo):
+	""" Removes an existing Mercurial repository.
+
+	 @pre The requested repository to delete must already exist.
+
+	 @param[in] repo	The targeted repository for which to delete.
+
+	 @post Upon any error the standard error buffer contains an error message.
+
+	 @return When the process is successful gives true, else-wise false.
+	"""
+	import shutil
+	import os
+	import stat
+
+	os.chmod(settings.RepositoryPath + os.sep + repo.StorageName + os.sep + ".hg" + os.sep + "hgrc", stat.S_IWRITE)
+	shutil.rmtree(settings.RepositoryPath + os.sep + repo.StorageName)
+	return True
+
 def __generateRepository(repo):
 	""" Initialises a new Mercurial repository.
 
 	 @pre The requested repository to create cannot already exist.
 
-	 @param[in] repo		The targeted repository for which to create.
+	 @param[in] repo	The targeted repository for which to create.
 
 	 @post Upon any error the standard error buffer contains an error message.
 
@@ -103,7 +198,7 @@
 	import sys
 
 	config = settings.RepositoryPath + os.sep + repo.StorageName + os.sep + ".hg" + os.sep + "hgrc"
-	with open(config, "w") as hgrc:
+	with open(config, "a") as hgrc:
 		hgrc.write("[web]\n")
 		if repo.DisplayName is None:
 			hgrc.write("name = " + repo.StorageName + '\n')
@@ -128,7 +223,7 @@
 def __guardWindowsRepository(repo):
 	""" Guards the supplied repository when on a Windows system.
 
-	 @param[in] repo		The targeted repository for which to secure.
+	 @param[in] repo	The targeted repository for which to secure.
 
 	 @return The exit status value from securing the repository. The value will
 	  be zero upon success, all other values indicate failure.
@@ -147,8 +242,7 @@
 def __registerRepository(repo):
 	""" Registers the repository with HgWeb.
 
-	 @param[in] repo		The targeted repository for which to register in
-						 HgWeb.
+	 @param[in] repo	The targeted repository for which to register in HgWeb.
 
 	 @post The HgWeb configuration file contains the new repository details and
 	  is returned to a read-only state. Upon any error the standard error
@@ -161,11 +255,11 @@
 	import shutil
 
 	try:
-		with open(settings.HgWebPath + os.sep + "hgweb.config", "a") as hgweb:
+		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 Configuration error({0}): {1}".format(e.errno, e.strerror)
-		os.chmod(config, stat.S_IWRITE)
+		os.chmod(settings.RepositoryPath + os.sep + repo.StorageName + os.sep + ".hg" + os.sep + "hgrc", stat.S_IWRITE)
 		shutil.rmtree(settings.RepositoryPath + os.sep + repo.StorageName)
 		return False