builder.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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.logfile = infra.open_log_file(builddir, "build", logtofile)
  10. def build(self):
  11. if not os.path.isdir(self.builddir):
  12. os.makedirs(self.builddir)
  13. config_file = os.path.join(self.builddir, ".config")
  14. with open(config_file, "w+") as cf:
  15. cf.write(self.config)
  16. cmd = ["make",
  17. "O={}".format(self.builddir),
  18. "olddefconfig"]
  19. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile)
  20. if ret != 0:
  21. raise SystemError("Cannot olddefconfig")
  22. cmd = ["make", "-C", self.builddir]
  23. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile)
  24. if ret != 0:
  25. raise SystemError("Build failed")
  26. open(self.stamp_path(), 'a').close()
  27. def stamp_path(self):
  28. return os.path.join(self.builddir, "build-done")
  29. def is_finished(self):
  30. return os.path.exists(self.stamp_path())
  31. def delete(self):
  32. if os.path.exists(self.builddir):
  33. shutil.rmtree(self.builddir)