PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/light_logs/light_logs/src/octane/toolkit/octane_archives.clj

http://lighttexteditor.googlecode.com/
Clojure | 275 lines | 146 code | 31 blank | 98 comment | 20 complexity | 7dea8c95b08a9e72c7ddc06d89ec5204 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-2.0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;; Copyright (c) 2006-2007 Berlin Brown and botnode.com All Rights Reserved
  3. ;;;
  4. ;;; http://www.opensource.org/licenses/bsd-license.php
  5. ;;; All rights reserved.
  6. ;;; Redistribution and use in source and binary forms, with or without modification,
  7. ;;; are permitted provided that the following conditions are met:
  8. ;;; * Redistributions of source code must retain the above copyright notice,
  9. ;;; this list of conditions and the following disclaimer.
  10. ;;; * Redistributions in binary form must reproduce the above copyright notice,
  11. ;;; this list of conditions and the following disclaimer in the documentation
  12. ;;; and/or other materials provided with the distribution.
  13. ;;; * Neither the name of the Botnode.com (Berlin Brown) nor
  14. ;;; the names of its contributors may be used to endorse or promote
  15. ;;; products derived from this software without specific prior written permission.
  16. ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. ;;; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. ;;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. ;;; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. ;;;
  28. ;;; Date: 1/5/2009
  29. ;;; 7/15/2009 - Added Clojure 1.0, other performance fixes and cleanups.
  30. ;;;
  31. ;;; Main Description: Light Log Viewer is a tool for making it easier to search log files.
  32. ;;; Light Log Viewer adds some text highlighting, quick key navigation to text files, simple graphs
  33. ;;; and charts for monitoring logs, file database to quickly navigate to files of interest,
  34. ;;; and HTML to PDF convert tool.
  35. ;;; Light Log was developed with a combination of Clojure 1.0, Java and Scala with use of libs, SWT 3.4, JFreeChart, iText.
  36. ;;;
  37. ;;; Quickstart : the best way to run the Light Log viewer is to click on the win32 batch script light_logs.bat
  38. ;;; (you may need to edit the Linux script for Unix/Linux environments).
  39. ;;; Edit the win32 script to add more heap memory or other parameters.
  40. ;;; The clojure source is contained in : HOME/src/octane
  41. ;;; The java source is contained in : HOME/src/java/src
  42. ;;; To build the java source, see : HOME/src/java/build.xml and build_pdf_gui.xml
  43. ;;; Metrics: (as of 7/15/2009) Light Log Viewer consists of 6500 lines of Clojure code, and contains wrapper code
  44. ;;; around the Java source. There are 2000+ lines of Java code in the Java library for Light Log Viewer.
  45. ;;; Additional Development Notes: The SWT gui and other libraries are launched from a dynamic classloader. Clojure is also
  46. ;;; started from the same code, and reflection is used to dynamically initiate Clojure. See the 'start' package. The binary
  47. ;;; code is contained in the octane_start.jar library.
  48. ;;; Home Page: http://code.google.com/p/lighttexteditor/
  49. ;;;
  50. ;;; Contact: Berlin Brown <berlin dot brown at gmail.com>
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. (ns octane.toolkit.octane_archives
  53. (:use
  54. octane.toolkit.octane_main_constants
  55. octane.toolkit.octane_utils_common
  56. octane.toolkit.octane_utils
  57. octane.toolkit.public_objects
  58. octane.toolkit.octane_gui_utils
  59. octane.toolkit.octane_config
  60. octane.toolkit.octane_file_utils)
  61. (:import
  62. (java.io File)
  63. (org.eclipse.swt SWT)
  64. (org.eclipse.swt.widgets Display Shell Text Widget TabFolder TabItem)
  65. (org.eclipse.swt.widgets FileDialog MessageBox TableItem Button
  66. Composite Table TableColumn)
  67. (org.eclipse.swt.layout GridData GridLayout RowLayout RowData)
  68. (org.eclipse.swt.events VerifyListener SelectionAdapter ModifyListener SelectionListener
  69. SelectionEvent ShellAdapter ShellEvent)
  70. (org.eclipse.swt.widgets Label Menu MenuItem Control Listener)
  71. (com.octane.util.zip UncompressInputStream)
  72. (java.io FileInputStream InputStream ByteArrayOutputStream FileOutputStream)
  73. (java.util.zip ZipInputStream InflaterInputStream)
  74. (java.io InputStreamReader
  75. FileInputStream BufferedReader File FilenameFilter)))
  76. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  77. (defn open-archive-has-file?
  78. "Open an archive file and determine if there is only one entry"
  79. [infile]
  80. ;;;;;;;;;;;;
  81. (try (let [zin (new ZipInputStream (new FileInputStream infile))]
  82. (loop [entry (. zin getNextEntry)
  83. ctr 0]
  84. (when entry
  85. (recur (. zin getNextEntry) (+ ctr 1))))
  86. (. zin close))
  87. (catch Exception e (. e printStackTrace))))
  88. (defn open-compressed-file
  89. "Open an archive file and LZC unix compressed .Z extension"
  90. [infile]
  91. ;;;;;;;;;;;;
  92. (try (let [zin (new UncompressInputStream (new FileInputStream infile))
  93. bbuf #^"[B" (make-array (. Byte TYPE) 20480)
  94. bout (new ByteArrayOutputStream 20480)]
  95. (loop [got (. zin read bbuf)
  96. tot 0]
  97. (when (> got 0)
  98. (. bout write bbuf, 0 got)
  99. (recur (. zin read bbuf) (+ tot 1))))
  100. ;; With the byte array outputstream
  101. ;; Convert the bytes to string
  102. (. bout flush)
  103. (. zin close)
  104. (new String (. bout toByteArray)))
  105. (catch Exception e
  106. (. e printStackTrace)
  107. nil)))
  108. (defn stream-compressed-file
  109. "Open an archive file and LZC unix compressed .Z extension"
  110. [infile #^java.io.OutputStream out]
  111. ;;;;;;;;;;;;
  112. (println "DEBUG: attempting to open archive file => " infile)
  113. (try (let [zin (new UncompressInputStream (new FileInputStream infile))
  114. bbuf #^"[B" (make-array (. Byte TYPE) 20480)]
  115. (loop [got (. zin read bbuf)
  116. tot 0]
  117. (when (> got 0)
  118. (. out write bbuf, 0 got)
  119. (recur (. zin read bbuf) (+ tot 1))))
  120. ;; With the byte array outputstream
  121. ;; Convert the bytes to string
  122. (. out flush)
  123. (. zin close)
  124. (. out close))
  125. (catch Exception e
  126. (. e printStackTrace)
  127. nil)))
  128. (defn win-open-compressed-file
  129. "Open an archive file and LZC unix compressed .Z extension"
  130. [infile]
  131. ;;;;;;;;;;;;
  132. (let [data (open-compressed-file infile)]
  133. (async-status-history *display* (str "Open LZC (unix compressed) file => " infile *newline*))
  134. (async-add-main-text data)))
  135. (defn check-archive-handler
  136. "Determine the archive type based on the extension and handle accordingly"
  137. [disp file path]
  138. ;;;;;;;;;;;;;;;;;
  139. (cond (. path endsWith ".Z") (win-open-compressed-file file)
  140. (. path endsWith ".jar") (open-jar-file file)
  141. (. path endsWith ".zip") (println "Not implemented")
  142. :default (println "Not implemented")))
  143. (defn open-archive-file-handler
  144. "Open the archive file. Open the file in the main buffer if only ONE text file exists.
  145. If more than one exists than just list the entries."
  146. [disp file path]
  147. ;;;;;;;;;;;;;;;;;
  148. (check-archive-handler disp file path))
  149. (def open-archive-file-listener
  150. ;; Open the archive file. Open the file in the main buffer if only ONE text file exists.
  151. ;; If more than one exists than just list the entries."
  152. ;;;;;;;
  153. (proxy [SelectionAdapter] []
  154. (widgetSelected [e] (simple-dialog-open-file
  155. *display* open-archive-file-handler *zip-wildcard-seq* ))))
  156. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  157. ;; Archive Directory Handlers
  158. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  159. (def uncompress-filename-filter
  160. (proxy [FilenameFilter] []
  161. (accept [dir-file name]
  162. (if name
  163. (.endsWith name ".Z")
  164. false))))
  165. (defn zfiles-dir-handler
  166. "zfile-func takes two arguments, the input 'file' object, the output tmp directory"
  167. [file-path zfile-func tmp-dir]
  168. (let [file (new File file-path)]
  169. (doseq [cur-fil (.listFiles file uncompress-filename-filter)]
  170. (zfile-func cur-fil tmp-dir))))
  171. (def *invalid-dir-chars* "(\\\\|/|:|\\s|\\.)")
  172. (defn get-unique-path-name
  173. "We need to ensure that the tmp archive files have unique names.
  174. Take the directory name and append to the file name"
  175. [#^java.io.File file]
  176. ;;;;;;;;;;;;;;;;;;;;;;;
  177. (let [pfile (.getParentFile file)
  178. path (.getAbsolutePath pfile)]
  179. ;; Replace forward and backward slash with underscore
  180. (str "_" (.replaceAll path *invalid-dir-chars* "_") "_")))
  181. (defn stream-zfile-handler
  182. "Where -stream-func takes two arguments:
  183. File and FileOutputStream
  184. -create-file-func takes one argument File and returns a boolean"
  185. [my-date-str stream-func & [create-file-func? start-t end-t]]
  186. (fn [#^java.io.File file tmp-dir]
  187. ;; Create the output stream object.
  188. (let [fname (str tmp-dir *name-separator* (.getName file) (get-unique-path-name file) ".log")
  189. last-mod (. file lastModified)
  190. my-date-str-1 (str my-date-str " " (if (not (empty? start-t)) (.trim start-t) "00:01") ":00")
  191. my-date-str-2 (str my-date-str " " (if (not (empty? end-t)) (.trim end-t) "23:59") ":00")
  192. my-date-1 (.parse *simple-date-format-t* my-date-str-1)
  193. my-date-2 (.parse *simple-date-format-t* my-date-str-2)]
  194. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  195. ;; Also check if the last modified time is between
  196. ;; the current date we are interested in.
  197. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  198. (when (and (> last-mod (.getTime my-date-1))
  199. (< last-mod (.getTime my-date-2))
  200. (create-file-func? file))
  201. ;; create the new text file unarchived text file
  202. (let [fos (new FileOutputStream fname)]
  203. (try (stream-func file fos)
  204. (catch Exception e (.printStackTrace e))
  205. (finally (.flush fos) (.close fos) (println "Closing stream file"))))))))
  206. (defn simple-stream-zfile-handler
  207. [my-date-str]
  208. (stream-zfile-handler my-date-str
  209. (fn [#^java.io.File file #^java.io.FileOutputStream fos]
  210. (println "Streaming uncompressed file to tmp directory")
  211. (stream-compressed-file file fos)
  212. (println "Done Streaming file =>" (.getAbsolutePath file)))))
  213. (defn archive-search-grep
  214. [#^String filename term]
  215. ;;;;;;;;;;;;;
  216. (let [buf (new StringBuffer)]
  217. (doc-file-loop-handler filename
  218. (fn [line line-num]
  219. ;; Lambda function for on find string
  220. ;; Update the display with the 'grep' information.
  221. (when (simple-grep? line term)
  222. (.append buf
  223. (str filename ": line " line-num ": "
  224. (.trim line) \newline)))))
  225. (if (empty? (.toString buf)) nil (.toString buf))))
  226. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  227. ;; REVISION HISTORY - Light Logs Clojure Source
  228. ;;
  229. ;; -------------------------------------
  230. ;; + 1/5/2009 Berlin Brown : Project Create Date
  231. ;; + 1/5/2009 Berlin Brown : Add new headers
  232. ;; + 6/23/2009 : Major bug fixes
  233. ;; + 6/23/2009 : Move database file to classpath
  234. ;; + 6/23/2009 : Colorize log file
  235. ;; + 6/23/2009 : Show number of lines in a file
  236. ;; + 6/23/2009 : Quick Merge Files Together
  237. ;; + 6/23/2009 : Filter only the lines that have search terms and which line number
  238. ;; + 6/23/2009 : Have an additional merger but no true time merge (see cat command)
  239. ;; + 6/23/2009 : Print number of lines in buffer
  240. ;; -------------------------------------
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  243. ;; End of Script
  244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;