builder.py 1.5 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 = '\n'.join([line.lstrip() for line in
  8. config.splitlines()]) + '\n'
  9. self.builddir = builddir
  10. self.logfile = infra.open_log_file(builddir, "build", logtofile)
  11. def build(self):
  12. if not os.path.isdir(self.builddir):
  13. os.makedirs(self.builddir)
  14. config_file = os.path.join(self.builddir, ".config")
  15. with open(config_file, "w+") as cf:
  16. cf.write(self.config)
  17. # dump the defconfig to the logfile for easy debugging
  18. self.logfile.write("> start defconfig\n" + self.config +
  19. "> end defconfig\n")
  20. self.logfile.flush()
  21. cmd = ["make",
  22. "O={}".format(self.builddir),
  23. "olddefconfig"]
  24. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile)
  25. if ret != 0:
  26. raise SystemError("Cannot olddefconfig")
  27. cmd = ["make", "-C", self.builddir]
  28. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile)
  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)