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

Поисковые слова: galactic collision
#!/usr/bin/env python
#
#MODULE copy_visit_or_prop_into_lrp
#
#***********************************************************************
"""

**PURPOSE** --
Make an exact copy of an existing visit or prop from one lrp
to another lrp under the same name.

**DEVELOPER** --
Gary Bower

**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.
lrps = a list of from_lrp, to_lrp
lrp_relation_list = a list of ASSIST relations to be cleaned out.
options = a dictionary containing the options and values in args.
parms = a list of parameter values in args.
prop_id = the 4 or 5 digit proposal number.
relation = a generic variable used during loops.
to_lrp = the LRP to which the proposal is to be copied.

**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 LRPs.
6. Verify the existence of the proposal.
7. Define a list of db relations, then delete old plan windows.
8. Insert new plan windows (note that queries for different relations
are too different for common programming).
9. Commit database transaction and close connection.

**NOTES** --
none

**MODIFICATION HISTORY** --
Date Who What
==== === ====
05/04/05 Bower Initial implementation based on
two separate tools developed by Galas.
09/01/05 Chance fix bug in insert statements
"""
#***********************************************************************
__version__ = "09/01/05"
import os, spst_getopt, sys, stpydb, string

def run(args):
"""
Make an exact copy of an existing visit or prop from one lrp
to another lrp under the same name.

Usage:
do copy_visit_or_prop_into_lrp from_lrp to_lrp -su= | -prop=

from_lrp is the LRP from which the proposal is to be copied

to_lrp is the LRP to which the proposal is to be copied

-prop= specifies the 4 or 5 digit proposal number to be
deleted (optional).

-su= specifies the 7 digit SU 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) < 2:
print run.__doc__
raise ValueError("Must specify both input and output LRPs.")
if parms[0] == parms[1]:
raise ValueError("Input and output LRPs cannot be identical.")
if len(parms[0]) != 6 or len(parms[1]) != 6:
raise ValueError("Input and output 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
from_lrp = string.upper(parms[0])
to_lrp = string.upper(parms[1])

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

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

# Check to see if prop_id exists in the relation prop_track.
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
copy_into_lrp(db_connect,from_lrp,to_lrp,query_option)

# Check to see if sunit_id exists in the relation visit_derived_data.
if sunit_id:
dbQuery = """select visit_id from visit_derived_data where
sunit_id = @su"""
db_connect.query(dbQuery)
db_connect.setParam(['su',sunit_id])
existTest = [{}]
db_connect.executeAll(existTest)
if db_connect.getRowcount() == 0:
raise ValueError("Visit %s does not exist." % visit_id)
query_option = "sunit_id = '%s'" % sunit_id
copy_into_lrp(db_connect,from_lrp,to_lrp,query_option)

db_connect.close()

def copy_into_lrp(db_connect,from_lrp,to_lrp,query_option):
"""Copy records from an LRP to another LRP in the given relations."""

# Define a list of db relations for which edits will be made.
lrp_relation_list = ['plan_windows','plan_window_status','lrp_unschedulables',
'plan_orient','lrp_processing_errors']

db_connect.beginTransaction()
# Delete old plan windows if any.
for relation in lrp_relation_list:
print "Deleting %s from %s." % (query_option,relation)
db_delete = """ delete from %s where lrp_name = @lrp
and %s""" % (relation,query_option)
db_connect.query(db_delete)
db_connect.setParam([['lrp',to_lrp]])
db_connect.executeUpdate()

# Insert records into plan_window_status
print "Inserting into plan_window_status"
db_insert = """ insert into plan_window_status (prop_id, sunit_id,
version_num, lrp_name, visit_id,
su_track_status, active_flag, lrp_state,
usc, plib_ver, status, schedulable,
processing_error, execution_time)
select prop_id, sunit_id, version_num, '%s',
visit_id, su_track_status, active_flag, lrp_state,
usc, plib_ver, status, schedulable, processing_error,
execution_time from plan_window_status
where lrp_name = @lrp
and %s """ % (to_lrp,query_option)
db_connect.query(db_insert)
db_connect.setParam([['lrp',from_lrp]])
db_connect.executeUpdate()

# Insert records into plan_windows
print "Inserting into plan_windows"
db_insert = """ insert into plan_windows
(sunit_id, version_num, lrp_name,
visit_id, prop_id, window_begin, window_end)
select sunit_id, version_num, '%s',
visit_id, prop_id, window_begin, window_end
from plan_windows where lrp_name = @lrp
and %s """ % (to_lrp,query_option)
db_connect.query(db_insert)
db_connect.setParam([['lrp',from_lrp]])
db_connect.executeUpdate()

# Insert records into lrp_unschedulables
print "Inserting into lrp_unschedulables"
db_insert = """ insert into lrp_unschedulables (prop_id, visit_id,
sunit_id, lrp_name, failure_cause)
select prop_id, visit_id, sunit_id, '%s',
failure_cause from lrp_unschedulables
where lrp_name = @lrp
and %s """ % (to_lrp,query_option)
db_connect.query(db_insert)
db_connect.setParam([['lrp',from_lrp]])
db_connect.executeUpdate()

# Insert records into plan_orient
print "Inserting into plan_orient"
db_insert = """ insert into plan_orient (sunit_id, version_num,
lrp_name, visit_id, prop_id, v3_min_ang,
v3_max_ang, v3_pref_ang)
select sunit_id, version_num, '%s',
visit_id, prop_id, v3_min_ang, v3_max_ang,
v3_pref_ang from plan_orient
where lrp_name = @lrp
and %s """ % (to_lrp,query_option)
db_connect.query(db_insert)
db_connect.setParam([['lrp',from_lrp]])
db_connect.executeUpdate()

# Insert records into lrp_processing_errors
print "Inserting into lrp_processing_errors"
db_insert = """insert into lrp_processing_errors
(prop_id, visit_id, sunit_id,
lrp_name, processing_error_reason)
select prop_id, visit_id, sunit_id,
'%s', processing_error_reason
from lrp_processing_errors where lrp_name = @lrp
and %s """ % (to_lrp,query_option)
db_connect.query(db_insert)
db_connect.setParam([['lrp',from_lrp]])
db_connect.executeUpdate()
print "Comitting transaction."
db_connect.commitTransaction()

return

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