def save(self): for log in self.cache: if not self.changed.get(log, True): continue log_path = self._get_absolute_log_path(log) if os.path.basename(log).startswith("traceback"): mode = 0o600 else: mode = 0o644 save_to_file(log_path, self.cache[log], mode=mode) self.changed[log] = False
def test_save_to_file(self): save_to_file(self.tmp_file, "foo") self.assertEqual("\n".join(read_from_file(self.tmp_file)), "foo") save_to_file(self.tmp_file, "\nbar", append=True, mode=600) self.assertEqual("\n".join(read_from_file(self.tmp_file)), "foo\nbar") # append doesn't modify existing perms self.assertEqual(os.stat(self.tmp_file).st_mode & 0o777, 0o644) os.unlink(self.tmp_file) save_to_file(self.tmp_file, "foo", append=True, mode=0o600) self.assertEqual(os.stat(self.tmp_file).st_mode & 0o777, 0o600)
def setUp(self): self.tmp_dir = tempfile.mkdtemp() self.tmp_file = os.path.join(self.tmp_dir, "tmp_file") save_to_file(self.tmp_file, "test")