<commit_before># Copyright (c) 2009-2012 VMware, Inc.

module Bosh::Cli::Command
  class Release < Base
    DEFAULT_RELEASE_NAME = "bosh-release"

    include Bosh::Cli::DependencyHelper
    include Bosh::Cli::VersionCalc

    # usage "init release [<path>]"
    # desc  "Initialize release directory"
    # option "--git", "initialize git repository"
    # route :release, :init
    def init(base=nil, *options)
      if base[0..0] == "-"
        # TODO: need to add some option parsing helpers to avoid that
        options.unshift(base)
        base = nil
      end
      git = options.include?("--git")

      if base
        FileUtils.mkdir_p(base) unless Dir.exist?(base)
        Dir.chdir(base)
      end

      err("Release already initialized") if in_release_dir?
      git_init if git

      %w[config jobs packages src blobs].each do |dir|
        FileUtils.mkdir(dir)
      end

      # Initialize an empty blobs index
      File.open(File.join("config", "blobs.yml"), "w") do |f|
        YAML.dump({}, f)
      end

      say("Release directory initialized".green)
    end

    def git_init
      out = %x{git init 2>&1}
      if $? != 0
        say("error running 'git init':\n#{out}")
      else
        File.open(".gitignore", "w") do |f|
          f << <<-EOS.gsub(/^\s{10}/, '')
          config/dev.yml
          config/private.yml
          releases/*.tgz
          dev_releases
          .blobs
          blobs
          .dev_builds
          .idea
          .DS_Store
          .final_builds/jobs/**/*.tgz
          .final_builds/packages/**/*.tgz
          *.swp
          *~
          *#
          #*
          EOS
        end
      end
    rescue Errno::ENOENT
      say("Unable to run 'git init'".red)
    end

    # usage "verify release <path>"
    # desc  "Verify release"
    # route :release, :verify
    def verify(tarball_path)
      tarball = Bosh::Cli::ReleaseTarball.new(tarball_path)

      say("\nVerifying release...")
      tarball.validate
      nl

      if tarball.valid?
        say("'%s' is a valid release" % [tarball_path] )
      else
        say("'%s' is not a valid release:" % [tarball_path] )
        for error in tarball.errors
          say("- %s" % [error])
        end
      end
    end

    # usage "upload release [<path>]"
    # desc  "Upload release (<path> can point to tarball or manifest, " +
    #           "defaults to the most recently created release)"
    # route :release, :upload
    def upload(*options)
      auth_required

      # TODO: need option helpers badly!
      release_file = nil
      if options.size > 0 && options.first[0..0] != "-"
        release_file = options.shift
      end

      upload_options = {
        :rebase => !!options.delete("--rebase"),
        :repack => true
      }

      if options.size > 0
        err("Unknown options: #{options.join(", ")}")
      end

      if release_file.nil?
        check_if_release_dir
        release_file = release.latest_release_filename
        if release_file.nil?
          err("The information about latest generated release is missing, " +
              "please provide release filename")
        end
        unless confirmed?("Upload release " +
                          "`#{File.basename(release_file).green}' " +
                          "to `#{target_name.green}'")
          err("Canceled upload")
        end
      end

      file_type = `file --mime-type -b '#{release_file}'`

      if file_type =~ /text\/(plain|yaml)/
        upload_manifest(release_file, upload_options)
      else # Just assume tarball
        upload_tarball(release_file, upload_options)
      end
    end

    def upload_manifest(manifest_path, upload_options = {})
      package_matches = match_remote_packages(File.read(manifest_path))

      find_release_dir(manifest_path)

      blobstore = release.blobstore
      tmpdir = Dir.mktmpdir

      at_exit { FileUtils.rm_rf(tmpdir) }

      compiler = Bosh::Cli::ReleaseCompiler.new(
        manifest_path, blobstore, package_matches)
      need_repack = true

      unless compiler.exists?
        compiler.tarball_path = File.join(tmpdir, "release.tgz")
        compiler.compile
        need_repack = false
      end
<commit_msg>better error message when release file is missing<commit_after># Copyright (c) 2009-2012 VMware, Inc.

module Bosh::Cli::Command
  class Release < Base
    DEFAULT_RELEASE_NAME = "bosh-release"

    include Bosh::Cli::DependencyHelper
    include Bosh::Cli::VersionCalc

    # usage "init release [<path>]"
    # desc  "Initialize release directory"
    # option "--git", "initialize git repository"
    # route :release, :init
    def init(base=nil, *options)
      if base[0..0] == "-"
        # TODO: need to add some option parsing helpers to avoid that
        options.unshift(base)
        base = nil
      end
      git = options.include?("--git")

      if base
        FileUtils.mkdir_p(base) unless Dir.exist?(base)
        Dir.chdir(base)
      end

      err("Release already initialized") if in_release_dir?
      git_init if git

      %w[config jobs packages src blobs].each do |dir|
        FileUtils.mkdir(dir)
      end

      # Initialize an empty blobs index
      File.open(File.join("config", "blobs.yml"), "w") do |f|
        YAML.dump({}, f)
      end

      say("Release directory initialized".green)
    end

    def git_init
      out = %x{git init 2>&1}
      if $? != 0
        say("error running 'git init':\n#{out}")
      else
        File.open(".gitignore", "w") do |f|
          f << <<-EOS.gsub(/^\s{10}/, '')
          config/dev.yml
          config/private.yml
          releases/*.tgz
          dev_releases
          .blobs
          blobs
          .dev_builds
          .idea
          .DS_Store
          .final_builds/jobs/**/*.tgz
          .final_builds/packages/**/*.tgz
          *.swp
          *~
          *#
          #*
          EOS
        end
      end
    rescue Errno::ENOENT
      say("Unable to run 'git init'".red)
    end

    # usage "verify release <path>"
    # desc  "Verify release"
    # route :release, :verify
    def verify(tarball_path)
      tarball = Bosh::Cli::ReleaseTarball.new(tarball_path)

      say("\nVerifying release...")
      tarball.validate
      nl

      if tarball.valid?
        say("'%s' is a valid release" % [tarball_path] )
      else
        say("'%s' is not a valid release:" % [tarball_path] )
        for error in tarball.errors
          say("- %s" % [error])
        end
      end
    end

    # usage "upload release [<path>]"
    # desc  "Upload release (<path> can point to tarball or manifest, " +
    #           "defaults to the most recently created release)"
    # route :release, :upload
    def upload(*options)
      auth_required

      # TODO: need option helpers badly!
      release_file = nil
      if options.size > 0 && options.first[0..0] != "-"
        release_file = options.shift
      end

      upload_options = {
        :rebase => !!options.delete("--rebase"),
        :repack => true
      }

      if options.size > 0
        err("Unknown options: #{options.join(", ")}")
      end

      if release_file.nil?
        check_if_release_dir
        release_file = release.latest_release_filename
        if release_file.nil?
          err("The information about latest generated release is missing, " +
              "please provide release filename")
        end
        unless confirmed?("Upload release " +
                          "`#{File.basename(release_file).green}' " +
                          "to `#{target_name.green}'")
          err("Canceled upload")
        end
      end

      unless File.exist?(release_file)
        err("Release file doesn't exist")
      end

      file_type = `file --mime-type -b '#{release_file}'`

      if file_type =~ /text\/(plain|yaml)/
        upload_manifest(release_file, upload_options)
      else # Just assume tarball
        upload_tarball(release_file, upload_options)
      end
    end

    def upload_manifest(manifest_path, upload_options = {})
      package_matches = match_remote_packages(File.read(manifest_path))

      find_release_dir(manifest_path)

      blobstore = release.blobstore
      tmpdir = Dir.mktmpdir

      at_exit { FileUtils.rm_rf(tmpdir) }

      compiler = Bosh::Cli::ReleaseCompiler.new(
        manifest_path, blobstore, package_matches)
      need_repack = true

      unless compiler.exists?
        compiler.tarball_path = File.join(tmpdir, "release.tgz")
        compiler.compile
        need_repack = false
      end

{'fraction_non_alphanumeric': 0.11505149812734082, 'fraction_numerical': 0.004447565543071161, 'mean_word_length': 1.9846315054139014, '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': '1858645', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2530, 'n_words': 636}