Skip to content

Commit

Permalink
Merge pull request #240 from CPSSD/c/fix-args-casting#234
Browse files Browse the repository at this point in the history
Change communications with binaries to be in JSON
  • Loading branch information
CianLR authored Mar 28, 2017
2 parents f0be266 + 03c036b commit acb6bbe
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 29 deletions.
6 changes: 5 additions & 1 deletion dipla/api_support/script_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from base64 import b64decode
from types import CodeType
import pickle
import json
import sys
encoded_code = {}
Expand All @@ -19,5 +20,8 @@ def unwraped_func():
unwraped_func.__code__ = func_code
print(unwraped_func(*sys.argv[1:]))
args = json.loads(sys.argv[1])
output = dict()
output['data'] = unwraped_func(*args)
print(json.dumps(output))
"""
8 changes: 6 additions & 2 deletions dipla/client/command_line_binary_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from logging import getLogger
from subprocess import Popen, PIPE
from os.path import isfile
Expand Down Expand Up @@ -42,14 +43,17 @@ def _binary_exists(self, file_path):
def _run_binary(self, file_path, arguments):
self._logger.debug("About to run binary %s" % file_path)
process = Popen(
args=[file_path] + [str(x) for x in arguments],
args=[file_path] + [json.dumps(arguments)],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
shell=False
)
process_output = process.communicate(None)[0]
return process_output.strip().decode()
cleaned_output = process_output.strip().decode()
if cleaned_output:
return json.loads(cleaned_output)['data']
return {}


class InvalidArgumentsError(Exception):
Expand Down
7 changes: 2 additions & 5 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ def factor_verifier(inp, out):
# Input is a tuple of the inputs, even if there's only one input.
# E.g. (2,)
input_int = inp[0]
# Output comes in as a string. I.e. turn "[1, 2]" into [1, 2]
out_ints = list(map(int, out.strip('[]').split(',')))
for fac in out_ints:
for fac in out:
if input_int%fac:
# If there's a non-0 remainder
return False
print(out_ints, "are all factors of", input_int)
print(out, "are all factors of", input_int)
return True

@Dipla.distributable(verifier=factor_verifier)
def get_factors(n):
n = int(n)
facts = [1]
for i in range(2, n // 2 + 1):
if n % i == 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def test_that_binary_produces_valid_output(self):
self.given_using_a_github_resource()
self.given_searching_for("word")
self.when_the_binary_is_run()
self.then_the_result_will_be(["3"])
self.then_the_result_will_be([3])

def test_that_binary_produces_valid_output_with_another_url(self):
self.given_a_web_count_binary()
self.given_using_another_github_resource()
self.given_searching_for("BLARG")
self.when_the_binary_is_run()
self.then_the_result_will_be(["4"])
self.then_the_result_will_be([4])

def given_a_non_existent_binary(self):
self.filepath = "/dont_exist/binary"
Expand Down
8 changes: 4 additions & 4 deletions tests/client/command_line_binary_runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def test_that_no_exception_is_thrown_when_binary_exists(self):

def test_that_binary_produces_valid_output(self):
self.given_a_sums_binary()
self.given_summing("3", "5")
self.given_summing(3, 5)
self.when_the_binary_is_run()
self.then_the_result_will_be(["8"])
self.then_the_result_will_be([8])

def test_that_binary_produces_valid_output_with_more_input(self):
self.given_a_sums_binary()
self.given_summing("1", "2")
self.given_summing(1, 2)
self.when_the_binary_is_run()
self.then_the_result_will_be(["3"])
self.then_the_result_will_be([3])

def given_a_non_existent_binary(self):
self.filepath = "/dont_exist/binary"
Expand Down
6 changes: 3 additions & 3 deletions tests/example_binaries/build_binaries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ sudo apt-get install libcurl4-gnutls-dev

echo "About to build binaries..."

g++ -o $BIN_DIR/dynamic_web_count/example.exe $BIN_DIR/dynamic_web_count/example.cpp -lcurl
g++ -o $BIN_DIR/dynamic_web_count/example.exe $BIN_DIR/dynamic_web_count/example.cpp -lcurl -std=c++11

g++ -o $BIN_DIR/web_count/web_count.exe $BIN_DIR/web_count/web_count.cpp -lcurl
g++ -o $BIN_DIR/web_count/web_count.exe $BIN_DIR/web_count/web_count.cpp -lcurl -std=c++11

g++ -o $BIN_DIR/sums/sums.exe $BIN_DIR/sums/sums.cpp
g++ -o $BIN_DIR/sums/sums.exe $BIN_DIR/sums/sums.cpp -std=c++11

echo "Done."

17 changes: 13 additions & 4 deletions tests/example_binaries/sums/sums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ using namespace std;

int main(int argc, char *argv[]) {

if (argc < 3) {
if (argc < 2) {
cout << "Not enough arguments" << endl;
return 1;
}

int numA = atoi(argv[1]);
int numB = atoi(argv[2]);
string arg = argv[1];

// This code's purpose is to parse out the JSON provided on argv
int end_of_num_A = arg.find(",");
int numA = atoi(arg.substr(1, end_of_num_A - 1).c_str());
int numB = atoi(arg.substr(
end_of_num_A + 2,
arg.size() - end_of_num_A - 3).c_str());

int sum = numA + numB;

cout << sum << endl;
string output = "{\"data\": }";
output.insert(9, to_string(sum));
cout << output << endl;

return 0;
}
Expand Down
28 changes: 20 additions & 8 deletions tests/example_binaries/web_count/web_count.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,38 @@ int count_substr(const string& base, const string& sub) {

int main(int argc, char* argv[]) {

if (argc < 3) {
cout << "Usage: web_count <url> <search_term>" << endl;
if (argc < 2) {
cout << "Usage: web_count [<url>, <search_term>]" << endl;
return 1;
}

string arg = argv[1];

// This code's purpose is to parse out the JSON provided on argv
int end_of_url = arg.find("\",");
string url = arg.substr(2, end_of_url - 2);
string term = arg.substr(
end_of_url + 4,
arg.size() - end_of_url - 6);

CURL *curl = curl_easy_init();
CURLcode res;
string response;
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
cout << "-1";
} else {
cout << count_substr(response, argv[2]) << endl;
int num_substr = -1;
if(res == CURLE_OK) {
num_substr = count_substr(response, term);
}


string output = "{\"data\": }";
output.insert(9, to_string(num_substr));
cout << output << endl;

curl_easy_cleanup(curl);
return 0;
}

0 comments on commit acb6bbe

Please sign in to comment.