00001 from __future__ import with_statement
00002
00003 import os
00004 import sys
00005 import unittest
00006
00007 _opts = type("Options", (object,), {
00008 'Dispatch_x64': False,
00009 'Run_SVN': False,
00010 })
00011
00012 def DispatchEx(progid):
00013 import win32com.client
00014
00015 CLSCTX_ALL = 23
00016 CLSCTX_LOCAL_SERVER = 4
00017 CLSCTX_ACTIVATE_32_BIT_SERVER = 0x40000
00018 CLSCTX_ACTIVATE_64_BIT_SERVER = 0x80000
00019 if _opts.Dispatch_x64:
00020 return win32com.client.DispatchEx(progid, clsctx=CLSCTX_LOCAL_SERVER | CLSCTX_ACTIVATE_64_BIT_SERVER)
00021 else:
00022 import platform
00023
00024 if platform.system() != 'Java' and '64bit' in platform.architecture():
00025 return win32com.client.DispatchEx(progid, clsctx=CLSCTX_LOCAL_SERVER | CLSCTX_ACTIVATE_32_BIT_SERVER)
00026 return win32com.client.DispatchEx(progid)
00027
00028
00029 class disable_early_binding(object):
00030 def __enter__(self):
00031 import win32com.client.gencache
00032
00033 self._savedGetClassForCLSID = win32com.client.gencache.GetClassForCLSID
00034 win32com.client.gencache.GetClassForCLSID = lambda x: None
00035 return self
00036
00037 def __exit__(self, exc_type, exc_value, traceback):
00038 import win32com.client.gencache
00039
00040 win32com.client.gencache.GetClassForCLSID = self._savedGetClassForCLSID
00041
00042
00043 class NoopUsing(object):
00044 def __enter__(self):
00045 return self
00046
00047 def __exit__(self, exc_type, exc_value, traceback):
00048 pass
00049
00050 import platform
00051
00052 if platform.system() == 'Java':
00053 disable_early_binding = NoopUsing
00054
00055 def dec_disable_early_binding(f):
00056 def ret(*args, **kwargs):
00057 with disable_early_binding():
00058 f(*args, **kwargs)
00059
00060
00061 ret.__name__ = f.__name__
00062 return ret
00063
00064
00065 def register_xmp(xmpfile):
00066 import os.path
00067
00068 predef = {'SF': os.path.join(os.environ['GME_ROOT'], "Paradigms", "SF", "SF.xmp"),
00069 'MetaGME': os.path.join(os.environ['GME_ROOT'], "Paradigms", "MetaGME", "MetaGME.xmp")}
00070 if not os.path.isfile(xmpfile):
00071 xmpfile = predef[xmpfile]
00072 import gme
00073
00074 gme.register_if_not_registered(xmpfile)
00075
00076
00077 def parse_xme(connstr, xme=None, project=None):
00078 testdir = os.path.dirname(os.path.abspath(__file__))
00079 if xme is None:
00080 xme = os.environ['GME_ROOT'] + r"\Paradigms\MetaGME\MetaGME-model.xme"
00081 parser = DispatchEx("Mga.MgaParser")
00082
00083 if project is None:
00084 (paradigm, parversion, parguid, basename, ver) = parser.GetXMLInfo(xme)
00085 import sys
00086
00087 project = DispatchEx("Mga.MgaProject")
00088 project.Create(connstr, paradigm)
00089 parser.ParseProject(project, xme)
00090 return project
00091
00092
00093 def onerror(func, path, exc_info):
00094 """
00095 Error handler for ``shutil.rmtree``.
00096
00097 If the error is due to an access error (read only file)
00098 it attempts to add write permission and then retries.
00099
00100 If the error is for another reason it re-raises the error.
00101
00102 Usage : ``shutil.rmtree(path, onerror=onerror)``
00103 """
00104 import stat
00105
00106 if not os.access(path, os.W_OK):
00107
00108 os.chmod(path, stat.S_IWUSR)
00109 func(path)
00110 else:
00111 raise
00112
00113
00114 class MUTestMixin(
00115 unittest.TestCase):
00116 def __init__(self, name, **kwds):
00117 super(MUTestMixin, self).__init__(name, **kwds)
00118 import os.path
00119
00120 self.mgxdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "MUTest")
00121
00122 def setUp(self):
00123 super(MUTestMixin, self).setUp()
00124 import os.path
00125
00126 if os.path.isdir(self.mgxdir):
00127 import shutil
00128
00129 assert len(self.mgxdir) > 10
00130 shutil.rmtree(self.mgxdir, onerror=onerror)
00131
00132 @property
00133 def connstr(self):
00134 return "MGX=\"" + self.mgxdir + "\""
00135
00136
00137 class MUSVNTestMixin(MUTestMixin):
00138 def setUp(self):
00139 super(MUSVNTestMixin, self).setUp()
00140 svn_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "MUTestRepo")
00141 self.svn_url = "file:///" + svn_file
00142 if os.path.isdir(svn_file):
00143 import shutil
00144
00145 assert len(svn_file) > 10
00146 shutil.rmtree(svn_file, onerror=onerror)
00147 import subprocess
00148
00149 subprocess.check_call(['svnadmin', 'create', svn_file])
00150 with open(os.environ['USERPROFILE'] + '\\GME_MU_config.opt', 'w') as file:
00151 file.write('''
00152 AutomaticLogin=true
00153 UseAccountInfo=true
00154 AutoCommit=true
00155 account=''' + os.environ['USERNAME'])
00156
00157 @property
00158 def connstr(self):
00159 print 'opts: ' + self.opts()
00160 return "MGX=\"" + self.mgxdir + "\" svn=\"" + self.svn_url + "\"" + self.opts()
00161
00162
00163 def MUGenerator(module, test):
00164
00165 if _opts.Dispatch_x64: return
00166 module[test.__name__ + "MU"] = type(test.__name__ + "MU", (MUTestMixin, test), {})
00167 if not _opts.Run_SVN: return
00168
00169 def opts_f(opts):
00170 return lambda self: opts
00171
00172 for name, opts in (('MUSVN', ''), ('MUSVNHashed', ' hash=\"true\" hval=\"256\"')):
00173 module[test.__name__ + name] = type(test.__name__ + name, (MUSVNTestMixin, test), {'opts': opts_f(opts)})