builder.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. # dump the defconfig to the logfile for easy debugging
  17. self.logfile.write("> start defconfig\n" + self.config +
  18. "> end defconfig\n")
  19. self.logfile.flush()
  20. cmd = ["make",
  21. "O={}".format(self.builddir),
  22. "olddefconfig"]
  23. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile)
  24. if ret != 0:
  25. raise SystemError("Cannot olddefconfig")
  26. cmd = ["make", "-C", self.builddir]
  27. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile)
  28. if ret != 0:
  29. raise SystemError("Build failed")
  30. open(self.stamp_path(), 'a').close()
  31. def stamp_path(self):
  32. return os.path.join(self.builddir, "build-done")
  33. def is_finished(self):
  34. return os.path.exists(self.stamp_path())
  35. def delete(self):
  36. if os.path.exists(self.builddir):
  37. shutil.rmtree(self.builddir)