<commit_before>#!/opt/rightscale/sandbox/bin/ruby
# Copyright (c) 2010 by RightScale Inc., all rights reserved
#
# Write given user data to files in /var/spool/ec2 in text, shell and ruby 
# script formats

require 'fileutils'
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'lib', 'shell_utilities'))

begin
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'config', 'platform'))
  output_dir = File.join(RightScale::Platform.filesystem.spool_dir, 'ec2')
rescue Exception => e
  STDERR.puts "!!!!! FAILED TO DISCOVER EC2 USERDATA OUTPUT DIR"
  STDERR.puts "!!!!! Error: #{e}"
  STDERR.puts e.backtrace.join("\n")
  exit 1
end

OUTPUT_DIR    = output_dir
USER_PREFX    = File.join(OUTPUT_DIR, 'user-data')
SHEBANG_REGEX = /^#!/

module RightScale
  class UserDataWriter

    # Write given user data to files
    # May cause process to exit with code 1 in case of failure
    #
    # === Parameters
    # data(String):: Query string like or inline script user data
    #
    # === Return
    # true:: Always return true
    def self.write(data)
      FileUtils.mkdir_p OUTPUT_DIR
      begin
        File.open("#{USER_PREFX}.raw", "w") { |f| f.write data }
        if data =~ SHEBANG_REGEX
          handle_shebang_userdata(data)
        else
          handle_querystring_userdata(data)
        end
      rescue Exception => e
        STDERR.puts "!!!!! FAILED TO PROCESS EC2 USER DATA"
        STDERR.puts "!!!!! Error: #{e}"
        STDERR.puts e.backtrace.join("\n")
        exit 1
      end
      true
    end

    protected

    # Process inline script user data
    #
    # === Parameters
    # data(String):: Inline script user data
    #
    # === Return
    # true:: Always return true
    def handle_shebang_userdata(data)
      hash = {}

      File.open(File.join(OUTPUT_DIR, 'user-data.txt')) do |f|
        lines = f.readlines
        lines = lines.map { |l| l.chomp }
        lines.each do |line|
          name, value = line.split(/=/, 2)
          hash[name] = value
        end
      end

      write_userdata(hash, false)
    end

    # Process query string like user data
    #
    # === Parameters
    # data(String):: Query string user data
    #
    # === Return
    # true:: Always return true
    def handle_querystring_userdata(data)
      hash = {}

      data.split('&').each do |pair|
        name, value = pair.split(/=/, 2)
        hash[name] = value
      end

      write_userdata(hash, true)
    end

    # Write user data to shell and ruby scripts and optionally to text file
    #
    # === Parameters
    # hash(Hash):: Hash of user data keyed by name
    # include_txt(TrueClass|FalseClass):: Generate user data text file if true
    #
    # === Return
    # true:: Always return true
    def write_userdata(hash, include_txt)<commit_msg>Make all methods of UserDataWriter class methods<commit_after>#!/opt/rightscale/sandbox/bin/ruby
# Copyright (c) 2010 by RightScale Inc., all rights reserved
#
# Write given user data to files in /var/spool/ec2 in text, shell and ruby 
# script formats

require 'fileutils'
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'lib', 'shell_utilities'))

begin
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'config', 'platform'))
  output_dir = File.join(RightScale::Platform.filesystem.spool_dir, 'ec2')
rescue Exception => e
  STDERR.puts "!!!!! FAILED TO DISCOVER EC2 USERDATA OUTPUT DIR"
  STDERR.puts "!!!!! Error: #{e}"
  STDERR.puts e.backtrace.join("\n")
  exit 1
end

OUTPUT_DIR    = output_dir
USER_PREFX    = File.join(OUTPUT_DIR, 'user-data')
SHEBANG_REGEX = /^#!/

module RightScale
  class UserDataWriter

    # Write given user data to files
    # May cause process to exit with code 1 in case of failure
    #
    # === Parameters
    # data(String):: Query string like or inline script user data
    #
    # === Return
    # true:: Always return true
    def self.write(data)
      FileUtils.mkdir_p OUTPUT_DIR
      begin
        File.open("#{USER_PREFX}.raw", "w") { |f| f.write data }
        if data =~ SHEBANG_REGEX
          handle_shebang_userdata(data)
        else
          handle_querystring_userdata(data)
        end
      rescue Exception => e
        STDERR.puts "!!!!! FAILED TO PROCESS EC2 USER DATA"
        STDERR.puts "!!!!! Error: #{e}"
        STDERR.puts e.backtrace.join("\n")
        exit 1
      end
      true
    end

    protected

    # Process inline script user data
    #
    # === Parameters
    # data(String):: Inline script user data
    #
    # === Return
    # true:: Always return true
    def self.handle_shebang_userdata(data)
      hash = {}

      File.open(File.join(OUTPUT_DIR, 'user-data.txt')) do |f|
        lines = f.readlines
        lines = lines.map { |l| l.chomp }
        lines.each do |line|
          name, value = line.split(/=/, 2)
          hash[name] = value
        end
      end

      write_userdata(hash, false)
    end

    # Process query string like user data
    #
    # === Parameters
    # data(String):: Query string user data
    #
    # === Return
    # true:: Always return true
    def self.handle_querystring_userdata(data)
      hash = {}

      data.split('&').each do |pair|
        name, value = pair.split(/=/, 2)
        hash[name] = value
      end

      write_userdata(hash, true)
    end

    # Write user data to shell and ruby scripts and optionally to text file
    #
    # === Parameters
    # hash(Hash):: Hash of user data keyed by name
    # include_txt(TrueClass|FalseClass):: Generate user data text file if true
    #
    # === Return
    # true:: Always return true
    def self.write_userdata(hash, include_txt)
{'fraction_non_alphanumeric': 0.1268760907504363, 'fraction_numerical': 0.004537521815008726, 'mean_word_length': 2.50734394124847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5381249', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1819, 'n_words': 511}