Exemplo n.º 1
0
    def test_config_uri_in_command_line_args(self):
        """Flags should recognize --config-uri flag to read additional parameters from a JSON
        dictionary at the given uri. Parameters on the command-line argument list
        before the --config-uri flag are overridden by values in the --config-uri JSON.
        Parameters after the --config-uri flag override values from the JSON config.
        """
        with tempfile.NamedTemporaryFile(mode="wt", encoding="utf-8") as fp:
            config_parameters = dict(
                int="9",
                bool="yes",
                str_value="string",
                unparsed="foo",
                delimited_list=["a", "b"],
                list="3",
            )
            fp.write(base.json_encode(config_parameters, pretty=True))
            fp.flush()

            self.flags.parse(
                ("--int=7 --list=1 --list=2 --config=file:%s --bool=no "
                 "--delimited_list=c,d " % fp.name)
                .split()
            )
            self.assertEqual(9, self.flags.int)
            self.assertEqual(False, self.flags.bool)
            self.assertEqual("string", self.flags.str_value)
            self.assertListEqual(["1", "2", "3"], self.flags.list)
            self.assertListEqual(["a", "b", "c", "d"], self.flags.delimited_list)
            self.assertGreater(
                len(self.flags.get_unparsed()),
                0,
                "There should still be unparsed data from the config file: {}".format(
                    self.flags.get_unparsed()
                )
            )
Exemplo n.º 2
0
    def _write_to_file(config, path):
        """Writes the configuration to the config file.

        Args:
            config: The configuration dictionary.
            path: Path of the file to write to.
        """
        if os.path.exists(path):
            backup = '%s.backup.%d' % (path, base.now_ms())
            os.rename(path, backup)
            os.chmod(path=backup, mode=0o600)
        logging.debug('Writing configuration to file: %r', path)
        os.makedirs(os.path.dirname(path), exist_ok=True)
        with open(path, 'wt') as f:
            json = base.json_encode(config)
            f.write(json)
        os.chmod(path=path, mode=0o600)
Exemplo n.º 3
0
 def test_parse_config_uri_parameter_for_child(self):
     """Unparsed flags read in a JSON config uri should be available to child_flags.parse."""
     with tempfile.NamedTemporaryFile(mode="wt", encoding="utf-8") as f:
         f.write(
             base.json_encode(
                 dict(
                     child_int=1,
                     child_bool=False,
                     child_list=["a", "b", "c"]
                 ),
                 pretty=True))
         f.flush()
         self.flags.parse([], config_uri="file:" + f.name)
     self.child_flags.parse(self.flags.get_unparsed())
     self.assertEqual(1, self.child_flags.child_int)
     self.assertEqual(False, self.child_flags.child_bool)
     self.assertListEqual(["a", "b", "c"], self.child_flags.child_list)
Exemplo n.º 4
0
    def _write_to_file(config, path):
        """Writes the configuration to the config file.

        Args:
            config: The configuration dictionary.
            path: Path of the file to write to.
        """
        if os.path.exists(path):
            backup = '%s.backup.%d' % (path, base.now_ms())
            os.rename(path, backup)
            os.chmod(path=backup, mode=0o600)
        logging.debug('Writing configuration to file: %r', path)
        os.makedirs(os.path.dirname(path), exist_ok=True)
        with open(path, 'wt') as f:
            json = base.json_encode(config)
            f.write(json)
        os.chmod(path=path, mode=0o600)
Exemplo n.º 5
0
 def test_parse_config_uri_parameter(self):
     """Flags should be able to read parameters from a JSON config uri."""
     with tempfile.NamedTemporaryFile(mode="wt", encoding="utf-8") as f:
         f.write(
             base.json_encode(
                 {
                     "int": "09",
                     "str-value": "string",
                     "bool": "True",
                     "list": ["a", "b", "c"],
                     "delimited-list": "1, 2, 3",
                 },
                 pretty=True))
         f.flush()
         self.flags.parse([], config_uri="file:" + f.name)
     self.assertEqual(9, self.flags.int)
     self.assertEqual(True, self.flags.bool)
     self.assertEqual("string", self.flags.str_value)
     self.assertListEqual(["a", "b", "c"], self.flags.list)
     self.assertListEqual(["1", "2", "3"], self.flags.delimited_list)
Exemplo n.º 6
0
 def test_get_unparsed(self):
     """get_unparsed() should return string representation of arguments."""
     with tempfile.NamedTemporaryFile(mode="wt", encoding="utf-8") as fp:
         config_parameters = {
             "unparsed_str": "abc",
             "unparsed-list": ["a", "b", "c"],
         }
         fp.write(base.json_encode(config_parameters, pretty=True))
         fp.flush()
         self.flags.parse(
             ("unparsed --config=file:%s --unparsed-int=1" % fp.name).split()
         )
     unparsed = self.flags.get_unparsed()
     self.assertEqual("unparsed", unparsed[0])
     self.assertEqual("--unparsed-int=1", unparsed[-1])
     self.assertGreater(
         len(unparsed),
         2,
         "there should be at least one command-line argument for missing value from config: {}"
         .format(unparsed)
     )
Exemplo n.º 7
0
    def test_config_uri_in_command_line_args_for_child(self):
        """Unparsed flags on the command line and config file should be passed to the child
        flags object in their original order.
        """
        with tempfile.NamedTemporaryFile(mode="wt", encoding="utf-8") as fp:
            config_parameters = dict(
                child_int="9",
                child_list=["b", "c"],
            )
            fp.write(base.json_encode(config_parameters, pretty=True))
            fp.flush()

            self.flags.parse(
                (
                    "--child-int=8 --child-list=a --config=file:%s --child_bool=no --child-list=d"
                    % fp.name
                ).split()
            )
            self.child_flags.parse(self.flags.get_unparsed())
            self.assertEqual(9, self.child_flags.child_int)
            self.assertEqual(False, self.child_flags.child_bool)
            self.assertListEqual(["a", "b", "c", "d"], self.child_flags.child_list)
Exemplo n.º 8
0
def main(args):
    conf = Configuration.Get()
    print(base.json_encode(conf._config))
    conf.Write()
Exemplo n.º 9
0
    def run(self, args):
        assert (len(args) == 0), "Unexpected launcher command-line arguments: {!r}".format(args)

        assert os.path.exists(self.flags.java_path), \
            "JVM executable not found: {!r}".format(self.flags.java_path)

        self.extract()

        # Identify which profile to use:
        profiles = self.config["profiles"]

        if self.flags.profile is not None:
            # Profile explicitly specified by user must exist:
            assert (self.flags.profile in profiles), \
                "Invalid profile {!r}, use one of {}." \
                .format(self.flags.profile, sorted(profiles.keys()))
            profile_name = self.flags.profile
        else:
            # No explicit override, default is to use the program name if possible,
            # falling back to the configured default profile if needed.
            profile_name = base.get_program_name()
            if profile_name not in profiles:
                profile_name = self.config["default_profile"]
        profile = profiles[profile_name]

        # Compute the JVM arguments from explicit extension/overrides and from global map:
        jvm_args = list()
        if not self.flags.ignore_profile_jvm_args:
            jvm_args.extend(profile.get("jvm_args", tuple()))
        jvm_args.extend(arg_map[JVM_ARGS])

        # Recover program arguments from global map:
        program_args = arg_map[PROGRAM_ARGS]

        # Compute concrete classpath and set environment CLASSPATH accordingly:
        cp_entries = tuple(self.make_classpath_entries(profile))
        env = dict(os.environ)
        env["CLASSPATH"] = ":".join(cp_entries)

        # Handle --print-X launcher commands:
        should_exit = False  # becomes true if a special command is invoked (eg. --print-foo)
        if self.flags.print_classpath:
            print(":".join(cp_entries))
            should_exit = True
        if self.flags.print_config:
            print(base.json_encode(self.config))
            should_exit = True
        if should_exit:
            return os.EX_OK

        # Determine which Java class to invoke:
        class_name = profile["main_class"]
        if self.flags.class_name is not None:
            class_name = self.flags.class_name

        log.info("Using Java executable: {!r}", self.flags.java_path)
        log.info("Using JVM arguments: {!r}", jvm_args)
        log.info("Using Java main class: {!r}", class_name)
        log.info("Using classpath: {}", base.json_encode(cp_entries))
        log.info("Using Java program arguments: {!r}", program_args)
        log.debug("Using environment: {}", base.json_encode(env))

        args = list()
        args.append(self.flags.java_path)
        args.extend(jvm_args)
        args.append(class_name)
        args.extend(program_args)

        os.execve(self.flags.java_path, args, env)
Exemplo n.º 10
0
def main(args):
    conf = Configuration.Get()
    print(base.json_encode(conf._config))
    conf.Write()