builder.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 configure(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. def build(self):
  30. env = {"PATH": os.environ["PATH"]}
  31. if "http_proxy" in os.environ:
  32. self.logfile.write("Using system proxy: " +
  33. os.environ["http_proxy"] + "\n")
  34. self.logfile.flush()
  35. env['http_proxy'] = os.environ["http_proxy"]
  36. env['https_proxy'] = os.environ["http_proxy"]
  37. cmd = ["make", "-C", self.builddir]
  38. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile,
  39. env=env)
  40. if ret != 0:
  41. raise SystemError("Build failed")
  42. open(self.stamp_path(), 'a').close()
  43. def stamp_path(self):
  44. return os.path.join(self.builddir, "build-done")
  45. def is_finished(self):
  46. return os.path.exists(self.stamp_path())
  47. def delete(self):
  48. if os.path.exists(self.builddir):
  49. shutil.rmtree(self.builddir)