rust/hgcli/src/main.rs
author Martin von Zweigbergk <martinvonz@google.com>
Mon, 21 Jun 2021 09:54:36 -0700
changeset 47459 de54d11040e7
parent 45620 426294d06ddc
permissions -rw-r--r--
rebase: keep str-keyed opts long enough to make `action` a str This is just another little py3 cleanup. Differential Revision: https://phab.mercurial-scm.org/D10891
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44638
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
use pyembed::MainPythonInterpreter;
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
// Include an auto-generated file containing the default
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
// `pyembed::PythonConfig` derived by the PyOxidizer configuration file.
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
//
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
// If you do not want to use PyOxidizer to generate this file, simply
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
// remove this line and instantiate your own instance of
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
// `pyembed::PythonConfig`.
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
include!(env!("PYOXIDIZER_DEFAULT_PYTHON_CONFIG_RS"));
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
fn main() {
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    12
    // The following code is in a block so the MainPythonInterpreter is
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    13
    // destroyed in an orderly manner, before process exit.
44638
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
    let code = {
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    15
        // Load the default Python configuration as derived by the PyOxidizer
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    16
        // config file used at build time.
44638
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
        let config = default_python_config();
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    19
        // Construct a new Python interpreter using that config, handling any
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    20
        // errors from construction.
44638
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
        match MainPythonInterpreter::new(config) {
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
            Ok(mut interp) => {
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    23
                // And run it using the default run configuration as specified
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    24
                // by the configuration. If an uncaught Python
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    25
                // exception is raised, handle it.
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    26
                // This includes the special SystemExit, which is a request to
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44729
diff changeset
    27
                // terminate the process.
44638
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
                interp.run_as_main()
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
            }
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
            Err(msg) => {
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
                eprintln!("{}", msg);
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
                1
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
            }
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
        }
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
    };
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
    // And exit the process according to code execution results.
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
    std::process::exit(code);
af739894a4c1 hgcli: add stub PyOxidizer project
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
}