c5e0cd94181a13dceeeb00c0fa5809349e01e2b1
5 import xml
.etree
.ElementTree
as ET
6 from threading
import Thread
7 from threading
import Lock
8 from collections
import deque
12 tree
= ET
.Element('mal')
16 ET
.SubElement(tree
, 'argv')
17 for a
in tree
.iter('mal'):
18 for a
in a
.iter('argv'):
20 ET
.SubElement(a
, 'arg').text
= arg
21 ET
.SubElement(tree
, 'no_repl')
23 tree
= ET
.ElementTree(tree
)
27 readline
.read_history_file('.xslt_mal_history')
33 init_t
= time
.time() * 1000
34 readline_queue
= deque()
35 os
.system('rm -rf xsl_error.xml')
36 os
.system('mkfifo xsl_error.xml')
38 def setup_request_file():
39 os
.system('rm -rf xsl_input-string')
40 os
.system('mkfifo xsl_input-string')
43 def read_nonblocking(path
, bufferSize
=100, timeout
=.1):
46 pipe
= os
.open(path
, os
.O_RDONLY | os
.O_NONBLOCK
)
50 buf
= os
.read(pipe
, bufferSize
)
54 content
= buf
.decode("utf-8")
55 line
= content
.split("\n")
58 if e
.errno
== 11 and grace
:
59 # grace period, first write to pipe might take some time
60 # further reads after opening the file are then successful
67 if e
.errno
== errno
.ENOENT
:
76 def serve_one_request():
77 res
= read_nonblocking('xsl_error.xml', 1024)
82 xtree
= ET
.fromstring("<data>" + res
.strip('\x00') + "</data>")
83 # stdout.write(xtree.attrib['kind'])
85 if req
.attrib
['kind'] == 'readline':
87 if len(readline_queue
) > 0:
88 x
= readline_queue
.popleft()
90 x
= input(req
.attrib
['value'])
91 with
open('xsl_input-string', 'w') as fx
:
93 # stdout.write(' = ' + x)
94 elif req
.attrib
['kind'] == 'display':
95 stdout
.write(req
.attrib
['value'] + '\n')
96 elif req
.attrib
['kind'] == 'time':
97 x
= time
.time() * 1000 - init_t
98 # stdout.write(' = ' + str(int(x)))
99 with
open('xsl_input-string', 'w') as fx
:
100 fx
.write(str(int(x
)))
102 elif req
.attrib
['kind'] == 'xpath-eval':
103 xpath
= req
.attrib
['value']
104 with
open('xsl-eval.xslt', 'w') as f
:
105 f
.write(f
'<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:env="ENV" xmlns:core="CORE" exclude-result-prefixes="env core xs xsl map fn"><xsl:output omit-xml-declaration="yes"/><xsl:template match="/"><xsl:sequence select="{xpath}" /></xsl:template></xsl:stylesheet>')
106 with
open('xsl-null.xml', 'w') as f
:
107 f
.write(req
.attrib
['context'])
109 if os
.system(f
'saxon -xsl:xsl-eval.xslt -s:xsl-null.xml > xsl-eval_output.xml'):
112 with
open('xsl-eval_output.xml', 'r') as f
:
114 with
open('xsl_input-string', 'w') as fx
:
117 stdout
.write("UNKNOWN REQUEST " + req
.attrib
['kind'])
119 except Exception as e
:
120 # if str(e) != 'no element found: line 1, column 0':
122 # print(e, list(x for x in f.read()))
124 # with open('xsl_error.xml', 'w') as f:
127 def serve_requests():
133 except Exception as e
:
138 th
= Thread(target
=serve_requests
)
141 def transform(do_print
=True):
144 tree
.write('xslt_input.xml')
145 if os
.system(f
'saxon -xsl:"{fname}" -s:xslt_input.xml -TP:perf.html > xslt_output.xml 2> xsl_error.xml'):
147 tree
= ET
.parse('xslt_output.xml')
150 for a
in tree
.iter('mal'):
151 for a
in a
.iter('stdout'):
159 readline_queue
.append(f
'(load-file "{args0}")')
160 transform(do_print
=False)
162 if fname
== 'stepA_mal.xslt':
163 readline_queue
.append('(println (str "Mal [" *host-language* "]"))')
164 transform(do_print
=False)
167 readline
.write_history_file('.xslt_mal_history')