#! /usr/bin/python import socket import subprocess import errno import sys import os import json import argparse import traceback class ArgumentParser(argparse.ArgumentParser): def __init__(self, \ prog=None, \ usage=None, \ description=None, \ epilog=None, \ parents=[], \ formatter_class=argparse.HelpFormatter, \ prefix_chars='-', \ fromfile_prefix_chars=None, \ argument_default=None, \ conflict_handler='error', \ add_help=True): self._parse_safe = False super(ArgumentParser, self).__init__(prog=prog, \ usage=usage, \ description=description, \ epilog=epilog, \ parents=parents, \ formatter_class=formatter_class, \ prefix_chars=prefix_chars, \ fromfile_prefix_chars=fromfile_prefix_chars, \ argument_default=argument_default, \ conflict_handler=conflict_handler, \ add_help=add_help) def _get_action_from_name(self, name): """Given a name, get the Action instance registered with this parser. If only it were made available in the ArgumentError object. It is passed as it's first arg... """ container = self._actions if name is None: return None for action in container: if '/'.join(action.option_strings) == name: return action elif action.metavar == name: return action elif action.dest == name: return action def has_dest(self, name): for a in self._actions: if a.dest == name: return True return False def parse_args_safe(self, args=None, namespace=None): print "parse safe" self._parse_safe = True ret = None try: ret = self.parse_args(args, namespace) except: print "AE: " + str(sys.exc_info()[1]) ret = None print "done" self._parse_safe = False return ret def error(self, message): print "error" if self._parse_safe: exc = sys.exc_info()[1] if exc: exc.argument = self._get_action_from_name(exc.argument_name) raise exc else: raise argparse.ArgumentError(None, message) super(ArgumentParser, self).error(message) 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()