====================================== HowTo: Determine 3DBM and GSM Projects ====================================== .. include:: ..\version.h .. include:: howto-reference.h .. contents:: Table of Contents :backlinks: top .. The steps for creating a howto are, .. .. 1. Copy the howto-boilerplate to the desired filename (X). .. 2. Update the index-howto.txt file to have a reference to X. .. 3. Update the howto-reference.h file with the appropriate .. reference. ------- 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`__. .. __: rptpcftype.py .. Python:: 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 :f:`os.path.walk` [#os.path.walk]_ function from Python to iterate through all the directories starting at the given :a:`startDir`, and attempt to open up any file that matches the "\*10.\*" pattern" --------- Reference --------- .. [#os.path.walk] "os.path" *Python Library Reference*. 21 December 2001. 23 November 2007.