Class: Serverspec::Type::Virtualenv

Inherits:
Base
  • Object
show all
Defined in:
lib/serverspec_extended_types/virtualenv.rb

Instance Method Summary collapse

Instance Method Details

#get_pip_freezenil (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a hash for the ‘pip freeze` output; set @pip_freeze

Returns:

  • (nil)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/serverspec_extended_types/virtualenv.rb', line 91

def get_pip_freeze()
  pip_path = ::File.join(@name, 'bin', 'pip')
  tmp = @runner.run_command("#{pip_path} freeze")
  @pip_freeze = Hash.new()
  if tmp.exit_status.to_i != 0
    return @pip_freeze
  end
  lines = tmp.stdout.split("\n")
  lines.each do |line|
    line.strip!
    if line =~ /^-e /
      @pip_freeze[line] = ''
      next
    end
    parts = line.split(/==/)
    @pip_freeze[parts[0]] = parts[1]
  end
  @pip_freeze
end

#get_pip_versionnil (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the pip version from the venv; set @pip_version

Returns:

  • (nil)


116
117
118
119
120
121
122
123
124
125
# File 'lib/serverspec_extended_types/virtualenv.rb', line 116

def get_pip_version()
  pip_path = ::File.join(@name, 'bin', 'pip')
  pip_path = ::File.join(@name, 'bin', 'pip')
  tmp = @runner.run_command("#{pip_path} --version")
  if ( tmp.stdout =~ /^pip (\d+\S+)/ )
    @pip_version = $1
  else
    @pip_version = ''
  end
end

#get_python_versionnil (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the python version from the venv; set @python_version

Returns:

  • (nil)


132
133
134
135
136
137
138
139
140
141
142
# File 'lib/serverspec_extended_types/virtualenv.rb', line 132

def get_python_version()
  python_path = ::File.join(@name, 'bin', 'python')
  tmp = @runner.run_command("#{python_path} --version")
  if ( tmp.stderr =~ /^[Pp]ython (\d+\S+)/ )
    @python_version = $1
  elsif ( tmp.stdout =~ /^[Pp]ython (\d+\S+)/ )
    @python_version = $1
  else
    @python_version = ''
  end
end

#pip_freezeHash

Return a hash of all packages present in ‘pip freeze` output for the venv

Note that any editable packages (‘-e something`) are returned as hash keys with an empty value

Examples:

describe virtualenv('/path/to/venv') do
  its(:pip_freeze) { should include('wsgiref' => '0.1.2') }
  its(:pip_freeze) { should include('requests') }
  its(:pip_freeze) { should include('pytest' => /^2\.6/) }
  its(:pip_freeze) { should include('-e [email protected]:jantman/someproject.git@1d8a380e3af9d081081d7ef685979200a7db4130#egg=someproject') }
end

Returns:

  • (Hash)


82
83
84
# File 'lib/serverspec_extended_types/virtualenv.rb', line 82

def pip_freeze
  @pip_freeze || get_pip_freeze
end

#pip_versionString

Return the version of pip installed in the virtualenv

Examples:

describe virtualenv('/path/to/venv') do
  its(:pip_version) { should match /^6\.0\.6$/ }
end

Returns:

  • (String)


50
51
52
# File 'lib/serverspec_extended_types/virtualenv.rb', line 50

def pip_version
  @pip_version || get_pip_version
end

#python_versionString

Return the version of python installed in the virtualenv

Examples:

describe virtualenv('/path/to/venv') do
  its(:python_version) { should match /^2\.7\.9$/ }
end

Returns:

  • (String)


63
64
65
# File 'lib/serverspec_extended_types/virtualenv.rb', line 63

def python_version
  @python_version || get_python_version
end

#virtualenv?Boolean

Test whether this appears to be a working venv

Tests performed:

  • venv_path/bin/pip executable by owner?

  • venv_path/bin/python executable by owner?

  • venv_path/bin/activate readable by owner?

  • ‘export VIRTUAL_ENV’ in venv_path/bin/activate?

Examples:

describe virtualenv('/path/to/venv') do
  it { should be_virtualenv }
end

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
# File 'lib/serverspec_extended_types/virtualenv.rb', line 30

def virtualenv?
  pip_path = ::File.join(@name, 'bin', 'pip')
  python_path = ::File.join(@name, 'bin', 'python')
  act_path = ::File.join(@name, 'bin', 'activate')
  cmd = "grep -q 'export VIRTUAL_ENV' #{act_path}"
  @runner.check_file_is_executable(pip_path, 'owner') and
    @runner.check_file_is_executable(python_path, 'owner') and
    @runner.check_file_is_readable(act_path, 'owner') and
    @runner.run_command(cmd).exit_status.to_i == 0
end