Batch draft transcripts and reuse verified source versions
This commit is contained in:
1 parent
373204a8d8
commit
c6b5c9ed56
4 files changed
+108
-47
No files matched your search
+30
-24
@@ -33,7 +33,7 @@ pub use texrra_tex_core::resources::{WorldFailure, WorldObservation};
|
||||
use texrra_tex_core::{
|
||||
execution::{
|
||||
Cancellation, ExecutionFailure, Executor, FormatImage, InputProfile, JobTime, Limits,
|
||||
NativeSession, RevisionError, RevisionExecution, RevisionFailure, RevisionJob, SourceFile,
|
||||
NativeSession, Profile, RevisionError, RevisionExecution, RevisionFailure, RevisionJob, SourceFile,
|
||||
SourceProvider, State, WriteDestination,
|
||||
},
|
||||
pdf_unicode::XdvGlyphNames,
|
||||
@@ -783,23 +783,6 @@ fn prepare_checked(
|
||||
data: loaded.data,
|
||||
};
|
||||
check(cancellation)?;
|
||||
let state = if matches!(request.entry, Entry::Initex) {
|
||||
State::initex()
|
||||
} else {
|
||||
State::default()
|
||||
}
|
||||
.with_job_time(request.time)
|
||||
.with_input_profile(request.input_profile);
|
||||
let state = if request.target == Target::Shipouts && !dump {
|
||||
state.with_pdf_output()
|
||||
} else {
|
||||
state
|
||||
};
|
||||
let state = if request.etex {
|
||||
state.with_etex()
|
||||
} else {
|
||||
state
|
||||
};
|
||||
let translation = request.translation_file.as_ref().map(|name| {
|
||||
if request.input_profile != InputProfile::TeX8Bit {
|
||||
return Err("a TCX file requires the TeX 8-bit profile".to_owned());
|
||||
@@ -809,6 +792,11 @@ fn prepare_checked(
|
||||
.map_err(|e| format!("TCX {}: {e}", loaded.version.path.display()))
|
||||
}).transpose()?;
|
||||
let (executor, format_preparation) = if let Entry::Format(name) = &request.entry {
|
||||
let profile = if request.etex {
|
||||
Profile::EtexBootstrap
|
||||
} else {
|
||||
Profile::Bootstrap
|
||||
};
|
||||
let loaded =
|
||||
world.read_shared(ResourceKind::Tex, &name.display().to_string(), &path)?;
|
||||
check(cancellation)?;
|
||||
@@ -864,7 +852,7 @@ fn prepare_checked(
|
||||
let image = &cached.image;
|
||||
let executor = if kernel == KernelPreparation::Resume {
|
||||
image.validate_shared_job_inputs(
|
||||
state.profile(),
|
||||
profile,
|
||||
request.input_profile,
|
||||
&mut world,
|
||||
read,
|
||||
@@ -873,7 +861,7 @@ fn prepare_checked(
|
||||
None
|
||||
} else {
|
||||
Some(image.fork_job_with_file_dates(
|
||||
state.profile(),
|
||||
profile,
|
||||
request.input_profile,
|
||||
request.time,
|
||||
request.limits,
|
||||
@@ -894,7 +882,7 @@ fn prepare_checked(
|
||||
image.bind_inputs(&bindings);
|
||||
let executor = if kernel == KernelPreparation::Resume {
|
||||
image.validate_shared_job_inputs(
|
||||
state.profile(),
|
||||
profile,
|
||||
request.input_profile,
|
||||
&mut world,
|
||||
read,
|
||||
@@ -903,7 +891,7 @@ fn prepare_checked(
|
||||
None
|
||||
} else {
|
||||
Some(image.new_job_with_file_dates(
|
||||
state.profile(),
|
||||
profile,
|
||||
request.input_profile,
|
||||
request.time,
|
||||
request.limits,
|
||||
@@ -916,8 +904,26 @@ fn prepare_checked(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let executor =
|
||||
(kernel == KernelPreparation::Fresh).then(|| Executor::new(state, request.limits));
|
||||
let executor = (kernel == KernelPreparation::Fresh).then(|| {
|
||||
let state = if matches!(request.entry, Entry::Initex) {
|
||||
State::initex()
|
||||
} else {
|
||||
State::default()
|
||||
}
|
||||
.with_job_time(request.time)
|
||||
.with_input_profile(request.input_profile);
|
||||
let state = if request.target == Target::Shipouts && !dump {
|
||||
state.with_pdf_output()
|
||||
} else {
|
||||
state
|
||||
};
|
||||
let state = if request.etex {
|
||||
state.with_etex()
|
||||
} else {
|
||||
state
|
||||
};
|
||||
Executor::new(state, request.limits)
|
||||
});
|
||||
(executor, None)
|
||||
};
|
||||
let executor = match (executor, translation) {
|
||||
|
||||
@@ -109,9 +109,12 @@ impl SourceProvider for RecordingHost<'_> {
|
||||
}
|
||||
fn read(&mut self, name: &str, from: &str) -> Result<SourceFile, String> {
|
||||
let (name, from) = (name.to_owned(), from.to_owned());
|
||||
call!(self, read(&name, &from); |result| {
|
||||
result.as_ref().map(|file| (file.name.clone(), digest(&file.data))).map_err(Clone::clone)
|
||||
})
|
||||
self.record(
|
||||
"read",
|
||||
move |files| files.read_versioned_source(&name, &from),
|
||||
|result| result.as_ref().map(|(_, version)| version.clone()).map_err(Clone::clone),
|
||||
)
|
||||
.map(|(source, _)| source)
|
||||
}
|
||||
fn resource(
|
||||
&mut self,
|
||||
@@ -312,6 +315,7 @@ impl SourceProvider for RecordingHost<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn digest(bytes: &[u8]) -> String {
|
||||
use sha2::{Digest, Sha256};
|
||||
format!("{:x}", Sha256::digest(bytes))
|
||||
|
||||
+45
-20
@@ -12,7 +12,7 @@ use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
ffi::OsStr,
|
||||
fs::{self, File, OpenOptions},
|
||||
io::{self, BufRead, BufReader, Seek, Write},
|
||||
io::{self, BufRead, BufReader, BufWriter, Seek, Write},
|
||||
path::{Path, PathBuf},
|
||||
sync::{
|
||||
Arc,
|
||||
@@ -167,7 +167,7 @@ pub struct Files {
|
||||
next_handle: u64,
|
||||
stream_io: StreamJournal,
|
||||
log_path: PathBuf,
|
||||
log: Option<File>,
|
||||
log: Option<BufWriter<File>>,
|
||||
draft: Option<DraftEffects>,
|
||||
protected: Vec<PathBuf>,
|
||||
producer: PathBuf,
|
||||
@@ -186,6 +186,13 @@ pub struct Files {
|
||||
preparation: HostPreparation,
|
||||
runtime_observations: RefCell<HostPreparation>,
|
||||
}
|
||||
fn transcript_writer(file: File, draft: bool) -> BufWriter<File> {
|
||||
// Draft bytes remain private until cleanup and publication. Live jobs
|
||||
// keep reporting actual writes to the transcript during execution.
|
||||
let capacity = if draft { 8192 } else { 0 };
|
||||
BufWriter::with_capacity(capacity, file)
|
||||
}
|
||||
|
||||
fn output_column(previous: usize, text: &str) -> usize {
|
||||
match text.rsplit_once('\n') {
|
||||
Some((_, tail)) => tail.chars().count(),
|
||||
@@ -561,7 +568,7 @@ impl Files {
|
||||
next_handle: 0,
|
||||
stream_io: StreamJournal::default(),
|
||||
log_path,
|
||||
log: Some(log),
|
||||
log: Some(transcript_writer(log, draft.is_some())),
|
||||
draft,
|
||||
protected,
|
||||
producer: source.to_owned(),
|
||||
@@ -890,6 +897,20 @@ impl Files {
|
||||
.map(PathBuf::as_path),
|
||||
)
|
||||
}
|
||||
pub(crate) fn read_versioned_source(
|
||||
&mut self,
|
||||
name: &str,
|
||||
from: &str,
|
||||
) -> Result<(SourceFile, ResourceVersion), String> {
|
||||
let resource = self.world.read_tex_input(name, Path::new(from))?;
|
||||
self.observe_resource(ResourceKind::Tex, &resource);
|
||||
let source = SourceFile {
|
||||
name: resource.version.path.display().to_string(),
|
||||
data: resource.data,
|
||||
};
|
||||
Ok((source, resource.version))
|
||||
}
|
||||
|
||||
fn observe_resource(&mut self, kind: ResourceKind, resource: &LoadedResource) {
|
||||
let path = &resource.version.path;
|
||||
if kind == ResourceKind::Tex && self.ledger.owns(path) && !self.is_protected(path) {
|
||||
@@ -1081,14 +1102,12 @@ impl Files {
|
||||
};
|
||||
if destination != WriteDestination::TerminalOnly {
|
||||
if self.log.is_none() {
|
||||
self.log = Some(
|
||||
File::create(
|
||||
self.draft
|
||||
.as_ref()
|
||||
.map_or(&self.log_path, |draft| &draft.log.staged.path),
|
||||
)
|
||||
.map_err(|e| format!("native log {}: {e}", self.log_path.display()))?,
|
||||
);
|
||||
let path = self.draft
|
||||
.as_ref()
|
||||
.map_or(&self.log_path, |draft| &draft.log.staged.path);
|
||||
let file = File::create(path)
|
||||
.map_err(|e| format!("native log {}: {e}", self.log_path.display()))?;
|
||||
self.log = Some(transcript_writer(file, self.draft.is_some()));
|
||||
}
|
||||
let log = self.log.as_mut().unwrap();
|
||||
if start_line && self.log_column != 0 {
|
||||
@@ -1189,11 +1208,16 @@ impl Files {
|
||||
queue: &DraftSyncQueue,
|
||||
cancellation: &Cancellation,
|
||||
) -> Result<Option<DraftPublication>, String> {
|
||||
let draft = self.draft.take().ok_or("native draft host required")?;
|
||||
let draft = self.draft.as_ref().ok_or("native draft host required")?;
|
||||
if !draft.finished {
|
||||
return Err("native draft must finish successfully before publication".into());
|
||||
}
|
||||
let log = self.log.take();
|
||||
let draft = self.draft.take().expect("finished draft");
|
||||
let log = self.log.take().map(|log| {
|
||||
let (file, buffer) = log.into_parts();
|
||||
debug_assert!(buffer.is_ok_and(|bytes| bytes.is_empty()));
|
||||
file
|
||||
});
|
||||
let initial = self
|
||||
.preparation
|
||||
.generated_initial
|
||||
@@ -1441,6 +1465,13 @@ impl PendingDraft {
|
||||
|
||||
impl Drop for Files {
|
||||
fn drop(&mut self) {
|
||||
if self.draft.is_some() {
|
||||
// A discarded draft has no reader. Drop pending bytes without
|
||||
// triggering BufWriter's implicit flush on cancellation/rejection.
|
||||
if let Some(log) = self.log.take() {
|
||||
drop(log.into_parts());
|
||||
}
|
||||
}
|
||||
// Keep the caller's fatal diagnostic on its own physical line when
|
||||
// execution aborts before finish closes the input entry.
|
||||
if self.terminal_sink && self.terminal_column != 0 {
|
||||
@@ -1638,13 +1669,7 @@ impl SourceProvider for Files {
|
||||
self.emit_diagnostic(WriteDestination::TerminalOnly, terminal, false)
|
||||
}
|
||||
fn read(&mut self, name: &str, from: &str) -> Result<SourceFile, String> {
|
||||
let resource = self.world.read_tex_input(name, Path::new(from))?;
|
||||
self.observe_resource(ResourceKind::Tex, &resource);
|
||||
let path = resource.version.path;
|
||||
Ok(SourceFile {
|
||||
name: path.display().to_string(),
|
||||
data: resource.data,
|
||||
})
|
||||
self.read_versioned_source(name, from).map(|(source, _)| source)
|
||||
}
|
||||
fn resource(
|
||||
&mut self,
|
||||
|
||||
Reference in new issue
Block a user