Skip to content

Commit

Permalink
Fixes #43
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlux committed Jul 24, 2015
1 parent 4e5e06e commit 9117e8e
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions deleteDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,51 @@

scriptName, serverUrl, datasetIdentifier, datasetId = sys.argv

# TODO: Should probably check the database to see if the server/identifier combo is still
# registered elsewhere

# Get the path to the map file
filePath = serverUrl.split("=")
print "This is the file path: " + str(filePath)
if len(filePath) < 2:
raise Exception("Incorrect map file path.")

# Remove the layer from the map file
mapobject = mapscript.mapObj(filePath[1])
layer = mapobject.getLayerByName(datasetIdentifier)
layerData = layer.data
mapobject.removeLayer(layer.index)
mapobject.save(filePath[1])

# Delete data file if it exists
if os.path.exists(layerData):
os.remove(layerData)
else:
raise Exception("Sorry, I cannot remove %s file." % layerData)

# Delete map file if no layers are left
if mapobject.numlayers <= 0:
if os.path.exists(filePath[1]):
os.remove(filePath[1])
else:
raise Exception("Sorry, I cannot remove %s file." % filePath[1])

try:
id = "meta-" + str(datasetId)
csw = CatalogueServiceWeb(pycsw_url)
csw.transaction(ttype='delete', typename='gmd:MD_Metadata', identifier=id)
except:
print "Warning: transaction error while deleting the catalogue record"
continue
continue

# Check the database to see if the server/identifier combo is still registered elsewhere
try:
# connect to iGUESS database
conn=psycopg2.connect( "host={0} dbname={1} user={2} password={3}".format(dbServer, dbName, dbUsername, dbPassword))
cur = conn.cursor()
cur.execute("SELECT d.id FROM iguess_dev.datasets d where d.server_url = '" + serverUrl + "' and d.identifier = '" + datasetIdentifier +"'")
datasets = cur.fetchall()

# Only if there is a single occurrence of the same combination of dataset identifier and server_url,
# the dataset and layer in mapfile will be deleted
if len(datasets) == 1:
# Get the path to the map file
filePath = serverUrl.split("=")
print "This is the file path: " + str(filePath)
if len(filePath) < 2:
raise Exception("Incorrect map file path.")

# Remove the layer from the map file
mapobject = mapscript.mapObj(filePath[1])
layer = mapobject.getLayerByName(datasetIdentifier)
layerData = layer.data
mapobject.removeLayer(layer.index)
mapobject.save(filePath[1])

# Delete data file if it exists
if os.path.exists(layerData):
os.remove(layerData)
else:
raise Exception("Sorry, I cannot remove %s file." % layerData)

# Delete map file if no layers are left
if mapobject.numlayers <= 0:
if os.path.exists(filePath[1]):
os.remove(filePath[1])
else:
raise Exception("Sorry, I cannot remove %s file." % filePath[1])
except:
print "I am unable to connect to the database."


0 comments on commit 9117e8e

Please sign in to comment.