<commit_before># Copyright 2006-2008 by <NAME>. All rights reserved.
require 'capistrano'
require 'fileutils'

module Deprec2
  
  # Temporarily modify ROLES if HOSTS not set
  # Capistrano's default behaviour is for HOSTS to override ROLES
  def for_roles(roles)
    old_roles = ENV['ROLES']
    ENV['ROLES'] = roles.to_s unless ENV['HOSTS']
    yield
    ENV['ROLES'] = old_roles.to_s unless ENV['HOSTS']
  end
  
  # Temporarily ignore ROLES and HOSTS
  def ignoring_roles_and_hosts
    old_roles = ENV['ROLES']
    old_hosts = ENV['HOSTS']
    ENV['ROLES'] = nil
    ENV['HOSTS'] = nil
    yield
    ENV['ROLES'] = old_roles
    ENV['HOSTS'] = old_hosts
  end
  
  DEPREC_TEMPLATES_BASE = File.join(File.dirname(__FILE__), 'templates')

  # Render template (usually a config file) 
  # 
  # Usually we render it to a file on the local filesystem.
  # This way, we keep a copy of the config file under source control.
  # We can make manual changes if required and push to new hosts.
  #
  # If the options hash contains :path then it's written to that path.
  # If it contains :remote => true, the file will instead be written to remote targets
  # If options[:path] and options[:remote] are missing, it just returns the rendered
  # template as a string (good for debugging).
  #
  #  XXX I would like to get rid of :render_template_to_file
  #  XXX Perhaps pass an option to this function to write to remote
  #
  def render_template(app, options={})
    template = options[:template]
    path = options[:path] || nil
    remote = options[:remote] || false
    mode = options[:mode] || 0755
    owner = options[:owner] || nil
    stage = exists?(:stage) ? fetch(:stage).to_s : ''
    # replace this with a check for the file
    if ! template
      puts "render_template() requires a value for the template!"
      return false 
    end
  
    # If local copies of deprec templates exist they will be used 
    # If you don't specify the location with the local_template_dir option
    # it defaults to config/templates.
    # e.g. config/templates/nginx/nginx.conf.erb
    local_template = File.join(local_template_dir, app.to_s, template)
    if File.exists?(local_template)
      puts
      puts "Using local template (#{local_template})"
      template = ERB.new(IO.read(local_template), nil, '-')
    else
      template = ERB.new(IO.read(File.join(DEPREC_TEMPLATES_BASE, app.to_s, template)), nil, '-')
    end
    rendered_template = template.result(binding)
  
    if remote 
      # render to remote machine
      puts 'You need to specify a path to render the template to!' unless path
      exit unless path
      sudo "test -d #{File.dirname(path)} || #{sudo} mkdir -p #{File.dirname(path)}"
      std.su_put rendered_template, path, '/tmp/', :mode => mode
      sudo "chown #{owner} #{path}" if defined?(owner)
    elsif path 
      # render to local file
      full_path = File.expand_path(File.join('config', stage, app.to_s, path))
      path_dir = File.dirname(full_path)
      if File.exists?(full_path)
        if IO.read(full_path) == rendered_template
          puts "[skip] File exists and is identical (#{full_path})."
          return false
        elsif overwrite?(full_path, rendered_template)
          File.delete(full_path)
        else
          puts "[skip] Not overwriting #{full_path}"
          return false
        end
      end
      FileUtils.mkdir_p "#{path_dir}" if ! File.directory?(path_dir)
      # added line above to make windows compatible
      # system "mkdir -p #{path_dir}" if ! File.directory?(path_dir) <commit_msg>Too big a change - reverting the expand_path<commit_after># Copyright 2006-2008 by <NAME>. All rights reserved.
require 'capistrano'
require 'fileutils'

module Deprec2
  
  # Temporarily modify ROLES if HOSTS not set
  # Capistrano's default behaviour is for HOSTS to override ROLES
  def for_roles(roles)
    old_roles = ENV['ROLES']
    ENV['ROLES'] = roles.to_s unless ENV['HOSTS']
    yield
    ENV['ROLES'] = old_roles.to_s unless ENV['HOSTS']
  end
  
  # Temporarily ignore ROLES and HOSTS
  def ignoring_roles_and_hosts
    old_roles = ENV['ROLES']
    old_hosts = ENV['HOSTS']
    ENV['ROLES'] = nil
    ENV['HOSTS'] = nil
    yield
    ENV['ROLES'] = old_roles
    ENV['HOSTS'] = old_hosts
  end
  
  DEPREC_TEMPLATES_BASE = File.join(File.dirname(__FILE__), 'templates')

  # Render template (usually a config file) 
  # 
  # Usually we render it to a file on the local filesystem.
  # This way, we keep a copy of the config file under source control.
  # We can make manual changes if required and push to new hosts.
  #
  # If the options hash contains :path then it's written to that path.
  # If it contains :remote => true, the file will instead be written to remote targets
  # If options[:path] and options[:remote] are missing, it just returns the rendered
  # template as a string (good for debugging).
  #
  #  XXX I would like to get rid of :render_template_to_file
  #  XXX Perhaps pass an option to this function to write to remote
  #
  def render_template(app, options={})
    template = options[:template]
    path = options[:path] || nil
    remote = options[:remote] || false
    mode = options[:mode] || 0755
    owner = options[:owner] || nil
    stage = exists?(:stage) ? fetch(:stage).to_s : ''
    # replace this with a check for the file
    if ! template
      puts "render_template() requires a value for the template!"
      return false 
    end
  
    # If local copies of deprec templates exist they will be used 
    # If you don't specify the location with the local_template_dir option
    # it defaults to config/templates.
    # e.g. config/templates/nginx/nginx.conf.erb
    local_template = File.join(local_template_dir, app.to_s, template)
    if File.exists?(local_template)
      puts
      puts "Using local template (#{local_template})"
      template = ERB.new(IO.read(local_template), nil, '-')
    else
      template = ERB.new(IO.read(File.join(DEPREC_TEMPLATES_BASE, app.to_s, template)), nil, '-')
    end
    rendered_template = template.result(binding)
  
    if remote 
      # render to remote machine
      puts 'You need to specify a path to render the template to!' unless path
      exit unless path
      sudo "test -d #{File.dirname(path)} || #{sudo} mkdir -p #{File.dirname(path)}"
      std.su_put rendered_template, path, '/tmp/', :mode => mode
      sudo "chown #{owner} #{path}" if defined?(owner)
    elsif path 
      # render to local file
      full_path = File.join('config', stage, app.to_s, path)
      path_dir = File.dirname(full_path)
      if File.exists?(full_path)
        if IO.read(full_path) == rendered_template
          puts "[skip] File exists and is identical (#{full_path})."
          return false
        elsif overwrite?(full_path, rendered_template)
          File.delete(full_path)
        else
          puts "[skip] Not overwriting #{full_path}"
          return false
        end
      end
      FileUtils.mkdir_p "#{path_dir}" if ! File.directory?(path_dir)
      # added line above to make windows compatible
      # system "mkdir -p #{path_dir}" if ! File.directory?(path_dir) 
{'fraction_non_alphanumeric': 0.10143915048204555, 'fraction_numerical': 0.0036328070420567275, 'mean_word_length': 3.0349492671927845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '4022811', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2209, 'n_words': 744}