AttackContext.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import tempfile
  2. class AttackContext:
  3. def __init__(self, out_dir):
  4. # files allocated by attacks, intended to get copied next to the pcap
  5. # the keys are the suffix for later use, e.g. "_extra_info.txt"
  6. # the values are the respectable file names
  7. self.allocated_files = {}
  8. self.other_created_files = set()
  9. self.out_dir = out_dir
  10. def allocate_file(self, suffix):
  11. if suffix in self.allocated_files:
  12. raise ValueError("File with suffix %s is already allocated" % suffix)
  13. file = tempfile.NamedTemporaryFile(mode = "w", suffix = suffix, delete = False)
  14. self.allocated_files[suffix] = file.name
  15. return file
  16. def add_other_created_file(self, filepath):
  17. self.other_created_files.add(filepath)
  18. def get_other_created_files(self):
  19. return sorted(self.other_created_files)
  20. def reset(self):
  21. self.allocated_files.clear()
  22. def get_allocated_files(self):
  23. return_ = list(self.allocated_files.items())
  24. self.allocated_files.clear()
  25. return return_
  26. def get_output_dir(self):
  27. return self.out_dir