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

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

Cope with different incoming formats. Also add keywords from config file.

  • 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 this python script, a config file
4and the oaiClean.py class which cleans up discovery records after harvesting.
5The pre-processed files are then ingested to the eXist XML db.
6
7 Under this directory the following structure should be maintained:
8
9 ./data
10 - /DATACENTRE/
11                - discovery/:         Records with namespace, schema declaration deleted - after having run
12                                      the oaiClean script. Ready to ingest in the discovery service.
13                - oai/difYYYYMMDD/    Records as harvested from OAI
14
15 Where  /DATACENTRE  varies for the different data providers
16
17"""
18#History:
19# 12/05/06 SEL spelling correction
20# 30/05/06 SEL cope with many files for processing."Argument list too long" problem.
21# 31/05/06 SEL need to use passwords now. (replace xxxxxx in real version).
22# 16/10/06 SEL Changed to using python oaiClean.py module instead of java code.
23# 16/10/06 SEL exist db upgraded and deployed at different location, java upgrade.
24# 17/10/06 SEL cope with different discovery formats - not just DIF.
25
26import os
27import sys
28import commands
29import string
30import oaiClean
31
32status = 0
33numfilesproc = 0
34harvest_home = ""
35datacentre_groups = ""
36datacentre_format = ""
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 ('PATH', ':/usr/java/jdk1.5.0_03/jre:/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:.')
48os.putenv ('CLASSPATH','.:/usr/java/j2sdk1.4.2_04/bin:/usr/local/WSClients/OAIBatch')
49
50# Get the harvested records directory and groups for this datacentre from the datacentre specific config file
51# The harvested records directory depends on the datacentres OAI base url, the set and  format. These have to be know up-front.
52# The groups denote which 'portal groups' they belong to - for limiting searches to say NERC-only datacentres records.
53# Groups are added to the xml record by oaiClean.py.
54datacentre_config_filename = "/usr/local/WSClients/OAIBatch/" + datacentre + "_config.properties"
55print "Datacentre config file = %s" %datacentre_config_filename
56datacentre_config_file = open(datacentre_config_filename, "r")
57
58for line in datacentre_config_file.readlines():
59    words  = string.split(line)
60    if len(words) == 0:
61        continue
62    if words[0] == 'host_path':
63        harvest_home = string.rstrip(words[1])
64    if words[0] == 'groups':
65        datacentre_groups = words[1:]
66    if words[0] == 'format':
67        datacentre_format = words[1]
68datacentre_config_file.close()
69if harvest_home == "":
70    sys.exit("Failed at getting harvested records directory stage. datacentre config file tried = %s" %datacentre_config_filename)
71if datacentre_groups == "":
72    sys.exit("Failed at stage: getting datacentre groups. datacentre config file tried = %s" %datacentre_config_filename)
73if datacentre_format == "":
74    sys.exit("Failed at stage: getting datacentre format. datacentre config file tried = %s" %datacentre_config_filename)
75
76#any records to harvest?
77if len( os.listdir(harvest_home)) == 0:
78    print "Nothing to harvest this time from %s" %datacentre
79    sys.exit()
80
81# The directory to put things for a tape backup (should already exist)
82backupdir = '/disks/glue1/oaiBackup/'
83
84# Create/clear the 'in' directory pristine copy of the discovery records
85if os.path.isdir("/usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"):
86    commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/ | xargs -i rm /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/{\}"
87    print "Executing : " + commandline
88    status = os.system(commandline)
89else:
90    commandline = "mkdir /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"
91    print "Executing : " + commandline
92    status= os.system(commandline)
93
94if status != 0:
95    sys.exit("Failed at creating copy dir stage")
96
97# make the 'in' pristine copy. Cope with there being lots of files in the directory.
98
99commandline = "ls -1 " + harvest_home + "/ | xargs -i cp " + harvest_home + "/{\} /usr/local/WSClients/OAIBatch/data/" + datacentre + "/oai/originals"
100print "Executing : " + commandline
101status = os.system(commandline)
102if status !=0:
103    sys.exit("Failed at making pristine copy stage")
104
105# Create/clear the directory for the 'out' processed copy of the discovery records.
106if os.path.isdir("/usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"):
107    commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/ | xargs -i rm -f {\}"
108    print "Executing : " + commandline
109    status = os.system(commandline)
110else:
111    commandline = "mkdir /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"
112    print "Executing : " + commandline
113    status= os.system(commandline)
114
115# Removed 16/10/06 - directory will hold 'out' processed records only
116# make the processing copy
117#commandline = "ls -1 " + harvest_home + "/ | xargs -i cp " + harvest_home + "/{\} /usr/local/WSClients/OAIBatch/data/" + datacentre + "/discovery"
118#print "Executing : " + commandline
119#status = os.system(commandline)
120#if status !=0:
121#    sys.exit("Failed at making processing copy stage")
122
123# The file config.properties contains the location of the particular datacentres harvested records.
124# Copy the datacentre specific version of config to config.properties file.
125
126commandline = "cp /usr/local/WSClients/OAIBatch/" + datacentre +"_config.properties /usr/local/WSClients/OAIBatch/config.properties"
127print "Executing : " + commandline
128status = os.system(commandline)
129if status !=0:
130    sys.exit("Failed at copying config file stage")
131
132#Change os directory to that with the oaiClean.py in it. (need this?)
133os.chdir('/usr/local/WSClients/OAIBatch')
134
135
136#Execute the script which processes the files
137indir="/usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"
138outdir="/usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"
139filenames = os.listdir(indir)
140for filename in filenames:
141        if filename.find('.xml') != -1:
142                full_filename = indir + "/" + filename
143                print "Processing : " + full_filename
144                #try:
145                oaiClean.oaiClean(indir,outdir,filename,datacentre_groups)
146                #except:
147                #    sys.exit("Failed at processing file %s with oaiClean.py stage with status %s" %(full_filename, sys.exc_info()))
148                #    break
149                numfilesproc += 1
150        else:
151                print 'File %s is not xml format. Not processed'  %(full_filename)
152
153# Removed 16/10/06 Don't need this anymore
154#Once the pre-processing has finished remove the originals from the discovery directory:
155#commandline = "find /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/oai* -print | xargs -i rm /{\}"
156#print "Executing : " + commandline
157#status = os.system(commandline)
158#if status !=0:
159#    sys.exit("Failed at removing original oai style records from discovery directory")
160
161
162# ingest the datacentres records into eXist db (backups of exist happen nightly).
163commandline = "$EXIST_HOME/bin/client.sh -c /db/discovery/" +datacentre_format+ "/" + datacentre + " -u admin -P xxxxxx -p /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/"
164print "Executing : actual command to ingest into exist db"
165status = os.system(commandline)
166if status !=0:
167    sys.exit("Failed at ingesting into exist db. Datacentre =  %s. Status = %s" %(datacentre,status))
168
169#Make copies of discovery and oai/originals areas to backup area for tape backups
170this_backupdir = backupdir + datacentre + "_" + date_string + "_originals"
171commandline = "mkdir " + this_backupdir
172print "Executing : " + commandline
173status = os.system(commandline)
174if status !=0:
175    sys.exit("Failed at creating backup directory %s" %this_backupdir)
176
177commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/ | xargs -i cp /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/{\} " + this_backupdir
178print "Executing : " + commandline
179status = os.system(commandline)
180if status !=0:
181    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
182
183this_backupdir = backupdir + datacentre + "_" + date_string + "_discovery"
184commandline = "mkdir " + this_backupdir
185print "Executing : " + commandline
186status = os.system(commandline)
187if status !=0:
188    sys.exit("Failed at creating backup directory %s" %this_backupdir)
189
190commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/ | xargs -i cp /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/{\} " + this_backupdir
191print "Executing : " + commandline
192status = os.system(commandline)
193if status !=0:
194    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
195
196#Clear out the original harvest records area
197commandline = "ls -1 " + harvest_home + " | xargs -i rm " + harvest_home + "/{\}"
198print "Executing : " + commandline
199status = os.system(commandline)
200if status !=0:
201    sys.exit("Failed at clearing out original harvest records area %s" %harvest_home)
202
203
204print "======================================================"
205print "No. of files pre-processed = %s" %numfilesproc
206if status == 0:
207    print " Procedure oai_ingest.py ran to end"
208else:
209    print "Procedure oai_ingest.py FAILED with status %s" %status
210
211print "======================================================"
Note: See TracBrowser for help on using the repository browser.