source: TI01-discovery/trunk/ingestAutomation/OAIBatch/oai_ingest.py @ 1889

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/trunk/ingestAutomation/OAIBatch/oai_ingest.py@1889
Revision 1889, 11.7 KB checked in by selatham, 14 years ago (diff)

getting auto ingest working

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2""" Script oai_ingest.py takes parameter <datacentre>.
3The /usr/local/WSClients/OAIBatch directory contains:-
4 - this python script,
5 - a DataProvider specific config file,
6 - the d2b.jar moles creator class which creates moles discovery records,
7 - the python module for extracting spatiotemporal information and adding to postgres db.
8Under this directory the following structure should be maintained:
9 ./data
10 - /DATACENTRE/
11                - discovery/:         Re-named documents ready to ingest in the discovery service.
12                - oai/difYYYYMMDD/    Documents as harvested from OAI
13 Where  /DATACENTRE  varies for the different data providers
14"""
15#History:
16# 12/05/06 SEL spelling correction
17# 30/05/06 SEL cope with many files for processing."Argument list too long" problem.
18# 31/05/06 SEL need to use passwords now. (replace xxxxxx in real version).
19# 16/10/06 SEL Changed to using python oaiClean.py module instead of java code.
20# 16/10/06 SEL exist db upgraded and deployed at different location, java upgrade.
21# 17/10/06 SEL cope with different discovery formats - not just DIF.
22# 23/10/06 SEL keywords not mandatory in config file.
23# 24/10/06 SEL fix bug where 'in' directory not being cleared initially. display more information when running.
24
25import os
26import sys
27import commands
28import string
29#import oaiClean
30
31status = 0
32numfilesproc = 0
33harvest_home = ""
34datacentre_groups = ""
35datacentre_format = ""
36datacentre_namespace = ""
37
38if (len(sys.argv) < 2):
39    print "<datacentre>  parameter not supplied."
40    sys.exit()
41else:
42    datacentre = sys.argv[1]
43
44# Other settings and constants
45date_string = commands.getoutput ("date +'%y%m%d_%H%M'")
46os.putenv ('EXIST_HOME', '/usr/local/exist-client')
47os.putenv ('JAVA_HOME', '/usr/java/jdk1.5.0_03')
48os.putenv ('PATH', ':/usr/java/jdk1.5.0_03/bin:/usr/java/jdk1.5.0_03:/usr/java/jdk1.5.0_03/lib/tools.jar:/usr/local/WSClients/OAIBatch:/usr/local/exist-client/bin:/bin:/usr/bin:.')
49os.putenv ('CLASSPATH','.:/usr/java/jdk1.5.0_03/lib/tools.jar')
50
51# Get the harvested records directory and groups for this datacentre from the datacentre specific config file
52# The harvested records directory depends on the datacentres OAI base url, the set and  format. These have to be know up-front.
53# The groups denote which 'portal groups' they belong to - for limiting searches to say NERC-only datacentres records.
54# Groups are added to the intermediate MOLES when it is created.
55datacentre_config_filename = "/usr/local/WSClients/OAIBatch/" + datacentre + "_config.properties"
56print "Datacentre config file = %s" %datacentre_config_filename
57datacentre_config_file = open(datacentre_config_filename, "r")
58
59for line in datacentre_config_file.readlines():
60    words  = string.split(line)
61    if len(words) == 0:
62        continue
63    if words[0] == 'host_path':
64        harvest_home = string.rstrip(words[1])
65    if words[0] == 'groups':
66        datacentre_groups = words[1:]
67    if words[0] == 'format':
68        datacentre_format = words[1]
69    if words[0] == 'namespace':
70        datacentre_namespace = words[1]
71datacentre_config_file.close()
72
73if harvest_home == "":
74    sys.exit("Failed at getting harvested records directory stage. datacentre config file tried = %s" %datacentre_config_filename)
75else:
76    print "INFO: harvested records are in %s" %harvest_home
77
78if datacentre_groups == "":
79    print "INFO: No groups/keywords set for datacentre %s" %datacentre
80else:
81    print "INFO: datacentre groups/keywords = %s" %datacentre_groups
82
83if datacentre_format == "":
84    sys.exit("Failed at stage: getting datacentre format. datacentre config file tried = %s" %datacentre_config_filename)
85else:
86    print "INFO: format being harvested = %s" %datacentre_format
87
88if datacentre_namespace == "":
89    sys.exit("Failed at stage: getting datacentre namespace. datacentre config file tried = %s" %datacentre_config_filename)
90else:
91    print "INFO: datacentre namespace = %s" %datacentre_namespace
92
93#any records to harvest?
94if len( os.listdir(harvest_home)) == 0:
95    print "Nothing to harvest this time from %s" %datacentre
96    sys.exit()
97
98# The directory to put things for a tape backup (should already exist)
99backupdir = '/disks/glue1/oaiBackup/'
100
101# Create/clear the 'in' directory pristine copy of the discovery records
102if os.path.isdir("/usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"):
103    commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/ | xargs -i rm /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/{\}"
104    print "Executing : " + commandline
105    status = os.system(commandline)
106else:
107    commandline = "mkdir /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"
108    print "Executing : " + commandline
109    status= os.system(commandline)
110
111if status != 0:
112    sys.exit("Failed at creating copy dir stage")
113
114# make the 'in' pristine copy. Cope with there being lots of files in the directory.
115
116commandline = "ls -1 " + harvest_home + "/ | xargs -i cp " + harvest_home + "/{\} /usr/local/WSClients/OAIBatch/data/" + datacentre + "/oai/originals"
117print "Executing : " + commandline
118status = os.system(commandline)
119if status !=0:
120    sys.exit("Failed at making pristine copy stage")
121
122# Create/clear the directory for the 'out' processed copy of the discovery records.
123if os.path.isdir("/usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"):
124    commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/ | xargs -i rm /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/{\}"
125    print "Executing : " + commandline
126    status = os.system(commandline)
127else:
128    commandline = "mkdir /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"
129    print "Executing : " + commandline
130    status= os.system(commandline)
131
132# The file config.properties contains the location of the particular datacentres harvested records.
133# Copy the datacentre specific version of config to config.properties file.
134commandline = "cp /usr/local/WSClients/OAIBatch/" + datacentre +"_config.properties /usr/local/WSClients/OAIBatch/config.properties"
135print "Executing : " + commandline
136status = os.system(commandline)
137if status !=0:
138    sys.exit("Failed at copying config file stage")
139
140#Change os directory to that with the code in it.
141os.chdir('/usr/local/WSClients/OAIBatch')
142
143
144#Execute the script which processes/renames the files (changed 24/11/06 to simply create a re-named file in the outdir)
145indir="/usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"
146outdir="/usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"
147#wrapFlag=False
148filenames = os.listdir(indir)
149for filename in filenames:
150        if filename.find('.xml') != -1:
151                original_filename = indir + "/" + filename
152                #print "Creating renamed file : "
153                new_filename = outdir + "/" +datacentre_namespace+ "__" +filename.split('%3A')[-1]
154                #print "original file = %s, newfile = %s" %(original_filename, new_filename)
155                commandline = "cp "+original_filename+ " " +new_filename
156                #print "Executing : " + commandline
157                status = os.system(commandline)
158                if status !=0:
159                        sys.exit("Failed at re-naming file stage")
160                #oaiClean.oaiClean(indir,outdir,filename,wrapFlag)
161                numfilesproc += 1
162        else:
163                print 'File %s is not xml format. Not processed'  %(full_filename)
164
165# ingest the datacentres records into eXist db (backups of exist happen nightly).
166commandline = "$EXIST_HOME/bin/client.sh -c /db/discovery/original/"+datacentre_format+ "/" +datacentre_namespace+ " -u admin -P xxxxxx -p "+outdir
167print "Executing : actual command to ingest into exist db"
168status = os.system(commandline)
169if status !=0:
170    sys.exit("Failed at ingesting into exist db. Datacentre =  %s. Status = %s" %(datacentre,status))
171
172# Then run the minimum moles creator  which will run over all records in the supplied collection
173# creates a directory ./DIF2MOLES to pass back records with original filename
174commandline = "java -jar D2B/d2b.jar repositoryID " +datacentre_namespace+" repositoryLocalID "+datacentre+" format "+datacentre_format+" repository xmldb:exist://glue.badc.rl.ac.uk:8080/exist/xmlrpc userpw xxxxxx targetCollection /db/discovery/original/"+datacentre_format+"/"+datacentre_namespace
175print "Executing command to run d2b.jar"
176status= os.system(commandline)
177if status!=0:
178    print "ERROR: couldn't create the minimum moles records"
179    sys.exit
180
181# ingest the created discovery minimum molesrecords into eXist db.
182commandline = "$EXIST_HOME/bin/client.sh -c /db/discovery/moles -u admin -P xxxxxx -p ./DIF2MOLES"
183print "Executing : actual command to ingest into exist db"
184status = os.system(commandline)
185if status !=0:
186    sys.exit("Failed at ingesting into exist db. Datacentre =  %s. Status = %s" %(datacentre,status))
187
188#Extract the spatiotemporal info from created moles and put in Postgres db
189outdir = "./DIF2MOLES"
190try:
191    SpaceTimeIngestFromMOLES.main(outdir)
192except:
193    print "ERROR: SpaceTimeIngestFromMOLES failed. Carrying on to do backups"
194
195#Make copies of discovery and oai/originals and DIF2MOLES areas to backup area for tape backups
196this_backupdir = backupdir + datacentre + "_" + date_string + "_originals"
197commandline = "mkdir " + this_backupdir
198print "Executing : " + commandline
199status = os.system(commandline)
200if status !=0:
201    sys.exit("Failed at creating backup directory %s" %this_backupdir)
202
203commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/ | xargs -i cp /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/{\} " + this_backupdir
204print "Executing : " + commandline
205status = os.system(commandline)
206if status !=0:
207    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
208
209this_backupdir = backupdir + datacentre + "_" + date_string + "_discovery"
210commandline = "mkdir " + this_backupdir
211print "Executing : " + commandline
212status = os.system(commandline)
213if status !=0:
214    sys.exit("Failed at creating backup directory %s" %this_backupdir)
215
216commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/ | xargs -i cp /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/{\} " + this_backupdir
217print "Executing : " + commandline
218status = os.system(commandline)
219if status !=0:
220    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
221
222this_backupdir = backupdir + datacentre + "_" + date_string + "_DIF2MOLES"
223commandline = "mkdir " + this_backupdir
224print "Executing : " + commandline
225status = os.system(commandline)
226if status !=0:
227    sys.exit("Failed at creating backup directory %s" %this_backupdir)
228
229commandline = "ls -1 ./DIF2MOLES | xargs -i cp ./DIF2MOLES/{\} " + this_backupdir
230print "Executing : " + commandline
231status = os.system(commandline)
232if status !=0:
233    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
234
235#Clear out the original harvest records area and DIF2MOLES
236commandline = "ls -1 " + harvest_home + " | xargs -i rm " + harvest_home + "/{\}"
237print "Executing : " + commandline
238status = os.system(commandline)
239if status !=0:
240    sys.exit("Failed at clearing out original harvest records area %s" %harvest_home)
241
242commandline = "ls -1 ./DIF2MOLES | xargs -i rm ./DIF2MOLES/{\}"
243print "Executing : " + commandline
244status = os.system(commandline)
245if status !=0:
246    sys.exit("Failed at clearing out DIF2MOLES area %s" %harvest_home)
247
248#remove the DIF2MOLES directory
249commandline = "rmdir ./DIF2MOLES"
250print "Executing : " + commandline
251status = os.system(commandline)
252if status !=0:
253    sys.exit("Failed at removing DIF2MOLES directory %s" %harvest_home)
254
255print "======================================================"
256print "No. of files pre-processed = %s" %numfilesproc
257if status == 0:
258    print " Procedure oai_ingest.py ran to end"
259else:
260    print "Procedure oai_ingest.py FAILED with status %s" %status
261
262print "======================================================"
Note: See TracBrowser for help on using the repository browser.