forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: Create dep inference request type (pantsbuild#19001)
Boilerplate to introduce a request type for native dependency inference suggested here: pantsbuild#18985 (comment) Adds a "metadata" field to the CacheKey type. Is it naive to store the string as is in the key? Afaict, the `PersistentCache` is addressed by the hash of the bytes of the key, not the key bytes themselves -> Should be fine. --------- Co-authored-by: Joshua Cannon <[email protected]>
- Loading branch information
1 parent
0584653
commit 5becc13
Showing
12 changed files
with
256 additions
and
19 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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
from pants.engine.internals.native_engine import ( | ||
EMPTY_DIGEST, | ||
InferenceMetadata, | ||
NativeDependenciesRequest, | ||
) | ||
|
||
|
||
def test_can_construct_javascript_metadata() -> None: | ||
InferenceMetadata.javascript( | ||
package_root="some/dir", import_patterns={"a-pattern-*": ["replaces-me-*"]} | ||
) | ||
|
||
|
||
def test_can_construct_native_dependencies_request() -> None: | ||
NativeDependenciesRequest(EMPTY_DIGEST, None) | ||
NativeDependenciesRequest( | ||
EMPTY_DIGEST, InferenceMetadata.javascript(package_root="some/dir", import_patterns={}) | ||
) |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2023 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
use std::hash::{Hash, Hasher}; | ||
|
||
use crate::gen::pants::cache::dependency_inference_request::Metadata; | ||
use crate::gen::pants::cache::JavascriptInferenceMetadata; | ||
|
||
impl Hash for JavascriptInferenceMetadata { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
self.package_root.hash(state); | ||
for pattern in &self.import_patterns { | ||
pattern.pattern.hash(state); | ||
pattern.replacements.hash(state); | ||
} | ||
} | ||
} | ||
|
||
impl Hash for Metadata { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
match self { | ||
Metadata::Js(m) => m.hash(state), | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2023 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
use std::collections::hash_map::DefaultHasher; | ||
use std::hash::{Hash, Hasher}; | ||
|
||
use pyo3::basic::CompareOp; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyDict; | ||
use pyo3::{IntoPy, PyObject, Python}; | ||
|
||
use fs::DirectoryDigest; | ||
use protos::gen::pants::cache::{ | ||
dependency_inference_request, javascript_inference_metadata, JavascriptInferenceMetadata, | ||
}; | ||
|
||
use crate::externs::fs::PyDigest; | ||
|
||
pub(crate) fn register(m: &PyModule) -> PyResult<()> { | ||
m.add_class::<PyNativeDependenciesRequest>()?; | ||
m.add_class::<PyInferenceMetadata>() | ||
} | ||
|
||
#[pyclass(name = "InferenceMetadata")] | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct PyInferenceMetadata(pub dependency_inference_request::Metadata); | ||
|
||
#[pymethods] | ||
impl PyInferenceMetadata { | ||
#[staticmethod] | ||
fn javascript(package_root: String, import_patterns: &PyDict) -> PyResult<Self> { | ||
use javascript_inference_metadata::ImportPattern; | ||
let import_patterns: PyResult<Vec<ImportPattern>> = import_patterns | ||
.iter() | ||
.map(|(key, value)| { | ||
Ok(ImportPattern { | ||
pattern: key.extract()?, | ||
replacements: value.extract()?, | ||
}) | ||
}) | ||
.collect(); | ||
Ok(Self(dependency_inference_request::Metadata::Js( | ||
JavascriptInferenceMetadata { | ||
package_root, | ||
import_patterns: import_patterns?, | ||
}, | ||
))) | ||
} | ||
|
||
fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python) -> PyObject { | ||
match op { | ||
CompareOp::Eq => (self == other).into_py(py), | ||
CompareOp::Ne => (self != other).into_py(py), | ||
_ => py.NotImplemented(), | ||
} | ||
} | ||
|
||
fn __repr__(&self) -> String { | ||
format!("InferenceMetadata({:?})", self.0) | ||
} | ||
|
||
fn __hash__(&self) -> u64 { | ||
let mut s = DefaultHasher::new(); | ||
self.0.hash(&mut s); | ||
s.finish() | ||
} | ||
} | ||
|
||
#[pyclass(name = "NativeDependenciesRequest")] | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct PyNativeDependenciesRequest { | ||
pub directory_digest: DirectoryDigest, | ||
pub metadata: Option<dependency_inference_request::Metadata>, | ||
} | ||
|
||
#[pymethods] | ||
impl PyNativeDependenciesRequest { | ||
#[new] | ||
fn __new__(digest: PyDigest, metadata: Option<PyInferenceMetadata>) -> Self { | ||
Self { | ||
directory_digest: digest.0, | ||
metadata: metadata.map(|inner| inner.0), | ||
} | ||
} | ||
|
||
fn __hash__(&self) -> u64 { | ||
let mut s = DefaultHasher::new(); | ||
self.directory_digest.hash(&mut s); | ||
self.metadata.hash(&mut s); | ||
s.finish() | ||
} | ||
|
||
fn __repr__(&self) -> String { | ||
format!( | ||
"NativeDependenciesRequest('{}', {:?})", | ||
PyDigest(self.directory_digest.clone()), | ||
self.metadata | ||
) | ||
} | ||
|
||
fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python) -> PyObject { | ||
match op { | ||
CompareOp::Eq => (self == other).into_py(py), | ||
CompareOp::Ne => (self != other).into_py(py), | ||
_ => py.NotImplemented(), | ||
} | ||
} | ||
} |
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
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