def tearDown(self): try: for p in Path('.').glob('lib*'): p.unlink() Path('myprogram').unlink() except: pass
def test_mod_times(self): ComputeSum().make() file = 'item_5_price.txt' mod_time1 = Path(file).stat().st_mtime_ns time.sleep(1) with open(file, 'w', encoding='UTF-8') as f: f.write('150') mod_time2 = Path(file).stat().st_mtime_ns self.assertGreater(mod_time2, mod_time1)
class CreateProgram(Task): def __init__(self, prog_file): self._prog_file = Path(prog_file) self.trigger_exception_if_buit = False def target(self): return self._prog_file def build(self, lib1_obj, lib2_obj, lib3_obj=None): if self.trigger_exception_if_buit: raise Exception('Sould not be rebuilt') # os.system('gcc lib1.o lib2.o -o myprogram') assert lib1_obj.exists() assert lib2_obj.exists() if lib3_obj: assert lib3_obj.exists() self._prog_file.touch()
def build(self, *tax_files): sum_ = 0 for file in tax_files: with file.open(encoding='UTF-8') as f: sum_ += int(f.read()) with Path('total.txt').open('w', encoding='UTF-8') as f: f.write('%d' % sum_)
class CreateItem(Task): def __init__(self, num): self._output_path = Path('item_%d_price.txt' % num) def target(self): return self._output_path def build(self): with self._output_path.open('w', encoding='UTF-8') as f: f.write('100')
class AddTaxToItem(Task): def __init__(self, num): self._output_path = Path('item_%d_tax.txt' % num) def target(self): return self._output_path def build(self, input_path): with input_path.open(encoding='UTF-8') as f: price = int(f.read()) with self._output_path.open('w', encoding='UTF-8') as f: f.write('%d' % int(price * 1.3))
def setUp(self): Path('lib1.c').touch() Path('lib2.c').touch() Path('lib3.c').touch() create_lib1 = CreateObjFile('lib1') << Path('lib1.c') create_lib2 = CreateObjFile('lib1') << Path('lib2.c') create_lib3 = CreateObjFile('lib3') << Path('lib3.c') self.create_prog1 = CreateProgram('myprogram') << create_lib1 + \ create_lib2 self.create_prog2 = CreateProgram('myprogram') << create_lib1 + \ create_lib2 + \ create_lib3
def test_compile_with_three_dependencies(self): self.create_prog2.make() self.assertTrue(Path('myprogram').exists())
def test_recompile_from_scratch(self): self.create_prog1.make() self.assertTrue(Path('myprogram').exists()) time.sleep(1.1) self.create_prog1.trigger_exception_if_buit = True self.create_prog1.make()
def test_compile_from_scratch(self): self.create_prog1.make() self.assertTrue(Path('myprogram').exists())
def __init__(self, destination): self._destination = Path(destination)
def __init__(self, prog_file): self._prog_file = Path(prog_file) self.trigger_exception_if_buit = False
def target(self): return Path(self._decompressed)
def __init__(self, num): self._output_path = Path('item_%d_price.txt' % num)
def tearDown(self): for f in Path('.').glob('*.txt'): f.unlink()
def target(self): return Path('total.txt')
def target(self): return Path(self.filename + '.o')
def build(self, c_file): # os.system('gcc -c %s.o -o %s.o'%c_file) assert c_file.exists() Path(self.filename + '.o').touch()
def target(self): return Path(self._destination)
def build(self, compressed): self.debug('Decompressing %s to %s' % (compressed, self._decompressed)) with tarfile.open(str(compressed)) as f: f.extractall(str(Path(self._decompressed).parent)) assert Path(self._decompressed).exists() Path(self._decompressed).touch()