def test_directory(self):
     """
     L{filenameToModuleName} returns the correct module (a package) given a
     directory.
     """
     module = reflect.filenameToModuleName(self.path)
     self.assertEqual(module, 'fakepackage.test')
     module = reflect.filenameToModuleName(self.path + os.path.sep)
     self.assertEqual(module, 'fakepackage.test')
 def test_directory(self):
     """
     Tests it finds good name for directories/packages.
     """
     module = reflect.filenameToModuleName(os.path.join('twisted', 'test'))
     self.assertEquals(module, 'test')
     module = reflect.filenameToModuleName(os.path.join('twisted', 'test')
                                           + os.path.sep)
     self.assertEquals(module, 'test')
Exemple #3
0
    def parseArgs(self, *args):
        def _dbg(msg):
            if self._logObserver is not None:
                log.msg(iface=ITrialDebug, parseargs=msg)

        if self.extra is not None:
            args = list(args)
            args.extend(self.extra)

        for arg in args:
            _dbg("arg: %s" % (arg,))
            
            if not os.sep in arg and not arg.endswith('.py'):
                # simplest case, someone writes twisted.test.test_foo on the command line
                # only one option, use namedAny
                try:
                    self._tryNamedAny(arg)
                except ArgumentError:
                    pass
                else:
                    continue
            
            # make sure the user isn't smoking crack
            if not os.path.exists(arg):
                raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), arg)

            # if the argument ends in os.sep, it *must* be a directory (if it's valid)
            # directories must be modules/packages, so use filenameToModuleName
            if arg.endswith(os.sep) and arg != os.sep:
                _dbg("arg endswith os.sep")
                arg = arg[:-len(os.sep)]
                _dbg("arg now %s" % (arg,))
                modname = reflect.filenameToModuleName(arg)
                self._tryNamedAny(modname)
                continue
                
            elif arg.endswith('.py'):
                _dbg("*Probably* a file.")
                if osp.exists(arg):
                    self._handleFile(arg)
                    continue

            elif osp.isdir(arg):
                if osp.exists(opj(arg, '__init__.py')):
                    modname = reflect.filenameToModuleName(arg)
                    self._tryNamedAny(modname)
                    continue
                
            raise ArgumentError, ("argument %r appears to be "
                 "invalid, rather than doing something incredibly stupid, "
                 "I'm blowing up" % (arg,))
Exemple #4
0
    def parseArgs(self, *args):
        if self['extra'] is not None:
            args = list(args)
            args.append(self['extra'])
        for arg in args:
            if (os.sep in arg):
                # It's a file.
                if not os.path.exists(arg):
                    import errno
                    raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), arg)
                if arg.endswith(os.sep) and (arg != os.sep):
                    arg = arg[:-len(os.sep)]
                name = reflect.filenameToModuleName(arg)
                if os.path.isdir(arg):
                    self['packages'].append(name)
                else:
                    self['modules'].append(name)
                continue

            if arg.endswith('.py'):
                # *Probably* a file.
                if os.path.exists(arg):
                    arg = reflect.filenameToModuleName(arg)
                    self['modules'].append(arg)
                    continue

            # a non-default reactor must have been installed by now: it
            # imports the module, which installs a reactor
            try:
                arg = reflect.namedAny(arg)
            except ValueError:
                raise usage.UsageError, "Can't find anything named %r to run" % arg
            except:
                self['_couldNotImport'][arg] = failure.Failure()
                continue

            if inspect.ismodule(arg):
                filename = os.path.basename(arg.__file__)
                filename = os.path.splitext(filename)[0]
                if filename == '__init__':
                    self['packages'].append(arg)
                else:
                    self['modules'].append(arg)
            elif inspect.isclass(arg):
                self['testcases'].append(arg)
            elif inspect.ismethod(arg):
                self['methods'].append(arg)
            else:
                # Umm, seven?
                self['methods'].append(arg)
Exemple #5
0
def filenameToModule(fn):
    """
    Given a filename, do whatever possible to return a module object matching
    that file.

    If the file in question is a module in Python path, properly import and
    return that module. Otherwise, load the source manually.

    @param fn: A filename.
    @return: A module object.
    @raise ValueError: If C{fn} does not exist.
    """
    if not os.path.exists(fn):
        raise ValueError("%r doesn't exist" % (fn,))
    try:
        ret = reflect.namedAny(reflect.filenameToModuleName(fn))
    except (ValueError, AttributeError):
        # Couldn't find module.  The file 'fn' is not in PYTHONPATH
        return _importFromFile(fn)
    # ensure that the loaded module matches the file
    retFile = os.path.splitext(ret.__file__)[0] + '.py'
    # not all platforms (e.g. win32) have os.path.samefile
    same = getattr(os.path, 'samefile', samefile)
    if os.path.isfile(fn) and not same(fn, retFile):
        del sys.modules[ret.__name__]
        ret = _importFromFile(fn)
    return ret
Exemple #6
0
    def _determine_calling_module(self, by_module):
        if not by_module:
            # if the caller did not specify a module that made the logging call, attempt to find
            # the module by inspecting the stack: record 0 is this, 1 is _should_log, 2 is the
            # logging function, and 3 will be the caller.
            record = inspect.stack()[3]
            # the first element of the record is the frame, which contains the locals and globals
            frame = record[0]
            f_globals = frame.f_globals

            # check the stack frame's globals for the __name__ attribute, which is the module name
            if '__name__' in f_globals:
                by_module = f_globals['__name__']
            else:
                # but it might not be a regular python module (such as a service.tac),
                # in which case we have to fall back to using the __file__ attribute.
                by_module = reflect.filenameToModuleName(f_globals['__file__'])

        elif not isinstance(by_module, basestring):
            # if the caller gave us an actual module, and not just its name, determine its
            # name and use it.
            by_module = reflect.fullyQualifiedName(by_module)
        
        modules = by_module.split('.')
        return modules
Exemple #7
0
 def test_file(self):
     """
     L{filenameToModuleName} returns the correct module given the path to
     its file.
     """
     module = reflect.filenameToModuleName(os.path.join(self.path, "test_reflect.py"))
     self.assertEqual(module, "fakepackage.test.test_reflect")
 def test_file(self):
     """
     Test it finds good name for files.
     """
     module = reflect.filenameToModuleName(
         os.path.join('twisted', 'test', 'test_reflect.py'))
     self.assertEquals(module, 'test_reflect')
def main(target):
    mf = mymf(sys.path[:], 0, [])

    moduleNames = []
    for path, dirnames, filenames in os.walk(target):
        if 'test' in dirnames:
            dirnames.remove('test')
        for filename in filenames:
            if not filename.endswith('.py'):
                continue
            if filename == '__init__.py':
                continue
            if '-' in filename:
                # a script like update-documentation.py
                continue
            moduleNames.append(
                reflect.filenameToModuleName(os.path.join(path, filename)))

    with tempfile.NamedTemporaryFile() as tmpfile:
        for moduleName in moduleNames:
            tmpfile.write('import %s\n' % moduleName)
        tmpfile.flush()
        mf.run_script(tmpfile.name)

    with open('twisted-deps.json', 'wb') as outfile:
        json.dump(mf.as_json(), outfile)

    port_status = {}
    for module in dist3.modules:
        port_status[module] = 'ported'
    for module in dist3.almostModules:
        port_status[module] = 'almost-ported'

    with open('twisted-ported.json', 'wb') as outfile:
        json.dump(port_status, outfile)
Exemple #10
0
 def _packageRecurse(self, arg, dirname, names):
     import fnmatch
     OPJ = os.path.join
     testModuleNames = fnmatch.filter(names, self.moduleGlob)
     testModules = [ reflect.filenameToModuleName(OPJ(dirname, name))
                     for name in testModuleNames ]
     for module in testModules:
         self.addModule(module)
Exemple #11
0
 def _packageRecurse(self, suite, dirname, names):
     if not isPackageDirectory(dirname):
         names[:] = []
         return
     moduleNames = [reflect.filenameToModuleName(opj(dirname, filename))
                    for filename in fnmatch.filter(names, self.moduleGlob)]
     for moduleName in self.sort(moduleNames):
         suite.addTest(self.loadByName(moduleName))
Exemple #12
0
 def test_bytes(self):
     """
     L{filenameToModuleName} returns the correct module given a C{bytes}
     path to its file.
     """
     module = reflect.filenameToModuleName(os.path.join(self.path.encode("utf-8"), b"test_reflect.py"))
     # Module names are always native string:
     self.assertEqual(module, "fakepackage.test.test_reflect")
Exemple #13
0
def filenameToModule(fn):
    if not os.path.exists(fn):
        raise ValueError("%r doesn't exist" % (fn,))
    try:
        ret = reflect.namedAny(reflect.filenameToModuleName(fn))
    except (ValueError, AttributeError):
        return _importFromFile(fn)
    retFile = os.path.splitext(ret.__file__)[0] + '.py'
    same = getattr(os.path, 'samefile', samefile)
    if os.path.isfile(fn) and not same(fn, retFile):
        del sys.modules[ret.__name__]
        ret = _importFromFile(fn)
    return ret
Exemple #14
0
    def _packageRecurse(self, arg, dirname, names):

        # Only recurse into packages
        for ext in 'py', 'so', 'pyd', 'dll':
            if os.path.exists(os.path.join(dirname, os.extsep.join(('__init__', ext)))):
                break
        else:
            return

        testModuleNames = fnmatch.filter(names, self.moduleGlob)
        testModules = [ reflect.filenameToModuleName(opj(dirname, name))
                        for name in testModuleNames ]
        for module in testModules:
            self.addModule(module)
Exemple #15
0
    def __init__(self, original):
        self.original = o = original
        self.runs = 0
        self.klass = "doctests have no class"
        self.module = "doctests have no module"
        # the originating module of this doctest
        self.module = reflect.filenameToModuleName(self.original._dtest.filename)

        self.fullName = self.filename = o._dtest.filename
        self.lineno = o._dtest.lineno
        self.docstr= "doctest, file: %s lineno: %s" % (osp.split(self.filename)[1], self.lineno)
        self.name = o._dtest.name
        self.fullname = repr(o._dtest)
        self._regex = re.compile(r'\S')
Exemple #16
0
def filenameToModule(fn):
    if not os.path.exists(fn):
        raise ValueError("%r doesn't exist" % (fn,))
    try:
        ret = reflect.namedAny(reflect.filenameToModuleName(fn))
    except (ValueError, AttributeError):
        # Couldn't find module.  The file 'fn' is not in PYTHONPATH
        return _importFromFile(fn)
    # ensure that the loaded module matches the file
    retFile = os.path.splitext(ret.__file__)[0] + '.py'
    # not all platforms (e.g. win32) have os.path.samefile
    same = getattr(os.path, 'samefile', samefile)
    if os.path.isfile(fn) and not same(fn, retFile):
        del sys.modules[ret.__name__]
        ret = _importFromFile(fn)
    return ret
Exemple #17
0
 def _handleFile(self, filename):
     m = None
     if osp.isfile(filename):
         try:
             mname = reflect.filenameToModuleName(filename)
             self._tryNamedAny(mname)
         except ArgumentError:
             # okay, not in PYTHONPATH...
             path, fullname = osp.split(filename)
             name, ext = osp.splitext(fullname)
             m = new.module(name)
             sourcestring = file(filename, 'rb').read()
             exec sourcestring in m.__dict__
             sys.modules[name] = m
             
     self['modules'].append(m)
Exemple #18
0
    def loadFile(self, fileName, recurse=False):
        """
        Load a file, and then the tests in that file.

        @param fileName: The file name to load.
        @param recurse: A boolean. If True, inspect modules within packages
            within the given package (and so on), otherwise, only inspect
            modules in the package itself.
        """
        from importlib.machinery import SourceFileLoader

        name = reflect.filenameToModuleName(fileName)
        try:
            module = SourceFileLoader(name, fileName).load_module()
            return self.loadAnything(module, recurse=recurse)
        except OSError:
            raise ValueError("{} is not a Python file.".format(fileName))
def _runTest(testCase, testFilePath):
    """
    Run a functional test.

    @param testCase: The test case on which to call assertions.
    @type testCase: L{unittest.TestCase}

    @param testFilePath: The path to the module to test.
    @type testFilePath: L{str}
    """
    pathResultFile = testFilePath.replace(".py", ".result")
    moduleName = filenameToModuleName(testFilePath)
    outputStream = io.BytesIO()

    runner = Runner()
    runner.allowOptions = False
    runner.setOutput(outputStream)
    runner.setReporter(TestReporter())

    limits = _parseLimitMessages(testFilePath)
    if limits is not None:
        action, messages = limits
        _setLinterLimits(runner.linter, action, messages)

    _enablePEP8Checker(runner.linter)

    exitCode = None
    try:
        runner.run([moduleName])
    except SystemExit as error:
        exitCode = error.code

    # Check the results
    expectedResult = open(pathResultFile).read().strip()
    outputResult = outputStream.getvalue().strip()

    try:
        testCase.assertEqual(expectedResult, outputResult)
    except unittest.FailTest:
        testCase.fail(_formatResults(moduleName, expectedResult, outputResult))

    if not expectedResult:
        testCase.assertEqual(0, exitCode)
    else:
        testCase.assertNotEqual(0, exitCode)
def _testsForModules(testModules):
    """
    Return a dictionary of test names and test functions.

    @param testModules: An iterable list of functional test module names.
    @type testModules: L{iter}

    @return: A dictionary of test functions keyed by test name.
    @rtype: L{dict} of (L{str}, L{callable})
    """
    t = []
    for modulePath in testModules:
        moduleName = filenameToModuleName(modulePath)
        t.append(
            (_testNameFromModuleName(moduleName),
             _partial2(_runTest, testFilePath=modulePath))
        )
    return dict(t)
def main(target):
    mf = mymf(sys.path[:], 0, [])

    moduleNames = []
    for path, dirnames, filenames in os.walk(
            os.path.join(target, 'src', 'allmydata')):
        if 'test' in dirnames:
            dirnames.remove('test')
        for filename in filenames:
            if not filename.endswith('.py'):
                continue
            if filename in ('setup.py', ):
                continue
            if '-' in filename:
                # a script like update-documentation.py
                continue
            if filename != '__init__.py':
                filepath = os.path.join(path, filename)
            else:
                filepath = path
            moduleNames.append(reflect.filenameToModuleName(filepath))

    with tempfile.NamedTemporaryFile("w") as tmpfile:
        for moduleName in moduleNames:
            tmpfile.write('import %s\n' % moduleName)
        tmpfile.flush()
        mf.run_script(tmpfile.name)

    with open('tahoe-deps.json', 'w') as outfile:
        json_dump(mf.as_json(), outfile)
        outfile.write('\n')

    ported_modules_path = os.path.join(target, "src", "allmydata", "util",
                                       "_python3.py")
    with open(ported_modules_path) as f:
        ported_modules = {}
        exec(f.read(), ported_modules, ported_modules)
        port_status = dict.fromkeys(
            ported_modules["PORTED_MODULES"] +
            ported_modules["PORTED_TEST_MODULES"], "ported")
    with open('tahoe-ported.json', 'w') as outfile:
        json_dump(port_status, outfile)
        outfile.write('\n')
Exemple #22
0
def loadModule(app):
    """
    Given an module or python file, load the module. Returns a tuple
    containing the loaded module and the name of the app.
    """
    if hasattr(os, "getuid") and os.getuid() != 0:
        sys.path.insert(0, os.path.abspath(os.getcwd()))
    args = sys.argv[:]
    sys.argv = []
    if app[-3:] == ".py" and os.path.exists(app):
        path = os.path.dirname(app)
        if not path: path = "."
        sys.path.insert(0, path)
        from twisted.python import reflect
        app = reflect.filenameToModuleName(app)
    try:
        mod = __import__(app, None, None, app.split(".")[-1])
    except ImportError:
        sys.argv = args
        raise usage.UsageError("Application not found: " + app)
    sys.argv = args
    return mod, app
def main(target):
    mf = mymf(sys.path[:], 0, [])

    moduleNames = []
    for path, dirnames, filenames in os.walk(os.path.join(target, 'src', 'twisted')):
        if 'test' in dirnames:
            dirnames.remove('test')
        for filename in filenames:
            if not filename.endswith('.py'):
                continue
            if filename in ('setup.py',):
                continue
            if '-' in filename:
                # a script like update-documentation.py
                continue
            if filename != '__init__.py':
                filepath = os.path.join(path, filename)
            else:
                filepath = path
            moduleNames.append(reflect.filenameToModuleName(filepath))

    with tempfile.NamedTemporaryFile() as tmpfile:
        for moduleName in moduleNames:
            tmpfile.write('import %s\n' % moduleName)
        tmpfile.flush()
        mf.run_script(tmpfile.name)

    with open('twisted-deps.json', 'wb') as outfile:
        json_dump(mf.as_json(), outfile)
        outfile.write('\n')

    port_status = {}
    for module in mf._depgraph.iterkeys():
        if module not in _setup.notPortedModules:
            port_status[module] = 'ported'
    with open('twisted-ported.json', 'wb') as outfile:
        json_dump(port_status, outfile)
        outfile.write('\n')
Exemple #24
0
def main(target):
    mf = mymf(sys.path[:], 0, [])

    moduleNames = []
    for path, dirnames, filenames in os.walk(
            os.path.join(target, 'src', 'twisted')):
        if 'test' in dirnames:
            dirnames.remove('test')
        for filename in filenames:
            if not filename.endswith('.py'):
                continue
            if filename in {'__init__.py', 'setup.py'}:
                continue
            if '-' in filename:
                # a script like update-documentation.py
                continue
            moduleNames.append(
                reflect.filenameToModuleName(os.path.join(path, filename)))

    with tempfile.NamedTemporaryFile() as tmpfile:
        for moduleName in moduleNames:
            tmpfile.write('import %s\n' % moduleName)
        tmpfile.flush()
        mf.run_script(tmpfile.name)

    with open('twisted-deps.json', 'wb') as outfile:
        json_dump(mf.as_json(), outfile)
        outfile.write('\n')

    port_status = {}
    for module in dist3.modules:
        port_status[remove_dunder(module)] = 'ported'
    for module in dist3.almostModules:
        port_status[remove_dunder(module)] = 'almost-ported'

    with open('twisted-ported.json', 'wb') as outfile:
        json_dump(port_status, outfile)
        outfile.write('\n')
Exemple #25
0
    def getSynopsis(self):
        """
        Returns a string containing a description of these options and how to
        pass them to the executed file.
        """
        executableName = reflect.filenameToModuleName(sys.argv[0])

        if executableName.endswith('.__main__'):
            executableName = '{} -m {}'.format(os.path.basename(sys.executable), executableName.replace('.__main__', ''))

        if self.parent is None:
            default = "Usage: %s%s" % (executableName,
                                       (self.longOpt and " [options]") or '')
        else:
            default = '%s' % ((self.longOpt and "[options]") or '')
        synopsis = getattr(self, "synopsis", default)

        synopsis = synopsis.rstrip()

        if self.parent is not None:
            synopsis = ' '.join((self.parent.getSynopsis(),
                                 self.parent.subCommand, synopsis))
        return synopsis
Exemple #26
0
    def getSynopsis(self):
        """
        Returns a string containing a description of these options and how to
        pass them to the executed file.
        """
        executableName = reflect.filenameToModuleName(sys.argv[0])

        if executableName.endswith('.__main__'):
            executableName = '{} -m {}'.format(os.path.basename(sys.executable), executableName.replace('.__main__', ''))

        if self.parent is None:
            default = "Usage: %s%s" % (executableName,
                                       (self.longOpt and " [options]") or '')
        else:
            default = '%s' % ((self.longOpt and "[options]") or '')
        synopsis = getattr(self, "synopsis", default)

        synopsis = synopsis.rstrip()

        if self.parent is not None:
            synopsis = ' '.join((self.parent.getSynopsis(),
                                 self.parent.subCommand, synopsis))
        return synopsis
Exemple #27
0
def main(target):
    mf = mymf(sys.path[:], 0, [])

    moduleNames = []
    for path, dirnames, filenames in os.walk(
            os.path.join(target, 'src', 'allmydata')):
        if 'test' in dirnames:
            dirnames.remove('test')
        for filename in filenames:
            if not filename.endswith('.py'):
                continue
            if filename in ('setup.py', ):
                continue
            if '-' in filename:
                # a script like update-documentation.py
                continue
            if filename != '__init__.py':
                filepath = os.path.join(path, filename)
            else:
                filepath = path
            moduleNames.append(reflect.filenameToModuleName(filepath))

    with tempfile.NamedTemporaryFile() as tmpfile:
        for moduleName in moduleNames:
            tmpfile.write('import %s\n' % moduleName)
        tmpfile.flush()
        mf.run_script(tmpfile.name)

    with open('tahoe-deps.json', 'wb') as outfile:
        json_dump(mf.as_json(), outfile)
        outfile.write('\n')

    # no port status yet
    port_status = {}
    with open('tahoe-ported.json', 'wb') as outfile:
        json_dump(port_status, outfile)
        outfile.write('\n')
Exemple #28
0
    def do_trace(frame, event, arg):
        global _tracing, _indent

        if not _tracing:
            print '[tracing stopped]'
            return None

        co = frame.f_code

        if event == 'line':
            return do_trace
        if func_filter and not func_filter.search(co.co_name):
            return None
        if ignore_files_re and ignore_files_re.search(co.co_filename):
            return None
        elif event == 'call' or event == 'c_call':
            if co.co_name == '?':
                return None
            module = filenameToModuleName(co.co_filename)
            write(_indent, '%s:%d:%s():', module, frame.f_lineno, co.co_name)
            _indent += '  '
            return do_trace
        elif event == 'return' or event == 'c_return':
            if print_returns:
                write(_indent, 'return %r', arg)
            _indent = _indent[:-2]
            return None
        elif event == 'exception' or event == 'c_exception':
            if arg:
                write(_indent, 'Exception: %s:%d: %s (%s)', co.co_filename,
                      frame.f_lineno, arg[0].__name__, arg[1])
            else:
                write(_indent, 'Exception: (from C)')
            return do_trace
        else:
            write(_indent, 'unknown event: %s', event)
            return None
Exemple #29
0
    def do_trace(frame, event, arg):
        global _tracing, _indent

        if not _tracing:
            print '[tracing stopped]'
            return None

        co = frame.f_code

        if event == 'line':
            return do_trace
        if func_filter and not func_filter.search(co.co_name):
            return None
        if ignore_files_re and ignore_files_re.search(co.co_filename):
            return None
        elif event == 'call' or event == 'c_call':
            if co.co_name == '?':
                return None
            module = filenameToModuleName(co.co_filename)
            write(_indent, '%s:%d:%s():', module, frame.f_lineno, co.co_name)
            _indent += '  '
            return do_trace
        elif event == 'return' or event == 'c_return':
            if print_returns:
                write(_indent, 'return %r', arg)
            _indent = _indent[:-2]
            return None
        elif event == 'exception' or event == 'c_exception':
            if arg:
                write(_indent, 'Exception: %s:%d: %s (%s)', co.co_filename,
                      frame.f_lineno, arg[0].__name__, arg[1])
            else:
                write(_indent, 'Exception: (from C)')
            return do_trace
        else:
            write(_indent, 'unknown event: %s', event)
            return None
Exemple #30
0
 def _findTestModules(self, package):
     modGlob = os.path.join(os.path.dirname(package.__file__),
                            self.moduleGlob)
     return [ reflect.filenameToModuleName(filename)
              for filename in glob.glob(modGlob) ]
Exemple #31
0
    def findByName(self, _name, recurse=False):
        """
        Find and load tests, given C{name}.

        @param name: The qualified name of the thing to load.
        @param recurse: A boolean. If True, inspect modules within packages
            within the given package (and so on), otherwise, only inspect
            modules in the package itself.

        @return: If C{name} is a filename, return the module. If C{name} is a
        fully-qualified Python name, return the object it refers to.
        """
        if os.sep in _name:
            # It's a file, try and get the module name for this file.
            name = reflect.filenameToModuleName(_name)

            try:
                # Try and import it, if it's on the path.
                # CAVEAT: If you have two twisteds, and you try and import the
                # one NOT on your path, it'll load the one on your path. But
                # that's silly, nobody should do that, and existing Trial does
                # that anyway.
                __import__(name)
            except ImportError:
                # If we can't import it, look for one NOT on the path.
                return self.loadFile(_name, recurse=recurse)

        else:
            name = _name

        obj = parent = remaining = None

        for searchName, remainingName in _qualNameWalker(name):
            # Walk down the qualified name, trying to import a module. For
            # example, `twisted.test.test_paths.FilePathTests` would try
            # the full qualified name, then just up to test_paths, and then
            # just up to test, and so forth.
            # This gets us the highest level thing which is a module.
            try:
                obj = reflect.namedModule(searchName)
                # If we reach here, we have successfully found a module.
                # obj will be the module, and remaining will be the remaining
                # part of the qualified name.
                remaining = remainingName
                break

            except ImportError:
                # Check to see where the ImportError happened. If it happened
                # in this file, ignore it.
                tb = sys.exc_info()[2]

                # Walk down to the deepest frame, where it actually happened.
                while tb.tb_next is not None:
                    tb = tb.tb_next

                # Get the filename that the ImportError originated in.
                filenameWhereHappened = tb.tb_frame.f_code.co_filename

                # If it originated in the reflect file, then it's because it
                # doesn't exist. If it originates elsewhere, it's because an
                # ImportError happened in a module that does exist.
                if filenameWhereHappened != reflect.__file__:
                    raise

                if remaining == "":
                    raise reflect.ModuleNotFound(
                        "The module {} does not exist.".format(name))

        if obj is None:
            # If it's none here, we didn't get to import anything.
            # Try something drastic.
            obj = reflect.namedAny(name)
            remaining = name.split(".")[len(".".split(obj.__name__)) + 1:]

        try:
            for part in remaining:
                # Walk down the remaining modules. Hold on to the parent for
                # methods, as on Python 3, you can no longer get the parent
                # class from just holding onto the method.
                parent, obj = obj, getattr(obj, part)
        except AttributeError:
            raise AttributeError("{} does not exist.".format(name))

        return self.loadAnything(obj,
                                 parent=parent,
                                 qualName=remaining,
                                 recurse=recurse)
Exemple #32
0
    def findByName(self, _name, recurse=False):
        """
        Find and load tests, given C{name}.

        This partially duplicates the logic in C{unittest.loader.TestLoader}.

        @param name: The qualified name of the thing to load.
        @param recurse: A boolean. If True, inspect modules within packages
            within the given package (and so on), otherwise, only inspect
            modules in the package itself.
        """
        if os.sep in _name:
            # It's a file, try and get the module name for this file.
            name = reflect.filenameToModuleName(_name)

            try:
                # Try and import it, if it's on the path.
                # CAVEAT: If you have two twisteds, and you try and import the
                # one NOT on your path, it'll load the one on your path. But
                # that's silly, nobody should do that, and existing Trial does
                # that anyway.
                __import__(name)
            except ImportError:
                # If we can't import it, look for one NOT on the path.
                return self.loadFile(_name, recurse=recurse)

        else:
            name = _name

        obj = parent = remaining = None

        for searchName, remainingName in _qualNameWalker(name):
            # Walk down the qualified name, trying to import a module. For
            # example, `twisted.test.test_paths.FilePathTests` would try
            # the full qualified name, then just up to test_paths, and then
            # just up to test, and so forth.
            # This gets us the highest level thing which is a module.
            try:
                obj = reflect.namedModule(searchName)
                # If we reach here, we have successfully found a module.
                # obj will be the module, and remaining will be the remaining
                # part of the qualified name.
                remaining = remainingName
                break

            except ImportError:
                if remaining == "":
                    raise reflect.ModuleNotFound("The module {} does not exist.".format(name))

        if obj is None:
            # If it's none here, we didn't get to import anything.
            # Try something drastic.
            obj = reflect.namedAny(name)
            remaining = name.split(".")[len(".".split(obj.__name__))+1:]

        try:
            for part in remaining:
                # Walk down the remaining modules. Hold on to the parent for
                # methods, as on Python 3, you can no longer get the parent
                # class from just holding onto the method.
                parent, obj = obj, getattr(obj, part)
        except AttributeError:
            raise AttributeError("{} does not exist.".format(name))

        return self.loadAnything(obj, parent=parent, qualName=remaining,
                                 recurse=recurse)
Exemple #33
0
    def findByName(self, _name, recurse=False):
        """
        Find and load tests, given C{name}.

        This partially duplicates the logic in C{unittest.loader.TestLoader}.

        @param name: The qualified name of the thing to load.
        @param recurse: A boolean. If True, inspect modules within packages
            within the given package (and so on), otherwise, only inspect
            modules in the package itself.
        """
        if os.sep in _name:
            # It's a file, try and get the module name for this file.
            name = reflect.filenameToModuleName(_name)

            try:
                # Try and import it, if it's on the path.
                # CAVEAT: If you have two twisteds, and you try and import the
                # one NOT on your path, it'll load the one on your path. But
                # that's silly, nobody should do that, and existing Trial does
                # that anyway.
                __import__(name)
            except ImportError:
                # If we can't import it, look for one NOT on the path.
                return self.loadFile(_name, recurse=recurse)

        else:
            name = _name

        obj = parent = remaining = None

        for searchName, remainingName in _qualNameWalker(name):
            # Walk down the qualified name, trying to import a module. For
            # example, `twisted.test.test_paths.FilePathTests` would try
            # the full qualified name, then just up to test_paths, and then
            # just up to test, and so forth.
            # This gets us the highest level thing which is a module.
            try:
                obj = reflect.namedModule(searchName)
                # If we reach here, we have successfully found a module.
                # obj will be the module, and remaining will be the remaining
                # part of the qualified name.
                remaining = remainingName
                break

            except ImportError:
                if remaining == "":
                    raise reflect.ModuleNotFound(
                        "The module {} does not exist.".format(name))

        if obj is None:
            # If it's none here, we didn't get to import anything.
            # Try something drastic.
            obj = reflect.namedAny(name)
            remaining = name.split(".")[len(".".split(obj.__name__)) + 1:]

        try:
            for part in remaining:
                # Walk down the remaining modules. Hold on to the parent for
                # methods, as on Python 3, you can no longer get the parent
                # class from just holding onto the method.
                parent, obj = obj, getattr(obj, part)
        except AttributeError:
            raise AttributeError("{} does not exist.".format(name))

        return self.loadAnything(obj,
                                 parent=parent,
                                 qualName=remaining,
                                 recurse=recurse)
Exemple #34
0
 def _findTestModules(self, package):
     modGlob = os.path.join(os.path.dirname(package.__file__),
                            self.moduleGlob)
     return [ reflect.filenameToModuleName(filename)
              for filename in glob.glob(modGlob) ]
Exemple #35
0
 def opt_file(self, filename):
     "Filename of module to test"
     from twisted.python import reflect
     self['modules'].append(reflect.filenameToModuleName(filename))