Class: MyPrecious::PyPackageInfo::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/myprecious/python_packages.rb

Overview

Reads package requirements from a file

Instance Method Summary collapse

Constructor Details

#initialize(packages_fpath, only_constrain: false) ⇒ Reader

Returns a new instance of Reader.



732
733
734
735
736
# File 'lib/myprecious/python_packages.rb', line 732

def initialize(packages_fpath, only_constrain: false)
  super()
  @files = [Pathname(packages_fpath)]
  @only_constrain = only_constrain
end

Instance Method Details

#each_installed_packageObject

Enumerate packages targeted for installation by this instance

Each invocation of the block receives a PyPackageInfo object targeted for installation. Each of these PyPackageInfo object will have a resolved #name and #current_version (if possible).

An Enumerator is returned if no block is given.



786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# File 'lib/myprecious/python_packages.rb', line 786

def each_installed_package
  generator = Enumerator.new do |items|
    packages = {}
    
    each_package_constrained do |pkg|
      pkg.resolve_name!
      if packages.has_key?(pkg.name)
        packages[pkg.name].incorporate(pkg)
      else
        packages[pkg.name] = pkg
      end
    end
    
    to_install = []
    packages.each_value do |pkg|
      next unless pkg.install?
      to_install << pkg.name
    end
    
    while pkg_name = to_install.shift
      pkg = packages[pkg_name]
      pkg.resolve_version!
      items << pkg
    end
  end
  
  if block_given?
    generator.each {|item| yield item}
  else
    generator
  end
end

#each_package_constrainedObject

Enumerate packages described by requirements targeted by this instance

Each invocation of the block receives a PyPackageInfo object, which will have, at minimum, either a #name or #url not nil. It is possible that multiple iterations will process separate PyPackageInfo for the same package, in which case PyPackageInfo#incorporate is useful.

An Enumerator is returned if no block is given.



748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'lib/myprecious/python_packages.rb', line 748

def each_package_constrained
  generator = Enumerator.new do |items|
    continued_line = ''
    current_file.each_line do |pkg_line|
      pkg_line = pkg_line.chomp
      next if /^#/ =~ pkg_line
      if /(?<=\s)#.*$/ =~ pkg_line
        pkg_line = pkg_line[0...-$&.length]
      end
      
      # Yes, this _does_ happen after comment lines are skipped :facepalm:
      if /\\$/ =~ pkg_line
        continued_line += pkg_line[0..-2]
        next
      end
      pkg_line, continued_line = (continued_line + pkg_line).strip, ''
      next if pkg_line.empty?
      
      process_line_into(items, pkg_line)
    end
  end
  
  if block_given?
    generator.each {|item| yield item}
  else
    generator
  end
end