<commit_before>require 'deployml/exceptions/invalid_config'
require 'deployml/exceptions/unknown_scm'
require 'deployml/configuration'
require 'deployml/utils'

module DeploYML
  class Project

    include Utils

    # The configuration file name.
    CONFIG_FILE = 'deploy.yml'

    # Directories to search within for the deploy.yml file.
    SEARCH_DIRS = ['config','.']

    STAGING_DIR = '.deploy'

    # Mapping of possible :scm values to their SCM handler classes.
    SCMS = {
      :sub_version => SCM::SubVersion,
      :subversion => SCM::SubVersion,
      :svn => SCM::SubVersion,
      :hg => SCM::Mercurial,
      :mercurial => SCM::Mercurial,
      :git => SCM::Git,
      :rsync => SCM::Rsync
    }

    # The staging directory for deploying the project
    attr_reader :staging_dir

    # The project configuration
    attr_reader :config

    def initialize(path)
      unless File.file?(path)
        raise(InvalidConfig,"Could not find the DeploYML configuration file #{path.dump}",caller)
      end

      @path = File.expand_path(path)
      @staging_dir = File.join(File.dirname(@path),STAGING_DIR)

      config = YAML.load_file(@path)

      unless config.kind_of?(Hash)
        raise(InvalidConfig,"The DeploYML configuration file #{path.dump} must contain a Hash",caller)
      end

      @config = Configuration.new(config)

      initialize_scm
    end

    def initialize_scm
      unless SCMS.has_key?(config.scm)
        raise(InvalidConfig,"Unknown SCM #{config.scm} given for the :scm option",caller)
      end

      extend SCMS[config.scm]

      super()
    end

    #
    # Searches for the configuration file within various common directories.
    #
    # @return [Project]
    #   The project described by the configuration file.
    #
    # @raise [InvalidConfig]
    #   The configuration file could not be found in any of the common
    #   directories.
    #
    def Project.find
      path = SEARCH_DIRS.find do |dir|
        File.directory?(File.join(dir,CONFIG_FILE))
      end<commit_msg>Allow Project.find to be passed a root directory.<commit_after>require 'deployml/exceptions/invalid_config'
require 'deployml/exceptions/unknown_scm'
require 'deployml/configuration'
require 'deployml/utils'

module DeploYML
  class Project

    include Utils

    # The configuration file name.
    CONFIG_FILE = 'deploy.yml'

    # Directories to search within for the deploy.yml file.
    SEARCH_DIRS = ['config','.']

    STAGING_DIR = '.deploy'

    # Mapping of possible :scm values to their SCM handler classes.
    SCMS = {
      :sub_version => SCM::SubVersion,
      :subversion => SCM::SubVersion,
      :svn => SCM::SubVersion,
      :hg => SCM::Mercurial,
      :mercurial => SCM::Mercurial,
      :git => SCM::Git,
      :rsync => SCM::Rsync
    }

    # The staging directory for deploying the project
    attr_reader :staging_dir

    # The project configuration
    attr_reader :config

    def initialize(path)
      unless File.file?(path)
        raise(InvalidConfig,"Could not find the DeploYML configuration file #{path.dump}",caller)
      end

      @path = File.expand_path(path)
      @staging_dir = File.join(File.dirname(@path),STAGING_DIR)

      config = YAML.load_file(@path)

      unless config.kind_of?(Hash)
        raise(InvalidConfig,"The DeploYML configuration file #{path.dump} must contain a Hash",caller)
      end

      @config = Configuration.new(config)

      initialize_scm
    end

    def initialize_scm
      unless SCMS.has_key?(config.scm)
        raise(InvalidConfig,"Unknown SCM #{config.scm} given for the :scm option",caller)
      end

      extend SCMS[config.scm]

      super()
    end

    #
    # Searches for the configuration file within various common directories.
    #
    # @param [String] root
    #   The project root directory to search within.
    #
    # @return [Project]
    #   The project described by the configuration file.
    #
    # @raise [InvalidConfig]
    #   The configuration file could not be found in any of the common
    #   directories.
    #
    def Project.find(root=Dir.pwd)
      path = SEARCH_DIRS.find do |dir|
        File.directory?(File.join(root,dir,CONFIG_FILE))
      end
{'fraction_non_alphanumeric': 0.09736404654476372, 'fraction_numerical': 0.0, 'mean_word_length': 2.8256130790190737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '1967334', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1279, 'n_words': 344}