HowTo: Determine 3DBM and GSM Projects

Version: 16.2

Table of Contents

Preface

To illustrate the ability to query the Project Control File (PCF), this HowTo will discuss iterating across a set of directories and reporting whether or not the project is a GSM or 3DBM.

Getting Started

The following script will iterate through the given directory (defaulting to the current working directory). Open every PCF, and report whether or not that PCF is a GSM or 3DBM file. The original source can be found at rptpcftype.py.

import fnmatch
import os
import sys

from grail.data import pcf


def _walk(arg, dirname, fnames):
    
# Only consider files that pass this filter. Note that files like
    
# rpt110.rpt will pass, but they will have trouble loading later.
    
filter = "*10.*"
    
dats = fnmatch.filter(fnames, filter)
    
for d in dats:
        
path = os.path.join(dirname, d)
        
try:
            
# If the file was not a PCF file, then we will get a
            
# pcf.PCFError, that we catch and ignore.
            
p = pcf.Pcf(path)
            
print path,
            
if p.isgsm():
                
print "is GSM!"
            
else:
                
print "is 3DBM!"
        
except pcf.PcfError:
            
# Not that important, just keep iterating through the files.
            
pass


if __name__=="__main__":
   
# See if there is an argument to this program. If not then default to
   
# the current working directory.
   
try:
       
startDir = sys.argv[1]
   
except IndexError:
       
# Oh well, you didn't supply an argument. We will default to the
       
# current working directory.
       
startDir = os.getcwd()

   
print "walking through ", startDir
   
os.path.walk(startDir, _walk, None)

In this script we use the os.path.walk [1] function from Python to iterate through all the directories starting at the given startDir, and attempt to open up any file that matches the "*10.*" pattern"

Reference

[1]"os.path" Python Library Reference. 21 December 2001. 23 November 2007. <http://www.python.org/doc/2.2/lib/module-os.path.html#l2h-1290>