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

Поисковые слова: polar ring
#
#MODULE cms_util
#
#***********************************************************************
"""

**PURPOSE** --
A module for PASS CMS related utilities.

**DEVELOPER** --
Greg Wenzel

**MODIFICATION HISTORY** --

o initial implementation GWW 07/01/02
"""
#***********************************************************************
import file_util,spss_sys_util,os,string,re,exceptions
from boolean import *

class CMSError(exceptions.Exception):
pass

class cms:
"""a class for using cms library on vms from unix
"""
def __init__(self,library,working_directory=None):
"""a class for using cms library on vms from unix
"""
self.library = library
if not working_directory:
working_directory = os.path.join(
spss_sys_util.resolver("PASSOPS",return_vms_style=0),
"workspace")
self.working_directory = working_directory
if not os.path.exists(self.working_directory):
os.makedirs(self.working_directory)
self.working_directory_vms = spss_sys_util.unix_to_vms_path(
self.working_directory)

def set_library(self,library):
"""sets the library directory. NO checking is done since
this is used accross platforms unix-vms.
"""
self.library = library

def get_class_dict(self,filter='*', verbose=false):
"""get a dict of classes from the cms library that match the filter
"""
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
tmpfile = file_util.tempfile()
command_list.append('$cms show class %s /out=%s\n'
%(filter,tmpfile))
command_list.append('$type %s\n' %tmpfile)
command_list.append('$exit')
tmpcommandfile = file_util.tempfile()
command_file =open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
command = '@%s' %tmpcommandfile
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
if os.path.exists(tmpfile):
results_file = open(tmpfile,'r')
results_list = results_file.readlines()
results_file.close()
else:
raise IOError,'Class %s Not Found!' %filter
class_dict = {}
for line in results_list:
reg_exp = '^(\S+)\s+(.*)'
data = re.search(r'%s' %reg_exp,line)
if data:
cms_class = data.groups()[0]
remark = data.groups()[1]
if cms_class != "Classes":
class_dict.update({'%s' %cms_class :
'%s' %remark})
os.remove(tmpfile)
os.remove(tmpcommandfile)
os.chdir(current_directory)
return class_dict

def create_class(self,class_name,remark="",verbose=false):
"""create a class
"""
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
command_list.append('$cms create class %s "%s"\n'
%(class_name,remark))
command_list.append('$exit\n')
tmpcommandfile = file_util.tempfile()
command_file = open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
command = '@%s' %tmpcommandfile
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
os.remove(tmpcommandfile)
os.chdir(current_directory)
return status

def create_element(self,element_name,remark="",verbose=false):
"""create an element.
Note: the element must be in the working directory
"""
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
command_list.append('$cms create element %s "%s"\n'
%(element_name,remark))
command_list.append('$exit\n')
tmpcommandfile = file_util.tempfile()
command_file = open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
os.remove(tmpcommandfile)
os.chdir(current_directory)
return status

def insert_element(self,element_name,cms_group, remark="",verbose=false):
"""insert an element(S) into a group
"""
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
command_list.append('$cms insert element %s %s "%s"\n'
%(element_name,cms_group,remark))
command_list.append('$exit\n')
tmpcommandfile = file_util.tempfile()
command_file = open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
os.remove(tmpcommandfile)
os.chdir(current_directory)
return status

def insert_generation(self,element,cms_class, remark="",verbose=false):
"""insert current generation(s) into a class.
element = element or group expresion
"""
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
command_list.append('$cms insert generation %s %s "%s"\n'
%(element,cms_class,remark))
command_list.append('$exit\n')
tmpcommandfile = file_util.tempfile()
command_file = open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
os.remove(tmpcommandfile)
os.chdir(current_directory)
return status

def reserve_element(self,element_name,gen_name=None,
remark="",verbose=false):
"""reserve an element.
"""
if self.is_reserved(element_name,gen_name,verbose):
error_text = '\n\nElement %s is already reserved!' %element_name
error_text = error_text + '\nCheck CMS library %s' %self.library
raise CMSError(error_text)
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
if gen_name:
command_list.append('$cms reserve/noout %s/gen=%s "%s"\n'
%(element_name,gen_name,remark))
else:
command_list.append('$cms reserve/noout %s "%s"\n'
%(element_name,remark))
command_list.append('$exit\n')
tmpcommandfile = file_util.tempfile()
command_file = open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
os.remove(tmpcommandfile)
os.chdir(current_directory)
return status

def is_reserved(self,element_name,gen_name=None,verbose=false):
"""true if element(s) are reserved else false.
"""
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
if gen_name:
command_list.append('$cms show reservations %s/gen=%s\n'
%(element_name,gen_name))
else:
command_list.append('$cms show reservations %s\n'
%(element_name))
command_list.append('$exit\n')
tmpcommandfile = file_util.tempfile()
command_file = open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
os.remove(tmpcommandfile)
os.chdir(current_directory)
return_value = TRUE
search_rex = 'no reservations found'
for line in status:
if line:
if re.search(r'%s' %search_rex,line,re.I):
return_value = FALSE
return return_value

def unreserve_element(self,element_name,gen_name=None,
remark="",verbose=false):
"""reserve an element.
"""
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
if gen_name:
command_list.append('$cms unreserve %s/gen=%s "%s"\n'
%(element_name,gen_name,remark))
else:
command_list.append('$cms unreserve %s "%s"\n'
%(element_name,remark))
command_list.append('$exit\n')
tmpcommandfile = file_util.tempfile()
command_file = open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
os.remove(tmpcommandfile)
os.chdir(current_directory)
return status

def replace_element(self,element_name, remark="",verbose=false):
"""replace an element.
"""
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
command_list.append('$cms replace %s/if_changed "%s"\n'
%(element_name,remark))
command_list.append('$exit\n')
tmpcommandfile = file_util.tempfile()
command_file = open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
os.remove(tmpcommandfile)
os.chdir(current_directory)
return status

def fetch(self,element_name,gen_name=None,out="[]",remark="",verbose=false):
"""fetch an element
"""
current_directory = os.getcwd()
os.chdir(self.working_directory)
command_list = []
command_list.append('$set default %s\n' %self.working_directory_vms)
command_list.append('$cms set library %s\n' %self.library)
if gen_name:
command_list.append('$cms fetch %s/gen=%s/out=%s "%s"\n'
%(element_name,gen_name,out,remark))
else:
command_list.append('$cms fetch %s/out=%s "%s"\n'
%(element_name,out,remark))
command_list.append('$exit\n')
tmpcommandfile = file_util.tempfile()
command_file = open(tmpcommandfile,'w')
command_file.writelines(command_list)
command_file.close()
if spss_sys_util.on_vms():
command = '@%s' %tmpcommandfile
status = spss_sys_util.command(command)
if verbose:
for line in status:
if line:
print line
else:
comfilepath = spss_sys_util.unix_to_vms_path(tmpcommandfile,1)
status = spss_sys_util.rsh_unix_comfile_to_vms(
comfilepath,
os.environ['LOGNAME'],
spss_sys_util.get_environ_variable("VMS_PASS_NODE")[0])
if verbose:
for line in status:
if line:
print line
os.remove(tmpcommandfile)
os.chdir(current_directory)
return status