Merge remote-tracking branch 'origin/main' into t4270b

This commit is contained in:
FredApps
2026-11-16 07:38:59 +01:00
8 changed files with 1940 additions and 657 deletions
Generated
+19
View File
@@ -1403,6 +1403,15 @@ dependencies = [
"encoding_rs",
]
[[package]]
name = "craft-pdf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=0fd30aeb3e02207d9d4c7c601540d9da45abf8de#0fd30aeb3e02207d9d4c7c601540d9da45abf8de"
dependencies = [
"flate2",
"roxmltree",
]
[[package]]
name = "craft-psd"
version = "0.1.0"
@@ -4047,6 +4056,7 @@ dependencies = [
"craft-heif",
"craft-jxl",
"craft-mime",
"craft-pdf",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
"crc32fast",
"fs4",
@@ -6172,6 +6182,15 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "roxmltree"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9da7f59328b09405b181fee1018925fad50f157afb43c0c0f5c522c256dd5907"
dependencies = [
"memchr",
]
[[package]]
name = "rsa"
version = "0.9.10"
+1
View File
@@ -10,6 +10,7 @@ rust-version = "1.95"
repository = "https://github.com/storytold/lightcraft"
[workspace.dependencies]
craft-pdf-print = { package = "craft-pdf", git = "https://github.com/storytold/craft-libs", rev = "0fd30aeb3e02207d9d4c7c601540d9da45abf8de", default-features = false, features = ["write-raster"] }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["serde"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" }
# Canon CRW decoder shared with the other Craft apps
+26 -21
View File
@@ -40,7 +40,7 @@ impl<'a> Rd<'a> {
}
}
/// The header, colour mode data and image resources (`craft-psd`), with a reader at the layer and mask section.
/// The header (`craft-psd`), colour mode data and image resources, with a reader at the layer and mask section.
struct Start<'a> {
psb: bool,
channels: usize,
@@ -48,7 +48,8 @@ struct Start<'a> {
height: usize,
depth: u16,
mode: u16,
head: craft_psd::Head,
color_mode_data: &'a [u8],
resources: Vec<craft_psd::ImageResource>,
r: Rd<'a>,
}
@@ -57,13 +58,17 @@ fn start<'a>(bytes: &'a [u8], opts: &DecodeOptions) -> Result<Start<'a>> {
if let (Some(&[h0, h1, h2, h3]), Some(&[w0, w1, w2, w3])) = (bytes.get(14..18), bytes.get(18..22)) {
check_size(F, u64::from(u32::from_be_bytes([w0, w1, w2, w3])), u64::from(u32::from_be_bytes([h0, h1, h2, h3])), opts)?;
}
let head = craft_psd::Head::from_bytes(bytes).map_err(|e| match e {
let h = craft_psd::Header::from_bytes(bytes).map_err(|e| match e {
craft_psd::PsdError::UnexpectedEof { .. } => err("truncated"),
e => err(&e.to_string()),
})?;
let h = &head.header;
check_size(F, u64::from(h.width), u64::from(h.height), opts)?;
let p = usize::try_from(head.layer_section_offset).map_err(|_| err("truncated"))?;
let mut r = Rd { b: bytes, p: 26 };
let cm_len = r.u32()? as usize;
let color_mode_data = r.take(cm_len)?;
let res_len = r.u32()? as usize;
r.take(res_len)?;
// a resource section that doesn't parse is left out: the picture still decodes, without its profile and metadata
let resources = craft_psd::Head::from_bytes(bytes).map(|head| head.resources).unwrap_or_default();
Ok(Start {
psb: h.version.is_psb(),
channels: usize::from(h.channels),
@@ -71,15 +76,15 @@ fn start<'a>(bytes: &'a [u8], opts: &DecodeOptions) -> Result<Start<'a>> {
height: h.height as usize,
depth: h.depth,
mode: h.color_mode.as_u16(),
r: Rd { b: bytes, p },
head,
color_mode_data,
resources,
r,
})
}
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
let Start { psb, channels, width, height, depth, mode, head, mut r } = start(bytes, opts)?;
let color_mode_data = head.color_mode_data.as_slice();
let (icc, exif, xmp) = resources(&head);
let Start { psb, channels, width, height, depth, mode, color_mode_data, resources: res, mut r } = start(bytes, opts)?;
let (icc, exif, xmp) = resources(&res);
// Layer and mask information: only the layer count sign matters (merged transparency).
let lm_len = if psb { r.u64()? } else { u64::from(r.u32()?) };
@@ -352,9 +357,8 @@ fn packbits(src: &[u8], out: &mut Vec<u8>) {
/// depth, colour mode or compression, or merged image data that runs past the end of the file
/// (truncated). The run-length data itself is not unpacked.
pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> {
let Start { psb, channels, width, height, depth, mode, head, mut r } = start(bytes, &DecodeOptions::default())?;
let color_mode_data = head.color_mode_data.as_slice();
let (_, exif, _) = resources(&head);
let Start { psb, channels, width, height, depth, mode, color_mode_data, resources: res, mut r } = start(bytes, &DecodeOptions::default())?;
let (_, exif, _) = resources(&res);
let lm_len = if psb { r.u64()? as usize } else { r.u32()? as usize };
r.take(lm_len)?;
let compression = r.u16()?;
@@ -435,8 +439,8 @@ fn unpackbits(mut src: &[u8], row: &mut [u8]) -> Result<()> {
}
/// ICC (1039), EXIF (1058, without its optional `Exif\0\0` prefix) and XMP (1060) resources; the last of each id.
fn resources(head: &craft_psd::Head) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<String>) {
let last = |id: u16| head.resources.iter().rev().find(|r| r.id == id).map(|r| r.data.as_slice());
fn resources(res: &[craft_psd::ImageResource]) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<String>) {
let last = |id: u16| res.iter().rev().find(|r| r.id == id).map(|r| r.data.as_slice());
let icc = last(1039).map(<[u8]>::to_vec);
let exif = last(1058).map(|d| d.strip_prefix(b"Exif\0\0").unwrap_or(d).to_vec());
let xmp = last(1060).map(|d| String::from_utf8_lossy(d).trim_end_matches('\0').to_string());
@@ -510,14 +514,15 @@ mod tests {
}
#[test]
fn a_damaged_resource_section_is_malformed() {
// a resource section of 4 bytes that is not an `8BIM` record
fn a_damaged_resource_section_is_left_out() {
// a resource section of 4 bytes that is not an `8BIM` record: the picture decodes without it
let mut b = header(3, 1, 1, 8, 3, 0);
b[30..34].copy_from_slice(&4u32.to_be_bytes());
b.splice(34..34, *b"JUNK");
let e = decode(&b, &DecodeOptions::default()).unwrap_err();
assert!(matches!(e, Error::Malformed(_, ref m) if m.contains("signature")), "{e:?}");
assert!(super::header(&b).is_err());
b.extend_from_slice(&[1, 2, 3]);
let d = decode(&b, &DecodeOptions::default()).unwrap();
assert_eq!((d.width, d.height, d.icc.is_none()), (1, 1, true));
assert_eq!(super::header(&b).unwrap(), (1, 1, 1));
}
#[test]
+1
View File
@@ -9,6 +9,7 @@ description = "LightCraft engine: session, command registry, history, render job
workspace = true
[dependencies]
craft-pdf-print.workspace = true
craft-mime = { workspace = true }
# Paths stored in a Lightroom catalog are followed only to local files or the catalog's share.
craft-docpath = { workspace = true }
+13 -328
View File
@@ -1,334 +1,30 @@
//! A small PDF writer for print output: pages of a given size in points, each with raster images
//! placed on it, tagged with their ICC profile (`ICCBased`) so the numbers mean the same colour in
//! every reader and RIP, Flate-compressed (lossless) at 8 or 16 bits per component, or carried as
//! JPEG (`DCTDecode`) where small files matter more than exact samples. Optionally an output intent:
//! the printing condition (the printer / paper profile) the file was made for, as PDF/X readers
//! expect it in the catalog. Profiles shared by several images are written once.
//!
//! The file is PDF 1.6 (16-bit images need 1.5): classic cross-reference table, no object streams,
//! deterministic (no dates or IDs), so the same pages always give the same bytes.
//!
//! Sources: ISO 32000-1:2008 (§7.3 objects, §7.5 file structure, §8.6.5.5 ICCBased colour spaces,
//! §8.9 images, §14.11.5 output intents). Own design.
use std::collections::HashMap;
//! Print-page policy and adaptation to shared PDF raster serialization.
//! Layout, rendering, profile choice and job limits stay in the app.
pub use craft_pdf_print::write_raster::print::{ImageData, OutputIntent, PdfImage, PdfPage, Placement};
#[cfg(test)]
use std::sync::Arc;
/// Pages per document: a print job beyond this is a mistake, not a book.
pub const MAX_PAGES: usize = 1000;
/// Images per page.
pub const MAX_IMAGES_PER_PAGE: usize = 256;
/// Largest page side in points (200 inches): more than any printer's roll.
pub const MAX_PAGE_POINTS: f64 = 14_400.0;
/// Largest image, in pixels per side.
pub const MAX_IMAGE_SIDE: u32 = 65_535;
/// How an image's samples are stored.
#[derive(Clone, Debug, PartialEq)]
pub enum ImageData {
/// Interleaved samples, `bits` (8 or 16) per component, 16-bit values big-endian: compressed
/// losslessly in the file.
Raw { bits: u8, samples: Vec<u8> },
/// A baseline JPEG stream, copied into the file as it is.
Jpeg(Vec<u8>),
/// Samples already compressed with zlib (see [`PdfImage::deflated`]), `bits` per component.
Flate { bits: u8, data: Vec<u8> },
}
/// A raster image and the colour space its samples are in.
#[derive(Clone, Debug, PartialEq)]
pub struct PdfImage {
pub width: u32,
pub height: u32,
/// Components per pixel: 1 (grey), 3 (RGB) or 4 (CMYK).
pub channels: u8,
pub data: ImageData,
/// The ICC profile the samples are in; without one the image is device colour.
pub icc: Option<Arc<Vec<u8>>>,
}
impl PdfImage {
/// An image from interleaved 16-bit samples.
pub fn from_u16(width: u32, height: u32, channels: u8, samples: &[u16], icc: Option<Arc<Vec<u8>>>) -> PdfImage {
let bytes = samples.iter().flat_map(|v| v.to_be_bytes()).collect();
PdfImage { width, height, channels, data: ImageData::Raw { bits: 16, samples: bytes }, icc }
}
/// The same image with its samples compressed now (checked first), so a long print doesn't
/// hold every page's uncompressed samples until the file is written.
pub fn deflated(self) -> Result<PdfImage, String> {
self.check()?;
let data = match self.data {
ImageData::Raw { bits, samples } => ImageData::Flate { bits, data: deflate(&samples) },
other => other,
};
Ok(PdfImage { data, ..self })
}
fn check(&self) -> Result<(), String> {
if self.width == 0 || self.height == 0 || self.width > MAX_IMAGE_SIDE || self.height > MAX_IMAGE_SIDE {
return Err(format!("an image of {} × {} pixels can't be written", self.width, self.height));
}
if !matches!(self.channels, 1 | 3 | 4) {
return Err(format!("images have 1, 3 or 4 components, not {}", self.channels));
}
match &self.data {
ImageData::Raw { bits, samples } => {
if !matches!(bits, 8 | 16) {
return Err(format!("images have 8 or 16 bits per component, not {bits}"));
}
let want = u64::from(self.width) * u64::from(self.height) * u64::from(self.channels) * u64::from(bits / 8);
if samples.len() as u64 != want {
return Err(format!("the image needs {want} bytes of samples, not {}", samples.len()));
}
}
ImageData::Jpeg(data) => {
if !data.starts_with(&[0xFF, 0xD8]) {
return Err("the image isn't a JPEG stream".into());
}
}
ImageData::Flate { bits, data } => {
if !matches!(bits, 8 | 16) || data.is_empty() {
return Err("a compressed image has 8 or 16 bits per component and some data".into());
}
}
}
Ok(())
}
}
/// Where an image goes on its page, in points from the page's bottom-left corner.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Placement {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
/// One page: its size in points and the images on it, drawn in order (later ones on top).
#[derive(Clone, Debug, PartialEq)]
pub struct PdfPage {
pub width: f64,
pub height: f64,
pub images: Vec<(PdfImage, Placement)>,
}
/// The printing condition the document is made for (PDF/X-style output intent).
#[derive(Clone, Debug, PartialEq)]
pub struct OutputIntent {
/// The printer / paper profile.
pub icc: Arc<Vec<u8>>,
/// Its components: 1, 3 or 4.
pub channels: u8,
/// A name for the condition (the profile's description).
pub condition: String,
}
/// Document-level settings.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct PdfInfo {
pub title: Option<String>,
pub output_intent: Option<OutputIntent>,
}
fn finite(v: f64) -> bool {
v.is_finite()
}
/// A number as PDF writes it: up to four decimals, no exponent, no trailing zeros.
fn num(v: f64) -> String {
let s = format!("{:.4}", if finite(v) { v } else { 0.0 });
let s = s.trim_end_matches('0').trim_end_matches('.');
if s == "-0" || s.is_empty() { "0".into() } else { s.into() }
}
/// A text string: plain ASCII as a literal string, anything else as UTF-16BE with a byte order mark.
fn text(s: &str) -> String {
if s.bytes().all(|b| (0x20..0x7f).contains(&b)) {
let mut out = String::with_capacity(s.len() + 2);
out.push('(');
for c in s.chars() {
if matches!(c, '(' | ')' | '\\') {
out.push('\\');
}
out.push(c);
}
out.push(')');
out
} else {
let mut out = String::from("<FEFF");
for unit in s.encode_utf16() {
out.push_str(&format!("{unit:04X}"));
}
out.push('>');
out
}
}
struct Writer {
out: Vec<u8>,
offsets: Vec<usize>,
}
impl Writer {
/// Reserve an object number (filled later by [`Writer::object`]).
fn reserve(&mut self) -> usize {
self.offsets.push(0);
self.offsets.len()
}
fn object(&mut self, id: usize, dict: &str, stream: Option<&[u8]>) {
if let Some(slot) = self.offsets.get_mut(id.wrapping_sub(1)) {
*slot = self.out.len();
}
self.out.extend_from_slice(format!("{id} 0 obj\n{dict}").as_bytes());
if let Some(data) = stream {
self.out.extend_from_slice(b"\nstream\n");
self.out.extend_from_slice(data);
self.out.extend_from_slice(b"\nendstream");
}
self.out.extend_from_slice(b"\nendobj\n");
}
}
fn deflate(data: &[u8]) -> Vec<u8> {
miniz_oxide::deflate::compress_to_vec_zlib(data, 6)
}
/// Write a document. Errors name the first page or image that can't be written.
pub fn write(pages: &[PdfPage], info: &PdfInfo) -> Result<Vec<u8>, String> {
if pages.is_empty() {
return Err("there are no pages to write".into());
}
if pages.len() > MAX_PAGES {
return Err(format!("a document holds at most {MAX_PAGES} pages"));
}
for (n, page) in pages.iter().enumerate() {
let n = n + 1;
if !(finite(page.width) && finite(page.height))
|| page.width <= 0.0
|| page.height <= 0.0
|| page.width > MAX_PAGE_POINTS
|| page.height > MAX_PAGE_POINTS
{
return Err(format!("page {n} has no usable size"));
}
if page.images.len() > MAX_IMAGES_PER_PAGE {
return Err(format!("page {n} has more than {MAX_IMAGES_PER_PAGE} images"));
}
for (image, at) in &page.images {
image.check().map_err(|e| format!("page {n}: {e}"))?;
if ![at.x, at.y, at.width, at.height].into_iter().all(finite) || at.width <= 0.0 || at.height <= 0.0 {
return Err(format!("page {n}: an image has no usable placement"));
}
}
}
if let Some(intent) = &info.output_intent
&& !matches!(intent.channels, 1 | 3 | 4)
{
return Err("the output intent's profile has 1, 3 or 4 components".into());
}
let mut w = Writer { out: Vec::new(), offsets: Vec::new() };
w.out.extend_from_slice(b"%PDF-1.6\n%\xE2\xE3\xCF\xD3\n");
let catalog = w.reserve();
let pages_id = w.reserve();
let info_id = w.reserve();
// ICC profiles, each written once (keyed by content) with its component count
let mut profiles: HashMap<Vec<u8>, usize> = HashMap::new();
let mut profile = |w: &mut Writer, icc: &Arc<Vec<u8>>, n: u8| -> usize {
if let Some(&id) = profiles.get(icc.as_slice()) {
return id;
}
let id = w.reserve();
let data = deflate(icc);
let alternate = match n {
1 => "/DeviceGray",
4 => "/DeviceCMYK",
_ => "/DeviceRGB",
};
w.object(id, &format!("<< /N {n} /Alternate {alternate} /Filter /FlateDecode /Length {} >>", data.len()), Some(&data));
profiles.insert(icc.to_vec(), id);
id
};
let intent_id = info.output_intent.as_ref().map(|intent| profile(&mut w, &intent.icc, intent.channels));
let mut kids = Vec::with_capacity(pages.len());
for page in pages {
let page_id = w.reserve();
let mut xobjects = String::new();
let mut content = String::new();
for (k, (image, at)) in page.images.iter().enumerate() {
let space = match &image.icc {
Some(icc) => format!("[/ICCBased {} 0 R]", profile(&mut w, icc, image.channels)),
None => match image.channels {
1 => "/DeviceGray".into(),
4 => "/DeviceCMYK".into(),
_ => "/DeviceRGB".into(),
},
};
let id = w.reserve();
let (bits, filter, data): (u8, &str, std::borrow::Cow<[u8]>) = match &image.data {
ImageData::Raw { bits, samples } => (*bits, "/FlateDecode", deflate(samples).into()),
ImageData::Flate { bits, data } => (*bits, "/FlateDecode", data.as_slice().into()),
ImageData::Jpeg(jpeg) => (8, "/DCTDecode", jpeg.as_slice().into()),
};
w.object(
id,
&format!(
"<< /Type /XObject /Subtype /Image /Width {} /Height {} /ColorSpace {space} /BitsPerComponent {bits} /Filter {filter} /Length {} >>",
image.width,
image.height,
data.len()
),
Some(&data),
);
xobjects.push_str(&format!("/Im{k} {id} 0 R "));
content.push_str(&format!("q {} 0 0 {} {} {} cm /Im{k} Do Q\n", num(at.width), num(at.height), num(at.x), num(at.y)));
}
let content_id = w.reserve();
let stream = deflate(content.as_bytes());
w.object(content_id, &format!("<< /Filter /FlateDecode /Length {} >>", stream.len()), Some(&stream));
w.object(
page_id,
&format!(
"<< /Type /Page /Parent {pages_id} 0 R /MediaBox [0 0 {} {}] /Resources << /XObject << {xobjects}>> >> /Contents {content_id} 0 R >>",
num(page.width),
num(page.height)
),
None,
);
kids.push(format!("{page_id} 0 R"));
}
w.object(pages_id, &format!("<< /Type /Pages /Count {} /Kids [{}] >>", kids.len(), kids.join(" ")), None);
let intents = match (&info.output_intent, intent_id) {
(Some(intent), Some(id)) => format!(
" /OutputIntents [<< /Type /OutputIntent /S /GTS_PDFX /OutputConditionIdentifier (Custom) /Info {} /DestOutputProfile {id} 0 R >>]",
text(&intent.condition)
),
_ => String::new(),
};
w.object(catalog, &format!("<< /Type /Catalog /Pages {pages_id} 0 R{intents} >>"), None);
let title = info.title.as_deref().map(|t| format!(" /Title {}", text(t))).unwrap_or_default();
w.object(info_id, &format!("<< /Producer (LightCraft){title} >>"), None);
let xref = w.out.len();
let mut table = format!("xref\n0 {}\n0000000000 65535 f \n", w.offsets.len() + 1);
for offset in &w.offsets {
table.push_str(&format!("{offset:010} 00000 n \n"));
}
table.push_str(&format!("trailer\n<< /Size {} /Root {catalog} 0 R /Info {info_id} 0 R >>\nstartxref\n{xref}\n%%EOF\n", w.offsets.len() + 1));
w.out.extend_from_slice(table.as_bytes());
Ok(w.out)
craft_pdf_print::write_raster::print::write(
pages,
&craft_pdf_print::write_raster::print::PdfInfo {
title: info.title.clone(),
output_intent: info.output_intent.clone(),
producer: "LightCraft".into(),
},
)
}
#[cfg(test)]
mod tests {
use super::*;
use craft_pdf_print::write_raster::print::MAX_PAGES;
use lightcraft_codecs::NamedSpace;
/// The body of object `id` (dictionary and raw stream bytes), found through the xref table.
@@ -506,15 +202,4 @@ mod tests {
let info = PdfInfo { title: None, output_intent: Some(OutputIntent { icc: srgb(), channels: 2, condition: String::new() }) };
assert!(write(&[page(vec![])], &info).is_err());
}
#[test]
fn numbers_and_strings() {
assert_eq!(num(612.0), "612");
assert_eq!(num(72.5), "72.5");
assert_eq!(num(-0.00001), "0");
assert_eq!(num(f64::NAN), "0");
assert_eq!(num(1.0 / 3.0), "0.3333");
assert_eq!(text("a(b)c\\"), "(a\\(b\\)c\\\\)");
assert_eq!(text("é"), "<FEFF00E9>");
}
}
File diff suppressed because one or more lines are too long
@@ -2,13 +2,14 @@
"""Regenerate cargo-sources.json for the from-source Flatpak manifest.
The upstream flatpak-cargo-generator is downloaded from a pinned commit and
verified before use; the full script is intentionally not vendored here. Two
small, reviewed patches are applied in memory:
verified before use; the full script is intentionally not vendored here. The following
small patches are applied in memory:
1. Key cargo source replacements by the full source URL (including ?rev=...)
instead of only the repository URL, so two different revisions of the same
repository stay distinct.
2. Include Cargo.lock's [patch.unused] entries, so crates such as egui_demo_lib
2. Isolate Git package directories and replacement sources by locked identity.
3. Include Cargo.lock's [patch.unused] entries, so crates such as egui_demo_lib
are vendored even though they are not in the resolved package graph.
This only produces and validates the source inventory; a full Flatpak SDK
@@ -42,7 +43,7 @@ UPSTREAM_URL = (
f"{UPSTREAM_COMMIT}/cargo/flatpak-cargo-generator.py"
)
UPSTREAM_SHA256 = "0a2db6be87d75910facef28ab46d4d6460802e8419ab850d0caa6a364d26b380"
PATCHED_SHA256 = "1e7c61d98d18e88156fb8cd4b91f526e04a6c504b185ae7b12f775ec1dd48512"
PATCHED_SHA256 = "56e06136037ca106a690c3c6d50d5b385462f6567ca63afdd7efb257049efe86"
# Allow cold git caches while bounding stalled inventory regeneration.
GENERATOR_TIMEOUT_SECONDS = 30 * 60
@@ -85,6 +86,48 @@ PATCHES = (
),
)
# Isolate same-name/version Git packages by complete locked source identity.
PATCHES += (
(
'isolate Git directory sources',
""" logging.info("Adding package %s from %s", name, repo_url)
git_pkg = git_repo["commits"][commit][name]
""",
""" # Directory sources cannot distinguish equal name/version packages from
# different Git revisions. Isolate both the files and replacement source.
identity = hashlib.sha256(source.encode("utf-8")).hexdigest()
vendor_name = f"git-vendor-{identity}"
vendor_root = f"{CARGO_HOME}/vendor-git/{identity}"
for entry in cargo_vendored_entry.values():
entry["replace-with"] = vendor_name
cargo_vendored_entry[vendor_name] = {"directory": vendor_root}
logging.info("Adding package %s from %s", name, repo_url)
git_pkg = git_repo["commits"][commit][name]
""",
),
(
'create isolated Git package directory',
""" f'cp -r --reflink=auto "{pkg_repo_dir}" "{CARGO_CRATES}/{name}"'""",
""" f'mkdir -p "{vendor_root}" && '
f'cp -r --reflink=auto "{pkg_repo_dir}" "{vendor_root}/{name}"'""",
),
(
'isolate Git Cargo.toml destination',
""" "dest": f"{CARGO_CRATES}/{name}", # -{version}',
"dest-filename": "Cargo.toml",""",
""" "dest": f"{vendor_root}/{name}",
"dest-filename": "Cargo.toml",""",
),
(
'isolate Git .cargo-checksum.json destination',
""" "dest": f"{CARGO_CRATES}/{name}", # -{version}',
"dest-filename": ".cargo-checksum.json",""",
""" "dest": f"{vendor_root}/{name}",
"dest-filename": ".cargo-checksum.json",""",
),
)
def sha256(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
@@ -479,5 +479,49 @@ class InventoryFailures(unittest.TestCase):
self.assertEqual(list(root.glob('.sources.json.*.tmp')), [])
class GitSourceIsolationTests(unittest.TestCase):
"""Exercise the identity block inserted into the hash-pinned generator."""
def identity(self, source, name='same-crate'):
import logging
import textwrap
from urllib.parse import urlparse
repo = 'https://github.com/example/formats'
commit = urlparse(source).fragment
key = source.removeprefix('git+').split('#', 1)[0]
scope = {
'hashlib': hashlib, 'source': source, 'CARGO_HOME': 'cargo',
'cargo_vendored_entry': {key: {'git': repo, 'replace-with': 'vendored-sources'}},
'logging': logging, 'name': name, 'repo_url': repo, 'commit': commit,
'git_repo': {'commits': {commit: {name: object()}}},
}
block = next(new for label, _, new in wrapper.PATCHES if label == 'isolate Git directory sources')
exec(textwrap.dedent(block), scope)
mapping = scope['cargo_vendored_entry']
replacement = mapping[key]['replace-with']
self.assertEqual(mapping[replacement]['directory'], scope['vendor_root'])
self.assertNotEqual(replacement, 'vendored-sources')
return scope['vendor_root'], replacement
def test_same_package_and_version_at_two_revisions_are_isolated(self):
prefix = 'git+https://github.com/example/formats?rev='
first = self.identity(prefix + 'a' * 40 + '#' + 'a' * 40)
second = self.identity(prefix + 'b' * 40 + '#' + 'b' * 40)
self.assertNotEqual(first[0], second[0])
self.assertNotEqual(first[1], second[1])
self.assertTrue(first[0].startswith('cargo/vendor-git/'))
def test_packages_from_one_locked_source_share_one_root(self):
source = 'git+https://github.com/example/formats?rev=' + 'a' * 40 + '#' + 'a' * 40
self.assertEqual(self.identity(source, 'metadata'), self.identity(source, 'tiff'))
def test_distinct_source_aliases_do_not_repeat_copy_into_same_directory(self):
commit = 'a' * 40
first = self.identity('git+https://github.com/example/formats?rev=' + commit + '#' + commit)
second = self.identity('git+https://github.com/example/formats?tag=release#' + commit)
self.assertNotEqual(first, second)
if __name__ == '__main__':
unittest.main(verbosity=2)