Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Commit

Permalink
Try not to duplicate license file metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jul 6, 2021
1 parent 105e9cd commit a58e45b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
30 changes: 23 additions & 7 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,29 @@ pub(crate) fn generate_changelog_asset(options: &Config) -> CDResult<Option<Vec<
}
}

/// Generates the copyright file from the license file and adds that to the tar archive.
pub(crate) fn generate_copyright_asset(options: &Config) -> CDResult<Vec<u8>> {
let mut copyright: Vec<u8> = Vec::new();
writeln!(&mut copyright, "Upstream Name: {}", options.name)?;
fn append_copyright_metadata(copyright: &mut Vec<u8>, 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<Vec<u8>> {
let mut copyright: Vec<u8> = 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.
Expand All @@ -59,12 +68,19 @@ pub(crate) fn generate_copyright_asset(options: &Config) -> CDResult<Vec<u8>> {
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
Expand Down
4 changes: 2 additions & 2 deletions tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit a58e45b

Please sign in to comment.