#!/usr/bin/env python
#
# (C) 2016 Ed Bueler
#
# This script puts only the "meat" of codes into new files in cstrip/, for
# inclusion into the book.
#
# We remove "PetscErrorCode ierr" lines and "//STRIP" lines and
# characters "ierr = " and "CHKERRQ(ierr);"

import os
import re

# filenames should be distinct
files = {2 : "vecmatksp.c tri.c",
         3 : "poisson.c",
         4 : "expcircle.c ecjacobian.c reaction.c",
         5 : "plap.c",
         6 : "ode.c heat.c",
         7 : "ad3.c",
         10: "readmesh.c poissontools.c poissonfem.c",
         13: "obstacle.c"}

if not os.path.exists("cstrip/"):
    os.makedirs("cstrip/")
else:
    print "WARNING:  cstrip/ exists"

#FIXME  no actual need for .tmp names; we are already copying

# use for loop to read all values and indexes
csfiles = []
for chapt, chfiles in files.items():
    flist = chfiles.split()
    for fname in flist:
        src = '../c/ch' + str(chapt) + '/' + fname
        destroot = 'cstrip/' + fname
        csfiles.append(destroot)
        dest = destroot + '.tmp'
        print '  copying  %s to  %s' % (src.ljust(30),dest.ljust(30))
        os.system('cp %s %s' % (src,dest))

for name in csfiles:
    f = open(name + '.tmp','r')
    newf = open(name,'w')
    for line in f:
        if re.search('\/\/STRIP',line) or re.search('PetscErrorCode ierr',line):
            continue
        line = re.sub('ierr = ','', line).rstrip()
        line = re.sub('CHKERRQ\(ierr\);','', line).rstrip()
        newf.write(line + '\n')
    f.close()
    newf.close()


@@ -156,16 +156,41 @@
  remove 
+entire lines with either 
 %22PetscEr
@@ -203,25 +203,18 @@
 e ierr%22 
-lines and
+or
  %22//STRI
@@ -225,24 +225,21 @@
 ines
- and%0A# character
+.%0A# We string
 s %22i
@@ -266,16 +266,31 @@
 (ierr);%22
+ from any line.
 %0A%0Aimport
@@ -496,16 +496,26 @@
 c heat.c
+ pattern.c
 %22,%0A     
@@ -679,173 +679,8 @@
 /%22)%0A
-else:%0A    print %22WARNING:  cstrip/ exists%22%0A%0A#FIXME  no actual need for .tmp names; we are already copying%0A%0A# use for loop to read all values and indexes%0Acsfiles = %5B%5D
 %0Afor
@@ -687,23 +687,24 @@
  chapt, 
-ch
 files
+str
  in file
@@ -730,15 +730,16 @@
 t = 
-ch
 files
+str
 .spl
@@ -780,13 +780,17 @@
    s
-rc
+f
  = 
+open(
 '../
@@ -825,305 +825,103 @@
 name
-%0A        destroot = 'cstrip/' + fname%0A        csfiles.append(destroot)%0A        dest = destroot + '.tmp'%0A        print '  copying  %25s to  %25s' %25 (src.ljust(30),dest.ljust(30))%0A        os.system('cp %25s %25s' %25 (src,dest))%0A%0Afor name in csfiles:%0A    f = open(name + '.tmp','r')%0A    newf = open(name,'w')%0A
+,'r')  # source%0A        df = open('cstrip/' + fname,'w')                     # destination%0A    
     
@@ -936,11 +936,16 @@
  in 
+s
 f:%0A
+    
     
@@ -1021,16 +1021,20 @@
 ,line):%0A
+    
         
@@ -1046,32 +1046,36 @@
 ontinue%0A        
+    
 line = re.sub('i
@@ -1093,32 +1093,36 @@
  line).rstrip()%0A
+    
         line = r
@@ -1173,19 +1173,21 @@
         
-new
+    d
 f.write(
@@ -1203,16 +1203,21 @@
 n')%0A    
+    s
 f.close(
@@ -1222,19 +1222,21 @@
 e()%0A    
-new
+    d
 f.close(

Clean and simplify strip script.