def MakeDeployableBinary(self, template_path, output_path):
    """This will add the config to the client template and create a .deb."""
    buildpackage_binary = "/usr/bin/dpkg-buildpackage"
    if not os.path.exists(buildpackage_binary):
      logging.error("dpkg-buildpackage 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():
        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))

      # Generate the dpkg files.
      self.GenerateDPKGFiles(tmp_dir)

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

      # We need to strip leading /'s or .join will ignore everything that comes
      # before it.
      target_dir = config.CONFIG.Get(
          "ClientBuilder.target_dir", context=self.context).lstrip("/")
      agent_dir = os.path.join(
          template_dir, "debian",
          config.CONFIG.Get("ClientBuilder.package_name", context=self.context),
          target_dir)

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

      # Set the daemon to executable.
      os.chmod(
          os.path.join(
              agent_dir,
              config.CONFIG.Get("Client.binary_name", context=self.context)),
          0o755)

      arch = config.CONFIG.Get("Template.arch", context=self.context)

      try:
        old_working_dir = os.getcwd()
      except OSError:
        old_working_dir = os.environ.get("HOME", "/tmp")

      try:
        os.chdir(template_dir)
        command = [buildpackage_binary, "-uc", "-d", "-b", "-a%s" % arch]

        try:
          subprocess.check_output(command, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
          if "Failed to sign" not in e.output:
            logging.error("Error calling %s.", command)
            logging.error(e.output)
            raise

        filename_base = config.CONFIG.Get(
            "ClientBuilder.debian_package_base", context=self.context)
        output_base = config.CONFIG.Get(
            "ClientRepacker.output_basename", context=self.context)
      finally:
        try:
          os.chdir(old_working_dir)
        except OSError:
          pass

      utils.EnsureDirExists(os.path.dirname(output_path))

      for extension in [
          ".changes",
          config.CONFIG.Get(
              "ClientBuilder.output_extension", context=self.context)
      ]:
        input_name = "%s%s" % (filename_base, extension)
        output_name = "%s%s" % (output_base, extension)

        # TODO(user):pytype: incorrect move() definition in typeshed.
        # pytype: disable=wrong-arg-types
        shutil.move(
            os.path.join(tmp_dir, input_name),
            os.path.join(os.path.dirname(output_path), output_name))
        # pytype: enable=wrong-arg-types

      logging.info("Created package %s", output_path)
      return output_path
def MakeDeployableBinary ( self , template_path , output_path ) : buildpackage_binary = "/usr/bin/dpkg-buildpackage" if not os . path . exists ( buildpackage_binary ) : logging . error ( "dpkg-buildpackage 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 ( ) : 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 ) ) self . GenerateDPKGFiles ( tmp_dir ) client_context = [ "Client Context" ] + self . context client_config_content = self . GetClientConfig ( client_context ) target_dir = config . CONFIG . Get ( "ClientBuilder.target_dir" , context = self . context ) . lstrip ( "/" ) agent_dir = os . path . join ( template_dir , "debian" , config . CONFIG . Get ( "ClientBuilder.package_name" , context = self . context ) , target_dir ) with open ( os . path . join ( agent_dir , config . CONFIG . Get ( "ClientBuilder.config_filename" , context = self . context ) ) , "wb" ) as fd : fd . write ( client_config_content ) os . chmod ( os . path . join ( agent_dir , config . CONFIG . Get ( "Client.binary_name" , context = self . context ) ) , 0o755 ) arch = config . CONFIG . Get ( "Template.arch" , context = self . context ) try : old_working_dir = os . getcwd ( ) except OSError : old_working_dir = os . environ . get ( "HOME" , "/tmp" ) try : os . chdir ( template_dir ) command = [ buildpackage_binary , "-uc" , "-d" , "-b" , "-a%s" % arch ] try : subprocess . check_output ( command , stderr = subprocess . STDOUT ) except subprocess . CalledProcessError as e : if "Failed to sign" not in e . output : logging . error ( "Error calling %s." , command ) logging . error ( e . output ) raise filename_base = config . CONFIG . Get ( "ClientBuilder.debian_package_base" , context = self . context ) output_base = config . CONFIG . Get ( "ClientRepacker.output_basename" , context = self . context ) finally : try : os . chdir ( old_working_dir ) except OSError : pass utils . EnsureDirExists ( os . path . dirname ( output_path ) ) for extension in [ ".changes" , config . CONFIG . Get ( "ClientBuilder.output_extension" , context = self . context ) ] : input_name = "%s%s" % ( filename_base , extension ) output_name = "%s%s" % ( output_base , extension ) shutil . move ( os . path . join ( tmp_dir , input_name ) , os . path . join ( os . path . dirname ( output_path ) , output_name ) ) logging . info ( "Created package %s" , output_path ) return output_path
This will add the config to the client template and create a .deb.