Skip to content

Commit c10ddc5

Browse files
committed
Escape regex characters when matching apt repo lines.
This fixes an issue where the regex match fails because `[]` are included in the apt repo source.
1 parent 006a9a5 commit c10ddc5

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

pyinfra/operations/apt.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,25 @@ def repo(src, present=True, filename=None, state=None, host=None):
143143

144144
# Doesn't exist and we want it
145145
if not is_present and present:
146-
yield files.line(filename, src, state=state, host=host)
146+
yield files.line(
147+
filename,
148+
src,
149+
escape_regex_characters=True,
150+
state=state,
151+
host=host,
152+
)
147153
apt_sources.append(repo)
148154

149155
# Exists and we don't want it
150156
elif is_present and not present:
151157
yield files.line(
152-
filename, src,
158+
filename,
159+
src,
153160
present=False,
154161
assume_present=True,
155-
state=state, host=host,
162+
escape_regex_characters=True,
163+
state=state,
164+
host=host,
156165
)
157166
apt_sources.remove(repo)
158167

tests/operations/apt.repo/add.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"facts": {
44
"apt.AptSources": [],
55
"files.FindInFile": {
6-
"path=/etc/apt/sources.list, pattern=^.*deb http://archive.canonical.com/ubuntu hardy partner.*$, interpolate_variables=False": []
6+
"path=/etc/apt/sources.list, pattern=^.*deb http://archive\\.canonical\\.com/ubuntu hardy partner.*$, interpolate_variables=False": []
77
}
88
},
99
"commands": [

tests/operations/apt.repo/add_filename.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"facts": {
77
"apt.AptSources": [],
88
"files.FindInFile": {
9-
"path=/etc/apt/sources.list.d/somefile.list, pattern=^.*deb http://archive.canonical.com/ubuntu hardy partner.*$, interpolate_variables=False": []
9+
"path=/etc/apt/sources.list.d/somefile.list, pattern=^.*deb http://archive\\.canonical\\.com/ubuntu hardy partner.*$, interpolate_variables=False": []
1010
}
1111
},
1212
"commands": [

tests/operations/apt.repo/remove.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
}]
1414
},
1515
"commands": [
16-
"sed -i.a-timestamp 's/^.*deb http:\\/\\/archive.canonical.com\\/ubuntu hardy partner.*$//' /etc/apt/sources.list && rm -f /etc/apt/sources.list.a-timestamp"
16+
"sed -i.a-timestamp 's/^.*deb http:\\/\\/archive\\.canonical\\.com\\/ubuntu hardy partner.*$//' /etc/apt/sources.list && rm -f /etc/apt/sources.list.a-timestamp"
1717
]
1818
}

0 commit comments

Comments
 (0)