-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathsolverShell.py
More file actions
40 lines (29 loc) · 1.11 KB
/
solverShell.py
File metadata and controls
40 lines (29 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from subprocess import Popen, PIPE
def solve_it(input_data):
# Writes the inputData to a temporay file
tmp_file_name = 'tmp.data'
tmp_file = open(tmp_file_name, 'w')
tmp_file.write(input_data)
tmp_file.close()
# NB! dont forget to compile binary, e.g rustc knapsack.rs
# Runs the command: cat tmp_file_name | ./knapsack
# and pipes the content of a tmp_file directly into executable
process = Popen(['cat ' + tmp_file_name + ' | ./knapsack'],
stdout=PIPE, shell = True, universal_newlines = True
)
(stdout, stderr) = process.communicate()
# removes the temporay file
os.remove(tmp_file_name)
return stdout.strip()
import sys
if __name__ == '__main__':
if len(sys.argv) > 1:
file_location = sys.argv[1].strip()
with open(file_location, 'r') as input_data_file:
input_data = input_data_file.read()
print(solve_it(input_data))
else:
print('This test requires an input file. Please select one from the data directory. (i.e. python solver.py ./data/ks_4_0)')