-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathpython_embedded_resources.rs
More file actions
110 lines (96 loc) · 3.35 KB
/
python_embedded_resources.rs
File metadata and controls
110 lines (96 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use {
crate::{
py_packaging::binary::PythonBinaryBuilder,
starlark::env::{get_context, PyOxidizerEnvironmentContext},
},
anyhow::{anyhow, Context, Result},
slog::warn,
starlark::{
environment::TypeValues,
values::{
error::{RuntimeError, ValueError},
{Mutable, TypedValue, Value, ValueResult},
},
{
starlark_fun, starlark_module, starlark_parse_param_type, starlark_signature,
starlark_signature_extraction, starlark_signatures,
},
},
starlark_dialect_build_targets::{ResolvedTarget, ResolvedTargetValue, RunMode},
std::sync::Arc,
};
fn error_context<F, T>(label: &str, f: F) -> Result<T, ValueError>
where
F: FnOnce() -> anyhow::Result<T>,
{
f().map_err(|e| {
ValueError::Runtime(RuntimeError {
code: "PYOXIDIZER_PYTHON_EMBEDDED_RESOURCES",
message: format!("{:?}", e),
label: label.to_string(),
})
})
}
pub struct PythonEmbeddedResourcesValue {
pub exe: Arc<dyn PythonBinaryBuilder>,
}
impl TypedValue for PythonEmbeddedResourcesValue {
type Holder = Mutable<PythonEmbeddedResourcesValue>;
const TYPE: &'static str = "PythonEmbeddedResources";
fn values_for_descendant_check_and_freeze(&self) -> Box<dyn Iterator<Item = Value>> {
Box::new(std::iter::empty())
}
}
impl PythonEmbeddedResourcesValue {
fn build(
&self,
type_values: &TypeValues,
target: &str,
context: &PyOxidizerEnvironmentContext,
) -> Result<ResolvedTarget> {
let output_path = context
.build_path(type_values)
.map_err(|_| anyhow!("unable to resolve output path"))?;
warn!(
context.logger(),
"writing Python embedded artifacts to {}",
output_path.display()
);
let embedded = self.exe.to_embedded_python_context(
context.logger(),
context.env(),
&context.build_opt_level,
)?;
std::fs::create_dir_all(&output_path)
.with_context(|| format!("creating output directory: {}", output_path.display()))?;
embedded.write_files(&output_path)?;
embedded.write_extra_files(&output_path)?;
Ok(ResolvedTarget {
run_mode: RunMode::None,
output_path,
})
}
fn build_starlark(&self, type_values: &TypeValues, target: String) -> ValueResult {
let pyoxidizer_context_value = get_context(type_values)?;
let pyoxidizer_context = pyoxidizer_context_value
.downcast_ref::<PyOxidizerEnvironmentContext>()
.ok_or(ValueError::IncorrectParameterType)?;
let inner = error_context("PythonEmbeddedResources.build()", || {
self.build(type_values, &target, &pyoxidizer_context)
})?;
Ok(Value::new(ResolvedTargetValue { inner }))
}
}
starlark_module! { python_embedded_resources_module =>
PythonEmbeddedResources.build(
env env,
this,
target: String
) {
let this = this.downcast_ref::<PythonEmbeddedResourcesValue>().unwrap();
this.build_starlark(env, target)
}
}