00001
00002
00003
00004
00005 import sys
00006 from GPyUnit.util import DispatchEx
00007 import os
00008
00009 def mgafiles( dir, begin_pattern = "", end_pattern = "" ):
00010 ld = os.listdir( dir)
00011 mgas = []
00012 for k in ld:
00013 if k.endswith( end_pattern + ".mga") and k.startswith( begin_pattern):
00014 mgas.append( k )
00015
00016 return mgas
00017
00018
00019 def doit( source, target):
00020 errors = 0
00021 errors_with_files = ""
00022 fs = []
00023 source_dir = ''
00024 target_dir = target
00025
00026 if os.path.isdir( source):
00027 source_dir = source
00028 fs = mgafiles( os.path.abspath(os.path.join( os.path.curdir, source_dir)))
00029 elif os.path.isfile( source):
00030 fs.append( source)
00031 else:
00032 raise 'File/Directory not given consistently'
00033
00034 for i in fs:
00035 mganame = os.path.join( source_dir, i)
00036 mganame = os.path.abspath( os.path.join( os.path.curdir, mganame))
00037
00038 xmename = os.path.join( target_dir, os.path.split(i)[1] + ".xme")
00039 xmename = os.path.abspath( os.path.join( os.path.curdir, xmename))
00040
00041 try:
00042 gme = DispatchEx("GME.Application")
00043 gme.Visible = 0
00044 gme.OpenProject( "MGA=" + mganame )
00045
00046 gme.ExportProject( xmename )
00047 gme.CloseProject(0)
00048 except:
00049 errors = errors + 1
00050 errors_with_files = errors_with_files + mganame + "\n"
00051 gme.CloseProject(0)
00052 pass
00053 return ( errors, errors_with_files)
00054
00055
00056
00057 if __name__ == "__main__":
00058 if len( sys.argv) < 3:
00059 source = "..\\models"
00060 target = "xme"
00061 print "="*79
00062 print "Usage: mga2xme.py Source Target"
00063 print " where Source can be a directory or file"
00064 print " Target must be a directory"
00065 print "i.e.: mga2xme.py", source, target
00066 print "i.e.: mga2xme.py ..\\models\\_tc5_sf_A.mga xme\\"
00067 print "="*79
00068 sys.exit(1)
00069 else:
00070 source = sys.argv[1]
00071 target = sys.argv[2]
00072
00073 (nerrs, errfiles) = doit( source, target)
00074 if nerrs > 0:
00075 print "Problematic files: \n" + errfiles