Esempio n. 1
0
    def test_simple(self):
        original = {"foo": 1, "bar": 2}

        test = ReadOnlyDefaultDict(bool, original)

        self.assertEqual(original, test)

        self.assertEqual(test["foo"], 1)
    def test_simple(self):
        original = {'foo': 1, 'bar': 2}

        test = ReadOnlyDefaultDict(bool, original)

        self.assertEqual(original, test)

        self.assertEqual(test['foo'], 1)
Esempio n. 3
0
    def test_simple(self):
        original = {'foo': 1, 'bar': 2}

        test = ReadOnlyDefaultDict(original)

        self.assertEqual(original, test)

        self.assertEqual(test['foo'], 1)

        with self.assertRaises(KeyError):
            value = test['missing']
Esempio n. 4
0
    def __init__(self, config, path):
        """Create an empty mozbuild Sandbox.

        config is a ConfigStatus instance (the output of configure). path is
        the path of the main mozbuild file that is being executed. It is used
        to compute encountered relative paths.
        """
        Sandbox.__init__(self, allowed_variables=VARIABLES)

        self.config = config

        topobjdir = os.path.abspath(config.topobjdir)

        # This may not always hold true. If we ever have autogenerated mozbuild
        # files in topobjdir, we'll need to change this.
        assert os.path.normpath(path).startswith(
            os.path.normpath(config.topsrcdir))
        assert not os.path.normpath(path).startswith(
            os.path.normpath(topobjdir))

        relpath = os.path.relpath(path, config.topsrcdir).replace(os.sep, '/')
        reldir = os.path.dirname(relpath)

        with self._globals.allow_all_writes() as d:
            d['TOPSRCDIR'] = config.topsrcdir
            d['TOPOBJDIR'] = topobjdir
            d['RELATIVEDIR'] = reldir
            d['SRCDIR'] = os.path.join(config.topsrcdir,
                                       reldir).replace(os.sep, '/').rstrip('/')
            d['OBJDIR'] = os.path.join(topobjdir,
                                       reldir).replace(os.sep, '/').rstrip('/')

            d['CONFIG'] = ReadOnlyDefaultDict(config.substs,
                                              global_default=None)

            # Register functions.
            for name, func in FUNCTIONS.items():
                d[name] = getattr(self, func[0])

        self._normalized_topsrcdir = os.path.normpath(config.topsrcdir)
Esempio n. 5
0
    def __init__(self, config, path, metadata={}):
        """Create an empty mozbuild Sandbox.

        config is a ConfigStatus instance (the output of configure). path is
        the path of the main mozbuild file that is being executed. It is used
        to compute encountered relative paths.
        """
        Sandbox.__init__(self, allowed_variables=VARIABLES)

        self._log = logging.getLogger(__name__)

        self.config = config
        self.metadata = dict(metadata)

        topobjdir = mozpath.abspath(config.topobjdir)
        topsrcdir = config.topsrcdir
        norm_topsrcdir = mozpath.normpath(topsrcdir)

        if not path.startswith(norm_topsrcdir):
            external_dirs = config.substs.get('EXTERNAL_SOURCE_DIR', '').split()
            for external in external_dirs:
                external = mozpath.normpath(external)

                if not os.path.isabs(external):
                    external = mozpath.join(config.topsrcdir, external)

                external = mozpath.normpath(external)

                if not path.startswith(external):
                    continue

                topsrcdir = external

                # This is really hacky and should be replaced with something
                # more robust. We assume that if an external source directory
                # is in play that the main build system is built in a
                # subdirectory of its topobjdir. Therefore, the topobjdir of
                # the external source directory is the parent of our topobjdir.
                topobjdir = mozpath.dirname(topobjdir)

                # This is suboptimal because we load the config.status multiple
                # times. We should consider caching it, possibly by moving this
                # code up to the reader.
                config = ConfigEnvironment.from_config_status(
                    mozpath.join(topobjdir, 'config.status'))
                self.config = config
                break

        self.topsrcdir = topsrcdir

        relpath = mozpath.relpath(path, topsrcdir)
        reldir = mozpath.dirname(relpath)

        with self._globals.allow_all_writes() as d:
            d['TOPSRCDIR'] = topsrcdir
            d['TOPOBJDIR'] = topobjdir
            d['RELATIVEDIR'] = reldir
            d['SRCDIR'] = mozpath.join(topsrcdir, reldir).rstrip('/')
            d['OBJDIR'] = mozpath.join(topobjdir, reldir).rstrip('/')

            d['CONFIG'] = ReadOnlyDefaultDict(self.config.substs_unicode,
                global_default=None)

            var = metadata.get('var', None)
            if var and var in ['TOOL_DIRS', 'TEST_TOOL_DIRS']:
                d['IS_TOOL_DIR'] = True

            # Register functions.
            for name, func in FUNCTIONS.items():
                d[name] = getattr(self, func[0])

            # Initialize the exports that we need in the global.
            extra_vars = self.metadata.get('exports', dict())
            self._globals.update(extra_vars)
    def test_assignment(self):
        test = ReadOnlyDefaultDict(bool, {})

        with self.assertRaises(Exception):
            test['foo'] = True
    def test_defaults(self):
        test = ReadOnlyDefaultDict(bool, {'foo': 1})

        self.assertEqual(test['foo'], 1)

        self.assertEqual(test['qux'], False)
Esempio n. 8
0
    def test_defaults(self):
        test = ReadOnlyDefaultDict({}, defaults={'foo': 1})

        self.assertEqual(test['foo'], 1)
Esempio n. 9
0
    def __init__(self, config, path):
        """Create an empty mozbuild Sandbox.

        config is a ConfigStatus instance (the output of configure). path is
        the path of the main mozbuild file that is being executed. It is used
        to compute encountered relative paths.
        """
        Sandbox.__init__(self, allowed_variables=VARIABLES)

        self._log = logging.getLogger(__name__)

        self.config = config

        topobjdir = os.path.abspath(config.topobjdir)
        topsrcdir = config.topsrcdir
        norm_topsrcdir = os.path.normpath(topsrcdir)

        if not path.startswith(norm_topsrcdir):
            external_dirs = config.substs.get('EXTERNAL_SOURCE_DIR',
                                              '').split()
            for external in external_dirs:
                external = os.path.normpath(external)

                if not os.path.isabs(external):
                    external = os.path.join(config.topsrcdir, external)

                external = os.path.normpath(external)

                if not path.startswith(external):
                    continue

                topsrcdir = external

                # This is really hacky and should be replaced with something
                # more robust. We assume that if an external source directory
                # is in play that the main build system is built in a
                # subdirectory of its topobjdir. Therefore, the topobjdir of
                # the external source directory is the parent of our topobjdir.
                topobjdir = os.path.dirname(topobjdir)

                # This is suboptimal because we load the config.status multiple
                # times. We should consider caching it, possibly by moving this
                # code up to the reader.
                config = ConfigEnvironment.from_config_status(
                    os.path.join(topobjdir, 'config.status'))
                self.config = config
                break

        self.topsrcdir = topsrcdir

        relpath = os.path.relpath(path, topsrcdir).replace(os.sep, '/')
        reldir = os.path.dirname(relpath)

        with self._globals.allow_all_writes() as d:
            d['TOPSRCDIR'] = topsrcdir
            d['TOPOBJDIR'] = topobjdir
            d['RELATIVEDIR'] = reldir
            d['SRCDIR'] = os.path.join(topsrcdir,
                                       reldir).replace(os.sep, '/').rstrip('/')
            d['OBJDIR'] = os.path.join(topobjdir,
                                       reldir).replace(os.sep, '/').rstrip('/')

            # config.status does not yet use unicode. However, mozbuild expects
            # unicode everywhere. So, decode binary into unicode as necessary.
            # Bug 844509 tracks a better way to do this.
            substs = {}
            for k, v in config.substs.items():
                if not isinstance(v, text_type):
                    try:
                        v = v.decode('utf-8')
                    except UnicodeDecodeError:
                        log(
                            self._log, logging.INFO, 'lossy_encoding',
                            {'variable': k},
                            'Lossy Unicode encoding for {variable}. See bug 844509.'
                        )

                        v = v.decode('utf-8', 'replace')

                substs[k] = v

            d['CONFIG'] = ReadOnlyDefaultDict(substs, global_default=None)

            # Register functions.
            for name, func in FUNCTIONS.items():
                d[name] = getattr(self, func[0])
Esempio n. 10
0
    def test_defaults(self):
        test = ReadOnlyDefaultDict(bool, {"foo": 1})

        self.assertEqual(test["foo"], 1)

        self.assertEqual(test["qux"], False)