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

Поисковые слова: arp 220
#
# Module attach
#
#***********************************************************************
"""
**PURPOSE** --
A tool for finding useable parallel attachments.

**DEVELOPER** --
Don Chance

**MODIFICATION HISTORY** --
Initial implemetation 6/8/00
change sus_attached... filename to lowercase in print statement.
dc 3/29/04
SQR to python. gab 2/5/13
Load args. gab 3/29/13
"""
#***********************************************************************
import os, spss_sys_util, db_util
import ccl_util, time_util
import spst_getopt, stpydb, sys

SPSS_DB = spss_sys_util.get_environ_variable('SPSS_DB')[0]

def run(calendar=None, *args):
"""Extract parallel attachment info from the PMDB.

Usage:
do attach cclist [-sqr]

where cclist is the name of the cclist,
if option -sqr is included then the SQR code is used
else (default) is to use python.
"""
import os, spss_sys_util, db_util
import ccl_util, time_util

while not calendar:
calendar = raw_input("Enter the CCL >> ")

ccl = ccl_util.ccl(calendar)
if not ccl.exists():
raise ccl_util.NoCCLError('Calendar %s does not exist' % calendar)

print "Making a list of all useable parallel attachments"
print "and a list of SUs for the week. This will take a while...\n"

allowed_options = ['sqr']
options, parms = spst_getopt.spst_getopt(args, allowed_options)
if options.has_key("-sqr"):
report = spss_sys_util.resolver("PMSQR","attach_by_week.sqr")
print "Running %s...\n" % report
db_util.sqr(report, 'attachments.rpt', [ccl.get_name()])
else:
attach_by_week(ccl, 'attachments.rpt', 'attached.sus', 'attached.com', 'attached.csh')

weekid = time_util.weekid(ccl)
os.rename('attachments.rpt', str(weekid) + '.attachments')
os.rename('attached.sus', 'sus_attached_' + str(weekid) + '.dat')
os.rename('attached.com', str(weekid) + '_add_apar_sus.com')
os.rename('attached.csh', str(weekid) + '_add_apar_sus.csh')

print "The list of attachments is in %s.attachments." % str(weekid)
print "The SU list is in sus_attached_%s.dat.\n" % str(weekid)

print "Attached parallels can be added to the calendar with the command:"
if spss_sys_util.on_sun():
print "calendar -file %s_add_apar_sus.csh" % str(weekid)
else:
print "calendar/file %s_add_apar_sus.com" % str(weekid)

return spss_sys_util.SUCCESS

def attach_by_week(ccl, rptfile, susfile, comfile, cshfile):
"""Retrieve all the attachment records that are useable for
parallels for the specified week."""

# Get week ID from ccl.
weekid = time_util.weekid(ccl)
# Get name of ccl.
cclname = ccl.get_name()

# Open output and write header.

g = open(rptfile,'w')
time_to_report = time_util.spss_time().get_localtime().strftime('%d-%b-%Y %H:%M:%S')
g.write("DATE OF REPORT: %s" % time_to_report)
g.write(7*" "+"PARALLEL ATTACHMENTS FOR WEEK: %s\n\n" % cclname)
g.write("PARALLEL PRIMARY SEQ\n")
g.write("---------- ------------------------------\n")
g.write("SUNIT ID ID END ID NUM TARGET WEEK\n")
g.write("--------- -------------- --------------\n")

dbConn = stpydb.stpydb(dbmsName=SPSS_DB)

dbConn.query('select distinct qsb.sunit_id, qpa.sunit_id, qpa.version_num, qpa.prim_prop_id, ')
dbConn.query('qpa.prim_obs_id, qpa.prim_ali_id, qpa.prim_version, qpa.last_prim_obs_id, ')
dbConn.query('qpa.last_prim_ali_id, qpa.sequence_num, qpa.par_target, qpa.week_id ')
dbConn.query('from qparallels qpa, qsbranching qsb, ')
dbConn.query('wistat_sunit wis, qalignment qal ')
dbConn.query('where qsb.proposal_id = qpa.prim_prop_id ')
dbConn.query('and qsb.proposal_id = qal.proposal_id ')
dbConn.query('and qsb.obset_id = qpa.prim_obs_id ')
dbConn.query('and qsb.obset_id = qal.obset_id ')
dbConn.query('and qal.alignment_id = qpa.prim_ali_id ')
dbConn.query('and qsb.version_num = qpa.prim_version ')
dbConn.query('and qsb.version_num = qal.version_num ')
dbConn.query('and qal.target_type != "M" ')
dbConn.query('and wis.sunit_id = qsb.sunit_id ')
dbConn.query('and wis.version_num = qsb.version_num ')
dbConn.query('and wis.sunit_status >= 2 ')
dbConn.query('and wis.cc_or_sms_na = @cclname ')
# This clause excludes SU's scheduled on a baselined calendar.
dbConn.query('and wis.sunit_id not in ')
dbConn.query('(select distinct wis.sunit_id ')
dbConn.query('from wistat_sunit wis, wbase_cat wba, qparallels qpa ')
dbConn.query('where wis.cc_or_sms_na = wba.ccl_name ')
dbConn.query('and wis.cc_or_sms_vn = wba.ccl_vers ')
dbConn.query('and wis.sunit_id = qpa.sunit_id ')
dbConn.query('and wis.version_num = qpa.version_num ')
dbConn.query('and wis.sunit_status >= 2 ')
dbConn.query('and wba.base_name = "GLOBAL" ')
dbConn.query('and wba.ccl_name != @cclname) ')
# This clause excludes SU's scheduled on a baselined calendar for the current week.
dbConn.query('and qpa.sunit_id not in ')
dbConn.query('(select distinct wis.sunit_id ')
dbConn.query('from wistat_sunit wis, wbase_cat wba, qparallels qpa ')
dbConn.query('where wis.cc_or_sms_na = wba.ccl_name ')
dbConn.query('and wis.cc_or_sms_vn = wba.ccl_vers ')
dbConn.query('and wis.sunit_id = qpa.sunit_id ')
dbConn.query('and wis.version_num = qpa.version_num ')
dbConn.query('and wis.sunit_status >= 2 ')
dbConn.query('and wba.base_name = "GLOBAL" ')
dbConn.query('and wba.ccl_name != @cclname) ')
dbConn.query('and ((qpa.week_id = @weekid or (qpa.week_id in ("99999"," "))))')
dbConn.query('order by qpa.prim_prop_id, qpa.prim_obs_id, qpa.prim_ali_id, qpa.prim_version, ')
dbConn.query('qpa.sequence_num, qpa.sunit_id, qpa.version_num')
dbConn.setParam([('cclname', cclname), ('weekid', str(weekid))])

result = [{}]
while dbConn.executeAll(result):
pass
if result != [{}]:
for su in result:
g.write("%s:%s * " % (su['sunit_id'],su['version_num']))
g.write("%s:%s:" % (su['prim_prop_id'],su['prim_obs_id']))
g.write("%s:%s " % (su['prim_ali_id'],su['prim_version']))
g.write("%s:%s:" % (su['prim_prop_id'],su['last_prim_obs_id']))
g.write("%s:%s " % (su['last_prim_ali_id'],su['prim_version']))
g.write("%s %6s %18s\n" % (su['sequence_num'],su['par_target'],su['week_id']))

# Close the rptfile and generate the other output files.
g.close()
g = open(susfile,'w')
for su in result:
g.write("%s:%s\n" % (su['sunit_id'],su['version_num']))
g.close()
g = open(comfile,'w')
attachcom = '$calendar /addcand /ctype=p'
a = ',,,,,A'
for su in result:
g.write("%s %s " % (attachcom,su['sunit_id']))
g.write("%s%s:" % (su['prim_prop_id'],su['prim_obs_id']))
g.write("%s:%s:" % (su['prim_prop_id'],su['last_prim_obs_id']))
g.write("%s:%s%s\n" % (su['last_prim_ali_id'],su['prim_version'],a))
g.close()
g = open(cshfile,'w')
attachcsh = 'calendar -addcand -ctype=p'
for su in result:
g.write("%s %s " % (attachcsh,su['sunit_id']))
g.write("%s%s:" % (su['prim_prop_id'],su['prim_obs_id']))
g.write("%s:%s:" % (su['prim_prop_id'],su['last_prim_obs_id']))
g.write("%s:%s%s\n" % (su['last_prim_ali_id'],su['prim_version'],a))
g.close()


else:
raise ValueError ("Failed to get sunit_id data from database.")

return spss_sys_util.SUCCESS

if __name__ == '__main__':
run(sys.argv[1])