Skip to content

Commit 6f5508f

Browse files
committed
more more
Signed-off-by: Nick Mitchell <[email protected]>
1 parent 1798167 commit 6f5508f

File tree

5 files changed

+22
-32
lines changed

5 files changed

+22
-32
lines changed

pdl-live-react/src-tauri/src/cli/run.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub fn run_pdl_program(
5555
println!("Updated! {:?}", updated_program);
5656
let mut out_str = String::new();
5757
let mut emitter = YamlEmitter::new(&mut out_str);
58+
emitter.multiline_strings(true);
5859
emitter.dump(&updated_program).map_err(|e| match e {
5960
EmitError::FmtError(ee) => tauri::Error::Anyhow(ee.into()),
6061
})?;

pdl-live-react/src-tauri/src/interpreter/extract.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,28 @@ pub fn extract_requirements(program: &Yaml) -> (Vec<String>, Yaml) {
3838

3939
let req_hash = shasum::sha256sum_str(requirements_text.as_str()).unwrap();
4040
let code_text = if let Some(c) = h[&code].as_str() {
41-
c
41+
format!("{}\nprint(result)", c)
4242
} else {
43-
""
43+
"".to_string()
4444
};
45-
let code_hash = shasum::sha256sum_str(code_text).unwrap();
45+
let code_hash = shasum::sha256sum_str(&code_text.as_str()).unwrap();
4646

4747
let tmp = Builder::new()
4848
.prefix(&format!("pdl-program-{}", code_hash))
4949
.suffix(".pdl")
5050
.tempfile()
5151
.unwrap(); // TODO tmpfile_in(source dir)
5252
write(&tmp, code_text).unwrap();
53+
let (_, tmp_path) = tmp.keep().unwrap();
5354

5455
h.remove(&requirements);
56+
h[&lang] = Yaml::String("command".to_string());
57+
h.insert(Yaml::String("file".to_string()), Yaml::Boolean(true));
5558
h[&code] = Yaml::String(format!(
56-
"\"/Users/nickm/Library/Caches/pdl/venvs/{}/{}/python /tmp/{}.pdl\"",
59+
"\"/Users/nickm/Library/Caches/pdl/venvs/{}/{}/python {}\"",
5760
req_hash,
5861
if cfg!(windows) { "Scripts" } else { "bin" },
59-
code_hash
62+
tmp_path.display(),
6063
));
6164

6265
Yaml::Hash(h.clone())

pdl-live-react/src-tauri/src/interpreter/pip.rs

+5-25
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@ fn pip_install_if_needed_with_hash(
1616
cache_path: &Path,
1717
requirements_path: &Path,
1818
hash: String,
19-
force: bool,
2019
) -> Result<PathBuf, tauri::Error> {
2120
create_dir_all(&cache_path)?;
2221

2322
let venv_path = cache_path.join("venvs").join(hash);
2423
let bin_path = venv_path.join(if cfg!(windows) { "Scripts" } else { "bin" });
2524

26-
// re: force, this is part of the short-term hack to install all
27-
// code block dependencies in the main interpreter venv. Once we
28-
// figure out how to support a separate venv for each code block
29-
// (that needs it), we can undo this hack.
3025
if !venv_path.exists() {
3126
println!("Creating virtual environment...");
3227
let python = if cfg!(target_os = "macos") {
@@ -36,16 +31,10 @@ fn pip_install_if_needed_with_hash(
3631
};
3732
cmd!(python, "-mvenv", &venv_path).run()?;
3833

39-
if !force {
40-
cmd!(bin_path.join("pip"), "install", "-r", &requirements_path).run()?;
41-
42-
let cached_requirements_path = venv_path.join("requirements.txt");
43-
copy(requirements_path, cached_requirements_path)?;
44-
}
45-
}
46-
47-
if force {
4834
cmd!(bin_path.join("pip"), "install", "-r", &requirements_path).run()?;
35+
36+
let cached_requirements_path = venv_path.join("requirements.txt");
37+
copy(requirements_path, cached_requirements_path)?;
4938
}
5039

5140
Ok(bin_path.to_path_buf())
@@ -57,7 +46,7 @@ fn pip_install_if_needed(
5746
requirements_path: &Path,
5847
) -> Result<PathBuf, tauri::Error> {
5948
let hash = shasum::sha256sum(&requirements_path)?;
60-
pip_install_if_needed_with_hash(cache_path, requirements_path, hash, false)
49+
pip_install_if_needed_with_hash(cache_path, requirements_path, hash)
6150
}
6251

6352
#[cfg(desktop)]
@@ -67,11 +56,6 @@ pub async fn pip_install_code_blocks_if_needed(
6756
) -> Result<Option<Yaml>, tauri::Error> {
6857
let cache_path = app_handle.path().cache_dir()?.join("pdl");
6958

70-
// for now, install the requirements in the main interpreter venv
71-
let requirements_path = app_handle
72-
.path()
73-
.resolve("interpreter/requirements.txt", BaseDirectory::Resource)?;
74-
7559
let (reqs, updated_program) = extract::extract_requirements(&program);
7660
let n = reqs
7761
.into_par_iter()
@@ -80,12 +64,8 @@ pub async fn pip_install_code_blocks_if_needed(
8064
.prefix("pdl-requirements-")
8165
.suffix(".txt")
8266
.tempfile()?;
83-
// This is part of the "force" hack described above, where
84-
// we force the code block dependencies to be installed in
85-
// the main interpreter venv.
86-
let hash = shasum::sha256sum(&requirements_path)?;
8767
write(&req_path, req)?;
88-
pip_install_if_needed_with_hash(&cache_path, &req_path.path(), hash, true)?;
68+
pip_install_if_needed(&cache_path, &req_path.path())?;
8969
Ok(1)
9070
})
9171
.count();

src/pdl/pdl_ast.py

+3
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ class CodeBlock(LeafBlock):
475475
code: "BlockType"
476476
"""Code to execute.
477477
"""
478+
file: Optional[bool] = None
479+
"""Code to execute is a file path.
480+
"""
478481

479482
@model_validator(mode="after")
480483
def lang_is_python(self):

src/pdl/pdl_interpreter.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ def process_call_code(
14561456
) from exc
14571457
case "command":
14581458
try:
1459-
result = call_command(code_s)
1459+
result = call_command(code_s, block.file == True)
14601460
background = PdlList(
14611461
[
14621462
PdlDict( # type: ignore
@@ -1530,7 +1530,10 @@ def call_python(code: str, scope: ScopeType) -> PdlLazy[Any]:
15301530
return PdlConst(result)
15311531

15321532

1533-
def call_command(code: str) -> PdlLazy[str]:
1533+
def call_command(code: str, is_file: bool) -> PdlLazy[str]:
1534+
if is_file:
1535+
with open(code, "r") as f:
1536+
code = f.read()
15341537
args = shlex.split(code)
15351538
p = subprocess.run(
15361539
args, capture_output=True, text=True, check=False, shell=False

0 commit comments

Comments
 (0)