From b98ff23cd94b01382aad0c01e138fd752d4ea3f3 Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Tue, 11 Jun 2019 16:38:50 +0200 Subject: [PATCH] [IMP] runbot: add write_file and make_dirs methods When using a "python job", it's sometimes useful to write a file or create a directory. Instead of giving a wide open access to the os module in the "_run_python" context, this commit adds a write_file and make_dirs methods on the build which is usable in the _run_python eval context. --- runbot/models/build.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/runbot/models/build.py b/runbot/models/build.py index 70bad943..36a44c86 100644 --- a/runbot/models/build.py +++ b/runbot/models/build.py @@ -899,6 +899,25 @@ class runbot_build(models.Model): self._log('readfile', 'exception: %s' % e) return False + def write_file(self, file, data, mode='w'): + file_path = self._path(file) + file_dir = os.path.split(file_path)[0] + os.makedirs(file_dir, exist_ok=True) + try: + with open(file_path, mode) as f: + f.write(data) + except Exception as e: + self._log('write_file', 'exception: %s' % e) + return False + + def make_dirs(self, dir_path): + full_path = self._path(dir_path) + try: + os.makedirs(full_path, exist_ok=True) + except Exception as e: + self._log('make_dirs', 'exception: %s' % e) + return False + def build_type_label(self): self.ensure_one() return dict(self.fields_get('build_type', 'selection')['build_type']['selection']).get(self.build_type, self.build_type)