[csw-devel] SF.net SVN: gar:[13218] csw/mgar/gar/v2-uwatch2

wbonnet at users.sourceforge.net wbonnet at users.sourceforge.net
Mon Feb 7 00:02:47 CET 2011


Revision: 13218
          http://gar.svn.sourceforge.net/gar/?rev=13218&view=rev
Author:   wbonnet
Date:     2011-02-06 23:02:47 +0000 (Sun, 06 Feb 2011)

Log Message:
-----------
Add regexp auto generation (contributed by Maciej)

Modified Paths:
--------------
    csw/mgar/gar/v2-uwatch2/bin/uwatch
    csw/mgar/gar/v2-uwatch2/gar.lib.mk

Modified: csw/mgar/gar/v2-uwatch2/bin/uwatch
===================================================================
--- csw/mgar/gar/v2-uwatch2/bin/uwatch	2011-02-06 22:57:29 UTC (rev 13217)
+++ csw/mgar/gar/v2-uwatch2/bin/uwatch	2011-02-06 23:02:47 UTC (rev 13218)
@@ -1,3 +1,4 @@
+#!/usr/bin/env python2.6
 #!/opt/csw/bin/python
 
 # TODO : check arguments for valid date
@@ -184,7 +185,7 @@
         self.optionParser = OptionParser()
 
         # Add options to parser
-        self.optionParser.add_option("-V", "--verbose",     help="Activate verbose mode", action="store_true", dest="verbose")
+        self.optionParser.add_option("--verbose",     help="Activate verbose mode", action="store_true", dest="verbose")
         self.optionParser.add_option("--current-version",    help="Current package version", action="store", dest="current_version")
         self.optionParser.add_option("--target-location",    help="Target location. This is the directory in which the branch will be created (parent of the branch directory). Default value is ../branches", action="store", dest="target_location")
         self.optionParser.add_option("--regexp",            help="Version matching regular expression", action="store", dest="regexp")
@@ -434,6 +435,17 @@
     # -----------------------------------------------------------------------------------------------------------------
 
     def getRegexp(self):
+        # In case the regexp is not define, we try to guess it using the distfile
+        if (self._regexp == None) & (self._gar_distfiles != None):
+            # Instanciate a regexp generator
+            urg = UwatchRegexGenerator()
+
+            # Retrieve the regexp list
+            auto_regex_list = urg.GenerateRegex(self._catalog_name, self._gar_distfiles)
+
+            # Use the first item as current regexp
+            self._regexp = auto_regex_list[0]
+
         return self._regexp
 
     # -----------------------------------------------------------------------------------------------------------------
@@ -724,8 +736,6 @@
             # Check if we have found some results
             if len(matches) == 0:
                 raise NoUpstreamVersionFoundException(self.config.getUpstreamURL(), self.config.getRegexp())
-                print "No match found, we should trigger some error since even current version has not been found"
-                return False
             else:
                 newestVersion = self.config.getCurrentVersion()
                 while len(matches) > 0:
@@ -802,7 +812,7 @@
 
             # Need a way to check that all options needed are available
             self.checkArgument()
-
+    
             # Call the method in charge of retrieving upstream content
             content = self.UrlContentRetrieve(self.config.getUpstreamURL())
 
@@ -813,10 +823,8 @@
             # Check if we have found some results
             if len(matches) == 0:
                 raise NoUpstreamVersionFoundException(self.config.getUpstreamURL(), self.config.getRegexp())
-                print "No match found, we should trigger some error since even current version has not been found"
-                return False
             else:
-                newestVersion = matches.pop(0)
+                newestVersion = matches.pop(0)                
                 while len(matches) > 0:
                     newestVersion = self.CompareVersionAndGetNewest(newestVersion, matches.pop(0))
 
@@ -1582,6 +1590,66 @@
             return 2
 
 
+class UwatchRegexGenerator(object):
+  
+  WS_RE = re.compile(r'\s+')
+  DIGIT_RE = re.compile(r'\d+')
+  DIGIT_REMOVAL_RE = re.compile(r'\d+(?:\.\d+)*[a-z]?')
+  DIGIT_MATCH_MAKE_RE_1 = r'(\d+(?:\.\d+)*[a-z]?)'
+  DIGIT_MATCH_MAKE_RE_2 = r'(\d+(?:\.\d+)*)'
+  ARCHIVE_RE = re.compile(r"\.(?:tar(?:\.(?:gz|bz2))|tgz)?$")
+
+  def _ChooseDistfile(self, file_list):
+    # First heuristic: files with numbers are distfiles
+    for f in file_list:
+      if self.ARCHIVE_RE.search(f):
+        return f
+    for f in file_list:
+      if self.DIGIT_RE.search(f):
+        return f
+
+  def _SeparateSoftwareName(self, catalogname, filename):
+    """Separate the software name from the rest of the file name.
+
+    Software name sometimes contains digits, which we don't want to
+    include in the regexes.
+    """
+    # The first approach is to split by '-'
+    assert filename
+    parts = filename.split('-')
+    if self._CanBeSoftwarename(parts[0], catalogname):
+      return parts[0], '-' + '-'.join(parts[1:])
+    return '', filename
+  
+  def _SeparateArchiveName(self, filename):
+    if self.ARCHIVE_RE.search(filename):
+      first_part = self.ARCHIVE_RE.split(filename)[0]
+      archive_part = ''.join(self.ARCHIVE_RE.findall(filename))
+      return first_part, archive_part
+    return filename, ''
+
+  def _CanBeSoftwarename(self, s, catalogname):
+    if s == catalogname:
+      return True
+    # This is stupid.  But let's wait for a real world counterexample.
+    return True
+
+  def GenerateRegex(self, catalogname, distnames):
+    dist_list = self.WS_RE.split(distnames)
+    dist_file = self._ChooseDistfile(dist_list)
+    if not dist_file:
+      return []
+    softwarename, rest_of_filename = self._SeparateSoftwareName(
+        catalogname, dist_file)
+    rest_of_filename, archive_part = self._SeparateArchiveName(rest_of_filename)
+    no_numbers = self.DIGIT_REMOVAL_RE.split(rest_of_filename)
+    regex_list = []
+    regex_list.append(softwarename + self.DIGIT_MATCH_MAKE_RE_1.join(no_numbers) + archive_part)
+    regex_list.append(softwarename + self.DIGIT_MATCH_MAKE_RE_2.join(no_numbers) + archive_part)
+    return regex_list
+
+
+
 # ---------------------------------------------------------------------------------------------------------------------
 #
 # Fonction principale

Modified: csw/mgar/gar/v2-uwatch2/gar.lib.mk
===================================================================
--- csw/mgar/gar/v2-uwatch2/gar.lib.mk	2011-02-06 22:57:29 UTC (rev 13217)
+++ csw/mgar/gar/v2-uwatch2/gar.lib.mk	2011-02-06 23:02:47 UTC (rev 13218)
@@ -194,6 +194,16 @@
 		echo "$(NAME) - Upstream Watch is disabled" ; \
 	else \
 		echo "$(NAME) - Upstream Watch is enabled" ; \
+		if [ ! -n '$(CATALOGNAME)' ]; then \
+			echo "$(NAME) - CATALOGNAME is not set" ; \
+		else \
+			echo "$(NAME) - CATALOGNAME is : $(CATALOGNAME)" ; \
+		fi ; \
+		if [ ! -n '$(DISTFILES)' ]; then \
+			echo "$(NAME) - DISTFILES is not set" ; \
+		else \
+			echo "$(NAME) - DISTFILES are : $(DISTFILES)" ; \
+		fi ; \
 		if [ ! -n '$(UFILES_REGEX)' ]; then \
 			echo "$(NAME) - UFILES_REGEX is not set" ; \
 		else \
@@ -231,8 +241,7 @@
 	else \
 		UWATCHCONFCHECK="Ok" ; \
 		if [ ! -n '$(UFILES_REGEX)' ]; then \
-			echo "$(NAME) - Error UFILES_REGEX is not set" ; \
-			UWATCHCONFCHECK="Error" ; \
+			echo "$(NAME) - UFILES_REGEX is not set - trying to guess it" ; \
 		fi; \
 		if [ ! -n '$(UPSTREAM_MASTER_SITES)' ]; then \
 			echo "$(NAME) - Error UPSTREAM_MASTER_SITES is not set" ; \
@@ -245,7 +254,11 @@
 		if [ "$$UWATCHCONFCHECK" -ne "Ok" ] ; then \
 			exit 1 ; \
 		fi ; \
-		VERSIONLIST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch get-upstream-version-list --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)"` ; \
+		if [ ! -n '$(UFILES_REGEX)' ]; then \
+			VERSIONLIST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch get-upstream-version-list --upstream-url="$(UPSTREAM_MASTER_SITES)" --gar-distfiles="$(DISTFILES)" --catalog-name="$(CATALOGNAME)"` ; \
+		else \
+			VERSIONLIST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch get-upstream-version-list --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)"` ; \
+		fi ; \
 		if [ "$$?" -ne "0" ] ; then \
 			echo "Error occured while executing uwatch get-upstream-version-list. Please check configuration with target get-uwatch-configuration. Here is the output of uwatch command :" ; \
 			echo "Output : $$VERSIONLIST" ; \
@@ -271,8 +284,7 @@
 	else \
 		UWATCHCONFCHECK="Ok" ; \
 		if [ ! -n '$(UFILES_REGEX)' ]; then \
-			echo "$(NAME) - Error UFILES_REGEX is not set" ; \
-			UWATCHCONFCHECK="Error" ; \
+			echo "$(NAME) - UFILES_REGEX is not set - trying to guess it" ; \
 		fi; \
 		if [ ! -n '$(UPSTREAM_MASTER_SITES)' ]; then \
 			echo "$(NAME) - Error UPSTREAM_MASTER_SITES is not set" ; \
@@ -285,7 +297,11 @@
 		if [ "$$UWATCHCONFCHECK" -ne "Ok" ] ; then \
 			exit 1 ; \
 		fi ; \
-		LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch get-upstream-latest-version --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)"` ; \
+		if [ ! -n '$(UFILES_REGEX)' ]; then \
+			LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch get-upstream-latest-version --upstream-url="$(UPSTREAM_MASTER_SITES)" --gar-distfiles="$(DISTFILES)" --catalog-name="$(CATALOGNAME)"` ; \
+		else \
+			LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch get-upstream-latest-version --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)"` ; \
+		fi ; \
 		if [ "$$?" -ne "0" ] ; then \
 			echo "Error occured while executing uwatch get-upstream-latest-version. Please check configuration with target get-uwatch-configuration. Here is the output of uwatch command :" ; \
 			echo "$$LATEST" ; \
@@ -307,8 +323,7 @@
 	else \
 		UWATCHCONFCHECK="Ok" ; \
 		if [ ! -n '$(UFILES_REGEX)' ]; then \
-			echo "$(NAME) - Error UFILES_REGEX is not set" ; \
-			UWATCHCONFCHECK="Error" ; \
+			echo "$(NAME) - UFILES_REGEX is not set - trying to guess it" ; \
 		fi; \
 		if [ ! -n '$(UPSTREAM_MASTER_SITES)' ]; then \
 			echo "$(NAME) - Error UPSTREAM_MASTER_SITES is not set" ; \
@@ -321,7 +336,11 @@
 		if [ "$$UWATCHCONFCHECK" -ne "Ok" ] ; then \
 			exit 1 ; \
 		fi ; \
-		LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch check-upstream --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)" --current-version="$(VERSION)"` ; \
+		if [ ! -n '$(UFILES_REGEX)' ]; then \
+			LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch check-upstream --upstream-url="$(UPSTREAM_MASTER_SITES)" --gar-distfiles="$(DISTFILES)" --catalog-name="$(CATALOGNAME)" --current-version="$(VERSION)"` ; \
+		else \
+			LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch check-upstream --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)" --current-version="$(VERSION)"` ; \
+		fi ; \
 		if [ "$$?" -ne "0" ] ; then \
 			echo "Error occured while executing uwatch check-upstream. Please check configuration with target get-uwatch-configuration. Here is the output of uwatch command :" ; \
 			echo "$$LATEST" ; \
@@ -344,8 +363,7 @@
 	else \
 		UWATCHCONFCHECK="Ok" ; \
 		if [ ! -n '$(UFILES_REGEX)' ]; then \
-			echo "$(NAME) - Error UFILES_REGEX is not set" ; \
-			UWATCHCONFCHECK="Error" ; \
+			echo "$(NAME) - UFILES_REGEX is not set - trying to guess it" ; \
 		fi; \
 		if [ ! -n '$(UPSTREAM_MASTER_SITES)' ]; then \
 			echo "$(NAME) - Error UPSTREAM_MASTER_SITES is not set" ; \
@@ -358,7 +376,11 @@
 		if [ "$$UWATCHCONFCHECK" -ne "Ok" ] ; then \
 			exit 1 ; \
 		fi ; \
-		LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch check-upstream --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)" --current-version="$(VERSION)"` ; \
+		if [ ! -n '$(UFILES_REGEX)' ]; then \
+			LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch check-upstream --upstream-url="$(UPSTREAM_MASTER_SITES)" --gar-distfiles="$(DISTFILES)" --catalog-name="$(CATALOGNAME)" --current-version="$(VERSION)"` ; \
+		else \
+			LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch check-upstream --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)" --current-version="$(VERSION)"` ; \
+		fi ; \
 		if [ "$$?" -ne "0" ] ; then \
 			echo "Error occured while executing uwatch check-upstream. Please check configuration with target get-uwatch-configuration. Here is the output of uwatch command :" ; \
 			echo "$$LATEST" ; \
@@ -415,8 +437,7 @@
 	else \
 		UWATCHCONFCHECK="Ok" ; \
 		if [ ! -n '$(UFILES_REGEX)' ]; then \
-			echo "$(NAME) - Error UFILES_REGEX is not set" ; \
-			UWATCHCONFCHECK="Error" ; \
+			echo "$(NAME) - UFILES_REGEX is not set - trying to guess it" ; \
 		fi; \
 		if [ ! -n '$(UPSTREAM_MASTER_SITES)' ]; then \
 			echo "$(NAME) - Error UPSTREAM_MASTER_SITES is not set" ; \
@@ -440,7 +461,11 @@
 			done ; \
 			exit 1 ; \
 		fi ; \
-		LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch get-upstream-latest-version --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)"` ; \
+		if [ ! -n '$(UFILES_REGEX)' ]; then \
+			LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch get-upstream-latest-version --upstream-url="$(UPSTREAM_MASTER_SITES)" --gar-distfiles="$(DISTFILES)" --catalog-name="$(CATALOGNAME)"` ; \
+		else \
+			LATEST=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch get-upstream-latest-version --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)"` ; \
+		fi ; \
 		if [ "$$?" -ne "0" ] ; then \
 			echo "Error occured while executing uwatch get-upstream-latest-version. Please check configuration with target get-uwatch-configuration. Here is the output of uwatch command :" ; \
 			echo "$$LATEST" ; \
@@ -461,7 +486,11 @@
 			GARPATH=`echo $$line | awk '{ print $$1 }'` ; \
 			CATALOGNAME=`echo $$line | awk '{ print $$2 }'` ; \
 			PKGNAME=`echo $$line | awk '{ print $$3 }'` ; \
-			REPORTVERSION=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch update-package-version-database --catalog-name="$$CATALOGNAME" --package-name="$$PKGNAME" --execution-date="$$EXECUTIONDATE" --gar-path="$$GARPATH" --gar-version=$(VERSION) --upstream-version="$$LATEST"  --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)" --gar-distfiles="$(DISTFILES)" --uwatch-output="Successful" ` ; \
+			if [ ! -n '$(UFILES_REGEX)' ]; then \
+				REPORTVERSION=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch update-package-version-database --catalog-name="$$CATALOGNAME" --package-name="$$PKGNAME" --execution-date="$$EXECUTIONDATE" --gar-path="$$GARPATH" --gar-version=$(VERSION) --upstream-version="$$LATEST"  --upstream-url="$(UPSTREAM_MASTER_SITES)"  --gar-distfiles="$(DISTFILES)" --uwatch-output="Successful" ` ; \
+			else \
+				REPORTVERSION=`http_proxy=$(http_proxy) ftp_proxy=$(ftp_proxy) $(GARBIN)/uwatch update-package-version-database --catalog-name="$$CATALOGNAME" --package-name="$$PKGNAME" --execution-date="$$EXECUTIONDATE" --gar-path="$$GARPATH" --gar-version=$(VERSION) --upstream-version="$$LATEST"  --upstream-url="$(UPSTREAM_MASTER_SITES)" --regexp="$(UFILES_REGEX)" --gar-distfiles="$(DISTFILES)" --uwatch-output="Successful" ` ; \
+			fi ; \
 			if [ "$$?" -ne "0" ] ; then \
 				echo "Error occured while executing uwatch update-package-version-database. Please check configuration with target get-uwatch-configuration. Here is the output of uwatch command :" ; \
 				echo "$$REPORTVERSION" ; \


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


More information about the devel mailing list