Skip to content

Commit

Permalink
Hook up pyc-zero-mtime handler
Browse files Browse the repository at this point in the history
Usage: add-determinism --handler pyc-zero-mtime /some/path
  • Loading branch information
keszybz committed Jul 17, 2024
1 parent 8357059 commit 5ffca30
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ and cleans up unused "flag references".
It is a Rust reimplementation of
the [MarshalParser Python module](https://github.com/fedora-python/marshalparser).

### `pyc-zero-mtime`

Accepts `*.pyc`.

This handler sets the internal timestamp in `.pyc` file header to 0,
and sets the mtime on the corresponding source `.py` file to 0.
This is intended to be used on [OSTree](https://github.com/ostreedev/ostree)
systems where mtimes are discarded,
causing a mismatch between the timestamp embedded in the `.pyc` file
and the filesystem metadata of the `.py` file.

This handler is not enabled by default and must be explicitly requested
via `--handlers pyc-zero-mtime`.

## Notes

This project is inspired by
Expand Down
15 changes: 8 additions & 7 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,24 @@ impl Stats {

pub type HandlerBoxed = fn(&Rc<options::Config>) -> Box<dyn Processor>;

pub const HANDLERS: &[(&str, HandlerBoxed)] = &[
("ar", ar::Ar::boxed),
("jar", jar::Jar::boxed),
("javadoc", javadoc::Javadoc::boxed),
("pyc", pyc::Pyc::boxed),
pub const HANDLERS: &[(&str, bool, HandlerBoxed)] = &[
("ar", true, ar::Ar::boxed ),
("jar", true, jar::Jar::boxed ),
("javadoc", true, javadoc::Javadoc::boxed ),
("pyc", true, pyc::Pyc::boxed ),
("pyc-zero-mtime", false, pyc::PycZeroMtime::boxed),
];

pub fn handler_names() -> Vec<&'static str> {
HANDLERS.iter()
.map(|(name, _)| *name)
.map(|(name, _, _)| *name)
.collect()
}

pub fn make_handlers(config: &Rc<options::Config>) -> Result<Vec<Box<dyn Processor>>> {
let mut handlers: Vec<Box<dyn Processor>> = vec![];

for (name, func) in HANDLERS {
for (name, _, func) in HANDLERS {
if config.handler_names.contains(name) {
let mut handler = func(config);
match handler.initialize() {
Expand Down
39 changes: 30 additions & 9 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct Config {
pub strict_handlers: bool,
}

fn filter_by_name(name: &str, filter: &[&str]) -> bool {
fn filter_by_name(name: &str, enabled_by_default: bool, filter: &[&str]) -> bool {
let mut negative_filter = true;

for f in filter.iter().rev() {
Expand All @@ -90,7 +90,7 @@ fn filter_by_name(name: &str, filter: &[&str]) -> bool {
}
}

negative_filter
enabled_by_default && negative_filter
}

pub fn requested_handlers(filter: &[&str]) -> Result<(Vec<&'static str>, bool)> {
Expand All @@ -109,8 +109,8 @@ pub fn requested_handlers(filter: &[&str]) -> Result<(Vec<&'static str>, bool)>

let list: Vec<&'static str> = handlers::HANDLERS
.iter()
.filter(|(name, _)| filter_by_name(name, filter))
.map(|(name, _)| *name)
.filter(|(name, enabled_by_default, _)| filter_by_name(name, *enabled_by_default, filter))
.map(|(name, _, _)| *name)
.collect();

if list.is_empty() {
Expand Down Expand Up @@ -213,10 +213,31 @@ mod tests {

#[test]
fn test_filter_by_name() {
assert_eq!(filter_by_name("x", &vec!["x", "y"]), true);
assert_eq!(filter_by_name("x", &vec!["x"]), true);
assert_eq!(filter_by_name("x", &vec![]), true);
assert_eq!(filter_by_name("x", &vec!["-x"]), false);
assert_eq!(filter_by_name("x", &vec!["-y"]), true);
assert_eq!(filter_by_name("x", true, &vec!["x", "y"]), true);
assert_eq!(filter_by_name("x", true, &vec!["x"]), true);
assert_eq!(filter_by_name("x", true, &vec![]), true);
assert_eq!(filter_by_name("x", true, &vec!["-x"]), false);
assert_eq!(filter_by_name("x", true, &vec!["-y"]), true);

assert_eq!(filter_by_name("x", false, &vec!["x", "y"]), true);
assert_eq!(filter_by_name("x", false, &vec!["x"]), true);
assert_eq!(filter_by_name("x", false, &vec![]), false);
assert_eq!(filter_by_name("x", false, &vec!["-x"]), false);
assert_eq!(filter_by_name("x", false, &vec!["-y"]), false);
}

#[test]
fn test_requested_handlers() {
let (list, strict) = requested_handlers(&vec![]).unwrap();
assert_eq!(list, vec!["ar", "jar", "javadoc", "pyc"]);
assert_eq!(strict, false);

let (list, strict) = requested_handlers(&vec!["ar", "pyc-zero-mtime"]).unwrap();
assert_eq!(list, vec!["ar", "pyc-zero-mtime"]);
assert_eq!(strict, true);

let (list, strict) = requested_handlers(&vec!["-pyc-zero-mtime"]).unwrap();
assert_eq!(list, vec!["ar", "jar", "javadoc", "pyc"]);
assert_eq!(strict, true);
}
}

0 comments on commit 5ffca30

Please sign in to comment.