コード例 #1
0
def recompile(modulename):
    """	recompile module from modulename
	"""

    ### modulename is file type
    if os.path.isfile(modulename) and os.path.exists(modulename):
        import ZipManager
        zf = ZipManager.Zip(modulename)
        return zf.Recompile()
    else:

        try:
            ### first, see if the module can be imported at all...
            name, ext = os.path.splitext(modulename)
            pkg = '.'.join(modulename.split('.')[0:-1])
            tmp = importlib.import_module(name, package=pkg)
        #tmp = __import__(modulename, globals(), locals(), fromlist = [modulename.split('.')[-1]])

        except Exception as info:
            return info

        ### Use the imported module to determine its actual path
        pycfile = os.path.abspath(tmp.__file__)
        modulepath = pycfile.replace(
            '.pyc', '.py')  #string.replace(pycfile, ".pyc", ".py")

        ### Try to open the specified module as a file
        code = open(modulepath, 'rU').read()

        ### see if the file we opened can compile.  If not, return the error that it gives.
        ### if compile() fails, the module will not be replaced.
        try:
            compile(code, modulename, "exec")
        except:
            return "Error in compilation: " + str(
                sys.exc_info()[0]) + "\r\n" + listf(
                    format_exception(sys.exc_info()[0],
                                     sys.exc_info()[1],
                                     sys.exc_info()[2]))
        else:

            ### Ok, it compiled.  But will it execute without error?
            try:
                exec(compile(open(modulepath).read(), modulepath, 'exec'),
                     globals())
            except Exception:
                #return "Error '%s' happened on line %d" % (info[0], info[1][1])
                return "Error in execution: " + str(
                    sys.exc_info()[0]) + "\r\n" + listf(
                        format_exception(sys.exc_info()[0],
                                         sys.exc_info()[1],
                                         sys.exc_info()[2]))

            else:
                ### at this point, the code both compiled and ran without error.  Load it up
                ### replacing the original code.
                return importlib.reload(tmp)  #sys.modules[modulename])
コード例 #2
0
    def load_plugins(modulename):
        """ This reads a plugins list to load. It is so plug-in
			imports are more dynamic and you don't need to continue appending
			import statements to the top of a file.
		"""

        if modulename in sys.modules:
            return sys.modules[modulename]
        else:
            try:
                if PLUGINS_PATH not in sys.path:
                    sys.path.append(PLUGINS_PATH)
                name, ext = os.path.splitext(modulename)
                pkg = '.'.join(modulename.split('.')[0:-1])
                module = importlib.import_module(name, package=pkg)
                return module
            except Exception as info:
                msg = _("Path of plugins directory is wrong."
                        ) if not os.path.exists(PLUGINS_PATH) else str(
                            sys.exc_info()[0]) + "\r\n" + listf(
                                format_exception(sys.exc_info()[0],
                                                 sys.exc_info()[1],
                                                 sys.exc_info()[2]))
                sys.stderr.write(
                    _("Error trying to import plugin %s : %s\n%s") %
                    (modulename, info, msg))
                return info
コード例 #3
0
def recompile(modulename):
	"""	recompile module from modulename
	"""

	### modulename is file type
	if os.path.exists(modulename):
		import ZipManager
		zf = ZipManager.Zip(modulename)
		return zf.Recompile()
	else:

		try:
			### first, see if the module can be imported at all...
			tmp = __import__(modulename, globals(), locals(), fromlist = [modulename.split('.')[-1]])

		except Exception, info:
			return info

		### Use the imported module to determine its actual path
		pycfile = tmp.__file__
		modulepath = string.replace(pycfile, ".pyc", ".py")

		### Try to open the specified module as a file
		code = open(modulepath, 'rU').read()

		### see if the file we opened can compile.  If not, return the error that it gives.
		### if compile() fails, the module will not be replaced.
		try:
			compile(code, modulename, "exec")
		except:
			return "Error in compilation: " + str(sys.exc_info()[0]) +"\r\n" + listf(format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
		else:

			### Ok, it compiled.  But will it execute without error?
			try:
				execfile(modulepath)
			except Exception:
				#return "Error '%s' happened on line %d" % (info[0], info[1][1])
				return "Error in execution: " + str(sys.exc_info()[0]) +"\r\n" + listf(format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))

			else:

				### at this point, the code both compiled and ran without error.  Load it up
				### replacing the original code.
				return reload(sys.modules[modulename])
コード例 #4
0
ファイル: ZipManager.py プロジェクト: estevaofv/DEVSimPy
class Zip:
    def __init__(self, fn, files=[]):
        """ Constructor
		"""
        ### local copy
        self.fn = fn

        if files != []:
            self.Create(files)

    def Create(self, add_files=[]):
        dir_name, base_name = os.path.split(self.fn)
        name, ext = os.path.splitext(base_name)

        ### output zip file
        zout = zipfile.ZipFile(self.fn, "w")

        ### for all files wich could be added
        for fn in filter(
                lambda f: os.path.exists(f) or zipfile.is_zipfile(
                    os.path.dirname(f)), add_files):
            fn_dir_name, fn_base_name = os.path.split(fn)
            fn_name, fn_ext = os.path.splitext(fn_base_name)
            ### if adding file is compressed, we decompress and add it
            if zipfile.is_zipfile(fn_dir_name):
                zin = zipfile.ZipFile(fn_dir_name, 'r')
                buffer = zin.read(fn_base_name)
                ### if not .dat file and the name of file is not the same with the zip file
                #if fn_ext == '.py':
                #zout.writestr("%s%s"%(name,fn_ext), buffer)
                #else:
                zout.writestr(fn_base_name, buffer)
                zin.close()
            else:
                zout.write(fn, fn_base_name)

        zout.close()

    def Update(self, replace_files=[]):
        """ Update zip archive with the new replace file names
		"""

        ### delete empty fileName
        replace_files = filter(lambda f: f != '', replace_files)

        # call this function because : http://www.digi.com/wiki/developer/index.php/Error_messages
        self.ClearCache()

        zin = zipfile.ZipFile(self.fn, 'r')
        zout = zipfile.ZipFile("new_arch.zip", 'w')

        exclude_file = []

        ### write all replace file in the new archive
        for fn in replace_files:
            dir_name, base_name = os.path.split(fn)

            if zipfile.is_zipfile(dir_name):
                #print '1'
                z = zipfile.ZipFile(dir_name, 'r')
                data = z.read(base_name)
                ### this line is in comment because is zip file contain image file we can not encode it.
                zout.writestr(base_name, data.encode('utf-8'))
                #zout.writestr(base_name, data)
                z.close()
                #print '11'
                #sys.stdout.write("update %s from compressed %s\n"%(base_name, fn))
            elif os.path.exists(fn):
                #print '2'
                zout.write(fn, base_name)
                #print '22'
                #sys.stdout.write("update %s from %s\n"%(base_name, fn))
            elif os.path.exists(base_name) and dir_name != "":
                #print '3'
                zout.write(base_name, fn)
                #print '33'
                #sys.stdout.write("update %s from %s\n"%(fn, base_name))
            else:
                exclude_file.append(replace_files.index(fn))
                #sys.stdout.write("%s unknown\n"%(fn))

        ### try to rewrite not replaced files from original zip
        info_list = zin.infolist()
        for item in info_list:
            s = os.path.basename(item.filename)
            if s not in map(os.path.basename, replace_files
                            ) and info_list.index(item) not in exclude_file:
                buffer = zin.read(item.filename)
                zout.writestr(item, buffer)
                sys.stdout.write("%s rewrite\n" % (item.filename))

        ### close all files
        zout.close()
        zin.close()

        ### remove and rename the zip file
        self.ClearFiles()

    def Delete(self, delete_files=[]):
        """ Remove file in zip archive
		"""

        ### delete empty fileName
        delete_files = filter(lambda f: f != '', delete_files)

        # call this function because : http://www.digi.com/wiki/developer/index.php/Error_messages
        self.ClearCache()

        zin = zipfile.ZipFile(self.fn, 'r')
        zout = zipfile.ZipFile("new_arch.zip", 'w')

        ###
        info_list = zin.infolist()
        for item in info_list:
            if item.filename not in delete_files:
                buffer = zin.read(item.filename)
                zout.writestr(item, buffer)
                ##sys.stdout.write("%s rewrite\n"%(item.filename))

        ### close all files
        zout.close()
        zin.close()

        ### remove and rename the zip file
        self.ClearFiles()

    def GetImage(self, scaleW=16, scaleH=16):
        """ Get image object from image file stored in zip file.
			scaleH and scaleW are used to rescale image
		"""

        if zipfile.is_zipfile(self.fn):
            return None

        zf = zipfile.ZipFile(self.fn, 'r')

        ### find if python file has same name of model file
        L = filter(lambda f: f.endswith(('.jpg', 'jpeg', 'png', 'bmp')),
                   zf.namelist())

        if L != []:
            import wx
            f = zf.open(L.pop())
            buf = f.read()
            f.close()
            zf.close()
            sbuf = StringIO.StringIO(buf)
            image = wx.ImageFromStream(sbuf)
            sbuf.close()
            image.Rescale(scaleW, scaleH)
            return image
        else:
            zf.close()
            return None

    @staticmethod
    def GetPluginFile(fn):
        """ TODO: comment
		"""
        ### zipfile (amd or cmd)
        zf = zipfile.ZipFile(fn, 'r')
        nl = zf.namelist()
        zf.close()

        L = filter(lambda a: a != [],
                   map(lambda s: re.findall("^(plugins[/]?[\w]*.py)$", s), nl))
        return L.pop(0)[0] if L != [] else ""

    @staticmethod
    def HasPlugin(fn):
        """ TODO: comment
		"""

        ### zipfile (amd or cmd)
        zf = zipfile.ZipFile(fn, 'r')
        nl = zf.namelist()
        zf.close()
        ### plugin file is plugins.pi in root of zipfile or in plugins zipedd directory
        return any(map(lambda s: re.search("^(plugins[/]*[\w]*.py)$", s), nl))

    # BDD Test----------------------------------------------------------------------
    @staticmethod
    def HasTests(fn):
        """ TODO: comment
		"""
        name = os.path.basename(getPythonModelFileName(fn)).split('.')[0]
        zf = zipfile.ZipFile(fn, 'r')
        nl = zf.namelist()
        zf.close()
        return any(
            map(
                lambda s: re.search("^(BDD/[\w*/]*\.py|BDD/[\w*/]*\.feature)$",
                                    s), nl))

    @staticmethod
    def GetTests(fn):
        """ Return feature, steps and environment files from .amd
		"""
        zf = zipfile.ZipFile(fn, 'r')
        nl = zf.namelist()

        zf.close()

        ###
        tests_files = filter(
            lambda a: a != [],
            map(
                lambda s: re.findall(
                    "^(BDD/[\w*/]*\.py|BDD/[\w*/]*\.feature)$", s), nl))
        tests_files = [a[0] for a in tests_files]

        return tests_files

        #### find feature and steps file
        #feat = None
        #steps = None
        #env = None

        #feat = filter(lambda t: t.endswith('.feature'), zf.namelist())[0]

        #if 'steps.py' in zf.namelist():
        #steps = "steps.py"
        #if 'environment.py' in zf.namelist():
        #env = "environment.py"

        #zf.close()

        #return feat, steps, env

    # ------------------------------------------------------------------------------

    def GetModule(self, rcp=False):
        """ Load module from zip file corresponding to the amd or cmd model.
			It used when the tree library is created.
		"""

        module_name = getPythonModelFileName(self.fn)

        # get module name
        try:
            module_name = getPythonModelFileName(self.fn)
        except Exception, info:
            sys.stderr.write(
                _("Error in ZipManager class for GetModule: no python file in the archive\n"
                  ))
            return info

        # if necessary, recompile (for update after editing code source of model)
        #if rcp: recompile(module_name)

        # import module
        try:
            ### clear to clean the import after exporting model (amd or cmd) and reload within the same instance of DEVSimPy
            zipimport._zip_directory_cache.clear()

            trigger_event("IMPORT_STRATEGIES", fn=self.fn)

            importer = zipimport.zipimporter(self.fn)

            ### allows to import the lib from its name (like import MyModel.amd). Dangerous because confuse!
            ### Import can be done using: import Name (ex. import MessageCollector - if MessageCollecor is .amd or .cmd)
            fullname = "".join([
                os.path.basename(os.path.dirname(self.fn)),
                module_name.split('.py')[0]
            ])
            module = importer.load_module(module_name.split('.py')[0])
            module.__name__ = path_to_module(module_name)

            ### allows to import with a reference from the parent directory (like parentName.model).
            ### Now import of .amd or .cmd module is composed by DomainModel (no point!).
            ### Example : import CollectorMessageCollector
            sys.modules[fullname] = module

            ### TODO make a recursive method to go up until the Domain dir, for not external lib!

        except Exception, info:
            msg_i = _("Error in execution: ")
            msg_o = listf(
                format_exception(sys.exc_type, sys.exc_value,
                                 sys.exc_traceback))
            try:
                sys.stderr.write(msg_i + str(sys.exc_info()[0]) + "\r\n" +
                                 msg_o)
            except UnicodeDecodeError:
                sys.stderr.write(
                    msg_i +
                    str(sys.exc_info()[0]).decode('latin-1').encode("utf-8") +
                    "\r\n" + msg_o)
            return info
コード例 #5
0
ファイル: ZipManager.py プロジェクト: Orsucciu/DEVSimPy
    def GetModule(self, rcp=False):
        """ Load module from zip file corresponding to the amd or cmd model.
			It used when the tree library is created.
		"""

        module_name = getPythonModelFileName(self.fn)

        # get module name
        try:
            module_name = getPythonModelFileName(self.fn)
        except Exception as info:
            sys.stderr.write(
                _("Error in ZipManager class for GetModule: no python file in the archive\n"
                  ))
            return info

        # if necessary, recompile (for update after editing code source of model)
        #if rcp: recompile(module_name)

        # import module
        try:
            ### clear to clean the import after exporting model (amd or cmd) and reload within the same instance of DEVSimPy
            zipimport._zip_directory_cache.clear()

            trigger_event("IMPORT_STRATEGIES", fn=self.fn)

            importer = zipimport.zipimporter(self.fn)

            ### allows to import the lib from its name (like import MyModel.amd). Dangerous because confuse!
            ### Import can be done using: import Name (ex. import MessageCollector - if MessageCollecor is .amd or .cmd)
            p = os.path.dirname(os.path.dirname(self.fn))
            if p not in sys.path:
                sys.path.append(p)
            fullname = "".join([
                os.path.basename(os.path.dirname(self.fn)),
                module_name.split('.py')[0]
            ])
            module = importer.load_module(module_name.split('.py')[0])
            module.__name__ = path_to_module(module_name)

            ### allows to import with a reference from the parent directory (like parentName.model).
            ### Now import of .amd or .cmd module is composed by DomainModel (no point!).
            ### Example : import CollectorMessageCollector
            sys.modules[fullname] = module

            ### TODO make a recursive method to go up until the Domain dir, for not external lib!

        except Exception as info:
            msg_i = _("Error in execution: ")
            msg_o = listf(
                format_exception(sys.exc_info()[0],
                                 sys.exc_info()[1],
                                 sys.exc_info()[2]))
            try:
                sys.stderr.write(msg_i + str(sys.exc_info()[0]) + "\r\n" +
                                 msg_o)
            except UnicodeDecodeError:
                sys.stderr.write(
                    msg_i +
                    str(sys.exc_info()[0]).decode('latin-1').encode("utf-8") +
                    "\r\n" + msg_o)
            return info

        else:
            return module
コード例 #6
0
ファイル: ZipManager.py プロジェクト: dunli/DESMining
                module_name.split('.py')[0]
            ])
            module = importer.load_module(module_name.split('.py')[0])
            module.__name__ = path_to_module(module_name)

            ### allows to import with a reference from the parent directory (like parentName.model).
            ### Now import of .amd or .cmd module is composed by DomainModel (no point!).
            ### Example : import CollectorMessageCollector
            sys.modules[fullname] = module

            ### TODO make a recursive method to go up until the Domain dir, for not external lib!

        except Exception, info:
            msg_i = _("Error in execution: ")
            msg_o = listf(
                format_exception(sys.exc_type, sys.exc_value,
                                 sys.exc_traceback))
            try:
                sys.stderr.write(msg_i + str(sys.exc_info()[0]) + "\r\n" +
                                 msg_o)
            except UnicodeDecodeError:
                sys.stderr.write(
                    msg_i +
                    str(sys.exc_info()[0]).decode('latin-1').encode("utf-8") +
                    "\r\n" + msg_o)
            return info

        else:
            return module

    def Recompile(self):