示例#1
0
    def testAddPath(self):
        """Verify `ctx.add_path`"""
        # Create a temp directory
        with ctx.tmpdir() as path:
            # The temp dir should not be in the path yet.
            self.assertNotIn(path, sys.path)

            with ctx.add_path(path):
                # Now it should be in the path
                self.assertIn(path, sys.path)

            # After exiting the context, it should be gone again
            self.assertNotIn(path, sys.path)
示例#2
0
    def testAddPath(self):
        """Verify `ctx.add_path`"""
        # Create a temp directory
        with ctx.tmpdir() as path:
            # The temp dir should not be in the path yet.
            self.assertNotIn(path, sys.path)

            with ctx.add_path(path):
                # Now it should be in the path
                self.assertIn(path, sys.path)

            # After exiting the context, it should be gone again
            self.assertNotIn(path, sys.path)
示例#3
0
    def importThrift(self, path, **kwargs):
        """Compiles a .thrift file, importing its contents into its return value"""
        path = os.path.abspath(path)
        assert os.path.exists(path)
        assert os.path.isfile(path)

        srcdir = self.makeTemporaryIncludeDir()
        pathbase = os.path.basename(path)
        srcdir.symlink(pathbase, path)

        outdir = NamedTemporaryDirectory(prefix='to1_')
        outdir_recurse = NamedTemporaryDirectory(prefix='tor_')

        if self.debug:
            outdir.keep()
            outdir_recurse.keep()

        args = [self.thrift_bin] + self.makeIncludeArgs(srcdir) + \
               ["--gen", self.getThriftOptions(**kwargs), '-v',
                "-o", outdir.name, srcdir.join(pathbase)]
        check_output(args)

        args = [self.thrift_bin] + self.makeIncludeArgs(srcdir) + \
               ["--gen", self.getThriftOptions(**kwargs), '-v', '-r',
                "-o", outdir_recurse.name, srcdir.join(pathbase)]
        check_output(args)

        # Prepend output directory to the path
        with ctx.add_path(outdir_recurse.join('gen-py'), 0):

            thriftname = os.path.splitext(pathbase)[0]
            for dirpath, dirnames, filenames in os.walk(outdir.join('gen-py')):
                # Emulate relative imports badly
                dirpath = os.path.abspath(outdir.join('gen-py', dirpath))
                with ctx.add_path(dirpath):
                    # Add types to module first
                    if 'ttypes.py' in filenames:
                        ttypes = self.importPython(dirpath + '/ttypes.py')
                        result = ttypes
                        filenames.remove('ttypes.py')

                    # Then constants
                    if 'constants.py' in filenames:
                        result = self.mergeModules(
                            self.importPython(dirpath + '/constants.py'),
                            result)
                        filenames.remove('constants.py')

                    for filename in filenames:
                        # Skip pyremotes
                        if not filename.endswith('.py') or \
                           filename == '__init__.py':
                            continue

                        # Attach services as attributes on the module.
                        svcpath = dirpath + '/' + filename
                        svcname = os.path.splitext(filename)[0]
                        svcmod = self.importPython(svcpath)
                        svcmod.__file__ = os.path.abspath(svcpath)
                        svcmod.__name__ = '%s.%s (generated)' % \
                            (thriftname, svcname)
                        setattr(result, svcname, svcmod)

            assert result is not None, "No files generated by %s" % (path, )

            # Set the __file__ attribute to the .thrift file instead
            # of the dynamically generated jonx
            result.__file__ = os.path.abspath(path)
            result.__name__ = thriftname + " (generated)"
            return result
示例#4
0
    def importThrift(self, path, **kwargs):
        """Compiles a .thrift file, importing its contents into its return value"""
        path = os.path.abspath(path)
        assert os.path.exists(path)
        assert os.path.isfile(path)

        srcdir = self.makeTemporaryIncludeDir()
        pathbase = os.path.basename(path)
        srcdir.symlink(pathbase, path)

        outdir = NamedTemporaryDirectory(prefix='to1_')
        outdir_recurse = NamedTemporaryDirectory(prefix='tor_')

        if self.debug:
            outdir.keep()
            outdir_recurse.keep()

        args = [self.thrift_bin] + self.makeIncludeArgs(srcdir) + \
               ["--gen", self.getThriftOptions(**kwargs), '-v',
                "-out", outdir.name, srcdir.join(pathbase)]
        check_output(args)

        args = [self.thrift_bin] + self.makeIncludeArgs(srcdir) + \
               ["--gen", self.getThriftOptions(**kwargs), '-v', '-r',
                "-out", outdir_recurse.name, srcdir.join(pathbase)]
        check_output(args)

        # Prepend output directory to the path
        with ctx.add_path(outdir_recurse.name, 0):

            thriftname = os.path.splitext(pathbase)[0]
            for dirpath, dirnames, filenames in os.walk(outdir.name):
                # Emulate relative imports badly
                dirpath = os.path.abspath(os.path.join(outdir, dirpath))
                with ctx.add_path(dirpath):
                    # Add types to module first
                    if 'ttypes.py' in filenames:
                        ttypes = self.importPython(dirpath + '/ttypes.py')
                        result = ttypes
                        filenames.remove('ttypes.py')

                    # Then constants
                    if 'constants.py' in filenames:
                        result = self.mergeModules(
                            self.importPython(dirpath + '/constants.py'),
                            result)
                        filenames.remove('constants.py')

                    for filename in filenames:
                        # Skip pyremotes
                        if not filename.endswith('.py') or \
                           filename == '__init__.py':
                            continue

                        # Attach services as attributes on the module.
                        svcpath = dirpath + '/' + filename
                        svcname = os.path.splitext(filename)[0]
                        svcmod = self.importPython(svcpath)
                        svcmod.__file__ = os.path.abspath(svcpath)
                        svcmod.__name__ = '%s.%s (generated)' % \
                            (thriftname, svcname)
                        setattr(result, svcname, svcmod)

            assert result is not None, "No files generated by %s" % (path, )

            # Set the __file__ attribute to the .thrift file instead
            # of the dynamically generated jonx
            result.__file__ = os.path.abspath(path)
            result.__name__ = thriftname + " (generated)"
            return result