From a58e45b11ad7dab9ee2b8703f86170bae713b602 Mon Sep 17 00:00:00 2001 From: Kornel Date: Tue, 6 Jul 2021 15:02:04 +0100 Subject: [PATCH] Try not to duplicate license file metadata #187 --- src/data.rs | 30 +++++++++++++++++++++++------- tests/command.rs | 4 ++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/data.rs b/src/data.rs index 1a71a19..9a8d366 100644 --- a/src/data.rs +++ b/src/data.rs @@ -35,20 +35,29 @@ pub(crate) fn generate_changelog_asset(options: &Config) -> CDResult CDResult> { - let mut copyright: Vec = Vec::new(); - writeln!(&mut copyright, "Upstream Name: {}", options.name)?; +fn append_copyright_metadata(copyright: &mut Vec, options: &Config) -> Result<(), CargoDebError> { + writeln!(copyright, "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/")?; + writeln!(copyright, "Upstream-Name: {}", options.name)?; if let Some(source) = options.repository.as_ref().or(options.homepage.as_ref()) { - writeln!(&mut copyright, "Source: {}", source)?; + writeln!(copyright, "Source: {}", source)?; } - writeln!(&mut copyright, "Copyright: {}", options.copyright)?; + writeln!(copyright, "Copyright: {}", options.copyright)?; if let Some(ref license) = options.license { - writeln!(&mut copyright, "License: {}", license)?; + writeln!(copyright, "License: {}", license)?; } + Ok(()) +} + +/// Generates the copyright file from the license file and adds that to the tar archive. +pub(crate) fn generate_copyright_asset(options: &Config) -> CDResult> { + let mut copyright: Vec = Vec::new(); if let Some(ref path) = options.license_file { let license_string = fs::read_to_string(options.path_in_workspace(path)) .map_err(|e| CargoDebError::IoFile("unable to read license file", e, path.to_owned()))?; + if !has_copyright_metadata(&license_string) { + append_copyright_metadata(&mut copyright, options)?; + } + // Skip the first `A` number of lines and then iterate each line after that. for line in license_string.lines().skip(options.license_file_skip_lines) { // If the line is a space, add a dot, else write the line. @@ -59,12 +68,19 @@ pub(crate) fn generate_copyright_asset(options: &Config) -> CDResult> { copyright.write_all(b"\n")?; } } + } else { + append_copyright_metadata(&mut copyright, options)?; } // Write a copy to the disk for the sake of obtaining a md5sum for the control archive. Ok(copyright) } +fn has_copyright_metadata(file: &str) -> bool { + file.lines().take(10) + .any(|l| l.starts_with("License: ") || l.starts_with("Source: ") || l.starts_with("Upstream-Name: ") || l.starts_with("Format: ")) +} + /// Compress man page assets per Debian Policy. /// /// # References diff --git a/tests/command.rs b/tests/command.rs index d0575ec..9113214 100644 --- a/tests/command.rs +++ b/tests/command.rs @@ -137,7 +137,7 @@ fn run_cargo_deb_command_on_example_dir_with_variant() { assert!(md5sums.contains("b1946ac92492d2347c6235b4d2611184 var/lib/example/1.txt\n")); assert!(md5sums.contains("591785b794601e212b260e25925636fd var/lib/example/2.txt\n")); assert!(md5sums.contains("835a3c46f2330925774ebf780aa74241 var/lib/example/4.txt\n")); - assert!(md5sums.contains("f4b165c5ea1f9ec1b87abd72845627fd usr/share/doc/example-debug/copyright\n")); + assert!(md5sums.contains("2455967cef930e647146a8c762199ed3 usr/share/doc/example-debug/copyright\n")); let ddir = TempDir::new("cargo-data-test").unwrap(); assert!(Command::new("tar") @@ -210,7 +210,7 @@ fn run_cargo_deb_command_on_example_dir_with_version() { assert!(md5sums.contains("b1946ac92492d2347c6235b4d2611184 var/lib/example/1.txt\n")); assert!(md5sums.contains("591785b794601e212b260e25925636fd var/lib/example/2.txt\n")); assert!(md5sums.contains("1537684900f6b12358c88a612adf1049 var/lib/example/3.txt\n")); - assert!(md5sums.contains("4176f128e63dbe2f7ba37490bd0368db usr/share/doc/example/copyright\n")); + assert!(md5sums.contains("4176f128e63dbe2f7ba37490bd0368db usr/share/doc/example/copyright\n"), "has:\n{}", md5sums); let ddir = TempDir::new("cargo-data-test").unwrap(); assert!(Command::new("tar")