def SetupWorkdir()

in pkg/make_rpm.py [0:0]


  def SetupWorkdir(self,
                   spec_file,
                   original_dir,
                   preamble_file=None,
                   description_file=None,
                   install_script_file=None,
                   pre_scriptlet_path=None,
                   post_scriptlet_path=None,
                   preun_scriptlet_path=None,
                   postun_scriptlet_path=None,
                   file_list_path=None):
    """Create the needed structure in the workdir."""

    # Create the rpmbuild-expected directory structure.
    for name in RpmBuilder.DIRS:
      if not os.path.exists(name):
        os.makedirs(name, 0o777)

    # Copy the to-be-packaged files into the BUILD directory
    for f in self.files:
      dst_dir = os.path.join(RpmBuilder.BUILD_DIR, os.path.dirname(f))
      if not os.path.exists(dst_dir):
        os.makedirs(dst_dir, 0o777)
      shutil.copy(os.path.join(original_dir, f), dst_dir)

    # The code below is related to assembling the RPM spec template and
    # everything else it needs to produce a valid RPM package.
    #
    # There two different types of substitution going on here: textual, directly
    # into the spec file, and macro; done when we call rpmbuild(8).
    #
    # Plans to clean this up are tracked in #209.

    # Slurp in the scriptlets...
    self.pre_scriptlet = \
      SlurpFile(os.path.join(original_dir, pre_scriptlet_path)) if pre_scriptlet_path is not None else ''
    self.post_scriptlet = \
      SlurpFile(os.path.join(original_dir, post_scriptlet_path)) if post_scriptlet_path is not None else ''
    self.preun_scriptlet = \
      SlurpFile(os.path.join(original_dir, preun_scriptlet_path)) if preun_scriptlet_path is not None else ''
    self.postun_scriptlet = \
      SlurpFile(os.path.join(original_dir, postun_scriptlet_path)) if postun_scriptlet_path is not None else ''

    # Then prepare for textual substitution.  This is typically only the case for the
    # experimental `pkg_rpm`.
    tpl_replacements = {
      'PRE_SCRIPTLET': "%pre\n" + self.pre_scriptlet,
      'POST_SCRIPTLET': "%post\n" + self.post_scriptlet,
      'PREUN_SCRIPTLET': "%preun\n" + self.preun_scriptlet,
      'POSTUN_SCRIPTLET': "%postun\n" + self.postun_scriptlet,
    }

    # If the spec file has "Version" and "Release" tags specified in the spec
    # file's preamble, the values are filled in immediately afterward.  These go
    # into "replacements".  This is typically only the case for the "original"
    # `pkg_rpm`.
    #
    # The "tpl_replacements" are used for direct text substitution of scriptlets
    # into the spec file, typically only for the "experimental" `pkg_rpm`.
    spec_origin = os.path.join(original_dir, spec_file)
    self.spec_file = os.path.basename(spec_file)
    replacements = {}
    if self.version:
      replacements['Version:'] = self.version
    if self.release:
      replacements['Release:'] = self.release
    CopyAndRewrite(spec_origin, self.spec_file,
                   replacements=replacements,
                   template_replacements=tpl_replacements)

    # "Preamble" template substitutions.  Currently only support values for the
    # "Version" and "Release" tags.
    #
    # This is only the case for `pkg_rpm` in experimental/rpm.bzl.
    #
    # This is substituted by rpmbuild(8) via macro expansion.
    if preamble_file:
      # Copy in the various other files needed to build the RPM
      self.preamble_file = os.path.basename(preamble_file)
      tpl_replacements = {}
      if self.version:
        tpl_replacements['VERSION_FROM_FILE'] = self.version
      if self.release:
        tpl_replacements['RELEASE_FROM_FILE'] = self.release
      CopyAndRewrite(os.path.join(original_dir, preamble_file),
                     self.preamble_file,
                     template_replacements=tpl_replacements)

    # The below are all copied into place within the RPM spec root.  It may be
    # possible to directly some, if not all, of these out of the Bazel build
    # root instead. "file_list_path" may be the problematic one here,
    # as it must be there.
    #
    # These are substituted by rpmbuild(8) via macro expansion.

    # Used in %description
    if description_file:
      shutil.copy(os.path.join(original_dir, description_file), os.getcwd())
      self.description_file = os.path.basename(description_file)

    # Used in %install
    if install_script_file:
      shutil.copy(os.path.join(original_dir, install_script_file), os.getcwd())
      self.install_script_file = os.path.basename(install_script_file)

    # Used in %files -f
    if file_list_path:
      shutil.copy(os.path.join(original_dir, file_list_path), RpmBuilder.BUILD_DIR)
      self.file_list_path = os.path.join(RpmBuilder.BUILD_DIR, os.path.basename(file_list_path))