예제 #1
0
    def add_module(self, fqname, filename, path=None, is_archive=False):
        """Save timestamp here"""
        if fqname in self.modules:
            return self.modules[fqname]
        # print 'pre-adding %s' % fqname
        if not filename:  # ignore any builtin or extension
            return

        if is_archive:
            # module's filename is set to the actual archive
            relfilename = os.path.split(filename)[1]
            # cloudpickle needs to know about this to deserialize correctly:
        else:
            # get relative path:
            numsplits = fqname.count('.') + 1
            relfilename = ""
            absfilename = filename
            for i in xrange(numsplits):
                absfilename, tmp = os.path.split(absfilename)
                relfilename = tmp + '/' + relfilename
                if '__init__' in tmp:
                    # additional split as this is a package and
                    # __init__ is not in fqname
                    absfilename, tmp = os.path.split(absfilename)
                    relfilename = tmp + '/' + relfilename
            relfilename = relfilename[:-1]  # remove terminating /

        self.modules[fqname] = m = modulefinder.Module(fqname,
                                                       relfilename, path)
        # picloud: Timestamp module for update checks
        m.timestamp = long(os.path.getmtime(filename))
        m.is_archive = is_archive
        return m
예제 #2
0

# Mock for sys.modules, which are searched to find the modules that need
# reloading. PyTest will inspect sys.modules to do its magic so we extend
# a copy of the real sys.modules rather than just creating a totally bogus
# new one.
MOCK_SYSMODULES = sys.modules.copy()
MOCK_SYSMODULES.update({
    name: MockModule(name, filepath, MODULE_ATTRS.get(name, []))
    for name, filepath in MOCK_MODULES.items()})

# Mapping of module name to a modulefinder.Module object, which is used by
# the modulefinder module to store metadata about a module - e.g. its name,
# filepath and the names of its attributes.
MOCK_MODULEFINDER_MODS = {
    name: modulefinder.Module(name, file=filepath)
    for name, filepath in MOCK_MODULES.items()}


def _set_module_globals():
    """
    Set the globalnames attribute on the mocked modulefinder modules, and
    set the __module__ attribute on each attribute to match its owning module.

    WARNING: this function mutates MOCK_MODULEFINDER_MODS, it should only be
    called once.
    """
    for mod_name, mod in MOCK_MODULEFINDER_MODS.items():
        for attr in MODULE_ATTRS.get(mod_name, []):
            mod.globalnames[attr.__name__] = 1
            attr.__module__ = mod_name