builder.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. env = {"PATH": os.environ["PATH"]}
  22. cmd = ["make",
  23. "O={}".format(self.builddir),
  24. "olddefconfig"]
  25. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile,
  26. env=env)
  27. if ret != 0:
  28. raise SystemError("Cannot olddefconfig")
  29. cmd = ["make", "-C", self.builddir]
  30. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile,
  31. env=env)
  32. if ret != 0:
  33. raise SystemError("Build failed")
  34. open(self.stamp_path(), 'a').close()
  35. def stamp_path(self):
  36. return os.path.join(self.builddir, "build-done")
  37. def is_finished(self):
  38. return os.path.exists(self.stamp_path())
  39. def delete(self):
  40. if os.path.exists(self.builddir):
  41. shutil.rmtree(self.builddir)