Skip to content

Commit

Permalink
[scripts] use cl-json
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Hall <hsw@openmoko.com>
  • Loading branch information
hxw committed Aug 22, 2012
1 parent 026e04b commit 128ddab
Showing 1 changed file with 42 additions and 97 deletions.
139 changes: 42 additions & 97 deletions scripts/GenerateIndex
Expand Up @@ -23,9 +23,14 @@ exec sbcl --script "$0" "$0" "$@"
;;; You should have received a copy of the GNU General Public License ;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.


(load (merge-pathnames "quicklisp/setup.lisp"
(user-homedir-pathname)))

(require 'asdf) (require 'asdf)
(require 'getopt) (require 'getopt)
(require 'md5) (require 'md5)
(require 'cl-json)



(defvar *program* (second sb-ext:*posix-argv*) "name of this script file") (defvar *program* (second sb-ext:*posix-argv*) "name of this script file")
(defvar *debug* nil "set true for debug output") (defvar *debug* nil "set true for debug output")
Expand Down Expand Up @@ -200,109 +205,52 @@ exec sbcl --script "$0" "$0" "$@"
collect (append (identify k) (list v))))) collect (append (identify k) (list v)))))




(defvar *xml-indent* 0 "for indenting the XML output")
(defvar *xml-stack* nil "for xml-close to match the tags")
(defvar *xml-stream* t "current xml output stream")

(defun xml-set-stream (out-stream)
"set the xml output stream"
(if out-stream
(setq *xml-stream* out-stream)
(setq *xml-stream* t))) ;; sets output to stdout

(defun xml-current-indent ()
"a number of spaces corresponding to *xml-indent*"
(make-string *xml-indent* :initial-element #\Space))


(defun xml-string-of (item)
"return string of item, but downcase symbols"
(if (symbolp item)
(string-downcase item)
(format nil "~a" item)))


(defun xml-attribute-string (attributes)
"convert: ((key value)...) to string form: key=''value''..."
(loop for attr in attributes
append (list (format nil " ~a=\"~a\""
(xml-string-of (first attr))
(xml-string-of (second attr))))
into attr-list
finally
(return (apply #'concatenate 'string attr-list))))


(defun xml-entry (tag data &rest attributes)
"display a single line XML entry"
(format *xml-stream* "~a<~a~a>~a</~a>~%" (xml-current-indent)
(xml-string-of tag)
(xml-attribute-string attributes)
data
(xml-string-of tag)))


(defun xml-open (tag &rest attributes)
"start the XML tag"
(format *xml-stream* "~a<~a~a>~%" (xml-current-indent) (xml-string-of tag) (xml-attribute-string attributes))
(setq *xml-indent* (+ *xml-indent* 2))
(push (xml-string-of tag) *xml-stack*))


(defun xml-close ()
"close the most recently opened tag"
(when *xml-stack*
(setq *xml-indent* (- *xml-indent* 2))
(format *xml-stream* "~a</~a>~%" (xml-current-indent) (pop *xml-stack*))))


(defun output-archive (file-list uncompressed-size)
"output xml for a single archive ((file size MD5)...)"
(xml-open 'archives (list 'count (length file-list)) (list 'size uncompressed-size))
(loop
for count = 1 then (1+ count)
for item in file-list
do
(xml-entry 'file (concatenate 'string (first item)"?torrent")
(list 'id count) (list 'size (second item)) (list 'md5 (third item))))
(xml-close))


(defun output-volumes (structure &key base) (defun output-volumes (structure &key base)
"output the volume list" "output the volume list"
(loop (loop
for item in structure for item in structure
with count = 0
when (eq base (string= (first item) ".")) when (eq base (string= (first item) "."))
do collect
(incf count)
(xml-open 'volume (list 'id count))
(let* ((lang (third item)) (let* ((lang (third item))
(id (first lang)) (id (first lang))
(english (second lang)) (english (second lang))
(native (third lang))) (native (third lang))
(xml-entry 'language native (list 'id id) (list 'name english))) (name (second (fourth item)))
(xml-entry 'name (second (fourth item))) (date (second item))
(xml-entry 'date (second item)) (folder (first item))
(xml-entry 'folder (first item)) (a (fifth item))
(let* ((a (fifth item))
(uncompressed-size (first a)) (uncompressed-size (first a))
(archives (rest a))) (archives (rest a)))
(output-archive archives uncompressed-size)) (list
(xml-close))) (cons 'id id)

(cons 'english english)

(cons 'native native)
(defun output-structure (dir-name structure) (cons 'name name)
"output the XML structure" (cons 'date date)
(xml-open 'release) (cons 'folder folder)
(xml-entry 'version (file-namestring dir-name)) (cons 'size uncompressed-size)
(xml-open 'compatibility '(select single)) (cons 'files (map 'list #'make-file-item archives))))))
(output-volumes structure :base t)
(xml-close)
(xml-open 'documents '(select multiple)) (defun make-file-item (file)
(output-volumes structure) "create a file object"
(xml-close) (let* ((name (first file))
(xml-close)) (size (second file))
(md5 (third file)))
(list
(cons 'name name)
(cons 'size size)
(cons 'md5 md5))))


(defun output-structure (stream dir-name structure)
"output the JSON structure"
(let ((bases (output-volumes structure :base t))
(volumes (output-volumes structure))
(table (make-hash-table :test #'equal)))
(setf (gethash 'bases table) bases)
(setf (gethash 'volumes table) volumes)
(json:encode-json table stream)))




(defun main (args) (defun main (args)
Expand Down Expand Up @@ -337,14 +285,11 @@ exec sbcl --script "$0" "$0" "$@"
(load-languages languages) (load-languages languages)
(with-open-file (out-stream index :direction :output :if-exists :supersede (with-open-file (out-stream index :direction :output :if-exists :supersede
:if-does-not-exist :create) :if-does-not-exist :create)
(xml-set-stream out-stream)
;;(xml-set-stream t)
(loop for dir in (sort directories #'string<) (loop for dir in (sort directories #'string<)
do do
(when *verbose* (when *verbose*
(format t "Directory: ~a~%" dir)) (format t "Directory: ~a~%" dir))
(output-structure dir (partition-files dir (list-of-files dir)))) (output-structure out-stream dir (partition-files dir (list-of-files dir))))))))
(xml-set-stream nil)))))




;;; run the main program ;;; run the main program
Expand Down

0 comments on commit 128ddab

Please sign in to comment.