-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubpics.cl
799 lines (745 loc) · 24.9 KB
/
pubpics.cl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
;; Web publisher for digital photos.
;;
;; This software is Copyright (c) Kevin Layer, 2000-2002.
;; Kevin Layer grants you the rights to distribute
;; and use this software as governed by the terms
;; of the Lisp Lesser GNU Public License
;; (http://opensource.franz.com/preamble.html),
;; known as the LLGPL.
;;
;; See readme.txt for more information.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; $Id$
(in-package :user)
(defvar *version* "1.33")
(eval-when (compile)
;; An experimental run-shell-command scheduler that allows multiple,
;; concurrent run-shell-command's, in order to take advantage of multiple
;; processors. Because ImageMagik takes so much memory, this doesn't
;; turn out to save a lot of time (unless you have LOTS of RAM) on the
;; Linux kernel I used (2.2.x). Perhaps in 2.6 it will have an effect.
#+ignore (push :rsc-scheduler *features*)
;;(push :debug-pubpics *features*)
(compile-file-if-needed
(or (probe-file "exif-utils/exifinfo.cl")
#+mswindows (probe-file "c:/src/exif-utils/exifinfo.cl")
(error "Could not find exifinfo.cl"))))
(eval-when (compile eval load)
(require :exifinfo
(or (probe-file "exif-utils/exifinfo.fasl")
#+mswindows (probe-file "c:/src/exif-utils/exifinfo.fasl")))
(use-package :util.exif)
(require :uri)
(require :aserve
(or (probe-file "sys:aserve;aserve.fasl")
(probe-file "sys:code;aserve.fasl")))
(use-package :net.html.generator)
(require :aclwin)
(require :fileutil))
(setq *read-init-files* nil)
(setq excl::*internal-read-init-files* nil)
(setq *print-startup-message* nil)
(setq excl::.dump-lisp-suppress-allegro-cl-banner. t)
(setq excl::*force-quiet-exit* t) ; 6.0
(setq sys::.ignore-command-line-arguments. t)
(defvar *quiet* nil)
(defvar *debug* nil)
(defvar *usage*
"~
Usage: [-a file] [-D] [-p] [-c name] [-n size] [-V] [-q] [-t title]
[-d description] source-dir dest-dir
-a file - annotations file
-B dir - set ImageMagick binary root to `dir'
-D - debug
-c name - for the largest image size, add copyright in a border for
`name'
-d description - addition information for the index page
-n size - max number of images on each index page--probably should
be a multiple of 3, since there are three thumbnails per
row on each index page
-p - pause after completion
-V - print version info and exit
-q - quiet mode--do not print informative messages
-r - recurse on source-dir
-t title - set title of generated pages to `title', and use this
title at the top of each photo page
source-dir - directory containing images
dest-dir - non-existent directory for web pages
")
(defvar *image-magick-root*
;; must be in path
nil)
(defvar *quality* 90
;; used to use quality 50, but it's really crappy with the LX3, for some
;; reason.
)
(defvar *large-divisor* 1.5 "How much `large' images are scaled down.")
(defvar *medium-divisor* 2 "How much `medium' images are scaled down.")
(defvar *small-divisor* 3 "How much `small' images are scaled down.")
(defvar .cleanup-forms. nil)
(defvar .annotations. nil)
(defvar .error-sleep. nil)
(defun pubpics-init-function (&aux do-pause)
;; The following makes run-shell-command interruptable. Why, I have no
;; idea. It appears that /bin/csh fixes something in how our signal
;; handling works.
#+linux (setf (sys:getenv "SHELL") "/bin/csh")
(flet ((doit ()
(sys:with-command-line-arguments
("a:B:c:Dd:fn:pqrt:V"
annotations-file image-magick-root copyright debug description
force-flag index-size pause *quiet* recurse title
print-version-and-exit)
(rest)
(when pause (setq do-pause t))
(when print-version-and-exit
(format t "pubpics: ~a~%" *version*)
(exit 0 :quiet t))
(when (/= 2 (length rest)) (error *usage*))
(when debug
(trace pubpics)
(setq *debug* t))
(when annotations-file
(when (not (probe-file annotations-file))
(error "Annotations file (~a) does not exist."
annotations-file))
(with-open-file (s annotations-file :direction :input)
(setq .annotations. (ignore-errors (read s)))))
(when image-magick-root
(when (not (probe-file image-magick-root))
(error "~a does not exist." image-magick-root))
(setq *image-magick-root*
(pathname-as-directory image-magick-root)))
#+rsc-scheduler (setq *quiet* t)
(pubpics (first rest) (second rest) :force-flag force-flag
:title title :description description
:copyright copyright
:recurse recurse
:index-size (when index-size
(read-from-string index-size)))
#+rsc-scheduler (rsc-finalize)
(when pause
(format t "hit ENTER to continue:")
(force-output)
(read-line))
(exit 0 :quiet t))))
#+debug-pubpics (doit)
#-debug-pubpics
(if* *debug*
then (doit)
else (handler-case
(doit)
(error (c)
(format t "~a~%" c)
(dolist (form .cleanup-forms.) (funcall form c))
(when do-pause
(format t "hit ENTER to continue:")
(force-output)
(read-line))
(exit 1 :quiet t))))))
(defun pubpics (source-dir dest-dir
&key force-flag title description copyright index-size recurse
&aux (pictures '())
(sizes '(:small :medium :large))
i npics)
(setq source-dir (pathname-as-directory (pathname source-dir)))
(setq dest-dir (pathname-as-directory (pathname dest-dir)))
(when (not (probe-file source-dir))
(error "~a does not exist." source-dir))
;; Make sure dest-dir doesn't exist, and remove it if it does and
;; force-flag is non-nil.
(when (probe-file dest-dir)
(if* force-flag
then (when (not (file-directory-p dest-dir))
(error "~a is not a directory." dest-dir))
(delete-directory-and-files dest-dir :force t)
else (error "~a already exists." dest-dir)))
(when (null title)
(setq title (car (last (pathname-directory source-dir)))))
(when (null description)
(setq description
(multiple-value-bind (second minute hour day month year)
(decode-universal-time (file-write-date source-dir))
(declare (ignore second minute hour))
(format nil "~d~a~d"
day
(aref #(0 "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
"Sep" "Oct" "Nov" "Dec")
month)
year))))
(let ((cleanup-form
#'(lambda (&optional condition)
(format t "~&~@[~a~%~]Cleaning up...~%" condition)
(force-output)
(when (probe-file dest-dir)
(delete-directory-and-files dest-dir :force t)))))
(push cleanup-form .cleanup-forms.)
(add-signal-handler
2
#'(lambda (sig cont)
(format t "In SIGINT cleanup handler...~%")
(force-output)
(dolist (form .cleanup-forms.) (funcall form))
(excl::sig-handler-exit sig cont))))
(dolist (dir '("large/" "medium/" "small/" "thumbs/"
"pages/" "pages/small/" "pages/medium/" "pages/large/"))
(ensure-directories-exist (merge-pathnames dir dest-dir)))
(when (not *quiet*)
(format t "~%collecting information:")
(force-output))
(setq i 0)
(map-over-directory
#'(lambda (p)
(when (equalp "jpg" (pathname-type p))
(incf i)
(let ((info (image-info p)))
(push (list p info) pictures))))
source-dir
:recurse recurse)
(when (= i 0)
(error "There are no .jpg (or .JPG) files in ~a." source-dir))
(setq npics i)
(when (not *quiet*) (format t " ~d pictures~%" i))
;; Put the pictures in "numerical" order.
(setq pictures
(handler-case
(let ((sample-file (pathname-name (car (car pictures)))))
(sort (copy-list pictures)
(if* (date-based-filename-p sample-file)
then #'date-based-filename-sort-function
elseif (numbered-filename-p sample-file)
then #'numbered-filename-sort-function
else (error "can't figure out naming scheme"))))
(error ()
;; they might not have the new names, so don't sort if an error
;; occurs.
(format t "~
Could not figure out how to sort files, so not sorting them...")
pictures)))
(when (not *quiet*)
(format t "~%make html pages:")
(force-output))
(dolist (image '("home.gif" "next.gif" "previous.gif" "blank.gif"))
(sys:copy-file
(if* (probe-file image)
thenret
else (merge-pathnames image "sys:"))
(merge-pathnames image dest-dir)))
(dolist (size sizes)
(when (not *quiet*) (format t " ~a" size)(force-output))
(do* ((pictures-info pictures (cdr pictures-info))
(current (car pictures-info) (car pictures-info))
(prev nil picture)
(picture (car current) (car current))
(next #1=(caar (cdr pictures-info)) #1#)
(htm (make-pathname :type "htm"))
(image-number 1 (1+ image-number))
page)
((null picture))
(setq page (merge-pathnames
htm
(merge-pathnames
(file-namestring picture)
(merge-pathnames (format nil "pages/~a/" size)
dest-dir))))
(with-open-file (s page :direction :output)
(make-page size
s
(format nil "../../~a/~a" size (file-namestring picture))
image-number
index-size
(when prev (format nil "~a.htm" (pathname-name prev)))
(when next (format nil "~a.htm" (pathname-name next)))
title
(remove size sizes)))))
(when (not *quiet*) (format t "~%"))
(when (not *quiet*) (format t "~%make index pages:"))
(let ((pictures (mapcar #'car pictures)))
(when (not *quiet*) (format t " index.htm")(force-output))
(make-index :medium "index" dest-dir
pictures title description index-size)
(when (not *quiet*) (format t " index_small.htm")(force-output))
(make-index :small "index_small" dest-dir
pictures title description index-size)
(when (not *quiet*) (format t " index_large.htm")(force-output))
(make-index :large "index_large" dest-dir
pictures title description index-size))
(when (not *quiet*) (format t "~%"))
(when (not *quiet*) (format t "~%generate images:~%"))
(setq i 1)
(dolist (stuff pictures)
(destructuring-bind (picture picture-info) stuff
(when (not *quiet*)
(format t " [~d of ~d] ~a:" i npics (file-namestring picture))
(format t " thumbnail")(force-output))
(make-image
:thumbnail picture-info picture
(merge-pathnames (file-namestring picture)
(merge-pathnames "thumbs/" dest-dir)))
(when (not *quiet*) (format t " large")(force-output))
(make-image
:large picture-info picture
(merge-pathnames (file-namestring picture)
(merge-pathnames "large/" dest-dir))
:copyright copyright)
(when (not *quiet*) (format t " medium")(force-output))
(make-image
:medium picture-info picture
(merge-pathnames (file-namestring picture)
(merge-pathnames "medium/" dest-dir)))
(when (not *quiet*) (format t " small")(force-output))
(make-image
:small picture-info picture
(merge-pathnames (file-namestring picture)
(merge-pathnames "small/" dest-dir)))
(when (not *quiet*) (format t "~%")))
(incf i))
(values))
(defun date-based-filename-p (filename)
(match-regexp (load-time-value (compile-regexp "^[0-9]+-[0-9]+"))
filename
:return nil))
(defun numbered-filename-p (filename)
(match-regexp (load-time-value (compile-regexp "^[a-zA-Z]+[0-9]+"))
filename
:return nil))
(defun date-based-filename-sort-function (item1 item2)
;; return true, iff item1 < item2
(let* ((item1 (mapcar #'read-from-string
(delimited-string-to-list
(pathname-name (car item1))
"-")))
(item2 (mapcar #'read-from-string
(delimited-string-to-list
(pathname-name (car item2))
"-"))))
(do* ((x1 item1 (cdr x1))
(x2 item2 (cdr x2)))
((or (null x1) (null x2)) nil)
(when (< (car x1) (car x2)) (return t))
(when (> (car x1) (car x2)) (return nil)))))
(defun numbered-filename-sort-function (item1 item2)
;; return true, iff item1 < item2
(let* ((re (load-time-value (compile-regexp "[-a-zA-Z_]+\\([0-9]+\\)")))
(n1 (multiple-value-bind (found ignore i)
(match-regexp re (pathname-name (car item1)))
(declare (ignore ignore))
(when (not found) (error "can't parse filename1"))
(read-from-string i)))
(n2 (multiple-value-bind (found ignore i)
(match-regexp re (pathname-name (car item2)))
(declare (ignore ignore))
(when (not found) (error "can't parse filename2"))
(read-from-string i))))
(< n1 n2)))
(defun image-info (file)
(let ((ed (parse-exif-data file)))
(when (null ed) (error "No exif info in ~a!" file))
`((:dimensions ,(exif-info-image-width ed)
,(exif-info-image-length ed)))))
(defun make-image (type info from to &key copyright)
(let* ((dimensions (calc-dimensions type info from))
(x (first dimensions))
(y (second dimensions))
(convert-command
;; The -size argument causes the convert command to operate
;; significantly faster on the thumbnail, medium and small
;; images.
(if* (eq :large type)
then ;; Only put a copyright notice on the large one, since
;; that's the one that would be useful to someone wanting
;; to steal one of my images (hey, it's possible!).
(with-output-to-string (s)
(format s "~
~@[~a~]convert -quality ~a ~
-size ~a~@[x~a~] ~
-geometry \"~a~@[x~a~]>\" "
*image-magick-root*
*quality*
x y x y)
(when copyright
(format
s "~
-border 20x20 -bordercolor black ~
-pointsize 12 ~
-fill white ~
-font ~a ~
-draw \"text 3,9 '~a ~d ~a'\" "
;; maybe newer versions on Windows don't require
;; the absolute path... it's bogus, for sure.
#+mswindows "C:/Winnt/fonts/arialbd.ttf"
#-mswindows "arialbd"
#+mswindows "\\0x00a9" ;; Unicode copyright symbol
#-mswindows "Copyright"
(nth-value 5 (decode-universal-time
(get-universal-time)))
copyright))
(format s "\"~a\" \"~a\""
(forward-slashify (namestring from))
(forward-slashify (namestring to))))
else (format nil
"~
~@[~a~]convert -quality ~a -size ~a~@[x~a~] -geometry \"~a~@[x~a~]>\" \"~a\" \"~a\""
*image-magick-root* *quality* x y x y
(forward-slashify (namestring from))
(forward-slashify (namestring to)))))
(status
(progn
(when *debug* (format t "~a~%~%" convert-command))
#+rsc-scheduler (rsc-scheduler convert-command)
#-rsc-scheduler
(run-shell-command convert-command :show-window :hide))))
#+rsc-scheduler
status
#-rsc-scheduler
(when (/= 0 status)
(error "Convert command failed on ~a with status ~d."
(file-namestring from) status))
t))
(defun calc-dimensions (type info file)
(assert (member type '(:thumbnail :large :medium :small) :test #'eq))
(let* ((dimensions
(or (cdr (assoc :dimensions info))
(error "Could not determine dimensions of ~a." file)))
(width (first dimensions))
(height (second dimensions))
(maxsize
(case type
(:large 1365)
(:medium 1025)
(:small 682)
(:thumbnail 100))))
;; width == current width
;; height == current height
;; maxsize == largest either width or height can be
(if* (>= width height)
then ;; landscape image, using the maxsize as the width to the
;; ImageMagick's convert works fine.
(list maxsize nil)
else ;; portrait image, need to provide scaled W & H
;; w new-width
;; --- = --------------------------
;; h maxsize (aka new-height)
(list
;; new-width =
(floor (* maxsize (/ width height)))
maxsize))))
(defun make-page (size s image image-number index-size previous-page
next-page title other-sizes
&aux (basename (file-namestring image))
(annotation (cdr (assoc basename .annotations.
:test #'string=))))
(html-stream
s
(:head (:title (:princ-safe basename)))
:newline
((:body :bgcolor "#ffffff" :link "#ff0000" :vlink "#52188C")
:newline
((:table :border "0" :cellpadding "5" :cellspacing "2" :width "100%"
:bgcolor "#f0f0f0")
:newline
(:tr
:newline
(:td
:newline
((:a :href
(forward-slashify
(namestring
(merge-pathnames
(page-number-to-index image-number index-size size)
"../../"))))
((:img :src "../../home.gif" :height "30" :width "30"
:border "0" :alt "Home"))))
:newline
(:td
:newline
(if* previous-page
then (html
((:a :href (forward-slashify (namestring previous-page)))
((:img :src "../../previous.gif" :height "30" :width "30"
:border "0" :alt "Previous"))))
else (html
((:img :src "../../blank.gif" :height "30" :width "30"
:border "0" :alt "")))))
:newline
(:td
:newline
(if* next-page
then (html
((:a :href (forward-slashify (namestring next-page)))
:newline
((:img :src "../../next.gif" :height "30" :width "30"
:border "0" :alt "Next"))))
else (html
((:img :src "../../blank.gif" :height "30" :width "30"
:border "0" :alt "")))))
:newline
(:td
:newline
(:h3
(:princ-safe (format nil "~a (~a) " title (file-namestring image)))
(dolist (size other-sizes)
(html
" / "
((:a :href (format nil "../~a/~a.htm" size (pathname-name image)))
(:princ-safe (format nil "~a image" size)))))))))
:newline
(when annotation (html (:p (:princ-safe annotation))))
:newline
(:center
:newline
((:img :src image :border "0" :alt (file-namestring image)))))))
(defun make-index (size index-name dest-dir pictures title description
index-size)
(let* ((n-pictures (length pictures))
(n-pages (ceiling n-pictures index-size)))
(flet
((gen-index (s size title description pictures
&optional indicies prev-index next-index)
(html-stream
s
(:head
:newline
(:title (:princ-safe title))
:newline
((:body :bgcolor "#ffffff" :link "#ff0000" :vlink "#52188C")
:newline
((:table :border "0" :cellpadding "5" :cellspacing "2"
:width "100%" :bgcolor "#f0f0f0")
:newline
(:tr
:newline
(:td
:newline
(:h2 (:princ-safe title))
:newline
(:p (:princ-safe description)))))
:newline
;;;; the index buttons:
(when indicies
(html
(:p
:newline
(:center
:newline
((:table :border "0" :cellpadding "0" :cellspacing "2")
:newline
(:tr
:newline
((:td :colspan "2" :align "center")
(do* ((index indicies (cdr index))
(i 1 (1+ i)))
((null index))
(html ((:a :href (car index)) (:princ i)))
(when (cdr index)
(html " | ")))))
:newline
(:tr
:newline
(if* prev-index
then (html
((:td :align "right" :width "50%")
:newline
((:a :href (file-namestring prev-index))
((:img :src "previous.gif"
:height "30" :width "30"
:border "0"
:alt "Previous index page")))))
else (html ((:td :width "50%") " ")))
:newline
(if* next-index
then (html
((:td :align "left" :width "50%")
:newline
((:a :href (file-namestring next-index))
:newline
((:img :src "next.gif" :height "30" :width "30"
:border "0" :alt "Next index page")))))
else (html ((:td :width "50%") " ")))))))))
;;;; the thumbnails:
:newline
(:center
:newline
((:table :cellspacing 10 :cellpadding 0 :border 0)
:newline
(dolist (row (chop-list pictures 3))
(make-index-row size row))))))))
(index (index-name page dest-dir)
(merge-pathnames
(merge-pathnames
(if* (= page 1)
then index-name
else (format nil "~a~a" index-name page))
(make-pathname :type "htm"))
dest-dir)))
(if* (and index-size (> n-pages 1))
then (do* ((indicies
(do* ((res '())
(page n-pages (1- page)))
((= page 0) res)
(push (file-namestring
(index index-name page dest-dir))
res)))
(page 1 (1+ page))
(prev-index nil index)
(index #1=(index index-name page dest-dir)
#1#)
(next-index
#2=(when (< page n-pages)
(index index-name (1+ page) dest-dir))
#2#))
((> page n-pages))
(with-open-file (s index :direction :output)
(gen-index s size title description
(list-section pictures page index-size)
indicies
prev-index next-index)))
else (with-open-file (s (index index-name 1 dest-dir)
:direction :output)
(gen-index s size title description pictures))))))
(defun make-index-row (size pictures)
(html
(:tr
(dolist (picture pictures)
(let ((picture (format nil "pages/~a/~a.htm" size
(pathname-name picture)))
(tn (format nil "thumbs/~a" (file-namestring picture)))
(name (pathname-name picture)))
(html
((:td :align "center")
((:a :href picture) ((:img :src tn :border 0 :alt name))))
:newline))))
:newline
(:tr
(dolist (picture pictures)
(let ((picture (format nil "pages/~a/~a.htm" size
(pathname-name picture)))
(name (pathname-name picture)))
(html
((:td :align "center" :valign "top")
((:a :href picture) ((:font :size "2") (:princ-safe name))))
:newline))))))
(defun page-number-to-index (image-number index-size size)
(let ((suffix (case size
(:small "_small")
(:medium "")
(:large "_large"))))
(forward-slashify
(namestring
(merge-pathnames
(make-pathname :type "htm")
(if* index-size
then (let ((page-number
(1+ (truncate (1- image-number) index-size))))
(format nil "index~a~a"
suffix
(if* (= page-number 1)
then ""
else page-number)))
else (format nil "index~a" suffix)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; utils
(defun list-section (list section-index section-size)
;; LIST is composed of SECTION-SIZE segments of elements. Return the
;; SECTION-INDEX subsequence of list elements.
(let* ((start (* (1- section-index) section-size))
(end (+ start section-size)))
(subseq list start (min end (length list)))))
(defun chop-list (list n)
(do* ((l list (cdr l))
(i 2 (1+ i))
(tmp '())
(res '()))
((null l)
(when tmp (push (nreverse tmp) res))
(nreverse res))
(push (car l) tmp)
(when (> i n)
(setq i 1)
(push (nreverse tmp) res)
(setq tmp nil))))
(defun forward-slashify (string)
#+mswindows (substitute #\/ #\\ string)
#-mswindows string)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; An experimental run-shell-command scheduler.
#+rsc-scheduler
(defparameter *rsc-number-of-processors* 3)
#+rsc-scheduler
(defparameter *rsc-status*
;; nil or (status . command)
nil)
#+rsc-scheduler
(defparameter *rsc-procs* nil)
#+rsc-scheduler
(defun rsc-scheduler (command)
(when (null *rsc-procs*) (rsc-initialize-processors))
(mp:without-scheduling
(let ((p (pop *rsc-procs*)))
(setq *rsc-procs* (append *rsc-procs* (list p)))
(push command (getf (mp:process-property-list p) :rsc-queue)))))
#+rsc-scheduler
(defun rsc-initialize-processors ()
(setq *rsc-procs* nil)
(setq *rsc-status* nil)
(dotimes (i *rsc-number-of-processors*)
(push
(mp:process-run-function (format nil "rsc processor ~d" i)
#'rsc-process-queue)
*rsc-procs*)))
#+rsc-scheduler
(defun rsc-process-queue (&aux (p mp:*current-process*))
;; our queue is stored on our plist
(loop
(mp:process-wait
"waiting for something to do"
(lambda ()
(or *rsc-status*
(getf (mp:process-property-list p) :rsc-queue))))
(when *rsc-status*
(mp:process-kill p)
(sleep 100000))
(let* ((command (pop (getf (mp:process-property-list p) :rsc-queue)))
status)
(when (eq :die command)
(mp:process-kill p)
(sleep 100000))
(multiple-value-bind (ignore1 ignore2 pid)
(run-shell-command command :show-window :hide :wait nil)
(declare (ignore ignore1 ignore2))
(format t "~a: ~a~%" (mp:process-name p) command)
(mp:process-wait
(format nil "waiting for pid ~d to finish" pid)
(lambda (pid)
(multiple-value-bind (exit-status xpid)
(sys:reap-os-subprocess :wait nil :pid pid)
(if* (not (eql pid xpid))
then ;; something is wrong, the process isn't running
(setq status (or exit-status -1))
t
elseif exit-status
then ;; process finished
(setq status exit-status)
t
else ;; still running
nil)))
pid)
(when (/= 0 status) (setq *rsc-status* (cons status command)))))))
#+rsc-scheduler
(defun rsc-finalize ()
(mp:without-scheduling
(dolist (p *rsc-procs*)
(setf (getf (mp:process-property-list p) :rsc-queue)
(append (getf (mp:process-property-list p) :rsc-queue)
(list :die)))))
(mp:process-wait
"waiting for rsc processors to finish"
(lambda ()
(dolist (p *rsc-procs* t)
(when (mp:process-active-p p) (return nil)))))
(if* *rsc-status*
then (format t "Error command ~s exited with status ~d."
(cdr *rsc-status*) (car *rsc-status*))
nil
else t)
(setq *rsc-procs* nil))