Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/delete_su_or_prop_from_lrp.py
Дата изменения: Fri Feb 28 14:46:09 2014
Дата индексирования: Sat Mar 1 23:11:04 2014
Кодировка:

Поисковые слова: guide 8.0
#!/usr/bin/env python
#
#***********************************************************************
"""

**PURPOSE** --
Delete an existing sunit_id or prop_id from a LRP.

**DEVELOPER** --
Gala Soutchkova

**KEY** --
args = a list containing the argumants from the command line.
db_connect = database connection object to ASSIST database.
existTest = a list of dictionaries temporarily holding database query
results on existence tests.
from_lrp = the LRP from which the proposal is to be copied.
legal_opts = legal options on the command line.
lrp_relation_list = a list of ASSIST relations to be cleaned out.
options = a dictionary containing the options and values in args.
lrp = the LRP from which database records are to be deleted.
parms = a list of parameter values in args.
prop_id = the 4 or 5 digit proposal number.
query_option = a query option to be included, e.g. prop_id = XXXXX.
relation = a generic variable used during loops.
sunit_id = the 7 character SU identification.

**CODE ALGORITHM** --
1. If no parameters, then print documentation string and exit.
2. Define options and parameters.
3. Trap common errors in input parameters.
4. Establish the database connection to ASSIST.
5. Verify the existence of the input LRP.
6. Depdending on SU or proposal query, verify the existence of the
SU or proposal. Then delete its database records from the LRP.
7. The database deletion is done in the function delete_from_lrp.
This function commits each database transaction as it is done.
8. Close the database connection.

**NOTES** --
none

**MODIFICATION HISTORY** --
Date Who What
==== === ====
06-20-01 GALAS Program written
03-25-03 GALAS Revised. Delete prop added.
NAME changed
CALL changed from
delete_visit_from_lrp from_lrp sunit_id
to
delete_su_or_prop_from_lrp <-su= | -prop=> from_lrp
04-26-05 Bower minor mods in print stmts and db queries
05-03-05 Bower restructured and re-factored
08-17-05 Chance modified hash-bang for move to python 2.4.1

"""
#***********************************************************************
__version__ = "8/17/05"
import os, sys, stpydb, string, spst_getopt

def run(args):
"""
Delete an existing sunit_id or prop_id from a LRP.

Usage:
do delete_su_or_prop_from_lrp from_lrp -su= | -prop=

from_lrp is the LRP from which the SU or proposal is to be deleted.

-su= specifies the SU to be deleted (optional).

-prop= specifies the proposal to be deleted (optional).

"""

if not args:
# Spew out the usage and quit when no parameters are provided.
print run.__doc__
sys.exit()

# Define options and parameters.
legal_opts = ['su=','prop=']
options, parms = spst_getopt.spst_getopt(args, legal_opts)
# Trap common errors in input parameters and assign variables.
if options.has_key('-su') and options.has_key('-prop'):
raise ValueError("Must specify either a SU or a proposal, but not both.")
if len(parms) != 1:
print run.__doc__
raise ValueError("Must specify one LRP.")
else:
from_lrp = string.upper(parms[0])
if len(parms[0]) != 6:
raise ValueError("Input LRPs must be six characters.")
if options.has_key('-su'):
if len(options['-su']) != 7:
raise ValueError("Input SU must be 7 characters.")
else:
sunit_id = string.upper(options['-su'])
else:
sunit_id = None
if options.has_key('-prop'):
if len(options['-prop']) !=4 and len(options['-prop']) != 5:
raise ValueError("Input proposal ID must be 4 or 5 characters.")
else:
prop_id = string.atoi(options['-prop'])
else:
prop_id = None

# Open connection to the ASSIST database:
db_connect = stpydb.stpydb(dbmsName=os.environ['ASSIST_DB'])

# Check to see if from_lrp exists in the lrp catalog.
#
dbQuery = "select lrp_name from lrp_cat where lrp_name = '%s' " % from_lrp
db_connect.query(dbQuery)
existTest = [{}]
db_connect.executeAll(existTest)
if db_connect.getRowcount() == 0:
raise ValueError("LRP %s does not exist." % from_lrp)

# Check to see if sunit_id exists in the relation su_track.
# If so, then do its database deletions.
if sunit_id:
dbQuery = "select sunit_id from su_track where sunit_id = '%s'" \
% sunit_id
db_connect.query(dbQuery)
existTest = [{}]
db_connect.executeAll(existTest)
if db_connect.getRowcount() == 0:
raise ValueError("SU %s does not exist." % sunit_id)
query_option = "sunit_id = '%s'" % sunit_id
delete_from_lrp(db_connect,from_lrp,query_option)

# Check to see if prop_id exists in the relation prop_track.
# If so, then do its database deletions.
if prop_id:
dbQuery = "select prop_id from prop_track where prop_id = %i" % prop_id
db_connect.query(dbQuery)
existTest = [{}]
db_connect.executeAll(existTest)
if db_connect.getRowcount() == 0:
raise ValueError("Proposal %i does not exist." % prop_id)
query_option = 'prop_id = %i' % prop_id
delete_from_lrp(db_connect,from_lrp,query_option)

db_connect.close()

def delete_from_lrp(db_connect,lrp,query_option):
"""Delete records from an LRP in the given relations."""
lrp_relation_list = ['plan_window_status','plan_windows','lrp_unschedulables',
'plan_orient','lrp_processing_errors']
for relation in lrp_relation_list:
print "Delete %s from %s" % (query_option,relation)
db_delete = "delete from %s where lrp_name = '%s' and %s" \
% (relation, lrp, query_option)
db_connect.query(db_delete)
db_connect.executeUpdate()
db_connect.commitTransaction()
return

# to run on commandline
if __name__ == '__main__':
run(sys.argv[1:])