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

Поисковые слова: туманность андромеды
#
#MODULE orderform_files
#
#***********************************************************************
"""

**PURPOSE** --
Query QSCHED_POOL relation to receive any "orderform" SUs. In the early
days of STScI, SUs were given to calendar builders via a manually written
orderform. Things have changed a lot since then, but the name "orderform"
lingers on. Currently, "orderform" SUs are special SUs not handled by the
Long Range Plan.

**DEVELOPER** --
Danny Jones

**MODIFICATION HISTORY** --
DCL tool ORDERFORM_FILES.COM originally by Merle Reinhart, 8/12/92
Converted to Python by Danny Jones, 11/19/99. Creation of obsolete files
in the original DCL tool have been removed.
Adjustments based upon code review, Danny Jones, 12/13/99.
Modifications to use SYPYDB module for database access. Use exception
classes instead of exception strings. Danny Jones, 3/17/00.
Location of tempfile() changed from spss_sys_util to file_util.
Danny Jones, 5/2/00

**USAGE** --
do_python orderform_files [] []
If the 2nd parameter is not specified the user will not be
prompted for it.
"""
#***********************************************************************
import sys, string, os, exceptions
import db_util, spss_sys_util, time_util, file_util
import stpydb


def check_orderform(week_id, order_num):
""" Checks the database for any SUs that have been reserved for the week
and on the global baseline.
"""

# Call CHECK_ORDERFORM.ISQL and parse the output. Raise exception if
# there is a problem calling the ISQL or parsing it.
check_tempfile = ""
try:
check_tempfile = file_util.tempfile()
db_util.isql_report(spss_sys_util.resolver("PMREP",
"check_orderform.isql", 1),
check_tempfile, [("WEEK", week_id)])
bad_su_file = ""
bad_sus = db_util.query_result_parser(check_tempfile, 2, "|",
["sunit_id", "version_num"])
if check_tempfile != "" and os.path.isfile(check_tempfile):
os.remove(check_tempfile)
except:
if check_tempfile != "" and os.path.isfile(check_tempfile):
os.remove(check_tempfile)
raise

# Write SU IDs to the BAD_SUS file if database query returned anything.
if bad_sus != []:
bad_su_file = "sus_bad_%s_%s.dat" % (week_id, order_num)
bad_file = open(bad_su_file, "w")
for next_su in bad_sus:
bad_file.write("%s:%s\n" % (next_su["sunit_id"],
next_su["version_num"]))
bad_file.close()
return bad_su_file


def run(week="", order_num=""):
""" Main function of ORDERFORM_FILES.PY.
"""

# If the 1st argument wasn't passed to the function, prompt the user for
# it.
spss_db = spss_sys_util.get_environ_variable("SPSS_DB")[0]
db_conn = stpydb.stpydb(dbmsName=spss_db)
week_id = None
while week_id is None:
try:
week_id = time_util.weekid(week)
except 'WeekIdError', extra_info:
if extra_info is not None:
print extra_info
week = string.strip(raw_input("Enter the week ID (yyddd): "))

# If the 2nd argument wasn't passed, assume the orderform version is 01.
# If it is passed, make sure the version entered currently exists in
# the database, including if 01 was passed. The 2nd parameter should
# only be passed in special circumstances.
if order_num != "":
order_num = string.upper(order_num)
db_query = "select distinct order_num" + \
" from qsched_pool" + \
" where week_id = @week" + \
" and order_num = @order_num"
db_conn.query(db_query)
db_conn.setParam([["week", `week_id`], ["order_num", order_num]])
existing_version = []
if db_conn.execute(existing_version) is None:
print "The requested orderform version (%s), is not" % order_num
print "a version in QSCHED_POOL...Aborting."
raise OrderformFilesVersionError(
"Error finding requested orderform version number in PMDB.")
db_conn.clear() # Needed since db_conn may not have reached empty row
else:
order_num = "01"

# Find all SUs in QSCHED_POOL that have the correct orderform version.
db_query = "select sunit_id, version_num" + \
" from qsched_pool" + \
" where week_id = @week" + \
" and (order_num = @order_num or order_num like 'O_')"
db_conn.query(db_query)
db_conn.setParam([["week", `week_id`], ["order_num", order_num]])
sus = [{}]
db_conn.executeAll(sus)

# Write all "orderform" SUs to a file. If there are no SUs, do not
# create the file.
orderform_su_file = "sus_%s_%s.dat" % (`week_id`, order_num)
if sus != []:
su_file = open(orderform_su_file, "w")
for next_su in sus:
su_file.write("%s:%s\n" % (next_su["sunit_id"],
next_su["version_num"]))
su_file.close()
print "File %s contains the orderform SUs for" % orderform_su_file,
print "the week."
else:
print "No orderform SUs were found in QSCHED_POOL for the " + `week_id`,
print "week."
print "Output file %s was not created." % orderform_su_file
orderform_su_file = ""

# Check if any currently reserved SUs are on the global baseline.
try:
bad_su_file = check_orderform(`week_id`, order_num)
except:
raise
if bad_su_file != "":
print "\n *** WARNING *** reserved SUs found on GLOBAL baseline !!!"
print "The BAD SUs are in %s\n" % bad_su_file
else:
print "\nNo SUs currently reserved for the %s week" % `week_id`,
print "are on the global baseline."
print "This is good.\n"

# Close database connection and return the names of the files created by
# this program.
db_conn.close()
return (orderform_su_file, bad_su_file)


class OrderformFilesVersionError(exceptions.Exception):
"""Raised when orderform version number does not exist."""
pass


if __name__ == '__main__':
week_id = ""
order_num = ""
if len(sys.argv) > 1:
week_id = sys.argv[1]
if len(sys.argv) > 2:
order_num = sys.argv[2]
run(week_id, order_num)