builder.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import os
  2. import shutil
  3. import subprocess
  4. import infra
  5. class Builder(object):
  6. def __init__(self, config, builddir, logtofile):
  7. self.config = config
  8. self.builddir = builddir
  9. self.logtofile = logtofile
  10. def build(self):
  11. if not os.path.isdir(self.builddir):
  12. os.makedirs(self.builddir)
  13. log = "{}-build.log".format(self.builddir)
  14. if not self.logtofile:
  15. log = None
  16. config_file = os.path.join(self.builddir, ".config")
  17. with open(config_file, "w+") as cf:
  18. cf.write(self.config)
  19. cmd = ["make",
  20. "O={}".format(self.builddir),
  21. "olddefconfig"]
  22. with infra.smart_open(log) as log_fh:
  23. ret = subprocess.call(cmd, stdout=log_fh, stderr=log_fh)
  24. if ret != 0:
  25. raise SystemError("Cannot olddefconfig")
  26. cmd = ["make", "-C", self.builddir]
  27. with infra.smart_open(log) as log_fh:
  28. ret = subprocess.call(cmd, stdout=log_fh, stderr=log_fh)
  29. if ret != 0:
  30. raise SystemError("Build failed")
  31. open(self.stamp_path(), 'a').close()
  32. def stamp_path(self):
  33. return os.path.join(self.builddir, "build-done")
  34. def is_finished(self):
  35. return os.path.exists(self.stamp_path())
  36. def delete(self):
  37. if os.path.exists(self.builddir):
  38. shutil.rmtree(self.builddir)