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

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

Allow no specification of groups/keywords in 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# 23/10/06 SEL keywords not mandatory in config file.
26
27import os
28import sys
29import commands
30import string
31import oaiClean
32
33status = 0
34numfilesproc = 0
35harvest_home = ""
36datacentre_groups = ""
37datacentre_format = ""
38
39if (len(sys.argv) < 2):
40    print "<datacentre>  parameter not supplied."
41    sys.exit()
42else:
43    datacentre = sys.argv[1]
44
45# Other settings and constants
46date_string = commands.getoutput ("date +'%y%m%d_%H%M'")
47os.putenv ('EXIST_HOME', '/usr/local/exist-client')
48os.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:.')
49os.putenv ('CLASSPATH','.:/usr/java/j2sdk1.4.2_04/bin:/usr/local/WSClients/OAIBatch')
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 xml record by oaiClean.py.
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]
69datacentre_config_file.close()
70if harvest_home == "":
71    sys.exit("Failed at getting harvested records directory stage. datacentre config file tried = %s" %datacentre_config_filename)
72if datacentre_groups == "":
73     print "INFO: No groups/keywords set for datacentre %s" %datacentre
74if datacentre_format == "":
75    sys.exit("Failed at stage: getting datacentre format. datacentre config file tried = %s" %datacentre_config_filename)
76
77#any records to harvest?
78if len( os.listdir(harvest_home)) == 0:
79    print "Nothing to harvest this time from %s" %datacentre
80    sys.exit()
81
82# The directory to put things for a tape backup (should already exist)
83backupdir = '/disks/glue1/oaiBackup/'
84
85# Create/clear the 'in' directory pristine copy of the discovery records
86if os.path.isdir("/usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"):
87    commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/ | xargs -i rm /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/{\}"
88    print "Executing : " + commandline
89    status = os.system(commandline)
90else:
91    commandline = "mkdir /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"
92    print "Executing : " + commandline
93    status= os.system(commandline)
94
95if status != 0:
96    sys.exit("Failed at creating copy dir stage")
97
98# make the 'in' pristine copy. Cope with there being lots of files in the directory.
99
100commandline = "ls -1 " + harvest_home + "/ | xargs -i cp " + harvest_home + "/{\} /usr/local/WSClients/OAIBatch/data/" + datacentre + "/oai/originals"
101print "Executing : " + commandline
102status = os.system(commandline)
103if status !=0:
104    sys.exit("Failed at making pristine copy stage")
105
106# Create/clear the directory for the 'out' processed copy of the discovery records.
107if os.path.isdir("/usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"):
108    commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/ | xargs -i rm -f {\}"
109    print "Executing : " + commandline
110    status = os.system(commandline)
111else:
112    commandline = "mkdir /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"
113    print "Executing : " + commandline
114    status= os.system(commandline)
115
116# Removed 16/10/06 - directory will hold 'out' processed records only
117# make the processing copy
118#commandline = "ls -1 " + harvest_home + "/ | xargs -i cp " + harvest_home + "/{\} /usr/local/WSClients/OAIBatch/data/" + datacentre + "/discovery"
119#print "Executing : " + commandline
120#status = os.system(commandline)
121#if status !=0:
122#    sys.exit("Failed at making processing copy stage")
123
124# The file config.properties contains the location of the particular datacentres harvested records.
125# Copy the datacentre specific version of config to config.properties file.
126
127commandline = "cp /usr/local/WSClients/OAIBatch/" + datacentre +"_config.properties /usr/local/WSClients/OAIBatch/config.properties"
128print "Executing : " + commandline
129status = os.system(commandline)
130if status !=0:
131    sys.exit("Failed at copying config file stage")
132
133#Change os directory to that with the oaiClean.py in it. (need this?)
134os.chdir('/usr/local/WSClients/OAIBatch')
135
136
137#Execute the script which processes the files
138indir="/usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"
139outdir="/usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"
140filenames = os.listdir(indir)
141for filename in filenames:
142        if filename.find('.xml') != -1:
143                full_filename = indir + "/" + filename
144                print "Processing : " + full_filename
145                #try:
146                oaiClean.oaiClean(indir,outdir,filename,datacentre_groups)
147                #except:
148                #    sys.exit("Failed at processing file %s with oaiClean.py stage with status %s" %(full_filename, sys.exc_info()))
149                #    break
150                numfilesproc += 1
151        else:
152                print 'File %s is not xml format. Not processed'  %(full_filename)
153
154# Removed 16/10/06 Don't need this anymore
155#Once the pre-processing has finished remove the originals from the discovery directory:
156#commandline = "find /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/oai* -print | xargs -i rm /{\}"
157#print "Executing : " + commandline
158#status = os.system(commandline)
159#if status !=0:
160#    sys.exit("Failed at removing original oai style records from discovery directory")
161
162
163# ingest the datacentres records into eXist db (backups of exist happen nightly).
164commandline = "$EXIST_HOME/bin/client.sh -c /db/discovery/" +datacentre_format+ "/" + datacentre + " -u admin -P xxxxxx -p /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/"
165print "Executing : actual command to ingest into exist db"
166status = os.system(commandline)
167if status !=0:
168    sys.exit("Failed at ingesting into exist db. Datacentre =  %s. Status = %s" %(datacentre,status))
169
170#Make copies of discovery and oai/originals areas to backup area for tape backups
171this_backupdir = backupdir + datacentre + "_" + date_string + "_originals"
172commandline = "mkdir " + this_backupdir
173print "Executing : " + commandline
174status = os.system(commandline)
175if status !=0:
176    sys.exit("Failed at creating backup directory %s" %this_backupdir)
177
178commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/ | xargs -i cp /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/{\} " + this_backupdir
179print "Executing : " + commandline
180status = os.system(commandline)
181if status !=0:
182    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
183
184this_backupdir = backupdir + datacentre + "_" + date_string + "_discovery"
185commandline = "mkdir " + this_backupdir
186print "Executing : " + commandline
187status = os.system(commandline)
188if status !=0:
189    sys.exit("Failed at creating backup directory %s" %this_backupdir)
190
191commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/ | xargs -i cp /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/{\} " + this_backupdir
192print "Executing : " + commandline
193status = os.system(commandline)
194if status !=0:
195    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
196
197#Clear out the original harvest records area
198commandline = "ls -1 " + harvest_home + " | xargs -i rm " + harvest_home + "/{\}"
199print "Executing : " + commandline
200status = os.system(commandline)
201if status !=0:
202    sys.exit("Failed at clearing out original harvest records area %s" %harvest_home)
203
204
205print "======================================================"
206print "No. of files pre-processed = %s" %numfilesproc
207if status == 0:
208    print " Procedure oai_ingest.py ran to end"
209else:
210    print "Procedure oai_ingest.py FAILED with status %s" %status
211
212print "======================================================"
Note: See TracBrowser for help on using the repository browser.