<commit_before>require 'capistrano'
require "capistrano/env/version"

if Capistrano::Configuration.instance
  Capistrano::Configuration.instance.load do
    require 'aws'

    _cset :aws_credentials, {}
    _cset(:s3) { AWS::S3.new(aws_credentials) }

    _cset :env_bucket_name do
      name = ENV['AWS_BUCKET_NAME'] || ENV['BUCKET_NAME'] || ENV['FOG_DIRECTORY']
      if name.nil?
        $stdout.puts <<-MESSAGE
:env_bucket_name must be specified explicitly if not in any of the ENV
variables:
* AWS_BUCKET_NAME
* BUCKET_NAME
* FOG_DIRECTORY
        MESSAGE
        exit 1
      end

      name
    end

    _cset :env_bucket do
      bucket = s3.buckets[env_bucket_name]
      bucket = s3.buckets.create(env_bucket_name) unless bucket.exists?
      bucket
    end

    _cset :env_object_key do
      if defined?(stage)
        "#{application}-#{stage}"
      else
        application
      end
    end

    namespace :env do
      _cset :object do
        env_bucket.objects[env_object_key]
      end

      _cset :pairs do
        content = object.read rescue ""
        Hash[content.split(/\n/).map { |line| line.split('=', 2) }]
      end

      desc <<-DOC
        Reads the .env file from S3.
      DOC
      task :read do
        begin
          $stdout.puts object.read
        rescue
          $stdout.puts "the object #{env_object_key} in bucket #{env_bucket_name} does not exist, set some values first"
        end
      end

      desc "Set a value"
      task :set do
        ARGV.shift

        ARGV.map do |pair|
          key, value = pair.split('=', 2)
          pairs[key] = value
        end
      end

      desc <<-DOC
        Persists any changes made to the .env file and uploads it to the instances via
        scp.
      DOC
      task :export do
        object.write pairs.map { |key, value| "#{key}=#{value}" }.join("\n") << "\n"
        put object.read, "#{latest_release}/.env", via: :scp
      end

      desc <<-DOC
        Unset values from the .env file. Has the slight drawback that the recommended
        usage causes capistrano to print an error about being unable to find tasks with
        the name of the environment variable(s) being unset.
      DOC
      task :unset do
        ARGV.shift

        ARGV.each { |key| pairs.delete(key) }
      end
    end

    after 'env:set', 'env:export'
    after 'env:unset', 'env:export'
    after 'deploy:finalize_update', 'env:export'
  end
end<commit_msg>Expose `env_pairs` and `env_object`<commit_after>require 'capistrano'
require "capistrano/env/version"

if Capistrano::Configuration.instance
  Capistrano::Configuration.instance.load do
    require 'aws'

    _cset :aws_credentials, {}
    _cset(:s3) { AWS::S3.new(aws_credentials) }

    _cset :env_bucket_name do
      name = ENV['AWS_BUCKET_NAME'] || ENV['BUCKET_NAME'] || ENV['FOG_DIRECTORY']
      if name.nil?
        $stdout.puts <<-MESSAGE
:env_bucket_name must be specified explicitly if not in any of the ENV
variables:
* AWS_BUCKET_NAME
* BUCKET_NAME
* FOG_DIRECTORY
        MESSAGE
        exit 1
      end

      name
    end

    _cset :env_bucket do
      bucket = s3.buckets[env_bucket_name]
      bucket = s3.buckets.create(env_bucket_name) unless bucket.exists?
      bucket
    end

    _cset :env_object_key do
      if defined?(stage)
        "#{application}-#{stage}"
      else
        application
      end
    end

    _cset :env_object do
      env_bucket.objects[env_object_key]
    end

    _cset :env_pairs do
      content = env_object.read rescue ""
      Hash[content.split(/\n/).map { |line| line.split('=', 2) }]
    end

    namespace :env do

      desc <<-DOC
        Reads the .env file from S3.
      DOC
      task :read do
        begin
          $stdout.puts env_object.read
        rescue
          $stdout.puts "the object #{env_object_key} in bucket #{env_bucket_name} does not exist, set some values first"
        end
      end

      desc "Set a value"
      task :set do
        ARGV.shift

        ARGV.map do |pair|
          key, value = pair.split('=', 2)
          env_pairs[key] = value
        end
      end

      desc <<-DOC
        Persists any changes made to the .env file and uploads it to the instances via
        scp.
      DOC
      task :export do
        env_object.write env_pairs.map { |key, value| "#{key}=#{value}" }.join("\n") << "\n"
        put env_object.read, "#{latest_release}/.env", via: :scp
      end

      desc <<-DOC
        Unset values from the .env file. Has the slight drawback that the recommended
        usage causes capistrano to print an error about being unable to find tasks with
        the name of the environment variable(s) being unset.
      DOC
      task :unset do
        ARGV.shift

        ARGV.each { |key| env_pairs.delete(key) }
      end
    end

    after 'env:set', 'env:export'
    after 'env:unset', 'env:export'
    after 'deploy:finalize_update', 'env:export'
  end
end
{'fraction_non_alphanumeric': 0.09823421960625127, 'fraction_numerical': 0.0032474122183884717, 'mean_word_length': 2.1732131358660656, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 3, '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': '771501', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1583, 'n_words': 464}