Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
E
evality
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
1
Konular (issue)
1
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
evality
Commits
b1c3fa57
Kaydet (Commit)
b1c3fa57
authored
May 12, 2019
tarafından
Gokberk Yaltirakli
Kaydeden (comit)
Batuhan Taşkaya
May 12, 2019
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Refactor executor and write tests (#2)
* Refactor executor * test: Executor tests
üst
728ac35f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
21 deletions
+55
-21
executor.py
evality/executor.py
+15
-20
namekeeper.py
evality/namekeeper.py
+1
-1
02-executor.t
t/02-executor.t
+39
-0
No files found.
evality/executor.py
Dosyayı görüntüle @
b1c3fa57
...
...
@@ -6,34 +6,24 @@ from contextlib import redirect_stderr, redirect_stdout
from
http.server
import
HTTPServer
,
SimpleHTTPRequestHandler
from
io
import
StringIO
from
namekeeper
import
NameKeeper
from
.
namekeeper
import
NameKeeper
MARSHAL_RAISES
=
(
KeyError
,
ValueError
,
TypeError
,
EOFError
)
nk
=
NameKeeper
()
def
execute
(
code
):
global
nk
class
Executor
:
def
__init__
(
self
,
host
=
""
,
port
=
18888
,
handler
=
SimpleHTTPRequestHandler
):
self
.
_httpd
=
HTTPServer
((
host
,
port
),
handler
)
self
.
_httpd
.
serve_forever
()
@staticmethod
def
execute
(
self
,
code
):
global
nk
out
=
StringIO
()
err
=
StringIO
()
with
redirect_stdout
(
out
),
redirect_stderr
(
err
):
_exec
=
exec
with
nk
.
secure_scope
():
_exec
(
code
)
return
{
"out"
:
out
.
getvalue
(),
"err"
:
err
.
getvalue
()}
out
=
StringIO
()
err
=
StringIO
()
with
redirect_stdout
(
out
),
redirect_stderr
(
err
):
_exec
=
exec
with
nk
.
secure_scope
():
_exec
(
code
)
return
{
"out"
:
out
.
getvalue
(),
"err"
:
err
.
getvalue
()}
class
Handler
(
SimpleHTTPRequestHandler
):
execute
=
Executor
.
execute
def
do_POST
(
self
,
*
args
,
**
kwargs
):
content_length
=
int
(
self
.
headers
[
"Content-Length"
])
raw_body
=
self
.
rfile
.
read
(
content_length
)
.
decode
()
...
...
@@ -55,6 +45,11 @@ class Handler(SimpleHTTPRequestHandler):
self
.
end_headers
()
self
.
wfile
.
write
(
json
.
dumps
({
"result"
:
result
})
.
encode
())
class
Executor
:
def
__init__
(
self
,
host
=
""
,
port
=
18888
,
handler
=
Handler
):
self
.
_httpd
=
HTTPServer
((
host
,
port
),
handler
)
self
.
_httpd
.
serve_forever
()
if
__name__
==
"__main__"
:
executor
=
Executor
(
port
=
os
.
environ
.
get
(
"EXECUTOR_PORT"
,
18888
),
handler
=
Handler
)
evality/namekeeper.py
Dosyayı görüntüle @
b1c3fa57
...
...
@@ -4,7 +4,7 @@ import types
from
collections
import
defaultdict
from
contextlib
import
contextmanager
from
sww
import
get_members
from
.
sww
import
get_members
FORBID_ACCESS
=
[
"open"
,
"exec"
,
"eval"
,
"compile"
,
"input"
]
FORBID_BY_TYPE_ACCESS
=
{
...
...
t/02-executor.t
0 → 100644
Dosyayı görüntüle @
b1c3fa57
#!/usr/bin/env python3
import
sys
sys
.
path
.
append
(
'.'
)
from
evality
.
executor
import
execute
from
testsimple
import
*
plan
(
tests
=
10
)
ok
(
execute
,
'Execute method exists'
)
diag
(
'Basic contract'
)
res
=
execute
(
''
)
ok
(
res
)
ok
(
'out'
in
res
)
ok
(
'err'
in
res
)
ok
(
not
res
[
'out'
])
ok
(
not
res
[
'err'
])
diag
(
'Getting stdout'
)
res
=
execute
(
'print("Test message")'
)
ok
(
res
[
'out'
]
==
'Test message\n'
)
ok
(
not
res
[
'err'
])
diag
(
'Multiline test'
)
res
=
execute
(
'''
result = "Hello, "
for c in "World!":
result += c
print(result)
'''
)
ok
(
res
[
'out'
]
==
'Hello, World!\n'
)
ok
(
not
res
[
'err'
])
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment