build_catboost.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import sys
  2. import os
  3. import shutil
  4. import subprocess
  5. def get_value(val):
  6. dct = val.split('=', 1)
  7. if len(dct) > 1:
  8. return dct[1]
  9. return ''
  10. class BuildCbBase(object):
  11. def run(self, cbmodel, cbname, cb_cpp_path):
  12. data_prefix = "CB_External_"
  13. data = data_prefix + cbname
  14. datasize = data + "Size"
  15. cbtype = "const NCatboostCalcer::TCatboostCalcer"
  16. cbload = "(ReadModel({0}, {1}, EModelType::CatboostBinary))".format(data, datasize)
  17. cb_cpp_tmp_path = cb_cpp_path + ".tmp"
  18. cb_cpp_tmp = open(cb_cpp_tmp_path, 'w')
  19. cb_cpp_tmp.write("#include <kernel/catboost/catboost_calcer.h>\n")
  20. ro_data_path = os.path.dirname(cb_cpp_path) + "/" + data_prefix + cbname + ".rodata"
  21. cb_cpp_tmp.write("namespace{\n")
  22. cb_cpp_tmp.write(" extern \"C\" {\n")
  23. cb_cpp_tmp.write(" extern const unsigned char {1}{0}[];\n".format(cbname, data_prefix))
  24. cb_cpp_tmp.write(" extern const ui32 {1}{0}Size;\n".format(cbname, data_prefix))
  25. cb_cpp_tmp.write(" }\n")
  26. cb_cpp_tmp.write("}\n")
  27. archiverCall = subprocess.Popen(
  28. [self.archiver, "-q", "-p", "-o", ro_data_path, cbmodel], stdout=None, stderr=subprocess.PIPE
  29. )
  30. archiverCall.wait()
  31. cb_cpp_tmp.write("extern {0} {1};\n".format(cbtype, cbname))
  32. cb_cpp_tmp.write("{0} {1}{2};".format(cbtype, cbname, cbload))
  33. cb_cpp_tmp.close()
  34. shutil.move(cb_cpp_tmp_path, cb_cpp_path)
  35. class BuildCb(BuildCbBase):
  36. def run(self, argv):
  37. if len(argv) < 5:
  38. print >> sys.stderr, "BuildCb.Run(<ARCADIA_ROOT> <archiver> <mninfo> <mnname> <cppOutput> [params...])"
  39. sys.exit(1)
  40. self.SrcRoot = argv[0]
  41. self.archiver = argv[1]
  42. cbmodel = argv[2]
  43. cbname = argv[3]
  44. cb_cpp_path = argv[4]
  45. super(BuildCb, self).run(cbmodel, cbname, cb_cpp_path)
  46. def build_cb_f(argv):
  47. build_cb = BuildCb()
  48. build_cb.run(argv)
  49. if __name__ == '__main__':
  50. if len(sys.argv) < 2:
  51. print >> sys.stderr, "Usage: build_cb.py <funcName> <args...>"
  52. sys.exit(1)
  53. if sys.argv[2:]:
  54. globals()[sys.argv[1]](sys.argv[2:])
  55. else:
  56. globals()[sys.argv[1]]()