<commit_before>require "open-uri"
require "puppet/provider/package"

Puppet::Type.type(:package).provide :compressed_app,
:parent => Puppet::Provider::Package do
  desc "Installs a compressed .app. Supports zip, tar.gz, tar.bz2"

  SOURCE_TYPES = %w(zip tar.gz tar.bz2 tgz tbz)

  confine  :operatingsystem => :darwin

  commands :curl  => "/usr/bin/curl"
  commands :ditto => "/usr/bin/ditto"
  commands :tar   => "/usr/bin/tar"
  commands :chown => "/usr/sbin/chown"
  commands :rm    => "/bin/rm"

  def self.instances_by_name
    Dir.entries("/var/db").find_all { |f|
      f =~ /^\.puppet_compressed_app_installed_/
    }.collect do |f|
      name = f.sub(/^\.puppet_compressed_app_installed_/, '')
      yield name if block_given?
      name
    end
  end

  def self.instances
    instances_by_name.collect do |name|
      new(:name => name, :provider => :compressed_app, :ensure => :installed)
    end
  end

  def self.install_compressed_app(name, source, flavor = nil)
    source_type = flavor ||
      source.match(/\.(zip|tgz|tbz|tar\.gz|tar\.bz)$/i){|m| m[0] } ||
      self.fail("Source must be .zip, .tar.gz, .tgz, .tar.bz2, or .tbz")

    FileUtils.mkdir_p '/opt/boxen/cache'
    curl source, "-Lqo", cached_path
    rm "-rf", "/Applications/#{name}.app", :uid => 'root'

    case source_type
    when 'zip'
      ditto "-xk", cached_path, "/Applications", :uid => 'root'
    when 'tar.gz', 'tgz'
      tar "-zxf", cached_path, "-C", "/Applications", :uid => 'root'
    when 'tar.bz2', 'tbz'
      tar "-jxf", cached_path, "-C", "/Applications", :uid => 'root'
    end

    chown "-R", "#{Facter[:boxen_user].value}:admin",
      "/Applications/#{name}.app", :uid => 'root'

    File.open("/var/db/.puppet_compressed_app_installed_#{name}", "w") do |t|
      t.print "name: '#{name}'\n"
      t.print "source: '#{source}'\n"
    end
  end

  def self.uninstall_compressed_app(name)
    rm "-rf", "/Applications/#{name}", :uid => 'root'
    rm "-f", "/var/db/.puppet_compressed_app_installed_#{name}"
  end

  def query
    if File.exists?("/var/db/.puppet_compressed_app_installed_#{@resource[:name]}")
      {
        :name   => @resource[:name],
        :ensure => :installed
      }
    end
  end

  def install
    unless source = @resource[:source]
      self.fail "OS X compressed apps must specify a package source"
    end

    unless name = @resource[:name]
      self.fail "OS X compressed apps must specify a package name"
    end

    if flavor = @resource[:flavor]
      unless SOURCE_TYPES.member? flavor
        self.fail "Unsupported flavor"
      end
    end

    self.class.install_compressed_app name, source, flavor
  end

  def uninstall
    self.class.uninstall_compressed_app @resource[:name]
  end

  def cached_path
    "/opt/boxen/cache/#{@resource[:name]}.app.#{source_type}"
  end

end
<commit_msg>why is this even a class method<commit_after>require "open-uri"
require "puppet/provider/package"

Puppet::Type.type(:package).provide :compressed_app,
:parent => Puppet::Provider::Package do
  desc "Installs a compressed .app. Supports zip, tar.gz, tar.bz2"

  SOURCE_TYPES = %w(zip tar.gz tar.bz2 tgz tbz)

  confine  :operatingsystem => :darwin

  commands :curl  => "/usr/bin/curl"
  commands :ditto => "/usr/bin/ditto"
  commands :tar   => "/usr/bin/tar"
  commands :chown => "/usr/sbin/chown"
  commands :rm    => "/bin/rm"

  def self.instances_by_name
    Dir.entries("/var/db").find_all { |f|
      f =~ /^\.puppet_compressed_app_installed_/
    }.collect do |f|
      name = f.sub(/^\.puppet_compressed_app_installed_/, '')
      yield name if block_given?
      name
    end
  end

  def self.instances
    instances_by_name.collect do |name|
      new(:name => name, :provider => :compressed_app, :ensure => :installed)
    end
  end

  def self.install_compressed_app(name, source, flavor = nil)
    source_type = flavor ||
      source.match(/\.(zip|tgz|tbz|tar\.gz|tar\.bz)$/i){|m| m[0] } ||
      self.fail("Source must be .zip, .tar.gz, .tgz, .tar.bz2, or .tbz")

    FileUtils.mkdir_p '/opt/boxen/cache'
    curl source, "-Lqo", cached_path
    rm "-rf", "/Applications/#{name}.app", :uid => 'root'

    case source_type
    when 'zip'
      ditto "-xk", cached_path, "/Applications", :uid => 'root'
    when 'tar.gz', 'tgz'
      tar "-zxf", cached_path, "-C", "/Applications", :uid => 'root'
    when 'tar.bz2', 'tbz'
      tar "-jxf", cached_path, "-C", "/Applications", :uid => 'root'
    end

    chown "-R", "#{Facter[:boxen_user].value}:admin",
      "/Applications/#{name}.app", :uid => 'root'

    File.open("/var/db/.puppet_compressed_app_installed_#{name}", "w") do |t|
      t.print "name: '#{name}'\n"
      t.print "source: '#{source}'\n"
    end
  end


  def query
    if File.exists?("/var/db/.puppet_compressed_app_installed_#{@resource[:name]}")
      {
        :name   => @resource[:name],
        :ensure => :installed
      }
    end
  end

  def install
    unless source = @resource[:source]
      self.fail "OS X compressed apps must specify a package source"
    end

    unless name = @resource[:name]
      self.fail "OS X compressed apps must specify a package name"
    end

    if flavor = @resource[:flavor]
      unless SOURCE_TYPES.member? flavor
        self.fail "Unsupported flavor"
      end
    end

    self.class.install_compressed_app name, source, flavor
  end

  def uninstall
    rm "-rf", "/Applications/#{@resource[:name]}", :uid => 'root'
    rm "-f", "/var/db/.puppet_compressed_app_installed_#{@resource[:name]}"
  end

  def cached_path
    "/opt/boxen/cache/#{@resource[:name]}.app.#{source_type}"
  end

end

{'fraction_non_alphanumeric': 0.1656288916562889, 'fraction_numerical': 0.001779042874933286, 'mean_word_length': 2.945263157894737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '2881035', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1986, 'n_words': 457}