[chef] Re: Re: Re: Re: How would I prevent python_pip from installing packages that are already installed?


Chronological Thread 
  • From: Fabien Delpierre < >
  • To: chef < >
  • Subject: [chef] Re: Re: Re: Re: How would I prevent python_pip from installing packages that are already installed?
  • Date: Sun, 8 Mar 2015 16:53:53 -0400

Success! Initially I had some trouble because I need to remove a few lines from my pip requirements file before passing it to Chef, and after running that .split, my code that did that no longer worked, so I struggled with that for a bit, then it occurred to me that I could just clean up the requirements file before splitting the package from the package version, and now I have working code, and Chef runs after the first one now take 50-60 seconds. Seems like I don't even need that not_if anymore, either. So here's my updated code if anyone cares. It's probably not very elegant but it's the best this newbie programmer can come up with, and it works.

# Install packages not loaded from the requirements file
node['foo']['venv_packages'].each do |pkg, pkg_version|
  python_pip pkg do
    virtualenv node['foo']['venv']
    version pkg_version
    action :install
  end
end

# Parse the pip requirements file and remove pyodbc, we'll deal with it separately
reqs = IO.readlines("/foo/setup/dev-requirements.txt").map {|r| r.chomp}
pyodbc = []

while reqs.index{|r| r.include?("pyodbc")} != nil
  match = reqs.index{|r| r.include?("pyodbc")}
  pyodbc.push(reqs[match])
  reqs.delete_at(match)
end

# Now re-parse the array to split the package names from the desired versions
reqs = reqs.map {|r| r.strip.split('==')}

# Install the packages we want in the virtual environment
reqs.each do |pkg, pkg_version|
  python_pip pkg do
    virtualenv node['foo']['venv']
    version pkg_version
    action :install
  end
end

# Now install pyodbc
match = pyodbc.index{|p| p.include?("pyodbc=")}
pyodbc_pkg = pyodbc[match].split("==") # this should now be = ["pyodbc","3.0.7"]
pyodbc.delete_at(match)
pyodbc_args = pyodbc * " " # this should now be = "--allow-external pyodbc --allow-public pyodbc"

python_pip pyodbc_pkg[0] do
  virtualenv node['foo']['venv']
  version pyodbc_pkg[1]
  options pyodbc_args
  action :install
end




On Fri, Mar 6, 2015 at 7:32 PM, Daniel DeLeo < " target="_blank"> > wrote:


On Friday, March 6, 2015 at 2:39 PM, Fabien Delpierre wrote:

> Daniel,
> Setting the version like:
> python_pip 'package' do
> version 'x'
> ...
> end
> ...sounds like something I should be able to figure out. I have a requirements file that looks like this:
> package==version
> And a long list of packages.

Ruby’s pretty good for quick and dirty scripting of this kind of thing. Something like this should work:

require ‘pp’ # just for debugging/showing the example

names_and_version_specs = IO.readlines('/tmp/example.txt')
names_and_versions = names_and_version_specs.map do |line|
  line.strip.split('==')
end

# debug
pp names_and_versions

# do something like this in your recipe:
# names_and_versions.each do |name, pkg_version|
# python_pip name do
# version pkg_version
# end
# end



HTH,

--
Daniel DeLeo





Archive powered by MHonArc 2.6.16.

§