def test_simple(self): # with self.DIR: tracer = FileTracer() # tracer.clear() @tracer.cache def main(): dumpOutput(InputFile('input1.html')) tracer.run(main) # print(self.) # assert tracer.fileSetByFunc)main].output_files == {OutputFile(u'input1.html.count').addTimeStamp()},tracer.fileSetByFunc[main].output_files assert list(tracer.byFunc[main].values())[0][0].output_files == { OutputFile(u'input1.html.count').addTimeStamp() }, tracer.fileSetByFunc[main].output_files assert list(tracer.byFunc[main].values())[0][0].input_files == { InputFile(u'input1.html').addTimeStamp() }, tracer.fileSetByFunc[main].input_files # assert tracer.output_files == {OutputFile(u'input1.html.count').addTimeStamp()},tracer.output_files # assert tracer.input_files == {InputFile(u'input1.html').addTimeStamp()},tracer.input_files tracer.dump_to_file(strict=1) tracer.clear() assert main not in tracer.byFunc
def test_loaded_run(self): print('-' * 100) tracerString, dumpOutput, main = self.test_log() #### pass pickle if file not read # tracer = dill.loads(tracerString) tracer = FileTracer() ## start from fresh tracer.clear() dumpOutput = dumpOutput._origin dumpOutput = tracer.cache(dumpOutput) with self.assertLogs('file_tracer', level='INFO') as logs: tracer.run(main) # main() regs = [ 'USING_CACHE', 'USING_CACHE', 'RECALC', 'USING_CACHE', 'RECALC', ] for logText, reg in zip(logs.output, regs): pass print(logText) self.assertRegex(logText, reg) # assert 0 # main() # main() # tracer.run(main) pass
def test_log(self): tracer = FileTracer() tracer.clear() # with self.DIR: @tracer.cache def dumpOutput(x): s = middleStep(x) d = collections.Counter(s) fn = OutputFile(x + '.count') fo = [ InputFile('input2.html'), ] # fo = (InputFile('input2.html'),) with open(('input2.html'), 'r') as f: pass # with open with open(fn, 'w') as f: map(f.write, [str(x) for x in d.items()]) # assert 0 # @tracer.cache def main(): print('[1]') dumpOutput(InputFile('input1.html')) time.sleep(0.02) print('[2]') dumpOutput(InputFile('input1.html')) time.sleep(0.02) with open('input2.html', 'w') as f: f.write('test111111111') # assert 0 print('[3]') dumpOutput(InputFile('input1.html')) print('[4]') dumpOutput(InputFile('input1.html')) print('[5]') dumpOutput(InputFile('input2.html')) tracer.DEBUG = 0 with self.assertLogs('file_tracer', level='INFO') as logs: tracer.run(main) # main() # logs.output regs = [ 'FIRST_RUN', 'USING_CACHE', 'RECALC', 'USING_CACHE', 'FIRST_RUN', ] for logText, reg in zip(logs.output, regs): pass print(logText) self.assertRegex(logText, reg) # return dill.dumps(tracer), dumpOutput, main return (tracer), dumpOutput, main
def test_tamp(self): # with self.DIR: tracer = FileTracer() v = InputFile('input1.html') v.addTimeStamp() time.sleep(0.02) vout = InputFile(v) vout.addTimeStamp() assert (v.stamp == vout.stamp), (v.stamp, vout.stamp) time.sleep(0.02) with open(v, 'w') as f: f.write('test111111111') vout = type(v)(v) vout.addTimeStamp() assert (v.stamp != vout.stamp), (v.stamp, vout.stamp)
import unittest2 import path from file_tracer import FileTracer, OutputFile tracer = FileTracer(DEBUG=2) # TEMPDIR = path.Path(__file__ +'.build.temp').makedirs_p().realpath() class SharedObject(object): DIR = path.Path(__file__ + '.build.temp').makedirs_p().realpath() pass @tracer.cache def job1(num): print('RUNNING', job1.__name__, num) with open(OutputFile(SharedObject.DIR / '%s.txt.temp' % num), 'w') as f: f.write('a' * num) return num # 6080328209477545137, # 5896892666927026437, # -1130546980651876245, # 3527539, # 3430019387558, # 3527539:3527539 # 6080328209477545137, # 5871464517938808325, # -1130546980651876245, # 3527539,
from file_tracer import FileTracer, InputFile, OutputFile, tree_as_string from collections import Counter tracer = FileTracer() import time import dill v = InputFile('input1.html') v.addTimeStamp() s = dill.dumps(v) time.sleep(0.02) vout = dill.loads(s) vout.addTimeStamp() assert (v.stamp == vout.stamp), (v.stamp, vout.stamp) time.sleep(0.02) with open(v, 'w') as f: f.write('test111111111') vout = type(v)(v) vout.addTimeStamp() assert (v.stamp != vout.stamp), (v.stamp, vout.stamp) print v.stamp print vout.stamp # stat_result.st_mtime print(hash(v)) v.addTimeStamp() print(hash(v)) print(hash(vout)) print(v == vout)
import collections import time from file_tracer import InputFile, OutputFile, FileTracer from path import Path tracer = FileTracer() with Path('build').makedirs_p(): with open(InputFile('input1.html'), 'w') as f: f.write('123') with open(InputFile('input2.html'), 'w') as f: f.write('abc') def readInput(x): with open((x), 'r') as f: return f.read() @tracer.cache def dumpOutput(x): s = readInput(x) d = collections.Counter(s) fn = OutputFile(x + '.count') ### merely referencing the file is enough to log dependency with open(InputFile('input2.html'), 'r') as f: pass with open(fn, 'w') as f: map(f.write, [str(x) for x in d.items()]) def main(): dumpOutput(InputFile('input1.html')) ## first_run time.sleep(0.02) dumpOutput(InputFile('input1.html')) ## using_cache