Pass filesystem mappings from wapm.toml to --mapdir

This commit is contained in:
Felix Schütt
2022-10-06 15:47:26 +02:00
parent 2414d44958
commit 2640a6d662
6 changed files with 49 additions and 8 deletions

1
Cargo.lock generated
View File

@@ -3858,6 +3858,7 @@ dependencies = [
"target-lexicon 0.12.4",
"tempfile",
"unix_mode",
"wapm-toml",
"wasmer",
"wasmer-cache",
"wasmer-compiler",

View File

@@ -59,6 +59,7 @@ dirs = { version = "4.0", optional = true }
serde_json = { version = "1.0", optional = true }
target-lexicon = { version = "0.12", features = ["std"] }
prettytable-rs = "0.9.0"
wapm-toml = "0.1.1"
[build-dependencies]
chrono = "0.4.22"

View File

@@ -253,7 +253,7 @@ fn parse_cli_args() -> Result<(), anyhow::Error> {
let mut args_without_package = args.clone();
args_without_package.remove(1);
return RunWithoutFile::try_parse_from(args_without_package.iter())?
.into_run_args(package.path)
.into_run_args(package.path, Some(package.manifest.clone()))
.execute();
}
@@ -270,12 +270,12 @@ fn parse_cli_args() -> Result<(), anyhow::Error> {
sp.close();
print!("\r\n");
match result {
Ok(o) => {
Ok((package, buf)) => {
// Try auto-installing the remote package
let mut args_without_package = args.clone();
args_without_package.remove(1);
return RunWithoutFile::try_parse_from(args_without_package.iter())?
.into_run_args(o)
.into_run_args(buf, Some(package.manifest.clone()))
.execute();
}
Err(e) => {

View File

@@ -75,7 +75,30 @@ pub struct RunWithoutFile {
impl RunWithoutFile {
/// Given a local path, returns the `Run` command (overriding the `--path` argument).
pub fn into_run_args(self, pathbuf: PathBuf) -> Run {
pub fn into_run_args(mut self, pathbuf: PathBuf, manifest: Option<wapm_toml::Manifest>) -> Run {
#[cfg(feature = "wasi")]
if let Some(fs) = manifest.as_ref().and_then(|m| m.fs.as_ref()) {
for (alias, real_dir) in fs.iter() {
let real_dir = if let Some(parent) = pathbuf.parent() {
parent.join(real_dir)
} else {
pathbuf.join(real_dir)
};
if !real_dir.exists() {
println!("warning: cannot map {alias:?} to {}: directory does not exist", real_dir.display());
continue;
}
println!("mapping {alias:?} -> {}", real_dir.display());
self.wasi.map_dir(alias, real_dir.clone());
#[cfg(feature = "wasi")] {
println!("setting PYTHONHOME to /{}", real_dir.display());
self.wasi.set_env("PYTHONHOME", &format!("{}", real_dir.display()));
}
}
}
Run {
#[cfg(feature = "cache")]
disable_cache: self.disable_cache,

View File

@@ -52,6 +52,16 @@ pub struct Wasi {
#[allow(dead_code)]
impl Wasi {
pub fn map_dir(&mut self, alias: &str, target_on_disk: PathBuf) {
self.mapped_dirs.push((alias.to_string(), target_on_disk));
self.pre_opened_directories.push(std::path::Path::new(alias).to_path_buf());
}
pub fn set_env(&mut self, key: &str, value: &str) {
self.env_vars.push((key.to_string(), value.to_string()));
}
/// Gets the WASI version (if any) for the provided module
pub fn get_versions(module: &Module) -> Option<BTreeSet<WasiVersion>> {
// Get the wasi version in strict mode, so no other imports are

View File

@@ -730,7 +730,7 @@ pub fn download_and_unpack_targz(url: &str, target_path: &Path) -> Result<PathBu
Ok(target_path.to_path_buf())
}
pub fn install_package(name: &str, version: Option<&str>) -> Result<PathBuf, String> {
pub fn install_package(name: &str, version: Option<&str>) -> Result<(LocalPackage, PathBuf), String> {
let registries = get_all_available_registries()?;
let mut url_of_package = None;
let mut error_packages = Vec::new();
@@ -817,13 +817,13 @@ pub fn install_package(name: &str, version: Option<&str>) -> Result<PathBuf, Str
let wapm_toml = toml::from_str::<wapm_toml::Manifest>(&wapm_toml)
.map_err(|e| format!("Could not parse toml for {name}@{version}: {e}"))?;
let commands = wapm_toml.command.unwrap_or_default();
let commands = wapm_toml.command.clone().unwrap_or_default();
let entrypoint_module = commands.first().ok_or(format!(
"Cannot run {name}@{version}: package has no commands"
))?;
let module_name = entrypoint_module.get_module();
let modules = wapm_toml.module.unwrap_or_default();
let modules = wapm_toml.module.clone().unwrap_or_default();
let entrypoint_module = modules
.iter()
.filter(|m| m.name == module_name)
@@ -832,7 +832,13 @@ pub fn install_package(name: &str, version: Option<&str>) -> Result<PathBuf, Str
"Cannot run {name}@{version}: module {module_name} not found in wapm.toml"
))?;
Ok(target_path.join(&entrypoint_module.source))
Ok((LocalPackage {
registry: package_info.registry.clone(),
name: wapm_toml.package.name.clone(),
version: wapm_toml.package.version.to_string(),
manifest: wapm_toml,
path: target_path.clone(),
}, target_path.join(&entrypoint_module.source)))
}
pub fn test_if_registry_present(registry: &str) -> Result<bool, String> {