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

Поисковые слова: п п п п п п п п п п п п п п п п п п п п п п
#MODULE build_prd_files
#
#***********************************************************************
"""

**PURPOSE** --
Builds a PRD file from the PRDUVM libraries

Notes on changes from 09/11/03 by R.Kantor follow.
The program is being modified so that a file can be built for a
specific PRD version, and also for an input tag. Files built using
an input tag will not have FD records.
[The database relation that stores the file building information may
end up being prd_version, which has fields prd_version, file_type,
uvm_library, and tag_id.]
Currently the tag ids come from the prdcm collection relation.
The program builds a list, where the first element is a string
representing the filename of the fd record, and the other elements are
tuples, each of which contains a list of files for a particular
subsystem, and a string that represents the associated tag. The data
structure for the list of tuples is called element_list.

**DEVELOPER** --
K. Clark

**MODIFICATION HISTORY** --

o Initial implementation 12/18/02
o Updated 8/14/03 KWC - Reorganized prd_file_tree to include prd_dir_tree.
o Updated 1/28/04 RSK - Enhanced to use prd version (collection id) and
tag id.
o Updated 2/ 3/04 KWC - PRDUVM change directory can be in separate area
from config directory.
o Updated 3/12/04 RSK - Added processing for differencing output with
operational files
o Updated 3/30/04 KWC - Changed module name to build_prd_files.
o Updated 4/15/04 KWC - Use new prd_file_data object.

"""
#***********************************************************************

import glob, os, string, sys, re
import prd_file_tree, prd_fetch_elements, prd_file_data, prd_update, rcslib

__version__ = " 4/15/04"

class build_PRD_file:

# STSci suppored PRD file types:
PRD_types = ['crpf', 'dfsc', 'plcp', 'ptld', 'schf', 'sicf', 'svdf',
'tfpf', 'tidf']

def __init__(self, prd_root_dir=None, prd_file_type=None, \
PRDUVM_branch_name=None, \
change_tree_root_dir=None, new_file_list=None, \
collection_id=None, tag_id=None):
""" Verify the all the class inputs and create a PRD tree object
from all the user inputs. This PRD tree object will be used
to build a PRD file.

Input parameters:

PRD root directory = Top level directory to scan for elements.
PRD file type = PLCP, TFPF, PTLD, ...
Library Version = OPS or SM4.
Change root = Top level directory to scan for change
directory elements (Optional).
New file list = List PRD elements to be merged into
pdb_config tree (Optional).
Collection id = PRD collection id (i.e., PRD063-01)
(Optional).
UVM tag id = Tag id for the files (Optional).
"""

self.root_dir = prd_root_dir
self.prd_file_type = prd_file_type
self.branch_name = PRDUVM_branch_name
self.file_list = new_file_list
self.collection_id = collection_id
self.tag_id = tag_id

# Storage for PRD tree object.
self.PRD_tree = None

# Storage for all the RCS,v files found in a PRD change directory tree.
self.change_rcs_file_list = None

# Storage for new element list.
self.new_elements = []

# Initialise fetch method.
self.PRD_fetch_obj = prd_fetch_elements.prd_fetch_elements()

# Storage for final PRD file object.
self.PRD_file_obj = None

# Check the validity of the user inputs.
if (self.verify_inputs (prd_root_dir,prd_file_type,new_file_list) != 1):
# Stop processing if these inputs are bad.
return None

if not self.branch_name:
# Scan all the release libraries for this PRD file type.
branch_tree = self.prd_file_type
else:
# Goto a specific release library directory tree.
branch_tree = self.prd_file_type + "_" + self.branch_name

# Define the directory where the "Configured" PRDUVM PRD
# libraries are.
prd_config_dir = os.path.join(self.root_dir, "pdb_config", branch_tree)

# Always need the configured directory tree to build on.
if not (os.path.isdir(prd_config_dir)):
print "\n *** ERROR - Unable to find pdb_config directory: ", \
prd_config_dir
return None

# Define the directory where the new "Change" PRDUVM PRD
# libraries are.
prd_change_dir = None
if (change_tree_root_dir != None):
prd_change_dir = os.path.join (change_tree_root_dir, branch_tree)

# Check that change directory exists for this file type.
if not (os.path.isdir(prd_change_dir)):
print "\n *** ERROR - Unable to find pdb_change directory: ", \
prd_change_dir
return None

# Create a PRD tree object from all the new inputs.
self.create_PRD_tree (branch_tree, prd_config_dir, prd_change_dir)

def verify_inputs (self, prd_root_dir, prd_file_type, new_file_list):
""" Check user inputs. Returns false if error found.
"""
status = 1
if not prd_root_dir:
# Spew out the usage and quit when no initial directory
# is provided.
print __init__.__doc__
status = -1

# User has to provide the PRD file name.
if not prd_file_type:
print "\n ERROR - build_PRD_file - Please pick from one of the ", \
"following PRD file types: ", self.PRD_types
status = -1

# Check to see if we support this PRD file type.
if self.PRD_types.count(prd_file_type) <= 0:
print "\n ERROR - build_PRD_file ", prd_file_type, \
"is not one of the following PRD file types: ", self.PRD_types
status = -1

if new_file_list != None:
# See if at least one file in the list.
if (len(new_file_list) == 0):
print "\n ERROR - No new PRD elements given."
status = -1
else:
# Make sure the files exist.
for file in new_file_list:
if not (os.path.isfile(file)):
print "\n ERROR - Unable to find PRD element: ", file
status = -1

# Return final status.
return status

def add_change_dir (self, branch_tree, prd_change_dir):
""" Find all the RCS,v files in the change directory and merge them
with the config tree.
"""
prd_NEW_FILES = prd_file_tree.prd_file_tree(branch_tree, prd_change_dir,
self.collection_id)

# Record the names of the elements found in the change directory.
self.change_rcs_file_list = prd_NEW_FILES.get_element_list()

if (self.PRD_tree != None):
# Now merge them into the main tree.
self.PRD_tree.merge_file_objs(prd_NEW_FILES)
else:
print "\n *** Error - build_PRD_file class not initialised ", \
"correctly for", self.prd_file_type, "file."

def add_new_elements (self, new_file_list):
""" Find all the new user PRD elements and merge them
with the config (and change) tree.
"""
if (len(new_file_list) > 0):
# Create a temporary directory for storing the new files in RCS
# libraries.
self.new_update_dir = prd_update.prd_update()
self.new_elements = self.new_update_dir.build_libs (new_file_list)

if (self.PRD_tree != None):
# Now merge them into the main tree.
self.PRD_tree.merge_dir_tree_list (self.new_elements)
else:
print "\n *** Error - build_PRD_file class not initialised ", \
"correctly for", self.prd_file_type, "file."

def create_PRD_tree (self, branch_tree, prd_config_dir, prd_change_dir):
""" Create a new PRD file tree from all the user inputs.
"""
# Create the PRD file tree object.
self.PRD_tree = prd_file_tree.prd_file_tree(branch_tree, prd_config_dir,
self.collection_id)

# If this is for a specific collection, verify that all of the
# subsystems for the collection exist in the uvm library.
if self.collection_id != None:
if (not self.PRD_tree.have_all_subsys_in_library()):
print "\n *** Error - ", self.prd_file_type, \
"file has not been created."
print " Bad collection ID:", self.collection_id

# Wipe out the PRD_tree object.
self.PRD_tree = None
return

#Merge the list of elements in the PRD UVM change directory with
# config.
if (prd_change_dir != None):
self.add_change_dir (branch_tree, prd_change_dir)

# Merge the list of new elements with the config (and change) tree.
self.new_elements = []
if (self.file_list != None):
self.add_new_elements (self.file_list)

def get_PRD_file_data (self):
""" Fetches all the elements that make up a PRD file.
Returns a prd_file_data object or None.
"""
if (self.PRD_tree == None):
print "\n *** Error - build_PRD_file class not initialised", \
"correctly for", self.prd_file_type, "file."
return None

# Generate a sorted list of elements in this file.
# No more than one of the parameters passed into get_element_list will
# have a non-null value (both parameters can be set to None)
element_list = self.PRD_tree.get_element_list(self.collection_id,\
self.tag_id)

if (len(element_list) > 0) :

# Fetch a copy of each UVM element into file_list and store all
# the fetch messages in a log.
PRD_file_data, log = self.PRD_fetch_obj.fetch_elements(element_list)

# Flush the output buffers (for log file use.)
sys.stdout.flush()
sys.stdout.flush()

# Create a PRD file data object from this data.
self.PRD_file_obj = prd_file_data.prd_file_data(self.prd_file_type, \
PRD_file_data, log)

# Delete the temporary RCS libraries for the new input files.
if (len (self.new_elements) > 0):
self.new_update_dir.del_tmpdir()

return self.PRD_file_obj

else:
print "\n No elements found for file type: ", self.prd_file_type,\
"\n in directory: ", prd_root_dir

return None

def fetch_changed_elements (self):
""" Fetch to disk a copy of all the configured changed elements.
Note that the changed_element_list is structured the following
way:
Each element is a list of data for a subsystem of the file type.
So for plcp, there's an astrom list, a stis list....Each of the
sublists has a list of files for the first element and a tag id for
the second.
"""

if (self.change_rcs_file_list != None):
rlib = rcslib.RCS()
for subsys_element in self.change_rcs_file_list:
subsys_file_list = subsys_element[0]
for file_element in subsys_file_list:
rlib.fetch (file_element)

# Flush the output buffers (for log file use.)
sys.stdout.flush()
sys.stderr.flush()

# -------------- Run time interface ------------------------------------

def run (prd_root_dir=None, prd_file_type=None, PRDUVM_branch_name=None, \
change_tree_root_dir=None, number_lines='n', \
collection_id=None, tag_id=None, *args):
"""Build a PRD file from the latest version elements in the PRDUVM libraries.

Usage:
do build_PRD_file


where:
PRD PRDUVM root = Top level directory to scan for elements.
PRD Type = PRD file type (PLCP, TFPF, PTLD, ...).
Lib Ver = Library Version (OPS or SM4).
Change root = Top level directory to scan for change
directory elements [Optional parameter].
Number = Number PRD file lines (Y|N:N)
[Optional parameter].
Collection Id = PRD collection id ('') [Optional parameter].
Tag Id = UVM library tag ('') [Optional parameter].
"""

if ((not prd_root_dir) or (not prd_file_type) or (not PRDUVM_branch_name)):
# Spew out the usage and quit when no initial directory
# is provided.
print run.__doc__
return

build_file = build_PRD_file(prd_root_dir, prd_file_type,
PRDUVM_branch_name, change_tree_root_dir,
None, collection_id, tag_id)
# Fetch the elements.
new_file = build_file.get_PRD_file_data()

# Number the lines and update the FD record.
if (number_lines == 'y'):
new_file.number_PRD_ele_list()
else:
new_file.pad_PRD_ele_list()

# Create the new PDB file.
new_file.write_file()


if __name__ == '__main__':

if (len(sys.argv) <= 3):
print run.__doc__
sys.exit(1)

prd_root_dir = sys.argv[1]
prd_file_type = string.lower(sys.argv[2])
PRDUVM_branch_name = string.lower(sys.argv[3])

#check for any nonblank in the string
nonblank = re.compile ("\S")

change_tree_root_dir = None
if (len(sys.argv) >= 5):
change_tree_root_dir = sys.argv[4]
if (nonblank.match(change_tree_root_dir) == None):
change_tree_root_dir = None

number_lines = "n"
if (len(sys.argv) >= 6):
number_lines = string.lower(sys.argv[5])

if (len(sys.argv) >= 7):
collection_id = sys.argv[6]
if (nonblank.match(collection_id) == None):
collection_id = None
else:
collection_id = None

if (len(sys.argv) >= 8):
tag_id = sys.argv[7]
if (nonblank.match(tag_id) == None):
tag_id = None
else:
tag_id = None

build_file = build_PRD_file(prd_root_dir, prd_file_type,
PRDUVM_branch_name, change_tree_root_dir,
None, collection_id, tag_id)
# Fetch the elements.
new_file = build_file.get_PRD_file_data()

# Number the lines and update the FD record.
if (number_lines == 'y'):
new_file.number_PRD_ele_list()
else:
new_file.pad_PRD_ele_list()

# Create the new PDB file.
new_file.write_file()