<commit_before>require "erb"
require "fpm/namespace"
require "fpm/package"
require "fpm/errors"
require "fpm/util"

class FPM::Target::Deb < FPM::Package

  # Supported Debian control files
  CONTROL_FILES = {
    "pre-install"       => [:script, "preinst"],
    "post-install"      => [:script, "postinst"],
    "pre-uninstall"     => [:script, "prerm"],
    "post-uninstall"    => [:script, "postrm"],
    "debconf-config"    => [:script, "config"],
    "debconf-templates" => [:text,   "templates"]
  }

  def self.flags(opts, settings)
    settings.target[:deb] = "deb"

    opts.on("--ignore-iteration-in-dependencies",
            "For = dependencies, allow iterations on the specified version.  Default is to be specific.") do |x|
      settings.target[:ignore_iteration] = true
    end

    opts.on("--pre-depends DEPENDENCY", "Add DEPENDENCY as Pre-Depends.") do |dep|
      (settings.target[:pre_depends] ||= []) << dep
    end

    # Take care about the case when we want custom control file but still use fpm ...
    opts.on("--custom-control FILEPATH",
            "Custom version of the Debian control file.") do |control|
      settings.target[:control] = File.expand_path(control)
    end
    
    # Add custom debconf config file
    opts.on("--config SCRIPTPATH",
            "Add SCRIPTPATH as debconf config file.") do |config|
      settings.scripts["debconf-config"] = File.expand_path(config)
    end
    
    # Add custom debconf templates file
    opts.on("--templates FILEPATH",
            "Add FILEPATH as debconf templates file.") do |templates|
      settings.scripts["debconf-templates"] = File.expand_path(templates)
    end
  end

  def needs_md5sums
    case %x{uname -s}.chomp
    when "Darwin"
      return false
    else
      return true
    end
  end # def needs_md5sums

  def architecture
    if @architecture.nil? or @architecture == "native"
      # Default architecture should be 'native' which we'll need
      # to ask the system about.
      arch = %x{dpkg --print-architecture 2> /dev/null}.chomp
      if $?.exitstatus != 0
        arch = %x{uname -m}.chomp
        @logger.warn("Can't find 'dpkg' tool (need it to get default " \
                     "architecture!). Please specificy --architecture " \
                     "specifically. (Defaulting now to #{arch})")
      end
      @architecture = arch
    elsif @architecture == "x86_64"
      # Debian calls x86_64 "amd64"
      @architecture = "amd64"
    end

    return @architecture
  end # def architecture

  def specfile(builddir)
    "#{builddir}/control"
  end

  def name
    if @name =~ /[A-Z]/
      @logger.warn("Debian tools (dpkg/apt) don't do well with packages " \
        "that use capital letters in the name. In some cases it will " \
        "automatically downcase them, in others it will not. It is confusing." \
        "Best to not use any capital letters at all.")
      @name = @name.downcase
    end

    if @name.include?("_")
      @logger.info("Package name '#{@name}' includes underscores, converting" \
                   " to dashes")
      @name = @name.gsub(/[_]/, "-")
    end

    return @name
  end

  def build!(params)
    control_files = [ "control" ]
    if File.exists? "./md5sums"
      control_files << "md5sums"
    end

    # Use custom Debian control file when given ...
    if self.settings[:control]
      %x{cp #{self.settings[:control]} ./control 2> /dev/null 2>&1}
      @logger.warn("Unable to process custom Debian control file (exit " \
                   "code: #{$?.exitstatus}). Falling back to default " \
                   "template.") unless $?.exitstatus == 0
    end

    # place the control files
    self.scripts.each do |name, path|
      ctrl_type, ctrl_file = CONTROL_FILES[name]
      if ctrl_file
        safesystem("cp",  path, "./#{ctrl_file}")
        safesystem("chmod", "a+x", "./#{ctrl_file}") if ctrl_type == :script
        control_files << ctrl_file
      else
        raise "Unsupported script name '#{name}' (path: #{path})"
      end
    end # self.scripts.each

    if self.config_files.any?
      File.open('conffiles', 'w'){ |f| f.puts(config_files.join("\n")) }
      control_files << 'conffiles'
    end

    # Make the control
    safesystem("tar", "--numeric-owner --owner=root --group=root -zcf", "control.tar.gz", *control_files)<commit_msg>Fix wrong arguments split on safesystem call.<commit_after>require "erb"
require "fpm/namespace"
require "fpm/package"
require "fpm/errors"
require "fpm/util"

class FPM::Target::Deb < FPM::Package

  # Supported Debian control files
  CONTROL_FILES = {
    "pre-install"       => [:script, "preinst"],
    "post-install"      => [:script, "postinst"],
    "pre-uninstall"     => [:script, "prerm"],
    "post-uninstall"    => [:script, "postrm"],
    "debconf-config"    => [:script, "config"],
    "debconf-templates" => [:text,   "templates"]
  }

  def self.flags(opts, settings)
    settings.target[:deb] = "deb"

    opts.on("--ignore-iteration-in-dependencies",
            "For = dependencies, allow iterations on the specified version.  Default is to be specific.") do |x|
      settings.target[:ignore_iteration] = true
    end

    opts.on("--pre-depends DEPENDENCY", "Add DEPENDENCY as Pre-Depends.") do |dep|
      (settings.target[:pre_depends] ||= []) << dep
    end

    # Take care about the case when we want custom control file but still use fpm ...
    opts.on("--custom-control FILEPATH",
            "Custom version of the Debian control file.") do |control|
      settings.target[:control] = File.expand_path(control)
    end
    
    # Add custom debconf config file
    opts.on("--config SCRIPTPATH",
            "Add SCRIPTPATH as debconf config file.") do |config|
      settings.scripts["debconf-config"] = File.expand_path(config)
    end
    
    # Add custom debconf templates file
    opts.on("--templates FILEPATH",
            "Add FILEPATH as debconf templates file.") do |templates|
      settings.scripts["debconf-templates"] = File.expand_path(templates)
    end
  end

  def needs_md5sums
    case %x{uname -s}.chomp
    when "Darwin"
      return false
    else
      return true
    end
  end # def needs_md5sums

  def architecture
    if @architecture.nil? or @architecture == "native"
      # Default architecture should be 'native' which we'll need
      # to ask the system about.
      arch = %x{dpkg --print-architecture 2> /dev/null}.chomp
      if $?.exitstatus != 0
        arch = %x{uname -m}.chomp
        @logger.warn("Can't find 'dpkg' tool (need it to get default " \
                     "architecture!). Please specificy --architecture " \
                     "specifically. (Defaulting now to #{arch})")
      end
      @architecture = arch
    elsif @architecture == "x86_64"
      # Debian calls x86_64 "amd64"
      @architecture = "amd64"
    end

    return @architecture
  end # def architecture

  def specfile(builddir)
    "#{builddir}/control"
  end

  def name
    if @name =~ /[A-Z]/
      @logger.warn("Debian tools (dpkg/apt) don't do well with packages " \
        "that use capital letters in the name. In some cases it will " \
        "automatically downcase them, in others it will not. It is confusing." \
        "Best to not use any capital letters at all.")
      @name = @name.downcase
    end

    if @name.include?("_")
      @logger.info("Package name '#{@name}' includes underscores, converting" \
                   " to dashes")
      @name = @name.gsub(/[_]/, "-")
    end

    return @name
  end

  def build!(params)
    control_files = [ "control" ]
    if File.exists? "./md5sums"
      control_files << "md5sums"
    end

    # Use custom Debian control file when given ...
    if self.settings[:control]
      %x{cp #{self.settings[:control]} ./control 2> /dev/null 2>&1}
      @logger.warn("Unable to process custom Debian control file (exit " \
                   "code: #{$?.exitstatus}). Falling back to default " \
                   "template.") unless $?.exitstatus == 0
    end

    # place the control files
    self.scripts.each do |name, path|
      ctrl_type, ctrl_file = CONTROL_FILES[name]
      if ctrl_file
        safesystem("cp",  path, "./#{ctrl_file}")
        safesystem("chmod", "a+x", "./#{ctrl_file}") if ctrl_type == :script
        control_files << ctrl_file
      else
        raise "Unsupported script name '#{name}' (path: #{path})"
      end
    end # self.scripts.each

    if self.config_files.any?
      File.open('conffiles', 'w'){ |f| f.puts(config_files.join("\n")) }
      control_files << 'conffiles'
    end

    # Make the control
    safesystem("tar", "--numeric-owner", "--owner=root", "--group=root",
               "-zcf", "control.tar.gz", *control_files)
{'fraction_non_alphanumeric': 0.1359578850995651, 'fraction_numerical': 0.0050354772259098195, 'mean_word_length': 2.4719904648390942, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1266784', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2698, 'n_words': 777}