Class: LicenseFinder::Pip

Inherits:
PackageManager show all
Defined in:
lib/license_finder/package_managers/pip.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PackageManager

#active?, command_exists?, #current_packages_with_relations, #detected_package_path, installed?, #project_root?, takes_priority_over

Constructor Details

#initialize(options = {}) ⇒ Pip

Returns a new instance of Pip.



7
8
9
10
11
12
# File 'lib/license_finder/package_managers/pip.rb', line 7

def initialize(options = {})
  super
  @requirements_path = options[:pip_requirements_path] || Pathname('requirements.txt')
  @python_version = options[:python_version] || '2'
  raise "Invalid python version \'#{@python_version}\'. Valid versions are '2' or '3'." unless %w[2 3].include?(@python_version)
end

Class Method Details

.package_management_commandObject

Used to detect if installed, but this is a static method and the options aren’t passed so we don’t know which python version was specified. Will fail later if the expected version isn’t installed. The Dockerfile now installs both versions so using the image is safe. TODO: Refactor PackageManager.installed?() to pass in the options?



31
32
33
# File 'lib/license_finder/package_managers/pip.rb', line 31

def self.package_management_command
  'pip2'
end

Instance Method Details

#current_packagesObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/license_finder/package_managers/pip.rb', line 14

def current_packages
  pip_output.map do |name, version, children, location|
    PipPackage.new(
      name,
      version,
      PyPI.definition(name, version),
      logger: logger,
      children: children,
      install_path: Pathname(location).join(name)
    )
  end
end

#possible_package_pathsObject



48
49
50
51
52
53
54
# File 'lib/license_finder/package_managers/pip.rb', line 48

def possible_package_paths
  if project_path.nil?
    [@requirements_path]
  else
    [project_path.join(@requirements_path)]
  end
end

#prepareObject



39
40
41
42
43
44
45
46
# File 'lib/license_finder/package_managers/pip.rb', line 39

def prepare
  prep_cmd = "#{prepare_command} -r #{@requirements_path}"
  _stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(prep_cmd) }
  return if status.success?

  log_errors stderr
  raise "Prepare command '#{prep_cmd}' failed" unless @prepare_no_fail
end

#prepare_commandObject



35
36
37
# File 'lib/license_finder/package_managers/pip.rb', line 35

def prepare_command
  "pip#{@python_version} install"
end