<commit_before>(ns apoya.services.apps
  (:require [apoya.config :as cfg]
            [apoya.resources.fs :as fs]
            [markdown.core :as md]
            [clojure.edn :as e]
            [org.jclouds.blobstore2 :as bstore]
            [clojure.tools.logging :as log]
            [clojure.string :as s]
            [clojure.java.io :as io])
  (:import [java.util.jar JarFile]))

(defn- read-entry [jar-file entry]
  (let [entry (.getEntry jar-file entry)]
    (with-open [is (.getInputStream jar-file entry)]
      (slurp is))))

(defn read-details [path]
  (let [apps-path (:apps-path (cfg/apoya-config))
        full-path (str apps-path "/" path)
        file (io/file full-path)
        jar-file (JarFile. file)
        changelog (-> (read-entry jar-file "classpath/APP_CHANGELOG.md") md/md-to-html-string)
        {:keys [commit version app-name]} (-> (read-entry jar-file "classpath/app-meta.edn") e/read-string)]
    {:commit commit :version version :changelog changelog :app-name app-name}))

(defn put-entry [jar-file entry]
  (let [parts (s/split (.getName entry) #"/")
        [container & parts] parts
        path (s/join "/" parts)]
    (if (.isDirectory entry)
      (fs/create-dir container path)
      (with-open [is (.getInputStream jar-file entry)]
        (log/info container path)
        (fs/put-any-blob container path is)))
    (if (and (= "classpath" container)
             (.endsWith path ".clj"))
      (let [nspace (-> path
                       (s/replace #".clj$" "")
                       (s/replace #"/" ".")
                       (symbol))]
        (log/info "Removing namespace" nspace)
        (remove-ns nspace)
        (log/info "Restoring namespace" nspace)
        (with-open [is (.getInputStream jar-file entry)]
          (eval (slurp is)))))))

(defn copy-contents [path]
  (let [apps-path (:apps-path (cfg/apoya-config))
        full-path (str apps-path "/" path)
        file (-> full-path (io/file) (JarFile.))
        file-entries (enumeration-seq (.entries file))]
    (doseq [entry file-entries]
      (try
        (put-entry file entry)
        (catch Exception e
          (log/fatal e "Cannot load entry" full-path))))))

(defn deploy [path]
  (let [containers ["classpath" "sites"]]
    (doseq [c containers]
      (bstore/clear-container fs/sites-base c))
    (copy-contents path)
    true))
<commit_msg>Reload apps<commit_after>(ns apoya.services.apps
  (:require [apoya.config :as cfg]
            [apoya.resources.fs :as fs]
            [markdown.core :as md]
            [clojure.edn :as e]
            [org.jclouds.blobstore2 :as bstore]
            [clojure.tools.logging :as log]
            [clojure.string :as s]
            [clojure.java.io :as io])
  (:import [java.util.jar JarFile]))

(defn- read-entry [jar-file entry]
  (let [entry (.getEntry jar-file entry)]
    (with-open [is (.getInputStream jar-file entry)]
      (slurp is))))

(defn read-details [path]
  (let [apps-path (:apps-path (cfg/apoya-config))
        full-path (str apps-path "/" path)
        file (io/file full-path)
        jar-file (JarFile. file)
        changelog (-> (read-entry jar-file "classpath/APP_CHANGELOG.md") md/md-to-html-string)
        {:keys [commit version app-name]} (-> (read-entry jar-file "classpath/app-meta.edn") e/read-string)]
    {:commit commit :version version :changelog changelog :app-name app-name}))

(defn put-entry [jar-file entry]
  (let [parts (s/split (.getName entry) #"/")
        [container & parts] parts
        path (s/join "/" parts)]
    (if (.isDirectory entry)
      (fs/create-dir container path)
      (with-open [is (.getInputStream jar-file entry)]
        (log/info container path)
        (fs/put-any-blob container path is)))))

(defn reload-entry [jar-file entry]
  (let [parts (s/split (.getName entry) #"/")
        [container & parts] parts
        path (s/join "/" parts)]
    (if (and (= "classpath" container)
             (.endsWith path ".clj"))
      (let [nspace (-> path
                       (s/replace #".clj$" "")
                       (s/replace #"/" ".")
                       (symbol))]
        (log/info "Restoring namespace" nspace)
        (with-open [is (.getInputStream jar-file entry)]
          (eval (slurp is)))))))

(defn copy-contents [path]
  (let [apps-path (:apps-path (cfg/apoya-config))
        full-path (str apps-path "/" path)
        file (-> full-path (io/file) (JarFile.))
        file-entries (enumeration-seq (.entries file))]
    (doseq [entry file-entries]
      (try
        (put-entry file entry)
        (catch Exception e
          (log/fatal e "Cannot load entry" full-path))))))

(defn refresh-classpath [path]
  (let [apps-path (:apps-path (cfg/apoya-config))
        full-path (str apps-path "/" path)
        file (-> full-path (io/file) (JarFile.))
        file-entries (enumeration-seq (.entries file))]
    (doseq [entry file-entries]
      (try
        (reload-entry file entry)
        (catch Exception e
          (log/fatal e "Cannot load entry" full-path))))))

(defn deploy [path]
  (let [containers ["classpath" "sites"]]
    (doseq [c containers]
      (bstore/clear-container fs/sites-base c))
    (copy-contents path)
    (refresh-classpath path)
    true))

{'fraction_non_alphanumeric': 0.1581866870696251, 'fraction_numerical': 0.00038255547054322876, 'mean_word_length': 2.2079754601226993, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '5193633', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1720, 'n_words': 450}