def MakeDeployableBinary(self, template_path, output_path=None):
    """This will add the config to the client template and create a .rpm."""
    if output_path is None:
      output_path = config_lib.CONFIG.Get("ClientBuilder.output_path",
                                          context=self.context)

    rpmbuild_binary = "/usr/bin/rpmbuild"
    if not os.path.exists(rpmbuild_binary):
      logging.error("rpmbuild not found, unable to repack client.")
      return

    with utils.TempDirectory() as tmp_dir:
      template_dir = os.path.join(tmp_dir, "dist")
      utils.EnsureDirExists(template_dir)

      zf = zipfile.ZipFile(template_path)
      for name in zf.namelist():
        if os.path.basename(name) == "build.yaml":
          self.MergeBuildConfig(data=zf.read(name))
          continue
        dirname = os.path.dirname(name)
        utils.EnsureDirExists(os.path.join(template_dir, dirname))
        with open(os.path.join(template_dir, name), "wb") as fd:
          fd.write(zf.read(name))

      # Set up a RPM building environment.

      rpm_root_dir = os.path.join(tmp_dir, "rpmbuild")

      rpm_build_dir = os.path.join(rpm_root_dir, "BUILD")
      utils.EnsureDirExists(rpm_build_dir)

      rpm_buildroot_dir = os.path.join(rpm_root_dir, "BUILDROOT")
      utils.EnsureDirExists(rpm_buildroot_dir)

      rpm_rpms_dir = os.path.join(rpm_root_dir, "RPMS")
      utils.EnsureDirExists(rpm_rpms_dir)

      rpm_specs_dir = os.path.join(rpm_root_dir, "SPECS")
      utils.EnsureDirExists(rpm_specs_dir)

      template_binary_dir = os.path.join(
          tmp_dir, "dist/rpmbuild/grr-client")

      target_binary_dir = "%s%s" % (
          rpm_build_dir, config_lib.CONFIG.Get("ClientBuilder.target_dir",
                                               context=self.context))

      utils.EnsureDirExists(os.path.dirname(target_binary_dir))
      try:
        shutil.rmtree(target_binary_dir)
      except OSError:
        pass
      shutil.move(template_binary_dir, target_binary_dir)
      client_name = config_lib.CONFIG.Get("Client.name", context=self.context)
      client_binary_name = config_lib.CONFIG.Get("Client.binary_name",
                                                 context=self.context)
      if client_binary_name != "grr-client":
        shutil.move(os.path.join(target_binary_dir, "grr-client"),
                    os.path.join(target_binary_dir, client_binary_name))

      spec_filename = os.path.join(rpm_specs_dir, "%s.spec" %
                                   client_name)
      self.GenerateFile(os.path.join(
          tmp_dir, "dist/rpmbuild/grr.spec.in"), spec_filename)

      initd_target_filename = os.path.join(
          rpm_build_dir, "etc/init.d", client_name)

      utils.EnsureDirExists(os.path.dirname(initd_target_filename))
      self.GenerateFile(
          os.path.join(tmp_dir, "dist/rpmbuild/grr-client.initd.in"),
          initd_target_filename)

      # Create a client config.
      client_context = ["Client Context"] + self.context
      client_config_content = self.GetClientConfig(client_context)

      with open(os.path.join(target_binary_dir, config_lib.CONFIG.Get(
          "ClientBuilder.config_filename", context=self.context)), "wb") as fd:
        fd.write(client_config_content)

      # Undo all prelinking for libs or the rpm might have checksum mismatches.
      logging.info("Undoing prelinking.")
      prelink = "/usr/sbin/prelink"
      if os.access(prelink, os.X_OK):
        libs = os.path.join(target_binary_dir, "lib*")
        subprocess.call("%s -u %s 2>/dev/null" % (prelink, libs), shell=True)
      else:
        logging.info("Can't execute %s, skipping.", prelink)

      # Set the daemon to executable.
      os.chmod(os.path.join(target_binary_dir, client_binary_name), 0755)

      client_arch = config_lib.CONFIG.Get("Client.arch", context=self.context)
      if client_arch == "amd64":
        client_arch = "x86_64"

      command = [
          rpmbuild_binary,
          "--define", "_topdir " + rpm_root_dir,
          "--target", client_arch,
          "--buildroot", rpm_buildroot_dir,
          "-bb", spec_filename]
      try:
        subprocess.check_output(command, stderr=subprocess.STDOUT)
      except subprocess.CalledProcessError as e:
        logging.error("Error calling %s.", command)
        logging.error(e.output)
        raise

      client_version = config_lib.CONFIG.Get("Client.version_string",
                                             context=self.context)
      rpm_filename = os.path.join(
          rpm_rpms_dir, client_arch,
          "%s-%s-1.%s.rpm" % (client_name, client_version, client_arch))

      utils.EnsureDirExists(os.path.dirname(output_path))
      shutil.move(rpm_filename, output_path)

      logging.info("Created package %s", output_path)
      self.Sign(output_path)
      return output_path
dataset/ETHPy150Open google/grr/grr/lib/build.py/CentosClientDeployer.MakeDeployableBinary/original