<commit_before>require 'erb'

module Capistrano
  module Supervisord
    def self.extended(configuration)
      configuration.load {
        namespace(:deploy) {
          desc("Start service.")
          task(:start, :roles => :app, :except => { :no_release => true }) {
            supervisord.start
          }

          desc("Stop service.")
          task(:stop, :roles => :app, :except => { :no_release => true }) {
            supervisord.stop
          }

          desc("Restart service.")
          task(:restart, :roles => :app, :except => { :no_release => true }) {
            supervisord.restart
          }
        }

        namespace(:supervisord) {
          _cset(:supervisord_configure_path, '/')
          _cset(:supervisord_configure_source_path, File.join(File.dirname(__FILE__), 'templates', 'supervisord'))
          _cset(:supervisord_configure_files, [])
          _cset(:supervisord_configure_cleanup_files, [])

          _cset(:supervisord_install_method, :apt)
          _cset(:supervisord_service_method, :sysvinit)

          namespace(:setup) {
            desc("Setup supervisord.")
            task(:default, :roles => :app, :except => { :no_release => true }) {
              transaction {
                install.install
                configure
                service.install
                reload
              }
            }
            after 'deploy:setup', 'supervisord:setup'

            task(:configure, :roles => :app, :except => { :no_release => true }) {
              tmp_files = []
              on_rollback {
                run("rm -f #{tmp_files.join(' ')}") unless tmp_files.empty?
              }
              supervisord_configure_files.each { |file|
                src_file = File.join(supervisord_configure_source_path, file)
                dst_file = File.join(supervisord_configure_path, file)
                tmp_file = File.join('/tmp', File.basename(file))
                if File.file?(src_file)
                  put(File.read(src_file), tmp_file)
                elsif File.file?("#{src_file}.erb")
                  src_file = "#{src_file}.erb"
                  put(ERB.new(File.read(src_file)).result(binding), tmp_file)
                else
                  abort("configure: no such template found: #{src_file} or #{src_file}.erb")
                end
                run("diff -u #{dst_file} #{tmp_file} || #{sudo} mv -f #{tmp_file} #{dst_file}; rm -f #{tmp_file}")
              }
              run("#{sudo} rm -rf #{supervisord_configure_cleanup_files.join(' ')}") unless supervisord_configure_cleanup_files.empty?
            }
          }

          namespace(:install) {
            task(:default, :roles => :app, :except => { :no_release => true }) {
              install
            }

            task(:install, :roles => :app, :except => { :no_release => true }) {
              __send__(supervisord_install_method).install if supervisord_install_method
            }

            namespace(:apt) {<commit_msg>Add ability to prepend a string to config files<commit_after>require 'erb'

module Capistrano
  module Supervisord
    def self.extended(configuration)
      configuration.load {
        namespace(:deploy) {
          desc("Start service.")
          task(:start, :roles => :app, :except => { :no_release => true }) {
            supervisord.start
          }

          desc("Stop service.")
          task(:stop, :roles => :app, :except => { :no_release => true }) {
            supervisord.stop
          }

          desc("Restart service.")
          task(:restart, :roles => :app, :except => { :no_release => true }) {
            supervisord.restart
          }
        }

        namespace(:supervisord) {
          _cset(:supervisord_configure_path, '/')
          _cset(:supervisord_configure_source_path, File.join(File.dirname(__FILE__), 'templates', 'supervisord'))
          _cset(:supervisord_configure_files, [])
          _cset(:supervisord_configure_file_prefix, '')
          _cset(:supervisord_configure_cleanup_files, [])

          _cset(:supervisord_install_method, :apt)
          _cset(:supervisord_service_method, :sysvinit)

          namespace(:setup) {
            desc("Setup supervisord.")
            task(:default, :roles => :app, :except => { :no_release => true }) {
              transaction {
                install.install
                configure
                service.install
                reload
              }
            }
            after 'deploy:setup', 'supervisord:setup'

            task(:configure, :roles => :app, :except => { :no_release => true }) {
              tmp_files = []
              on_rollback {
                run("rm -f #{tmp_files.join(' ')}") unless tmp_files.empty?
              }
              supervisord_configure_files.each { |file|
                src_file = File.join(supervisord_configure_source_path, file)
                dst_file = File.join(supervisord_configure_path, file.prepend(supervisord_configure_file_prefix))
                tmp_file = File.join('/tmp', File.basename(file))
                if File.file?(src_file)
                  put(File.read(src_file), tmp_file)
                elsif File.file?("#{src_file}.erb")
                  src_file = "#{src_file}.erb"
                  put(ERB.new(File.read(src_file)).result(binding), tmp_file)
                else
                  abort("configure: no such template found: #{src_file} or #{src_file}.erb")
                end
                run("diff -u #{dst_file} #{tmp_file} || #{sudo} mv -f #{tmp_file} #{dst_file}; rm -f #{tmp_file}")
              }
              run("#{sudo} rm -rf #{supervisord_configure_cleanup_files.join(' ')}") unless supervisord_configure_cleanup_files.empty?
            }
          }

          namespace(:install) {
            task(:default, :roles => :app, :except => { :no_release => true }) {
              install
            }

            task(:install, :roles => :app, :except => { :no_release => true }) {
              __send__(supervisord_install_method).install if supervisord_install_method
            }

            namespace(:apt) {
{'fraction_non_alphanumeric': 0.12889033729835425, 'fraction_numerical': 0.0, 'mean_word_length': 1.831180811808118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '4874909', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1713, 'n_words': 301}