[csw-devel] SF.net SVN: gar:[13079] csw/mgar/gar/v2/lib

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Mon Jan 24 01:10:40 CET 2011


Revision: 13079
          http://gar.svn.sourceforge.net/gar/?rev=13079&view=rev
Author:   wahwah
Date:     2011-01-24 00:10:40 +0000 (Mon, 24 Jan 2011)

Log Message:
-----------
csw-upload-pkg: Support removal from catalog

This change implements a DELETE method against an assignment of a package to
a catalog.  Using the same logic as when adding packages, it allows to remove
a package from a catalog(s).

Modified Paths:
--------------
    csw/mgar/gar/v2/lib/python/csw_upload_pkg.py
    csw/mgar/gar/v2/lib/web/releases_web.py

Modified: csw/mgar/gar/v2/lib/python/csw_upload_pkg.py
===================================================================
--- csw/mgar/gar/v2/lib/python/csw_upload_pkg.py	2011-01-24 00:09:24 UTC (rev 13078)
+++ csw/mgar/gar/v2/lib/python/csw_upload_pkg.py	2011-01-24 00:10:40 UTC (rev 13079)
@@ -51,6 +51,48 @@
             % parsed_basename["vendortag"])
       self._UploadFile(filename)
 
+  def Remove(self):
+    for filename in self.filenames:
+      self._RemoveFile(filename)
+
+  def _RemoveFile(self, filename):
+    md5_sum = self._GetFileMd5sum(filename)
+    file_in_allpkgs, file_metadata = self._GetSrv4FileMetadata(md5_sum)
+    osrel = file_metadata['osrel']
+    arch = file_metadata['arch']
+    self._IterateOverCatalogs(
+        filename, file_metadata,
+        arch, osrel, self._RemoveFromCatalog)
+
+  def _RemoveFromCatalog(self, filename, arch, osrel, file_metadata):
+    md5_sum = self._GetFileMd5sum(filename)
+    basename = os.path.basename(filename)
+    parsed_basename = opencsw.ParsePackageFileName(basename)
+    url = (
+        "%scatalogs/unstable/%s/%s/%s/"
+        % (BASE_URL, arch, osrel, md5_sum))
+    logging.debug("DELETE @ URL: %s %s", type(url), url)
+    c = pycurl.Curl()
+    d = StringIO()
+    h = StringIO()
+    c.setopt(pycurl.URL, str(url))
+    c.setopt(pycurl.CUSTOMREQUEST, "DELETE")
+    c.setopt(pycurl.WRITEFUNCTION, d.write)
+    c.setopt(pycurl.HEADERFUNCTION, h.write)
+    c.setopt(pycurl.HTTPHEADER, ["Expect:"]) # Fixes the HTTP 417 error
+    if self.debug:
+      c.setopt(c.VERBOSE, 1)
+    c.perform()
+    http_code = c.getinfo(pycurl.HTTP_CODE)
+    logging.debug(
+        "DELETE curl getinfo: %s %s %s",
+        type(http_code),
+        http_code,
+        c.getinfo(pycurl.EFFECTIVE_URL))
+    c.close()
+    if http_code >= 400 and http_code <= 499:
+      raise RestCommunicationError("%s - HTTP code: %s" % (url, http_code))
+
   def _GetFileMd5sum(self, filename):
     if filename not in self.md5_by_filename:
       logging.debug("_GetFileMd5sum(%s): Reading the file", filename)
@@ -61,6 +103,20 @@
         self.md5_by_filename[filename] = md5_sum
     return self.md5_by_filename[filename]
 
+  def _IterateOverCatalogs(self, filename, file_metadata, arch, osrel, callback):
+    # Implementing backward compatibility.  A package for SunOS5.x is also
+    # inserted into SunOS5.(x+n) for n=(0, 1, ...)
+    for idx, known_osrel in enumerate(common_constants.OS_RELS):
+      if osrel == known_osrel:
+        osrels = common_constants.OS_RELS[idx:]
+    if arch == 'all':
+      archs = ('sparc', 'i386')
+    else:
+      archs = (arch,)
+    for arch in archs:
+      for osrel in osrels:
+        callback(filename, arch, osrel, file_metadata)
+
   def _UploadFile(self, filename):
     md5_sum = self._GetFileMd5sum(filename)
     file_in_allpkgs, file_metadata = self._GetSrv4FileMetadata(md5_sum)
@@ -73,18 +129,9 @@
     logging.debug("file_metadata %s", repr(file_metadata))
     osrel = file_metadata['osrel']
     arch = file_metadata['arch']
-    # Implementing backward compatibility.  A package for SunOS5.x is also
-    # inserted into SunOS5.(x+n) for n=(0, 1, ...)
-    for idx, known_osrel in enumerate(common_constants.OS_RELS):
-      if osrel == known_osrel:
-        osrels = common_constants.OS_RELS[idx:]
-    if arch == 'all':
-      archs = ('sparc', 'i386')
-    else:
-      archs = (arch,)
-    for arch in archs:
-      for osrel in osrels:
-        self._InsertIntoCatalog(filename, arch, osrel, file_metadata)
+    self._IterateOverCatalogs(
+        filename, file_metadata,
+        arch, osrel, self._InsertIntoCatalog)
 
   def _InsertIntoCatalog(self, filename, arch, osrel, file_metadata):
     logging.info(
@@ -204,6 +251,9 @@
   parser.add_option("-d", "--debug",
       dest="debug",
       default=False, action="store_true")
+  parser.add_option("--remove",
+      dest="remove",
+      default=False, action="store_true")
   options, args = parser.parse_args()
   print "args:", args
   if options.debug:
@@ -211,4 +261,7 @@
   else:
     logging.basicConfig(level=logging.INFO)
   uploader = Srv4Uploader(args, debug=options.debug)
-  uploader.Upload()
+  if options.remove:
+    uploader.Remove()
+  else:
+    uploader.Upload()

Modified: csw/mgar/gar/v2/lib/web/releases_web.py
===================================================================
--- csw/mgar/gar/v2/lib/web/releases_web.py	2011-01-24 00:09:24 UTC (rev 13078)
+++ csw/mgar/gar/v2/lib/web/releases_web.py	2011-01-24 00:10:40 UTC (rev 13079)
@@ -40,6 +40,7 @@
 
 class Srv4List(object):
   def POST(self):
+    messages = []
     configuration.SetUpSqlobjectConnection()
     x = web.input(srv4_file={})
     # x['srv4_file'].filename
@@ -56,18 +57,21 @@
     basename = x['basename']
     save_attempt = False
     if declared_md5_sum == data_md5_sum:
-      srv4 = models.Srv4FileStats.selectBy(md5_sum=data_md5_sum).getOne()
-      if srv4.use_to_generate_catalogs:
-        SaveToAllpkgs(basename, x['srv4_file'].value)
-        save_attempt = True
+      save_attempt = True
+      try:
+        srv4 = models.Srv4FileStats.selectBy(md5_sum=data_md5_sum).getOne()
+        if srv4.use_to_generate_catalogs:
+          SaveToAllpkgs(basename, x['srv4_file'].value)
+      except sqlobject.main.SQLObjectNotFound, e:
+        messages.append("File %s not found in the db." % data_md5_sum)
     else:
       save_attempt = False
-    response_data = {
+    messages.append({
         "received_md5": data_md5_sum,
         "declared_md5": declared_md5_sum,
         "save_attempt": save_attempt,
-    }
-    return json.dumps(response_data)
+    })
+    return json.dumps(messages)
 
 
 class Srv4Detail(object):
@@ -186,7 +190,19 @@
       web.header('Content-Length', len(response))
       return response
 
+  def DELETE(self, catrel_name, arch_name, osrel_name, md5_sum):
+    configuration.SetUpSqlobjectConnection()
+    try:
+      srv4_to_remove = models.Srv4FileStats.selectBy(md5_sum=md5_sum).getOne()
+      c = checkpkg_lib.Catalog()
+      c.RemoveSrv4(srv4_to_remove, osrel_name, arch_name, catrel_name)
+    except (
+        sqlobject.main.SQLObjectNotFound,
+        sqlobject.dberrors.OperationalError), e:
+      # Some better error reporting would be good here.
+      raise web.internalerror()
 
+
 def SaveToAllpkgs(basename, data):
   """Saves a file to allpkgs."""
   target_path = os.path.join(ALLPKGS_DIR, basename)


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