-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "pyc-zero-mtime" handler to set embedded mtime in pyc to 0
Pyc files store the mtime, size, and optionally hash of the original py file. Mtime is used to validate the cached bytecode: the mtime of the .py file (if present) must be equal to the stored value in the .pyc file. If not, the pyc file is considered stale. On ostree systems, all mtimes are set to 0. The problem is that the .py file is created first, then the byte compilation step creates .pyc with some embedded timestamp, and later ostree discards mtimes on all files. On the end system, the bytecode cache is considered invalid. This new handler does the two very simple things: 1. zero out the mtime in the .pyc file 2. set mtime (in the filesystem) to zero for the .py file This second step is also done by ostree, so strictly speaking, it's not necessary. But it's easy to do it, and this way the bytecode still matches the file on disk, so if we called Python after this operation, it would be able to use the bytecode and wouldn't try to rewrite it. Replaces #26. See ostreedev/ostree#1469, https://gitlab.com/fedora/bootc/tracker/-/issues/3, https://pagure.io/workstation-ostree-config/pull-request/505.
- Loading branch information
Showing
3 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod test_ar; | ||
mod test_javadoc; | ||
mod test_pyc; | ||
mod test_pyc_zero_mtime; | ||
|
||
use anyhow::Result; | ||
use std::fs; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* SPDX-License-Identifier: GPL-3.0-or-later */ | ||
|
||
use std::fs::File; | ||
use std::os::linux::fs::MetadataExt; | ||
use std::time; | ||
|
||
use add_determinism::handlers; | ||
use add_determinism::handlers::pyc; | ||
|
||
use super::{prepare_dir, make_handler}; | ||
|
||
#[test] | ||
fn test_adapters() { | ||
let (dir, input) = prepare_dir("tests/cases/adapters.cpython-312.pyc").unwrap(); | ||
|
||
// We take the lazy step of creating an empty .py file here. | ||
// This is enough to be able to adjust the mtime. | ||
let py_file = File::options() | ||
.read(true) | ||
.write(true) | ||
.create_new(true) | ||
.open(dir.path().join("adapters.py")) | ||
.unwrap(); | ||
|
||
let pyc = make_handler(111, false, pyc::PycZeroMtime::boxed).unwrap(); | ||
|
||
assert!(pyc.filter(&*input).unwrap()); | ||
|
||
let orig = input.metadata().unwrap(); | ||
|
||
assert_eq!(pyc.process(&*input).unwrap(), handlers::ProcessResult::Replaced); | ||
|
||
let new = input.metadata().unwrap(); | ||
// because of timestamp granularity, creation ts might be equal | ||
assert!(orig.created().unwrap() <= new.created().unwrap()); | ||
assert_eq!(orig.modified().unwrap(), new.modified().unwrap()); | ||
assert_ne!(orig.st_ino(), new.st_ino()); | ||
|
||
let new2 = py_file.metadata().unwrap(); | ||
assert_eq!(new2.modified().unwrap(), time::UNIX_EPOCH); | ||
} |