changeset 33:f2e0f4de13ea

Prevent two repositories from having the same display name. IN: -
author John Schneiderman <JohnMS@CodeGNU.com>
date Tue, 12 Aug 2014 19:12:20 -0500
parents 6cd1a810f95a
children c480ef7a02eb
files doc/ChangeLog doc/TODO src/manager.py src/manrepo.py
diffstat 4 files changed, 65 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/doc/ChangeLog	Thu Aug 07 21:10:21 2014 -0500
+++ b/doc/ChangeLog	Tue Aug 12 19:12:20 2014 -0500
@@ -24,6 +24,7 @@
 - Write an ignore file to an existing managed repository.
 - HgWeb configuration now managed cleanly, i.e. no longer a raw read/write.
 - Multiple managers cannot work on the set-up at the same time.
+- Two repositories can no longer have the same display name.
 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	Thu Aug 07 21:10:21 2014 -0500
+++ b/doc/TODO	Tue Aug 12 19:12:20 2014 -0500
@@ -34,7 +34,6 @@
 - Manager should handle the HgWeb configuration file settings.
 - Manager should provide a REST API for repository management.
 - Manager should have an API key per user permission values.
-- Specify from command line where to search for a configuration file.
-- Manager should prevent two repositories from using the same display name.
 - Manager should list all managed repositories like Display_Name(Storage_Name).
 - Change functions to use exceptions for error handling, except for the command-line module.
+- Create a separate manager for the HgWeb configuration file.
--- a/src/manager.py	Thu Aug 07 21:10:21 2014 -0500
+++ b/src/manager.py	Tue Aug 12 19:12:20 2014 -0500
@@ -48,8 +48,12 @@
 	lock = guard.FileLock('HWM')
 	with lock:
 		print "Adding repository: %s" % repository.StorageName
-		if __doesRepositoryExist(repository):
-			print >>sys.stderr, "The repository '%s' already exists." % repository.StorageName
+		if __doesRepositoryStorageNameExist(repository):
+			print >>sys.stderr, "The repository storage name '%s' already exists." % repository.StorageName
+			return False
+
+		if __doesRepositoryDisplayNameExist(repository):
+			print >>sys.stderr, "The display name '%s' already exists." % repository.DisplayName
 			return False
 
 		# Create the requested repository.
@@ -97,7 +101,7 @@
 	lock = guard.FileLock('HWM')
 	with lock:
 		print "Registering the repository: %s" % repository.StorageName
-		if not __doesRepositoryExist(repository):
+		if not __doesRepositoryStorageNameExist(repository):
 			print >>sys.stderr, "The repository '%s' does not exists." % repository.StorageName
 			return False
 
@@ -134,10 +138,14 @@
 		print "Modifying repository: " + currentStorageName
 		oldRepository = Repository(currentStorageName)
 
-		if not __doesRepositoryExist(oldRepository):
+		if not __doesRepositoryStorageNameExist(oldRepository):
 			print >>sys.stderr, "The repository '%s' was not found." % oldRepository.StorageName
 			return False
 
+		if __doesRepositoryDisplayNameExist(newRepository):
+			print >>sys.stderr, "The display name '%s' already exists." % newRepository.DisplayName
+			return False
+
 		# Remove the current repository from the web registry.
 		if not __unregisterRepository(oldRepository):
 			print >>sys.stderr, "Failed to unregister repository."
@@ -201,7 +209,7 @@
 	print "Acquiring manager lock ..."
 	lock = guard.FileLock('HWM')
 	with lock:
-		if not __doesRepositoryExist(repository):
+		if not __doesRepositoryStorageNameExist(repository):
 			print >>sys.stderr, "The repository %s was not found." % repository.StorageName
 			return False
 
@@ -367,12 +375,12 @@
 		return False
 	return True
 
-def __doesRepositoryExist(repo):
-	""" Checks to see if a repository exists.
+def __doesRepositoryStorageNameExist(repo):
+	""" Checks to see if a repository storage name exists.
 
 	 @param[in] repo	The targeted repository for which to check.
 
-	 @return Gives true when the repository exists, false else-wise.
+	 @return Gives true when it exists, false else-wise.
 	"""
 	import os
 
@@ -383,6 +391,25 @@
 	else:
 		return False
 
+def __doesRepositoryDisplayNameExist(repo):
+	""" Checks to see if a repository display name exists.
+
+	 @param[in] repo	The targeted repository for which to check.
+
+	 @return Gives true when it exists, false else-wise.
+	"""
+	import os
+	from manrepo import Repository, ManagedCollection
+
+	if (repo is None) or (repo.StorageName is None) or (repo.DisplayName is None):
+		return False
+
+	repoCol = ManagedCollection()
+	for testRepo in repoCol.Repositories:
+		if (testRepo.DisplayName is not None) and testRepo.DisplayName.lower() == repo.DisplayName.lower():
+			return True
+	return False
+
 def __unregisterRepository(repo):
 	""" Removes the registration of the supplied repository.
 
--- a/src/manrepo.py	Thu Aug 07 21:10:21 2014 -0500
+++ b/src/manrepo.py	Tue Aug 12 19:12:20 2014 -0500
@@ -110,3 +110,31 @@
 		  else-wise gives false.
 		"""
 		return self.__storageName == other.__storageName
+
+class ManagedCollection(object):
+	""" A container of all the managed repositories. """
+
+	# All the managed repositories
+	__repositories = []
+
+
+	@property
+	def Repositories(self):
+		"""A collection of all managed repositories.
+
+		 @return An array of the managed repositories.
+		"""
+		return self.__repositories
+
+	def __init__(self):
+		""" Initialises the container with all the managed repositories.
+		"""
+		from ConfigParser import SafeConfigParser
+		import os
+
+		parser = SafeConfigParser()
+		if not parser.read(settings.HgWebPath + os.sep + 'hgweb.config'):
+			raise Exception("Failed to read HgWeb configuration.")
+
+		for name,path in parser.items('paths'):
+			self.__repositories.append(Repository(name))