Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add openssl and support stdin #14

Merged
merged 4 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project is a Pythonic binding to the Wandbox API, and CLI command.
* [GO](#Go)
* [Python](#Python)
* [Ruby](#Ruby)
* [OpenSSL](#OpenSSL)

### wandbox

Expand Down Expand Up @@ -183,3 +184,14 @@ Require files required for compilation are automatically added to the file list.
#### Ruby Example

> wandbox-ruby run sample.rb

### OpenSSL

Even just having wandbox would be enough.

* wandbox-ssl

#### OpenSSL Example

> wandbox-ssl genrsa -out test.key 2048
> wandbox-ssl rsa -in test.key -pubout -out test.key.pub
2 changes: 2 additions & 0 deletions samples/command/src/openssl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.key
*.key.pub
20 changes: 20 additions & 0 deletions samples/command/src/openssl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Makefile

OPENSSL:=openssl


local: test.key.pub

test.key:
${OPENSSL} genrsa -out test.key 2048

test.key.pub: test.key
${OPENSSL} rsa -in test.key -pubout -out test.key.pub

clean:
rm -rf test.key
rm -rf test.key.pub

wandbox:
make -C . local --no-print-directory OPENSSL="wandbox-ssl"
2 changes: 1 addition & 1 deletion samples/command/src/ruby/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Makefile

RUBY=ruby
RUBY:=ruby

local:
${RUBY} sample.rb
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
'wandbox-pypy = wandbox.__python__:pypy',
'wandbox-ruby = wandbox.__ruby__:main',
'wandbox-mruby = wandbox.__ruby__:mruby',
'wandbox-go = wandbox.__go__:main'
'wandbox-go = wandbox.__go__:main',
'wandbox-ssl = wandbox.__openssl__:main'
]
}
, install_requires=['requests']
Expand Down
4 changes: 1 addition & 3 deletions wandbox/__cxx__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ def reset(self):
self.expand = False
self.includes = []

def make_code(self, filepath, filename):
def make_code(self, file, filepath, filename):
files = dict()
code = ''
file = self.file_open(filepath, 'r')
for line in file:
m = self.EXPAND_INCLUDE_REGEX.match(line)
if m:
Expand All @@ -35,7 +34,6 @@ def make_code(self, filepath, filename):
expand_include_file_codes = self.open_code(include_path, include_path)
files.update(expand_include_file_codes)
code += line
file.close()
files[filename] = code
return files

Expand Down
104 changes: 104 additions & 0 deletions wandbox/__openssl__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import sys

from argparse import ArgumentParser
from argparse import SUPPRESS
from io import StringIO

from .cli import CLI


class OpenSSLCLI:

class InnerCLI(CLI):

def __init__(self):
self.output = None
super(OpenSSLCLI.InnerCLI, self).__init__('OpenSSL', 'openssl-head', False)

def on_run_response(self, response):
if self.output:
with open(self.output, 'w') as file:
if 'program_output' in response:
file.write(response['program_output'])
else:
file.write(response['program_message'])
return super(OpenSSLCLI.InnerCLI, self).on_run_response(response)

def __init__(self):
self.setup()

def command_run(self, args):
options = []
for o in args.options:
options.extend(o.split(','))
try:
self.run(args, options)
except Exception as e:
print(e)
self.print_help()
sys.exit(1)

# command line option
def setup(self):
self.parser = ArgumentParser(add_help=False)
self.parser.add_argument(
'-out',
help=SUPPRESS
)
self.parser.add_argument(
'-in',
dest='infile',
help=SUPPRESS
)
self.parser.add_argument(
'command',
nargs='?',
help=SUPPRESS
)
self.parser.add_argument(
'-n',
'--dryrun',
action='store_true',
help='dryrun'
)

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

def print_help(self):
self.parser.print_help()

def execute(self):
self.execute_with_args()

def execute_with_args(self, args=None):
opts, args = self.parse_command_line(args)
cmd = OpenSSLCLI.InnerCLI()
cmd.output = opts.out
if cmd.output:
cmd.result_key = 'program_error'
else:
cmd.result_key = 'program_message'
run_options = ['run']
cli_options = []
if opts.dryrun:
cli_options.append('--dryrun')
sslcmd = opts.command
sslopts = args
if opts.infile:
sslopts = ['-in', opts.infile] + sslopts
run_options.append(opts.infile)
command = ' '.join(['openssl', sslcmd] + sslopts)
code = StringIO(command)
sys.stdin = code
run_options.append('-')
cmd.execute_with_args(cli_options + run_options)


def main():
cli = OpenSSLCLI()
cli.execute()


if __name__ == '__main__':
main()
4 changes: 1 addition & 3 deletions wandbox/__python__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ def get_imports(self, path, module_name):
return self.open_code(module_path, module_file)
return dict()

def make_code(self, filepath, filename):
def make_code(self, file, filepath, filename):
if os.path.basename(filepath) == 'setup.py':
return self.make_from_setup_py(filepath, filename)
files = dict()
code = ''
file = self.file_open(filepath, 'r')
for line in file:
m = self.IMPORT_REGEX.match(line)
if m:
Expand All @@ -86,7 +85,6 @@ def make_code(self, filepath, filename):
else:
files.update(self.get_imports(os.path.dirname(filepath), module_name))
code += line
file.close()
files[filename] = code
# print(files.keys())
return files
Expand Down
4 changes: 1 addition & 3 deletions wandbox/__ruby__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class RubyRunner(Runner):
def reset(self):
self.required = []

def make_code(self, filepath, filename):
def make_code(self, file, filepath, filename):
files = dict()
code = ''
file = self.file_open(filepath, 'r')
for line in file:
m = self.REQUIRE_REGEX.match(line)
if m:
Expand All @@ -29,7 +28,6 @@ def make_code(self, filepath, filename):
module = m.group(1).strip('\'"')
files.update(self.require(os.path.dirname(filepath), module.strip()))
code += line
file.close()
files[filename] = code
return files

Expand Down
32 changes: 24 additions & 8 deletions wandbox/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import sys
import json
import traceback

from . import __version__ as VERSION
from .wandbox import Wandbox
Expand All @@ -26,6 +27,7 @@ class CLI:
"""wandbox CLI class"""

def __init__(self, lang=None, compiler=None, has_option=True):
self.result_key = None
self.setup(lang, compiler, has_option)

def command_list(self, args):
Expand Down Expand Up @@ -99,9 +101,9 @@ def command_options(self, args):
def command_permlink(self, args):
r = Wandbox.Call(lambda : Wandbox.GetPermlink(args.id[0]), args.retry, args.retry_wait)
p = r['parameter']
Runner.ShowParameter(p)
Wandbox.ShowParameter(p)
print('result:')
b = Runner.ShowResult(r['result'])
b = Wandbox.ShowResult(r['result'])
sys.exit(b)

def command_run(self, args):
Expand All @@ -110,8 +112,8 @@ def command_run(self, args):
options.extend(o.split(','))
try:
self.run(args, options)
except Exception as e:
print(e)
except Exception:
print(traceback.format_exc())
self.print_help()
sys.exit(1)

Expand All @@ -123,7 +125,7 @@ def setup_runner(self, args, enable_options, disable_options, runner):
runner.set_stdin(args.stdin)
runner.set_runtime_options(args.runtime_options)
runner.build_options(enable_options, disable_options, not args.no_default)
runner.build_compiler_options(args.compile_options)
runner.build_compiler_options(args.sources + args.compile_options)

def run(self, args, options):
if args.language and args.compiler is None:
Expand All @@ -141,8 +143,16 @@ def run_with_runner(self, args, runner):
runner.dump()
sys.exit(0)
r = runner.run()
b = Runner.ShowResult(r)
sys.exit(b)
exit_code = self.on_run_response(r)
sys.exit(exit_code)

def on_run_response(self, response):
if self.result_key:
exit_code, msg = Wandbox.GetResult(response, self.result_key)
print(msg)
else:
exit_code = Wandbox.ShowResult(response)
return exit_code

def command_help(self, args):
print(self.parser.parse_args([args.subcommand[0], '--help']))
Expand Down Expand Up @@ -276,11 +286,17 @@ def setup(self, lang, compiler, has_option):
help='build and run command. see `run +h`'
)
passthrough_cmd.set_defaults(handler=self.command_run)
passthrough_cmd.add_argument(
'sources',
metavar='SOURCE',
nargs='+',
help='source files'
)
passthrough_cmd.add_argument(
'compile_options',
metavar='COMPILE_OPTIONS',
nargs='*',
help='comiple command options'
help='comiple options'
)

subcommands = self.parser.format_usage().split('{')[1].split('}')[0]
Expand Down
Loading