db_import.cljs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. (ns db-import
  2. "Imports given file(s) to a db graph. This script is primarily for
  3. developing the import feature and for engineers who want to customize
  4. the import process"
  5. (:require ["fs" :as fs]
  6. ["fs/promises" :as fsp]
  7. ["path" :as node-path]
  8. [babashka.cli :as cli]
  9. [cljs.pprint :as pprint]
  10. [clojure.set :as set]
  11. [clojure.string :as string]
  12. [datascript.core :as d]
  13. [logseq.common.config :as common-config]
  14. [logseq.common.graph :as common-graph]
  15. [logseq.db.common.sqlite-cli :as sqlite-cli]
  16. [logseq.db.frontend.asset :as db-asset]
  17. [logseq.graph-parser.exporter :as gp-exporter]
  18. [logseq.outliner.cli :as outliner-cli]
  19. [logseq.outliner.pipeline :as outliner-pipeline]
  20. [nbb.classpath :as cp]
  21. [nbb.core :as nbb]
  22. [promesa.core :as p]))
  23. (def tx-queue (atom cljs.core/PersistentQueue.EMPTY))
  24. (def original-transact! d/transact!)
  25. (defn dev-transact! [conn tx-data tx-meta]
  26. (swap! tx-queue (fn [queue]
  27. (let [new-queue (conj queue {:tx-data tx-data :tx-meta tx-meta})]
  28. ;; Only care about last few so vary 10 as needed
  29. (if (> (count new-queue) 10)
  30. (pop new-queue)
  31. new-queue))))
  32. (original-transact! conn tx-data tx-meta))
  33. (defn- build-graph-files
  34. "Given a file graph directory, return all files including assets and adds relative paths
  35. on ::rpath since paths are absolute by default and exporter needs relative paths for
  36. some operations"
  37. [dir*]
  38. (let [dir (node-path/resolve dir*)]
  39. (->> (common-graph/get-files dir)
  40. (concat (when (fs/existsSync (node-path/join dir* "assets"))
  41. (common-graph/readdir (node-path/join dir* "assets"))))
  42. (mapv #(hash-map :path %
  43. ::rpath (node-path/relative dir* %))))))
  44. (defn- <read-file
  45. [file]
  46. (p/let [s (fsp/readFile (:path file))]
  47. (str s)))
  48. (defn- <read-asset-file [file assets]
  49. (p/let [buffer (fs/readFileSync (:path file))
  50. checksum (db-asset/<get-file-array-buffer-checksum buffer)]
  51. (swap! assets assoc
  52. (gp-exporter/asset-path->name (:path file))
  53. {:size (.-length buffer)
  54. :checksum checksum
  55. :type (db-asset/asset-path->type (:path file))
  56. :path (:path file)})
  57. buffer))
  58. (defn- <copy-asset-file [asset-m db-graph-dir]
  59. (p/let [parent-dir (node-path/join db-graph-dir common-config/local-assets-dir)
  60. _ (fsp/mkdir parent-dir #js {:recursive true})]
  61. (if (:block/uuid asset-m)
  62. (fsp/copyFile (:path asset-m) (node-path/join parent-dir (str (:block/uuid asset-m) "." (:type asset-m))))
  63. (when-not (:pdf-annotation? asset-m)
  64. (println "[INFO]" "Copied asset" (pr-str (node-path/basename (:path asset-m)))
  65. "by its name since it was unused.")
  66. (fsp/copyFile (:path asset-m) (node-path/join parent-dir (node-path/basename (:path asset-m))))))))
  67. (defn- notify-user [{:keys [continue debug]} m]
  68. (println (:msg m))
  69. (when (:ex-data m)
  70. (println "Ex-data:" (pr-str (merge (dissoc (:ex-data m) :error)
  71. (when-let [err (get-in m [:ex-data :error])]
  72. {:original-error (ex-data (.-cause err))}))))
  73. (println "\nStacktrace:")
  74. (if-let [stack (some-> (get-in m [:ex-data :error]) ex-data :sci.impl/callstack deref)]
  75. (println (string/join
  76. "\n"
  77. (map
  78. #(str (:file %)
  79. (when (:line %) (str ":" (:line %)))
  80. (when (:sci.impl/f-meta %)
  81. (str " calls #'" (get-in % [:sci.impl/f-meta :ns]) "/" (get-in % [:sci.impl/f-meta :name]))))
  82. (reverse stack))))
  83. (println (some-> (get-in m [:ex-data :error]) .-stack)))
  84. (when debug
  85. (when-let [matching-tx (seq (filter #(and (get-in m [:ex-data :path])
  86. (or (= (get-in % [:tx-meta ::gp-exporter/path]) (get-in m [:ex-data :path]))
  87. (= (get-in % [:tx-meta ::outliner-pipeline/original-tx-meta ::gp-exporter/path]) (get-in m [:ex-data :path]))))
  88. @tx-queue))]
  89. (println (str "\n" (count matching-tx)) "Tx Maps for failing path:")
  90. (pprint/pprint matching-tx))))
  91. (when (and (= :error (:level m)) (not continue))
  92. (js/process.exit 1)))
  93. (defn default-export-options
  94. [options]
  95. {;; common options
  96. :rpath-key ::rpath
  97. :notify-user (partial notify-user options)
  98. :<read-file <read-file
  99. ;; :set-ui-state prn
  100. ;; config file options
  101. ;; TODO: Add actual default
  102. :default-config {}})
  103. (defn- import-file-graph-to-db
  104. "Import a file graph dir just like UI does. However, unlike the UI the
  105. exporter receives file maps containing keys :path and ::rpath since :path
  106. are full paths"
  107. [file-graph-dir db-graph-dir conn options]
  108. (let [*files (build-graph-files file-graph-dir)
  109. config-file (first (filter #(string/ends-with? (:path %) "logseq/config.edn") *files))
  110. _ (assert config-file "No 'logseq/config.edn' found for file graph dir")
  111. options (merge options
  112. (default-export-options options)
  113. ;; asset file options
  114. {:<copy-asset (fn copy-asset [file]
  115. (<copy-asset-file file db-graph-dir))
  116. :<read-asset <read-asset-file})]
  117. (p/with-redefs [d/transact! dev-transact!]
  118. (gp-exporter/export-file-graph conn conn config-file *files options))))
  119. (defn- resolve-path
  120. "If relative path, resolve with $ORIGINAL_PWD"
  121. [path]
  122. (if (node-path/isAbsolute path)
  123. path
  124. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  125. (defn- import-files-to-db
  126. "Import specific doc files for dev purposes"
  127. [file conn {:keys [files] :as options}]
  128. (let [doc-options (gp-exporter/build-doc-options {:macros {}} (merge options (default-export-options options)))
  129. files' (mapv #(hash-map :path %)
  130. (into [file] (map resolve-path files)))]
  131. (p/with-redefs [d/transact! dev-transact!]
  132. (p/let [_ (gp-exporter/export-doc-files conn files' <read-file doc-options)]
  133. {:import-state (:import-state doc-options)}))))
  134. (def spec
  135. "Options spec"
  136. {:help {:alias :h
  137. :desc "Print help"}
  138. :verbose {:alias :v
  139. :desc "Verbose mode"}
  140. :debug {:alias :d
  141. :desc "Debug mode"}
  142. :continue {:alias :c
  143. :desc "Continue past import failures"}
  144. :all-tags {:alias :a
  145. :desc "All tags convert to classes"}
  146. :tag-classes {:alias :t
  147. :coerce []
  148. :desc "List of tags to convert to classes"}
  149. :files {:alias :f
  150. :coerce []
  151. :desc "Additional files to import"}
  152. :remove-inline-tags {:alias :r
  153. :desc "Remove inline tags"}
  154. :property-classes {:alias :p
  155. :coerce []
  156. :desc "List of properties whose values convert to classes"}
  157. :property-parent-classes
  158. {:alias :P
  159. :coerce []
  160. :desc "List of properties whose values convert to a parent class"}})
  161. (defn -main [args]
  162. (let [[file-graph db-graph-dir] args
  163. options (cli/parse-opts args {:spec spec})
  164. _ (when (or (< (count args) 2) (:help options))
  165. (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
  166. (cli/format-opts {:spec spec})))
  167. (js/process.exit 1))
  168. init-conn-args (sqlite-cli/->open-db-args db-graph-dir)
  169. db-name (if (= 1 (count init-conn-args)) (first init-conn-args) (second init-conn-args))
  170. db-full-dir (if (= 1 (count init-conn-args))
  171. (node-path/dirname (first init-conn-args))
  172. (apply node-path/join init-conn-args))
  173. file-graph' (resolve-path file-graph)
  174. conn (apply outliner-cli/init-conn (conj init-conn-args {:classpath (cp/get-classpath)
  175. :import-type :cli/db-import}))
  176. directory? (.isDirectory (fs/statSync file-graph'))
  177. user-options (cond-> (merge {:all-tags false} (dissoc options :verbose :files :help :continue))
  178. ;; coerce option collection into strings
  179. (:tag-classes options)
  180. (update :tag-classes (partial mapv str))
  181. true
  182. (set/rename-keys {:all-tags :convert-all-tags? :remove-inline-tags :remove-inline-tags?}))
  183. _ (when (:verbose options) (prn :options user-options))
  184. options' (merge {:user-options user-options}
  185. (select-keys options [:files :verbose :continue :debug]))]
  186. (p/let [{:keys [import-state]}
  187. (if directory?
  188. (import-file-graph-to-db file-graph' db-full-dir conn options')
  189. (import-files-to-db file-graph' conn options'))]
  190. (when-let [ignored-props (seq @(:ignored-properties import-state))]
  191. (println "Ignored properties:" (pr-str ignored-props)))
  192. (when-let [ignored-assets (seq @(:ignored-assets import-state))]
  193. (println "Ignored assets:" (pr-str ignored-assets)))
  194. (when-let [ignored-files (seq @(:ignored-files import-state))]
  195. (println (count ignored-files) "ignored file(s):" (pr-str (vec ignored-files))))
  196. (when (:verbose options') (println "Transacted" (count (d/datoms @conn :eavt)) "datoms"))
  197. (println "Created graph" (str db-name "!")))))
  198. (when (= nbb/*file* (nbb/invoked-file))
  199. (-main *command-line-args*))