#! /usr/bin/python
import socket
import errno
import sys
import os
import os.path
import json
import traceback
import shlex
import struct
from args import *
from job import *
from driver import *
def usage():
print "usage 1: ./exporter.py [--port/-p wrapper_communication_port] [--type/-t (1,3)] [--scale/-s skeletal_scale] [--base/-b base_dir] input_files..."
print " Exports the given file(s) with the given settings"
print
print "usage 2: ./exporter.py [--port/-p wrapper_communication_port] [--base/-b base_dir] -"
print " Reads commands (everything after --port in usage 1) as lines from stdin"
print
print "usage 3: ./export.py --help|-h"
print " Shows this help message"
if "-h" in sys.argv or "--help" in sys.argv:
usage()
sys.exit(0)
(err, args) = ArgParser().parse_args(sys.argv[1:])
if err is not None:
print err
usage()
sys.exit(2)
from_stdin = False
if "-" in args["input"]:
from_stdin = True
if len(args["input"]) > 1 or "type" in args or "scale" in args:
print "If reading from stdin ('-') is specified, no other input may be given"
usage()
sys.exit(2)
if not len(args["input"]):
usage()
sys.exit(0)
port = args["port"] if "port" in args else 7890
def make_base_job_args(args):
return {"type": args["type"] if "type" in args else 1, \
"scale": args["scale"] if "scale" in args else 1}
jobs = {}
driver = Driver(port)
if driver.start():
if not from_stdin:
base_args = make_base_job_args(args)
for i in args["input"]:
try:
job = Job(base_args, input = i)
jobs[job.id] = job
except Exception, e:
print e
driver.loop(handle_command, handle_stdin)
def handle_stdin(line):
parts = shlex.split(line)
(err, line_args) = ArgParser.parse_args(parts)
if err is not None:
print err
else:
base_args= make_base_job_args(line_args)
for i in line_args["input"]:
pass
#send_job(conn, base_cmd, i)
def handle_command(msg_str):
msg = json.loads(msg_str)
if "command" in msg:
cmd = msg["command"]
if cmd == "exit":
return False
if cmd == "print" and "string" in msg:
sys.stdout.write(msg["string"])
sys.stdout.flush()
if cmd == "done" and "job_num" in msg:
job_num = int(cmd["job_num"])
if job_num == num_jobs_sent:
return False
return True