1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#line 233 "interscript/src/sink_drivers.ipk"
import string
from interscript.drivers.sinks.base import sink_base
from interscript.drivers.sinks import sink_open_error
class named_file_sink(sink_base):
def __init__(self,pass_frame,input_filename, prefix='', eol='\n'):
self.pass_frame = pass_frame
self.process = pass_frame.process
self.site = self.process.site
self.platform = self.site.platform
if 'sinks' in self.process.trace:
self.process.acquire_object(self,'NAMED FILE SINK '+input_filename)
self.eol = eol
# compute absolute pathname, and create directories if necessary
# we don't use posixpath because we're enforcing an _interscript_
# pathname convention here
pathlist = string.split(input_filename,'/')
self.basename = pathlist[-1]
pathname = self.platform.mk_dir(prefix, pathlist)
if self.platform.file_exists(pathname):
self.tmp_filename = self.platform.tempfile.mktemp()
if 'sinks' in self.process.trace:
print 'Generating temporary',self.tmp_filename,'for',input_filename
try:
file =self.platform.open(self.tmp_filename,'w')
self.pass_frame.fdict[input_filename]='temporary'
except:
raise sink_open_error, self.tmp_filename
sink_base.__init__(self, filename = pathname, name = input_filename, file = file )
if pass_frame: self.pass_frame.flist.append(input_filename)
else:
if 'sinks' in self.process.trace:
print 'Generating original',input_filename
try:
file = self.platform.open(pathname,'w')
if pass_frame: self.pass_frame.fdict[input_filename]='original'
except:
raise sink_open_error,pathname
sink_base.__init__(self, filename = pathname, name = input_filename, file = file)
if pass_frame: self.pass_frame.flist.append(input_filename)
def __del__(self):
pass_frame = self.__dict__.get('pass_frame',None)
if 'sinks' in self.process.trace:
print 'closing', self.name
self.file.close()
if hasattr(self,'tmp_filename'):
if self.process.update_files:
original_file = self.platform.open(self.filename,'r')
original_lines = original_file.readlines()
original_file.close()
new_file = self.platform.open(self.tmp_filename,'r')
new_lines = new_file.readlines()
new_file.close()
if not original_lines == new_lines:
if 'changes' in self.process.trace:
print 'File',self.filename,'is CHANGED'
if pass_frame:
self.pass_frame.fdict[self.name]='changed'
file = self.platform.open(self.filename,'w')
file.writelines(new_lines)
file.close()
else:
if 'changes' in self.process.trace:
print 'File',self.filename,'is unchanged'
if pass_frame: self.pass_frame.fdict[self.name]='unchanged'
else:
print '*** System error inhibiting file update for',self.filename,'***'
if pass_frame: self.pass_frame.fdict[self.name]='cancelled'
self.platform.os.remove(self.tmp_filename)
else:
if 'changes' in self.process.trace:
print 'File',self.filename,'is NEW'
if hasattr(self,'process') and 'sinks' in self.process.trace:
self.process.release_object(self)
def raw_write(self,line): self.file.write(line)
def raw_eol(self): self.raw_write(self.eol)
|