Skip to content

Commit 19f3f3f

Browse files
committed
Fix python::pip installing $editable VCS packages every puppet run
This is due to VCS packages appearing in `pip freeze --all` as: -e git://example.com/package.git@<ref>#egg=<egg> Updates $grep_regex, which checks whether a given version is installed, to match against the two different output formats of `pip list`. No longer relies on pip freeze. Additionally, this commit: - Removes the multiple exec resources which were mostly the same and replaces with variables $pip_exec_name, $command, and $unless_command, which get used in a single exec resource at the end of the manifest. - Checks if $pkgname contains ==, and if so splits based on == and sets $real_pkgname to the first part, and $_ensure to the second part. Uses of $pkgname have been replaced with $real_pkgname. Fixes voxpupuli#193
1 parent 374abf8 commit 19f3f3f

1 file changed

Lines changed: 49 additions & 81 deletions

File tree

manifests/pip.pp

Lines changed: 49 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,23 @@
135135
fail('python::pip cannot provide uninstall_args with ensure => present')
136136
}
137137

138+
if $pkgname =~ /==/ {
139+
$parts = split($pkgname, '==')
140+
$real_pkgname = $parts[0]
141+
$_ensure = $ensure ? {
142+
'absent' => 'absent',
143+
default => $parts[1],
144+
}
145+
} else {
146+
$real_pkgname = $pkgname
147+
$_ensure = $ensure
148+
}
149+
138150
# Check if searching by explicit version.
139-
if $ensure =~ /^((19|20)[0-9][0-9]-(0[1-9]|1[1-2])-([0-2][1-9]|3[0-1])|[0-9]+\.\w+\+?\w*(\.\w+)*)$/ {
140-
$grep_regex = "^${pkgname}==${ensure}\$"
151+
if $_ensure =~ /^((19|20)[0-9][0-9]-(0[1-9]|1[1-2])-([0-2][1-9]|3[0-1])|[0-9]+\.\w+\+?\w*(\.\w+)*)$/ {
152+
$grep_regex = "^${real_pkgname}[[:space:]]\\+(\\?${_ensure}\\()$\\|$\\|, \\|[[:space:]]\\)"
141153
} else {
142-
$grep_regex = $pkgname ? {
143-
/==/ => "^${pkgname}\$",
144-
default => "^${pkgname}==",
145-
}
154+
$grep_regex = "^${real_pkgname}[[:space:]].*$"
146155
}
147156

148157
$extras_string = empty($extras) ? {
@@ -151,12 +160,12 @@
151160
}
152161

153162
$egg_name = $egg ? {
154-
false => "${pkgname}${extras_string}",
163+
false => "${real_pkgname}${extras_string}",
155164
default => $egg
156165
}
157166

158167
$source = $url ? {
159-
false => "${pkgname}${extras_string}",
168+
false => "${real_pkgname}${extras_string}",
160169
/^(\/|[a-zA-Z]\:)/ => "'${url}'",
161170
/^(git\+|hg\+|bzr\+|svn\+)(http|https|ssh|svn|sftp|ftp|lp|git)(:\/\/).+$/ => "'${url}'",
162171
default => "'${url}#egg=${egg_name}'",
@@ -165,111 +174,70 @@
165174
$pip_install = "${pip_env} --log ${log}/pip.log install"
166175
$pip_common_args = "${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${source}"
167176

177+
$pip_exec_name = "pip_install_${name}"
178+
168179
# Explicit version out of VCS when PIP supported URL is provided
169180
if $source =~ /^'(git\+|hg\+|bzr\+|svn\+)(http|https|ssh|svn|sftp|ftp|lp|git)(:\/\/).+'$/ {
170-
if $ensure != present and $ensure != latest {
171-
exec { "pip_install_${name}":
172-
command => "${pip_install} ${install_args} ${pip_common_args}@${ensure}#egg=${egg_name}",
173-
unless => "${pip_env} freeze --all | grep -i -e ${grep_regex}",
174-
user => $owner,
175-
group => $group,
176-
umask => $umask,
177-
cwd => $cwd,
178-
environment => $environment,
179-
timeout => $timeout,
180-
path => $_path,
181-
}
181+
if $_ensure != present and $_ensure != latest {
182+
$command = "${pip_install} ${install_args} ${pip_common_args}@${_ensure}#egg=${egg_name}"
183+
$unless_command = "${pip_env} list | grep -i -e '${grep_regex}'"
182184
} else {
183-
exec { "pip_install_${name}":
184-
command => "${pip_install} ${install_args} ${pip_common_args}",
185-
unless => "${pip_env} freeze --all | grep -i -e ${grep_regex}",
186-
user => $owner,
187-
group => $group,
188-
umask => $umask,
189-
cwd => $cwd,
190-
environment => $environment,
191-
timeout => $timeout,
192-
path => $_path,
193-
}
185+
$command = "${pip_install} ${install_args} ${pip_common_args}"
186+
$unless_command = "${pip_env} list | grep -i -e '${grep_regex}'"
194187
}
195188
} else {
196-
case $ensure {
189+
case $_ensure {
197190
/^((19|20)[0-9][0-9]-(0[1-9]|1[1-2])-([0-2][1-9]|3[0-1])|[0-9]+\.\w+\+?\w*(\.\w+)*)$/: {
198191
# Version formats as per http://guide.python-distribute.org/specification.html#standard-versioning-schemes
199192
# Explicit version.
200-
exec { "pip_install_${name}":
201-
command => "${pip_install} ${install_args} ${pip_common_args}==${ensure}",
202-
unless => "${pip_env} freeze --all | grep -i -e ${grep_regex} || ${pip_env} list | sed -e 's/[ ]\\+/==/' -e 's/[()]//g' | grep -i -e ${grep_regex}",
203-
user => $owner,
204-
group => $group,
205-
umask => $umask,
206-
cwd => $cwd,
207-
environment => $environment,
208-
timeout => $timeout,
209-
path => $_path,
210-
}
193+
$command = "${pip_install} ${install_args} ${pip_common_args}==${_ensure}"
194+
$unless_command = "${pip_env} list | grep -i -e '${grep_regex}'"
211195
}
212-
#
196+
213197
'present': {
214198
# Whatever version is available.
215-
exec { "pip_install_${name}":
216-
command => "${pip_install} ${pip_common_args}",
217-
unless => "${pip_env} freeze --all | grep -i -e ${grep_regex} || ${pip_env} list | sed -e 's/[ ]\\+/==/' -e 's/[()]//g' | grep -i -e ${grep_regex}",
218-
user => $owner,
219-
group => $group,
220-
umask => $umask,
221-
cwd => $cwd,
222-
environment => $environment,
223-
timeout => $timeout,
224-
path => $_path,
225-
}
199+
$command = "${pip_install} ${pip_common_args}"
200+
$unless_command = "${pip_env} list | grep -i -e '${grep_regex}'"
226201
}
227202

228203
'latest': {
229204
# Unfortunately this is the smartest way of getting the latest available package version with pip as of now
230205
# Note: we DO need to repeat ourselves with "from version" in both grep and sed as on some systems pip returns
231206
# more than one line with paretheses.
232-
$latest_version = join(["${pip_install} ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${pkgname}==notreallyaversion 2>&1",
207+
$latest_version = join(["${pip_install} ${pypi_index} ${proxy_flag} ${install_args} ${install_editable} ${real_pkgname}==notreallyaversion 2>&1",
233208
' | grep -oP "\(from versions: .*\)" | sed -E "s/\(from versions: (.*?, )*(.*)\)/\2/g"',
234209
' | tr -d "[:space:]"'])
235210

236211
# Packages with underscores in their names are listed with dashes in their place in `pip freeze` output
237-
$pkgname_with_dashes = regsubst($pkgname, '_', '-', 'G')
212+
$pkgname_with_dashes = regsubst($real_pkgname, '_', '-', 'G')
238213
$grep_regex_pkgname_with_dashes = "^${pkgname_with_dashes}=="
239214
$installed_version = join(["${pip_env} freeze --all",
240215
" | grep -i -e ${grep_regex_pkgname_with_dashes} | cut -d= -f3",
241216
" | tr -d '[:space:]'"])
242217

218+
$command = "${pip_install} --upgrade ${pip_common_args}"
243219
$unless_command = "[ \$(${latest_version}) = \$(${installed_version}) ]"
244-
245-
# Latest version.
246-
exec { "pip_install_${name}":
247-
command => "${pip_install} --upgrade ${pip_common_args}",
248-
unless => $unless_command,
249-
user => $owner,
250-
group => $group,
251-
umask => $umask,
252-
cwd => $cwd,
253-
environment => $environment,
254-
timeout => $timeout,
255-
path => $_path,
256-
}
257220
}
258221

259222
default: {
260223
# Anti-action, uninstall.
261-
exec { "pip_uninstall_${name}":
262-
command => "echo y | ${pip_env} uninstall ${uninstall_args} ${proxy_flag} ${name}",
263-
onlyif => "${pip_env} freeze --all | grep -i -e ${grep_regex}",
264-
user => $owner,
265-
group => $group,
266-
umask => $umask,
267-
cwd => $cwd,
268-
environment => $environment,
269-
timeout => $timeout,
270-
path => $_path,
271-
}
224+
$pip_exec_name = "pip_uninstall_${name}"
225+
$command = "echo y | ${pip_env} uninstall ${uninstall_args} ${proxy_flag} ${name}"
226+
$unless_command = "! ${pip_env} list | grep -i -e '${grep_regex}'"
272227
}
273228
}
274229
}
230+
231+
exec { $pip_exec_name:
232+
command => $command,
233+
unless => $unless_command,
234+
user => $owner,
235+
group => $group,
236+
umask => $umask,
237+
cwd => $cwd,
238+
environment => $environment,
239+
timeout => $timeout,
240+
path => $_path,
241+
}
242+
275243
}

0 commit comments

Comments
 (0)