scientific_workflow/provenance/
capture.rs1use crate::{
2 artifact::{ArtifactRef, ArtifactRole, RunArchive},
3 storage::Store,
4};
5use serde::{Deserialize, Serialize};
6use std::{collections::BTreeMap, io, path::Path};
7#[derive(Clone, Debug, Serialize, Deserialize)]
9pub struct ReproductionReport {
10 pub artifacts: usize,
12 pub bytes: u64,
14}
15pub(crate) type CaptureInventory = BTreeMap<String, ArtifactRef>;
16pub(crate) fn capture_file(
17 store: &Store,
18 source: &Path,
19 key: &str,
20 local: Option<&Path>,
21 inventory: &mut CaptureInventory,
22 role: ArtifactRole,
23) -> io::Result<()> {
24 if source.symlink_metadata()?.file_type().is_symlink() {
25 return Err(io::Error::other(format!(
26 "capture requires explicit ordinary files: {}",
27 source.display()
28 )));
29 }
30 let reference = store.publish(key, source, role)?;
31 if let Some(path) = local {
32 std::fs::create_dir_all(path.parent().unwrap())?;
33 crate::workspace::reserve_copy(path, reference.bytes)?;
34 std::fs::copy(source, path)?;
35 let actual = crate::artifact::digest_reader(std::fs::File::open(path)?)?;
36 if actual != (reference.sha256.clone(), reference.bytes) {
37 return Err(io::Error::other(format!(
38 "input changed during capture: {}",
39 source.display()
40 )));
41 }
42 }
43 inventory.insert(key.into(), reference);
44 Ok(())
45}
46pub(crate) fn capture_tree(
47 store: &Store,
48 source: &Path,
49 key: &str,
50 local: Option<&Path>,
51 inventory: &mut CaptureInventory,
52) -> io::Result<()> {
53 for entry in std::fs::read_dir(source)? {
54 let e = entry?;
55 let name = e.file_name();
56 let name = name
57 .to_str()
58 .ok_or_else(|| io::Error::other("capture requires UTF-8 paths"))?;
59 if [".git", "target", "__pycache__", ".pytest_cache", ".venv"].contains(&name) {
60 continue;
61 }
62 let next = format!("{key}/{name}");
63 let path = local.map(|p| p.join(name));
64 if e.file_type()?.is_dir() {
65 capture_tree(store, &e.path(), &next, path.as_deref(), inventory)?;
66 } else {
67 capture_file(
68 store,
69 &e.path(),
70 &next,
71 path.as_deref(),
72 inventory,
73 ArtifactRole::Reproduction,
74 )?;
75 }
76 }
77 Ok(())
78}
79pub fn verify_reproduction(archive: &RunArchive) -> io::Result<ReproductionReport> {
81 if !archive.manifest().capture_complete {
82 return Err(io::Error::other("reproduction capture is incomplete"));
83 }
84 let mut report = ReproductionReport {
85 artifacts: 0,
86 bytes: 0,
87 };
88 for (key, reference) in &archive.manifest().artifacts {
89 if reference.role == ArtifactRole::Reproduction {
90 archive.verify(key)?;
91 report.artifacts += 1;
92 report.bytes += reference.bytes;
93 }
94 }
95 if report.artifacts == 0 {
96 return Err(io::Error::other("reproduction bundle is empty"));
97 }
98 Ok(report)
99}
100
101pub(crate) fn capture_software_tree(
104 store: &Store,
105 source: &Path,
106 key: &str,
107 local: &Path,
108 inventory: &mut CaptureInventory,
109) -> io::Result<()> {
110 fn walk(
111 store: &Store,
112 source: &Path,
113 key: &str,
114 local: &Path,
115 inventory: &mut CaptureInventory,
116 stack: &mut Vec<std::path::PathBuf>,
117 ) -> io::Result<()> {
118 let source = source.canonicalize()?;
119 if stack.contains(&source) {
120 return Err(io::Error::other("cycle in software environment"));
121 }
122 stack.push(source.clone());
123 for e in std::fs::read_dir(&source)? {
124 let e = e?;
125 let name = e
126 .file_name()
127 .to_str()
128 .ok_or_else(|| io::Error::other("non-UTF8 software path"))?
129 .to_owned();
130 if ["__pycache__", ".git", "target"].contains(&name.as_str()) {
131 continue;
132 }
133 let path = e.path().canonicalize()?;
134 let key = format!("{key}/{name}");
135 let local = local.join(&name);
136 if path.is_dir() {
137 walk(store, &path, &key, &local, inventory, stack)?;
138 } else if path.is_file() {
139 capture_file(
140 store,
141 &path,
142 &key,
143 Some(&local),
144 inventory,
145 ArtifactRole::Reproduction,
146 )?;
147 }
148 }
149 stack.pop();
150 Ok(())
151 }
152 walk(store, source, key, local, inventory, &mut Vec::new())
153}