#! /usr/bin/python import socket import subprocess import errno import sys import os import json import argparse import traceback def parse_args(args, inputs): i = 0 options = [a for a in args if "options" in a] values = [a for a in args if "name" in a] # find options, like --port, or -f while i < len(args): arg = args[i].strip() if arg == "--": # after --, we only have positional arguments break if len(arg) and arg[0] == "-" for o in options: if arg in o["options"]: num_values = 1 if "num_values" in o and o["num_values"] != "*": num_values = int(o["num_values"]) if i == len(args) - num_values: print "Not enough values for " + arg vals = [] while num_values > 0: i += 1 vals.append(args[i]) num_values -= 1 i += 1 port = 7890 job_parser = ArgumentParser(description="Specify an export job") job_parser.add_argument("input_swf", help="location of a .swf file to export") job_parser.add_argument("-t", "--type", default=1, help="export type: 1=spritesheet, 3=skeletal", choices=[1,3]) desc = "Export .swf files, either from the command line or piped from stdin" cmd_parser_base = ArgumentParser(description=desc) cmd_parser_base.add_argument("-p", "--port", default=port, help="port to use for the wrapper server", type=int) cmd_parser_single = ArgumentParser(description=desc, parents=[cmd_parser_base, job_parser], conflict_handler="resolve") cmd_parser_stdin = ArgumentParser(description=desc, parents=[cmd_parser_base], conflict_handler="resolve") cmd_parser_stdin.add_argument("use_stdin", choices=["-"], help="parse export jobs from stdin") #args_single = cmd_parser_single.parse_args_safe() args_stdin = cmd_parser_stdin.parse_args_safe() #if args_single is None: # #if args_stdin is None: # # cmd_parser_single.print_usage() # # cmd_parser_stdin.print_usage() # # sys.exit(2) # #else: # # print "read from stdin" # cmd_parser_single.print_usage() # pass #else: # print "do single command: " + str(args_single) sys.exit(0) s = socket.socket() s.bind(("localhost", port)) args = [os.getcwd() + "/GraphicExport.exe"] # args = [os.getcwd() + "/GraphicExport.app/GraphicExport.exe", str(port)] g = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) s.settimeout(0.5) s.listen(0) conn = None while conn is None and g.returncode is None: try: (conn, _) = s.accept() except socket.timeout, e: pass except e: print e s.close() g.wait() sys.exit(1) g.poll() if conn: conn.setblocking(False) conn.send("EXPORT|" + "|".join(sys.argv[1:]) + "\0") while g.returncode is None: try: d = conn.recv(4096) if len(d): sys.stdout.write(d) sys.stdout.flush() except socket.error, e: if e.args[0] == errno.EWOULDBLOCK or e.args[0] == errno.EAGAIN: pass else: print e conn.close() s.close() g.wait() sys.exit(1) g.poll() if (conn): conn.close() s.close()