-
Notifications
You must be signed in to change notification settings - Fork 2
/
regex_process.rs
212 lines (192 loc) · 7.5 KB
/
regex_process.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::str::FromStr;
use regex::Regex;
// Submit an issue if you find this out-of-date!
// And assuming that all vars are distro_ver
const REGEX_REPLACEMENTS: &[(&str, &str)] = &[
// https://endoflife.date/debian
("${DEBIAN_CURRENT}", "(?<distro_ver>bullseye|bookworm)"),
// https://endoflife.date/ubuntu (excluding ESM)
("${UBUNTU_LTS}", "(?<distro_ver>focal|jammy|noble)"),
("${UBUNTU_NONLTS}", "(?<distro_ver>oracular)"),
// https://endoflife.date/fedora
("${FEDORA_CURRENT}", "(?<distro_ver>39|40|41)"),
// CentOS is no longer supported -- this regex is replaced to something that could match nothing
(
"${CENTOS_CURRENT}",
"(?<distro_ver>NONEXISTFILENAMESOITCOULDNEVERMATCHANYTHING)",
),
// https://endoflife.date/rhel (excluding ELCS)
("${RHEL_CURRENT}", "(?<distro_ver>8|9)"),
// https://endoflife.date/opensuse
("${OPENSUSE_CURRENT}", "(?<distro_ver>15.5|15.6)"),
// https://endoflife.date/sles
("${SLES_CURRENT}", "(?<distro_ver>12|15)"),
];
/// ExpandedRegex contains inner and rev_inner, and would transparently add '/' before string
/// (and convert regex with ^). A warning would be given if text input contains '/' at front.
#[derive(Debug, Clone)]
pub struct ExpandedRegex {
inner: Regex,
rev_inner: Regex,
}
impl FromStr for ExpandedRegex {
type Err = regex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// If starts with ^ and not ^/, change start matching character from ^ to ^/
let s = if s.starts_with('^') && !s.starts_with("^/") {
&format!("^/{}", &s[1..])
} else {
s
};
let mut s1 = s.to_string();
for (from, to) in REGEX_REPLACEMENTS {
s1 = s1.replace(from, to);
}
let mut s2 = s.to_string();
for (from, _) in REGEX_REPLACEMENTS.iter().rev() {
s2 = s2.replace(from, "(?<distro_ver>.+)");
}
Ok(Self {
inner: Regex::new(&s1)?,
rev_inner: Regex::new(&s2)?,
})
}
}
// Delegate to inner
impl ExpandedRegex {
fn text_transform(text: &str) -> String {
if !text.starts_with('/') {
tracing::warn!("(unexpected internal input: string given to match_str shall start with /, anything wrong?)");
format!("/{}", text)
} else {
text.to_string()
}
}
pub fn is_match(&self, text: &str) -> bool {
self.inner.is_match(&Self::text_transform(text))
}
pub fn is_others_match(&self, text: &str) -> bool {
let text = &Self::text_transform(text);
!self.inner.is_match(text) && self.rev_inner.is_match(text)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Comparison {
Stop,
ListOnly,
Ok,
}
#[derive(Debug, Clone)]
pub struct ExclusionManager {
/// Stop the task immediately if any of these regexes match.
instant_stop_regexes: Vec<ExpandedRegex>,
/// Continue, but don't download anything if any of these regexes match.
list_only_regexes: Vec<ExpandedRegex>,
/// Include only these regexes.
include_regexes: Vec<ExpandedRegex>,
}
impl ExclusionManager {
pub fn new(exclusions: &Vec<ExpandedRegex>, inclusions: &Vec<ExpandedRegex>) -> Self {
let mut instant_stop_regexes = Vec::new();
let mut list_only_regexes = Vec::new();
for exclusion in exclusions {
let regex_str = exclusion.inner.as_str();
let mut flag = false;
for inclusion in inclusions {
if inclusion.inner.as_str().starts_with(regex_str) {
list_only_regexes.push(exclusion.clone());
flag = true;
break;
}
}
if !flag {
instant_stop_regexes.push(exclusion.clone());
}
}
Self {
instant_stop_regexes,
list_only_regexes,
include_regexes: inclusions.clone(),
}
}
pub fn match_str(&self, text: &str) -> Comparison {
for regex in &self.instant_stop_regexes {
if regex.is_match(text) {
return Comparison::Stop;
}
}
for regex in &self.include_regexes {
if regex.is_match(text) {
return Comparison::Ok;
}
}
// Performance: it is possible that a regex for inclusion shown like this:
// ^fedora/${FEDORA_CURRENT}
// And the remote corresponding folder has a lot of subfolders.
// This is a "shortcut" to avoid checking all subfolders.
for regex in &self.include_regexes {
if regex.is_others_match(text) {
return Comparison::Stop;
}
}
for regex in &self.list_only_regexes {
if regex.is_match(text) {
return Comparison::ListOnly;
}
}
Comparison::Ok
}
}
#[cfg(test)]
mod tests {
use test_log::test;
use tracing::debug;
use super::*;
#[test]
fn test_expanded_regex() {
let regex = ExpandedRegex::from_str("^/deb/dists/${DEBIAN_CURRENT}").unwrap();
assert!(regex.is_match("/deb/dists/bookworm/Release"));
assert!(!regex.is_match("/deb/dists/wheezy/Release"));
}
#[test]
fn test_exclusion() {
let target =
"/debian/pmg/dists/stretch/pmgtest/binary-amd64/grub-efi-amd64-bin_2.02-pve6.changelog";
let exclusions =
vec![ExpandedRegex::from_str("pmg/dists/.+/pmgtest/.+changelog$").unwrap()];
let inclusions = vec![];
let exclusion_manager = ExclusionManager::new(&exclusions, &inclusions);
assert_eq!(exclusion_manager.match_str(target), Comparison::Stop);
}
#[test]
fn test_partial() {
let target1 = "/yum/mysql-tools-community/fc/24/x86_64";
let target2 = "/yum/mysql-tools-community/fc/40/x86_64";
let target3 = "/yum/mysql-tools-community/fc/";
let target4 = "/yum/mysql-tools-community/fc/24/";
let target5 = "/yum/mysql-tools-community/fc/40/";
let exclusions = vec![ExpandedRegex::from_str("/fc/").unwrap()];
let inclusions = vec![ExpandedRegex::from_str("/fc/${FEDORA_CURRENT}").unwrap()];
debug!("exclusions: {:?}", exclusions);
debug!("inclusions: {:?}", inclusions);
let exclusion_manager = ExclusionManager::new(&exclusions, &inclusions);
assert_eq!(exclusion_manager.match_str(target1), Comparison::Stop);
assert_eq!(exclusion_manager.match_str(target2), Comparison::Ok);
assert_eq!(exclusion_manager.match_str(target3), Comparison::ListOnly);
assert_eq!(exclusion_manager.match_str(target4), Comparison::Stop);
assert_eq!(exclusion_manager.match_str(target5), Comparison::Ok);
}
#[test]
fn test_exclude_dbg() {
let target1 = "/yum/mysql-8.0-community/docker/el/8/aarch64/mysql-community-server-minimal-8.0.33-1.el8.aarch64.rpm";
let target2 = "/yum/mysql-8.0-community/docker/el/8/debuginfo/x86_64/mysql-community-server-minimal-debuginfo-8.0.24-1.el8.x86_64.rpm";
let exclusions = vec![
ExpandedRegex::from_str("/el/").unwrap(),
ExpandedRegex::from_str("debuginfo").unwrap(),
];
let inclusions = vec![ExpandedRegex::from_str("/el/${RHEL_CURRENT}").unwrap()];
let exclusion_manager = ExclusionManager::new(&exclusions, &inclusions);
assert_eq!(exclusion_manager.match_str(target1), Comparison::Ok);
assert_eq!(exclusion_manager.match_str(target2), Comparison::Stop);
}
}