1use pyo3::{exceptions::PyRuntimeError, prelude::*};
2
3use anyhow::Result;
4
5pub mod entry;
6pub mod package;
7pub mod spec;
8pub mod util;
9
10fn register_submodule(
11 parent: &Bound<'_, PyModule>,
12 submodule: &Bound<'_, PyModule>,
13 full_name: &str,
14) -> PyResult<()> {
15 parent.add_submodule(submodule)?;
16
17 parent
19 .py()
20 .import("sys")?
21 .getattr("modules")?
22 .set_item(full_name, submodule)?;
23
24 Ok(())
25}
26
27fn register_module_package_outline(
28 parent_module: &Bound<'_, PyModule>,
29) -> PyResult<()> {
30 let child_module = PyModule::new(parent_module.py(), "outline")?;
31 use package::outline;
32
33 child_module.add_class::<outline::PackageOutline>()?;
34
35 register_submodule(parent_module, &child_module, "zpack.package.outline")?;
36
37 Ok(())
38}
39
40fn register_module_package_constraint(
41 parent_module: &Bound<'_, PyModule>,
42) -> PyResult<()> {
43 let child_module = PyModule::new(parent_module.py(), "constraint")?;
44 use package::constraint;
45
46 child_module.add_class::<constraint::Cmp>()?;
47 child_module.add_class::<constraint::CmpType>()?;
48 child_module.add_class::<constraint::Depends>()?;
49 child_module.add_class::<constraint::IfThen>()?;
50 child_module.add_class::<constraint::Maximize>()?;
51 child_module.add_class::<constraint::Minimize>()?;
52 child_module.add_class::<constraint::NumOf>()?;
53 child_module.add_class::<constraint::SpecOption>()?;
54 child_module.add_class::<constraint::Value>()?;
55
56 register_submodule(
57 parent_module,
58 &child_module,
59 "zpack.package.constraint",
60 )?;
61
62 Ok(())
63}
64
65fn register_module_package_version(
66 parent_module: &Bound<'_, PyModule>,
67) -> PyResult<()> {
68 let child_module = PyModule::new(parent_module.py(), "version")?;
69
70 child_module.add_class::<package::version::Version>()?;
71
72 register_submodule(parent_module, &child_module, "zpack.package.version")?;
73
74 Ok(())
75}
76
77fn register_module_package(
78 parent_module: &Bound<'_, PyModule>,
79) -> PyResult<()> {
80 let child_module = PyModule::new(parent_module.py(), "package")?;
81
82 register_module_package_outline(&child_module)?;
83 register_module_package_constraint(&child_module)?;
84 register_module_package_version(&child_module)?;
85
86 register_submodule(parent_module, &child_module, "zpack.package")?;
87
88 Ok(())
89}
90
91#[pyfunction]
92fn init_tracing() {
93 Python::attach(|_py| {
94 tracing::subscriber::set_global_default(
95 crate::util::subscriber::subscriber(),
96 )
97 .expect("Failed to set subscriber");
98 });
99
100 tracing::warn!("tracing activated");
101}
102
103#[pyfunction]
104fn main_entry() -> PyResult<()> {
105 entry::entry().map_err(|e| PyRuntimeError::new_err(e.to_string()))
106}
107
108#[pymodule]
109fn zpack(m: &Bound<'_, PyModule>) -> PyResult<()> {
110 register_module_package(m)?;
111
112 m.add_function(wrap_pyfunction!(init_tracing, m)?)?;
113
114 m.add_function(wrap_pyfunction!(main_entry, m)?)?;
115
116 Ok(())
117}