Skip to content

Commit

Permalink
add status command (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
srz-zumix authored Jan 25, 2022
1 parent 9542784 commit 2d1bc44
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 85 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
'wandbox-swift = wandbox.__swift__:main',
'wandbox-tsc = wandbox.__tsc__:main',
'wandbox-vim = wandbox.__vim__:main',
'wandbox-status = wandbox.__status__:main',
]
}
, install_requires=['requests', 'pyyaml', 'toml', 'ndjson']
Expand Down
76 changes: 2 additions & 74 deletions tests/test_wandbox_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,9 @@
import wandbox
from wandbox import Wandbox
from wandbox import cli as wandbox_cli
from wandbox import __bash__ as bash
from wandbox import __cc__ as cc
from wandbox import __cmake__ as cmake
from wandbox import __coffee__ as coffee
from wandbox import __cpp__ as cpp
from wandbox import __crystal__ as crystal
from wandbox import __csharp__ as cs
from wandbox import __cxx__ as cxx
from wandbox import __dmd__ as dmd
from wandbox import __elixir__ as elixir
from wandbox import __erlang__ as erlang
from wandbox import __fsharp__ as fs
from wandbox import __ghc__ as ghc
from wandbox import __go__ as go
from wandbox import __groovy__ as groovy
from wandbox import __java__ as java
from wandbox import __js__ as js
from wandbox import __julia__ as julia
from wandbox import __lazyk__ as lazyk
from wandbox import __lisp__ as lisp
from wandbox import __lua__ as lua
from wandbox import __nim__ as nim
from wandbox import __ocaml__ as ocaml
from wandbox import __openssl__ as openssl
from wandbox import __pascal__ as pascal
from wandbox import __perl__ as perl
from wandbox import __php__ as php
from wandbox import __pony__ as pony
from wandbox import __python__ as python
from wandbox import __r__ as rscript
from wandbox import __ruby__ as ruby
from wandbox import __rust__ as rust
from wandbox import __scala__ as scala
from wandbox import __sql__ as sql
from wandbox import __swift__ as swift
from wandbox import __tsc__ as tsc
from wandbox import __vim__ as vim
from wandbox.__all__ import get_all_cli

try:
import unittest2 as unittest
Expand Down Expand Up @@ -213,45 +179,7 @@ def tearDown(self):

def test_options_config(self):
l = test_wandbox_options.list_json
clis = [
# bash.BashCLI.InnerCLI(),
cc.CcCLI(),
cmake.CMakeCLI(),
coffee.CoffeeCLI(),
cpp.CppCLI(),
crystal.CrystalCLI(),
cs.CsCLI(),
cxx.CxxCLI(),
dmd.DCLI(),
elixir.ElixirCLI(),
erlang.ErlangCLI(),
fs.FsCLI(),
ghc.GhcCLI(),
go.GoCLI(),
groovy.GroovyCLI(),
java.JavaCLI(),
js.JsCLI(),
julia.JuliaCLI(),
lazyk.LazyKCLI(),
lisp.LispCLI(),
lua.LuaCLI(),
nim.NimCLI(),
ocaml.OCamlCLI(),
openssl.OpenSSLCLI.InnerCLI(),
pascal.PascalCLI(),
perl.PerlCLI(),
php.PhpCLI(),
pony.PonyCLI(),
python.PythonCLI(),
rscript.RscriptCLI(),
ruby.RubyCLI(),
rust.RustCLI(),
scala.ScalaCLI(),
sql.SqlCLI(),
swift.SwiftCLI(),
tsc.TscCLI(),
vim.VimCLI()
]
clis = get_all_cli()
for cli in clis:
with self.subTest(cli=cli):
ll = [x for x in l if cli.language == x['language']]
Expand Down
80 changes: 80 additions & 0 deletions wandbox/__all__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from wandbox import __bash__ as bash
from wandbox import __cc__ as cc
from wandbox import __cmake__ as cmake
from wandbox import __coffee__ as coffee
from wandbox import __cpp__ as cpp
from wandbox import __crystal__ as crystal
from wandbox import __csharp__ as cs
from wandbox import __cxx__ as cxx
from wandbox import __dmd__ as dmd
from wandbox import __elixir__ as elixir
from wandbox import __erlang__ as erlang
from wandbox import __fsharp__ as fs
from wandbox import __ghc__ as ghc
from wandbox import __go__ as go
from wandbox import __groovy__ as groovy
from wandbox import __java__ as java
from wandbox import __js__ as js
from wandbox import __julia__ as julia
from wandbox import __lazyk__ as lazyk
from wandbox import __lisp__ as lisp
from wandbox import __lua__ as lua
from wandbox import __nim__ as nim
from wandbox import __ocaml__ as ocaml
from wandbox import __openssl__ as openssl
from wandbox import __pascal__ as pascal
from wandbox import __perl__ as perl
from wandbox import __php__ as php
from wandbox import __pony__ as pony
from wandbox import __python__ as python
from wandbox import __r__ as rscript
from wandbox import __ruby__ as ruby
from wandbox import __rust__ as rust
from wandbox import __scala__ as scala
from wandbox import __sql__ as sql
from wandbox import __swift__ as swift
from wandbox import __tsc__ as tsc
from wandbox import __vim__ as vim


def get_all_cli():
clis = [
bash.BashCLI.InnerCLI(),
cc.CcCLI(),
cmake.CMakeCLI(),
coffee.CoffeeCLI(),
cpp.CppCLI(),
crystal.CrystalCLI(),
cs.CsCLI(),
cxx.CxxCLI(),
dmd.DCLI(),
elixir.ElixirCLI(),
erlang.ErlangCLI(),
fs.FsCLI(),
ghc.GhcCLI(),
go.GoCLI(),
groovy.GroovyCLI(),
java.JavaCLI(),
js.JsCLI(),
julia.JuliaCLI(),
lazyk.LazyKCLI(),
lisp.LispCLI(),
lua.LuaCLI(),
nim.NimCLI(),
ocaml.OCamlCLI(),
openssl.OpenSSLCLI.InnerCLI(),
pascal.PascalCLI(),
perl.PerlCLI(),
php.PhpCLI(),
pony.PonyCLI(),
python.PythonCLI(),
rscript.RscriptCLI(),
ruby.RubyCLI(),
rust.RustCLI(),
scala.ScalaCLI(),
sql.SqlCLI(),
swift.SwiftCLI(),
tsc.TscCLI(),
vim.VimCLI()
]
return clis
123 changes: 123 additions & 0 deletions wandbox/__status__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import re
import sys
from . import __version__ as VERSION

from .__all__ import get_all_cli

from .wandbox import Wandbox
from .wandbox_compile_response import WandboxCompileResponse
from .utils import text_transform
from argparse import ArgumentParser


class StatusCLI:

def __init__(self):
self.setup()

# command line option
def setup(self):
self.parser = ArgumentParser()
self.parser.add_argument(
'-v',
'--version',
action='version',
version=u'%(prog)s version ' + VERSION
)
self.parser.add_argument(
'-l',
'--language',
required=True,
help='specify language'
)
self.parser.add_argument(
'-c',
'--compiler',
help='specify compiler (can use fnmatch, use first match compiler. e.g. clang-3.9.*[!c] => clang-3.9.1)'
)
self.parser.add_argument(
'-V',
'--verbose',
action='store_true',
help='verbose log'
)

def parse_command_line(self, argv):
return self.parser.parse_args(argv)

def execute(self):
self.execute_with_args()

def execute_with_args(self, args=None):
opts = self.parse_command_line(args)

language, compiler, *_ = re.split(r'[\s:,]', opts.language, 2) + [None]
clis = get_all_cli()
cli = next((cli for cli in clis if cli.language == language), None)

if cli is None:
print("error: \"{}\" language is not found".format(language))
self.parser.print_help()
sys.exit(1)

if opts.compiler:
compiler = opts.compiler

run_options = ['run-template']
cli_options = ['-c', compiler] if compiler else []
StatusCLI.verbose = opts.verbose
if opts.verbose:
cli_options.append('-V')

cli.on_run_response = type(cli.on_run_response)(StatusCLI.OnRunResponse, cli)
cli.execute_with_args(cli_options + run_options)

@staticmethod
def OnRunResponse(cli, response):
r = WandboxCompileResponse(response)
if r.has_error():
print(r.error())
return 1
if StatusCLI.verbose:
Wandbox.ShowResult(response)
if StatusCLI.CheckResponse(cli, r):
if r.has_signal():
print(r.signal())
else:
print('Compile/Runtime Error')
return 1
print('OK')
return 0

@staticmethod
def CheckResponse(cli, r):
if r.has_program_output():
if not StatusCLI.CheckOutput(cli.language, text_transform(r.program_output())):
return 1
else:
return 1
if r.has_status():
return int(r.status())
return 0

@staticmethod
def CheckOutput(language, output):
if language == "OpenSSL":
return "PRIVATE KEY" in output
elif language == "CPP":
return "42" in output
else:
return "Hello" in output


def status():
cli = StatusCLI()
cli.execute()


def main():
status()


if __name__ == '__main__':
main()
11 changes: 11 additions & 0 deletions wandbox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@
def case_insensitive_glob(path, pattern):
name = ''.join(map(lambda c: '[{0}{1}]'.format(c.upper(), c.lower()) if c.isalpha() else c, pattern))
return glob.glob(os.path.join(path, name))


def text_transform(value):
try:
if isinstance(value, str):
return value.decode()
# elif isinstance(value, unicode):
# return value.encode('utf_8')
except Exception: # nosec
pass
return value
12 changes: 1 addition & 11 deletions wandbox/wandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,7 @@
from requests.exceptions import ConnectionError as RConnectionError
from requests.exceptions import ConnectTimeout as RConnectTimeout
from .wandbox_compile_response import WandboxCompileResponse


def text_transform(value):
try:
if isinstance(value, str):
return value.decode()
# elif isinstance(value, unicode):
# return value.encode('utf_8')
except Exception:
pass
return value
from .utils import text_transform


#
Expand Down

0 comments on commit 2d1bc44

Please sign in to comment.