def MakeDeployableBinary(self, template_path, output_path=None):
    """Repackage the template zip with the installer."""
    context = self.context + ["Client Context"]

    zip_data = cStringIO.StringIO()
    output_zip = zipfile.ZipFile(
        zip_data, mode="w", compression=zipfile.ZIP_DEFLATED)

    z_template = zipfile.ZipFile(open(template_path, "rb"))

    completed_files = []  # Track which files we've copied already.

    # Change the name of the main binary to the configured name.
    client_bin_name = config_lib.CONFIG.Get(
        "Client.binary_name", context=context)

    extension = ""
    if ("windows" ==
        config_lib.CONFIG.Get("Client.platform", context=context)):
      extension = ".exe"

    # The template always has the same binary name.
    bin_name = z_template.getinfo("grr-client" + extension)
    bin_dat = cStringIO.StringIO()
    bin_dat.write(z_template.read(bin_name))

    # Set output to console on binary if needed.
    SetPeSubsystem(bin_dat, console=config_lib.CONFIG.Get(
        "ClientBuilder.console", context=context))

    # Interpolate resource strings.
    for parameter in ["Client.company_name", "Client.description",
                      "Client.name",
                      "Client.version_string", "ClientBuilder.package_name"]:
      self.InterpolateVariableInBinary(bin_dat, parameter)

    output_zip.writestr(client_bin_name, self.Sign(bin_dat.getvalue()))

    CopyFileInZip(z_template, "%s.manifest" % bin_name.filename, output_zip,
                  "%s.manifest" % client_bin_name)
    completed_files.append(bin_name.filename)
    completed_files.append("%s.manifest" % bin_name.filename)

    # Read the build config from the template.
    for name in z_template.namelist():
      if os.path.basename(name) == "build.yaml":
        data = z_template.read(name)
        self.MergeBuildConfig(data=data)
        completed_files.append(name)
        break

    # Change the name of the service binary to the configured name.
    service_template = z_template.getinfo("GRRservice.exe")

    service_bin_dat = cStringIO.StringIO()
    service_bin_dat.write(z_template.read(service_template))

    # Set output to console on service binary if needed.
    SetPeSubsystem(service_bin_dat,
                   console=config_lib.CONFIG.Get("ClientBuilder.console",
                                                 context=context))

    service_bin_name = config_lib.CONFIG.Get("Nanny.service_binary_name",
                                             context=context)
    output_zip.writestr(service_bin_name, self.Sign(service_bin_dat.getvalue()))
    completed_files.append(service_template.filename)

    # Copy the rest of the files from the template to the new zip.  Current
    # practice is to only sign the grr client and nanny executables, not the
    # dlls.
    for template_file in z_template.namelist():
      if template_file not in completed_files:
        CopyFileInZip(z_template, template_file, output_zip)

    # Add any additional plugins to the deployment binary.
    for plugin in config_lib.CONFIG.Get("Client.plugins", context=context):
      logging.debug("Adding plugin %s to package.", plugin)
      output_zip.writestr(os.path.basename(plugin),
                          open(plugin, "rb").read(), zipfile.ZIP_STORED)

    output_zip.close()

    return self.RepackInstaller(zip_data.getvalue(), output_path)
dataset/ETHPy150Open google/grr/grr/lib/build.py/WindowsClientDeployer.MakeDeployableBinary/original