forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
named_caches.rs
99 lines (87 loc) · 2.63 KB
/
named_caches.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::collections::BTreeMap;
use std::path::PathBuf;
use fs::default_cache_path;
use crate::RelativePath;
#[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct CacheName(String);
impl CacheName {
pub fn new(name: String) -> Result<CacheName, String> {
if name
.chars()
.all(|c| (c.is_ascii_alphanumeric() && c.is_ascii_lowercase()) || c == '_')
{
Ok(CacheName(name))
} else {
Err(format!(
"Cache names may only contain lowercase alphanumeric characters or underscores: got {:?}",
name
))
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct CacheDest(String);
impl CacheDest {
pub fn new(dest: String) -> Result<CacheDest, String> {
// We validate as RelativePath, but store as a String to avoid needing to assert later that
// path is valid unicode.
let _ = RelativePath::new(&dest)?;
Ok(CacheDest(dest))
}
}
#[derive(Debug)]
pub struct NamedCacheSymlink {
pub src: PathBuf,
pub dst: PathBuf,
}
#[derive(Clone)]
pub struct NamedCaches {
///
/// The absolute path to the base of the directory storing named caches. Pants "owns" this
/// directory, and may clear or otherwise prune it at any time.
///
local_base: PathBuf,
}
impl NamedCaches {
pub fn new(local_base: PathBuf) -> NamedCaches {
NamedCaches { local_base }
}
// This default suffix is also hard-coded into the Python options code in global_options.py
pub fn default_path() -> PathBuf {
default_cache_path().join("named_caches")
}
///
/// Returns symlinks to create for the given set of NamedCaches.
///
pub fn local_paths<'a>(
&'a self,
caches: &'a BTreeMap<CacheName, CacheDest>,
) -> impl Iterator<Item = NamedCacheSymlink> + 'a {
caches
.iter()
.map(move |(cache_name, cache_dest)| NamedCacheSymlink {
src: self.local_base.join(&cache_name.0),
dst: PathBuf::from(&cache_dest.0),
})
}
///
/// An iterator over platform properties that should be added for the given named caches, and the
/// given named cache namespace value.
///
/// See https://docs.google.com/document/d/1n_MVVGjrkTKTPKHqRPlyfFzQyx2QioclMG_Q3DMUgYk/edit#.
///
pub fn platform_properties<'a>(
caches: &'a BTreeMap<CacheName, CacheDest>,
namespace: &'a Option<String>,
) -> impl Iterator<Item = (String, String)> + 'a {
namespace
.iter()
.map(|ns| ("x_append_only_cache_namespace".to_owned(), ns.to_owned()))
.chain(caches.iter().map(move |(cache_name, cache_dest)| {
(
format!("x_append_only_cache:{}", cache_name.0),
cache_dest.0.clone(),
)
}))
}
}