From: Han-Wen Nienhuys <hanwen@xs4all.nl>
Date: Mon, 22 Aug 2005 15:50:37 +0000 (+0000)
Subject: * scm/ps-to-png.scm: remove dir-re function.
X-Git-Tag: release/2.7.7~6
X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=ac92a973e996f5405551d9bf9f3ff84279ccde20;p=lilypond.git

* scm/ps-to-png.scm: remove dir-re function.
(make-ps-images): generate page names, instead of globbing them.
This brings down LilyPond memory usage for make web by a factor
10.

* scripts/lilypond-book.py (Lilypond_snippet.png_is_outdated):
don't use glob. With 3000 files, globbing
Documentation/user/out-www/ can take too much time.

* lily/lily-guile.cc (gulp_file_to_string): take size argument.
---

diff --git a/ChangeLog b/ChangeLog
index 7f3e001c05..0296084058 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2005-08-22  Han-Wen Nienhuys  <hanwen@xs4all.nl>
 
+	* scm/ps-to-png.scm: remove dir-re function.
+	(make-ps-images): generate page names, instead of globbing them.
+	This brings down LilyPond memory usage for make web by a factor
+	10.
+
+	* scripts/lilypond-book.py (Lilypond_snippet.png_is_outdated):
+	don't use glob. With 3000 files, globbing
+	Documentation/user/out-www/ can take too much time.
+
 	* lily/lily-guile.cc (gulp_file_to_string): take size argument.
 
 	* lily/general-scheme.cc (LY_DEFINE): take optional size argument.
diff --git a/lily/include/lily-guile.hh b/lily/include/lily-guile.hh
index eaa638ee00..672a0df804 100644
--- a/lily/include/lily-guile.hh
+++ b/lily/include/lily-guile.hh
@@ -33,7 +33,7 @@ SCM ly_to_symbol (SCM scm);
 
 extern SCM global_lily_module;
 
-String gulp_file_to_string (String fn, bool must_exist);
+String gulp_file_to_string (String fn, bool must_exist, int size);
 
 String ly_scm2string (SCM s);
 String ly_symbol2string (SCM);
diff --git a/lily/lily-guile.cc b/lily/lily-guile.cc
index 1ef5370e8e..856c9d30cd 100644
--- a/lily/lily-guile.cc
+++ b/lily/lily-guile.cc
@@ -103,7 +103,7 @@ gulp_file_to_string (String fn, bool must_exist, int size)
   if (be_verbose_global)
     progress_indication ("[" + s);
 
-  int n = sz;
+  int n = size;
   char *str = gulp_file (s, &n);
   String result ((Byte *) str, n);
   delete[] str;
diff --git a/lily/source-file.cc b/lily/source-file.cc
index fdd628fbfb..74bb5c3b40 100644
--- a/lily/source-file.cc
+++ b/lily/source-file.cc
@@ -73,7 +73,7 @@ gulp_file (String filename, int *filesize)
     warning (_f ("expected to read %d characters, got %d", bytes_read,
 		 read_count));
   fclose (f);
-
+  *filesize = bytes_read;
   return str;
 }
 
diff --git a/lily/text-metrics.cc b/lily/text-metrics.cc
index d69b59d3b4..9349525853 100644
--- a/lily/text-metrics.cc
+++ b/lily/text-metrics.cc
@@ -75,7 +75,7 @@ try_load_text_metrics (String basename)
   String path = global_path.find (basename + ".textmetrics");
   if (path != "")
     {
-      String contents (gulp_file_to_string (path, true));
+      String contents (gulp_file_to_string (path, true, -1));
       contents = "(quote (" + contents + "))";
 
       SCM lst = scm_c_eval_string (contents.to_str0 ());
diff --git a/python/lilylib.py b/python/lilylib.py
index 82b3818966..c50ab48e10 100644
--- a/python/lilylib.py
+++ b/python/lilylib.py
@@ -348,6 +348,13 @@ def print_environment ():
 		sys.stderr.write ("%s=\"%s\"\n" % (k, v)) 
 
 
+def ps_page_count (ps_name):
+	header = open (ps_name).read (1024)
+	m = re.search ('\n%%Pages: ([0-9]+)', header)
+	if m:
+		return string.atoi (m.group (1))
+	return 0
+
 def make_ps_images (ps_name, resolution = 90, papersize = "a4",
 		    rename_page1_p = 0):
 	base = os.path.basename (re.sub (r'\.e?ps', '', ps_name))
diff --git a/scm/ps-to-png.scm b/scm/ps-to-png.scm
index ddaa3bc18a..ee16349a83 100644
--- a/scm/ps-to-png.scm
+++ b/scm/ps-to-png.scm
@@ -12,7 +12,8 @@
  (ice-9 rw)
  (srfi srfi-1)
  (srfi srfi-13)
- (srfi srfi-14))
+ (srfi srfi-14)
+ )
 
 ;; gettext wrapper for guile < 1.7.2
 (if (defined? 'gettext)
@@ -45,14 +46,10 @@
     (read-string!/partial str port 0 max-length)
    str))
 
-(define (dir-listing dir-name)
-  (define (dir-helper dir lst)
-    (let ((e (readdir dir)))
-      (if (eof-object? e) lst (dir-helper dir (cons e lst)))))
-  (reverse (dir-helper (opendir dir-name) '())))
+(define (gulp-file nm len)
+  (gulp-port (open-file nm "r") len))
 
-(define (dir-re dir re)
-  (filter (lambda (x) (string-match re x)) (dir-listing dir)))
+;;; ARGH - cuases memory usage to explode with GUILE cvs.  
 
 (define BOUNDING-BOX-RE
   "^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
@@ -69,7 +66,7 @@
  -c quit 2>~S"
 			  file-name bbox))
 	 (status (system cmd))
-	 (s (gulp-port (open-file bbox "r") 10240))
+	 (s (gulp-file d bbox 10240))
 	 (m (string-match BOUNDING_BOX_RE s)))
     (display m)
     (newline)
@@ -111,6 +108,16 @@
   (delete-file old)
   ))
 
+
+(define-public (ps-page-count ps-name)
+  (let*
+      ((header (gulp-file ps-name 10240))
+       (match (string-match "%%Pages: ([0-9]+)" header))
+       (count (if match
+		  (string->number (match:substring match 1))
+		  0)))
+    count))
+
 (define-public (make-ps-images ps-name . rest)
   (let-optional
    rest ((resolution 90)
@@ -121,13 +128,12 @@
 	 )
    
    (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
-	  (header (ly:gulp-file ps-name))
-;	  (header (gulp-port (open-file ps-name "r") 10240))
+	  (header (gulp-file ps-name 10240))
 	  (png1 (string-append base ".png"))
 	  (pngn (string-append base "-page%d.png"))
-	  (pngn-re (re-sub "%d" "[0-9]*" pngn))
-	  (multi-page? (and (string-match "\n%%Pages: " header)
-			    (not (string-match "\n%%Pages: 1\n" header))))
+	  (page-count (ps-page-count ps-name))
+	  
+	  (multi-page? (> page-count 1))
 	  (output-file (if multi-page? pngn png1))
 
 	  ;;png16m is because Lily produces color nowadays.
@@ -154,10 +160,6 @@
 	  (status 0)
 	  (files '()))
 
-     
-     (for-each delete-file (append (dir-re "." png1)
-				   (dir-re "." pngn-re)))
-     
      ;; The wrapper on windows cannot handle `=' signs,
      ;; gs has a workaround with #.
      (if (eq? PLATFORM 'windows)
@@ -167,20 +169,26 @@
 
      (set! status (my-system verbose? #f cmd))
 
-     (set! files
-	   (append (dir-re "." png1) (dir-re "." pngn-re)))
-
+     (if multi-page?
+	 (set! files
+	       (map
+		(lambda (n)
+		  (format "~a-page~a.png" base n))
+		(iota page-count)))
+	 (list (format "~a.png" base)))
+     
      (if (not (= 0 status))
 	 (begin
 	   (map delete-file files)
 	   (exit 1)))
      
      (if (and rename-page-1? multi-page?)
-	 (rename-file (re-sub "%d" "1" pngn) png1))
-     
-     (set! files
-	   (append (dir-re "." png1) (dir-re "." pngn-re)))
-
+	 (begin
+	   (rename-file (re-sub "%d" "1" pngn) png1)
+	   (set! files
+		 (cons png1
+		       (cdr files)))
+	   ))
      
      (if (not (= 1 aa-factor))
 	 (for-each  (lambda (f) (scale-down-image verbose? aa-factor f))
diff --git a/scripts/lilypond-book.py b/scripts/lilypond-book.py
index 116d5a07ca..acb6f81dd0 100644
--- a/scripts/lilypond-book.py
+++ b/scripts/lilypond-book.py
@@ -954,9 +954,20 @@ class Lilypond_snippet (Snippet):
 		base = self.basename ()
 		ok = self.ly_is_outdated ()
 		if format == HTML or format == TEXINFO:
-			ok = ok and (os.path.exists (base + '.png')
-				     or glob.glob (base + '-page*.png'))
+			ok = ok and os.path.exists (base + '.eps')
+
+			page_count = 0
+			if ok:
+				page_count = ly.ps_page_count (base + '.eps')
+			
+			if page_count == 1:
+				ok = ok and os.path.exists (base + '.png')
+			elif page_count > 1:
+				for a in range (1, page_count + 1):
+						ok = ok and os.path.exists (base + '-page%d.png' % a)
+				
 		return not ok
+	
 	def texstr_is_outdated (self):
 		if backend == 'ps':
 			return 0
@@ -990,7 +1001,9 @@ class Lilypond_snippet (Snippet):
 		   and (not os.path.exists (single) \
 			or (os.stat (multiple)[stat.ST_MTIME] \
 			    > os.stat (single)[stat.ST_MTIME])):
-			images = glob.glob ('%(base)s-page*.png' % vars ())
+			count = ly.ps_page_count ('%(base)s.eps' % vars ())
+			images = ['%s-page%d.png' % (base, a) for a in range (1, count+1)]
+			images = tuple (images)
 		return images
 
 	def output_html (self):