workaround popen trouble on windows
This commit is contained in:
		@@ -1,12 +1,25 @@
 | 
				
			|||||||
(use-modules (ice-9 popen) (ice-9 textual-ports) (json parser))
 | 
					(if (not windows?) (use-modules (ice-9 popen)))
 | 
				
			||||||
 | 
					(use-modules (ice-9 textual-ports) (json parser))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					; We use Python to convert the data yamls like the authors.yml to json, that we can parse in scheme.
 | 
				
			||||||
 | 
					; Windows does not like Pipes, so we use a tmpfile instead.
 | 
				
			||||||
 | 
					; Be sure you have PyYAML installed. On Windows that could be done for example like "py -m pip install PyYAML"
 | 
				
			||||||
(define (yml-file->scm filename)
 | 
					(define (yml-file->scm filename)
 | 
				
			||||||
  (let* ((python_cmd (string-append "import sys, yaml, json; print(json.dumps(yaml.safe_load(open('" filename "'))))"))
 | 
					  (if windows?
 | 
				
			||||||
          ; WTF? On Windows there is "py" and we need to specify the encoding, on linux there is "python3" or "python"
 | 
					    (let* ((port (make-tmpfile #f))
 | 
				
			||||||
          ; but "python3" seems to work better. Be sure you have PyYAML installed.
 | 
					            (tmpfilepath (port-filename port))
 | 
				
			||||||
          (pipe (open-pipe (string-append "PYTHONHOME='' " (if windows? "py" "python3") " -X utf8 -c \"" python_cmd "\"") OPEN_READ))
 | 
					            (ignore (close-port port))
 | 
				
			||||||
 | 
					            (python_code (string-append "import sys, yaml, json; f = open(r'" tmpfilepath "', 'w'); f.write(json.dumps(yaml.safe_load(open(r'" filename "')))); f.close()"))
 | 
				
			||||||
 | 
					            (status (system (string-append (search-executable '("python3" "python" "py")) " -X utf8 -c \"" python_code "\"")))
 | 
				
			||||||
 | 
					            (readport (open-file tmpfilepath "r" #:encoding "UTF-8"))
 | 
				
			||||||
 | 
					            (json (get-string-all readport)))
 | 
				
			||||||
 | 
					      (close-port readport)
 | 
				
			||||||
 | 
					      (delete-file tmpfilepath)
 | 
				
			||||||
 | 
					      (json-string->scm (if (status:exit-val status) json "{}")))
 | 
				
			||||||
 | 
					    (let* ((python_code (string-append "import sys, yaml, json; print(json.dumps(yaml.safe_load(open(r'" filename "'))))"))
 | 
				
			||||||
 | 
					            (pipe (open-pipe (string-append "PYTHONHOME='' " (search-executable '("python3" "python" "py")) " -X utf8 -c \"" python_code "\"") OPEN_READ))
 | 
				
			||||||
            (json (get-string-all pipe)))
 | 
					            (json (get-string-all pipe)))
 | 
				
			||||||
      (close-pipe pipe)
 | 
					      (close-pipe pipe)
 | 
				
			||||||
    (json-string->scm json)))
 | 
					      (json-string->scm json))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(define (parse-yml-file filename) (resolve-inherits (yml-file->scm filename)))
 | 
					(define (parse-yml-file filename) (resolve-inherits (yml-file->scm filename)))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user