<commit_before>(ns circle.backend.build.config
  "Functions for reading and parsing the build config"
  (:use [clojure.core.incubator :only (-?>)])
  (:require [circle.backend.ssh :as ssh])
  (:require [clojure.string :as str])
  (:require [circle.backend.build :as build])
  (:require [circle.backend.build.run :as run])
  (:require [circle.model.project :as project])
  (:require [circle.model.spec :as spec])
  (:require [circle.backend.github-url :as github])
  (:require [circle.backend.ec2 :as ec2])
  (:require [circle.backend.git :as git])
  (:require [circle.backend.build.template :as template])
  (:require [circle.backend.build.nodes.rails :as rails])
  (:use [clojure.tools.logging :only (infof error)])
  (:use [circle.backend.action.bash :only (bash)])
  (:use [circle.util.model-validation :only (validate!)])
  (:use [circle.util.model-validation-helpers :only (is-map? require-predicate require-keys allow-keys)])
  (:use [circle.util.core :only (apply-if)])
  (:use [circle.util.except :only (assert! throw-if-not throwf)])
  (:use [clojure.core.incubator :only (-?>)])
  (:require [circle.backend.build.inference :as inference])
  (:require circle.backend.build.inference.rails)
  (:use [circle.util.map :only (rename-keys)])
  (:require [clj-yaml.core :as yaml])
  (:import java.io.StringReader)
  (:use [arohner.utils :only (inspect)])
  (:use [circle.util.except :only (eat)]))

(defn parse-config
  "Takes the unparsed config file contents."
  [config]
  (-?> config
      (StringReader.)
      yaml/parse-string))

(defn validate-config
  "validate the semantics of the config"
  [c]
  true)

(defn load-config [path]
  (let [config (-> path
                   (slurp)
                   (eat))]
    (when config
      (parse-config config))))

(defn db-config [commands]
  {:nodes {:www
           {:ami (-> circle.backend.nodes.rails/rails-node :ami)
            :instance-type "m1.small"
            :username "ubuntu"
            :security-groups ["www"]
            :availability-zone "us-east-1a"}}
   :jobs {:build
          {:template :build
           :node :www
           :commands commands}}
   :schedule {:commit
              {:job :build}}})

(defn spec-commands [spec]
  (->> spec
       ((juxt :setup :dependencies :compile :test))
       (mapcat (fn [line]
                 (str/split line #"\r\n")))
       (filter #(> (.length %) 0))))

(defn get-config-from-db [url]
  (let [project (project/get-by-url url)
        spec (spec/get-spec-for-project project)
        commands (spec-commands spec)]
    (when commands
      (db-config commands))))

(defn get-config-for-url
  "Given the canonical git URL for a repo, find and return the config file. Clones the repo if necessary."
  [url & {:keys [vcs-revision]}]
  {:pre [url]}
  (let [ssh-key (project/ssh-key-for-url url)
        repo (git/default-repo-path url)
        git-url (if (github/github? url)
                  (github/->ssh url)
                  url)]<commit_msg>When there is no Spec for a project, return nil so that inference will take over.<commit_after>(ns circle.backend.build.config
  "Functions for reading and parsing the build config"
  (:use [clojure.core.incubator :only (-?>)])
  (:require [circle.backend.ssh :as ssh])
  (:require [clojure.string :as str])
  (:require [circle.backend.build :as build])
  (:require [circle.backend.build.run :as run])
  (:require [circle.model.project :as project])
  (:require [circle.model.spec :as spec])
  (:require [circle.backend.github-url :as github])
  (:require [circle.backend.ec2 :as ec2])
  (:require [circle.backend.git :as git])
  (:require [circle.backend.build.template :as template])
  (:require [circle.backend.build.nodes.rails :as rails])
  (:use [clojure.tools.logging :only (infof error)])
  (:use [circle.backend.action.bash :only (bash)])
  (:use [circle.util.model-validation :only (validate!)])
  (:use [circle.util.model-validation-helpers :only (is-map? require-predicate require-keys allow-keys)])
  (:use [circle.util.core :only (apply-if)])
  (:use [circle.util.except :only (assert! throw-if-not throwf)])
  (:use [clojure.core.incubator :only (-?>)])
  (:require [circle.backend.build.inference :as inference])
  (:require circle.backend.build.inference.rails)
  (:use [circle.util.map :only (rename-keys)])
  (:require [clj-yaml.core :as yaml])
  (:import java.io.StringReader)
  (:use [arohner.utils :only (inspect)])
  (:use [circle.util.except :only (eat)]))

(defn parse-config
  "Takes the unparsed config file contents."
  [config]
  (-?> config
      (StringReader.)
      yaml/parse-string))

(defn validate-config
  "validate the semantics of the config"
  [c]
  true)

(defn load-config [path]
  (let [config (-> path
                   (slurp)
                   (eat))]
    (when config
      (parse-config config))))

(defn db-config [commands]
  {:nodes {:www
           {:ami (-> circle.backend.nodes.rails/rails-node :ami)
            :instance-type "m1.small"
            :username "ubuntu"
            :security-groups ["www"]
            :availability-zone "us-east-1a"}}
   :jobs {:build
          {:template :build
           :node :www
           :commands commands}}
   :schedule {:commit
              {:job :build}}})

(defn spec-commands [spec]
  (->> spec
       ((juxt :setup :dependencies :compile :test))
       (mapcat (fn [line]
                 (str/split line #"\r\n")))
       (filter #(> (.length %) 0))))

(defn get-config-from-db [url]
  (let [project (project/get-by-url url)
        spec (spec/get-spec-for-project project)]
    (when spec
      (println spec)
      (let [commands (spec-commands spec)]
        (db-config commands)))))

(defn get-config-for-url
  "Given the canonical git URL for a repo, find and return the config file. Clones the repo if necessary."
  [url & {:keys [vcs-revision]}]
  {:pre [url]}
  (let [ssh-key (project/ssh-key-for-url url)
        repo (git/default-repo-path url)
        git-url (if (github/github? url)
                  (github/->ssh url)
                  url)]
{'fraction_non_alphanumeric': 0.16465131687924467, 'fraction_numerical': 0.001656451880072884, 'mean_word_length': 2.860613810741688, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4993657', 'n_tokens_mistral': 2094, 'n_tokens_neox': 1968, 'n_words': 519}