Skip to content

Commit

Permalink
new classes
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Feb 14, 2019
1 parent bbc39bb commit 6696e64
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/lib/y2packager/known_repositories.rb
@@ -0,0 +1,81 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2019 SUSE LLC, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# ------------------------------------------------------------------------------

require "yaml"
require "yast"

module Y2Packager
# Store the known repositories
class KnownRepositories
include Yast::Logger

STATUS_FILE = "/var/lib/YaST2/known_repositories.yaml".freeze

# @return [String] Products on the medium
attr_reader :repositories

# Constructor
def initialize
Yast.import "Pkg"
@repositories = []
end

def repositories
@repositories ||= read_repositories
end

def write
current = current_repositories
log.info("Writing known repositories #{current.inspect} to #{status_file}")
File.write(status_file, current.to_yaml)
end

#
# Return new (unknown) repositories
#
# @return [Array<String>] List of new repositories (URLs)
#
def new_repositories
current_repositories - repositories
end

private

def read_repositories
if !File.exist?(status_file)
log.info("Status file #{status_file} not found")
return []
end

status = YAML.load_file(status_file)
# unify the file in case it was manually modified
status.uniq!
status.sort!

log.info("Read known repositories from #{status_file}: #{status}")
status
end

def current_repositories
# only the enabled repositories
repo_ids = Yast::Pkg.SourceGetCurrent(true)

urls = repo_ids.map { |r| Yast::Pkg.SourceGeneralData(r)["url"] }
urls.uniq!
urls.sort
end

def status_file
File.join(Yast::WFM.scr_root, STATUS_FILE)
end
end
end
63 changes: 63 additions & 0 deletions src/lib/y2packager/system_packages.rb
@@ -0,0 +1,63 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2019 SUSE LLC, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# ------------------------------------------------------------------------------

require "yast"

module Y2Packager
# Preselect the system packages
class SystemPackages
def find(repository_urls)
return [] if repository_urls.empty?

original_solver_flags = Pkg.GetSolverFlags

# solver flags for selecting minimal recommended packages (e.g. drivers)
Pkg.SetSolverFlags(
"ignoreAlreadyRecommended" => false,
"onlyRequires" => true
)
# select the packages
Pkg.PkgSolve(true)

ids = repo_ids(repository_urls)

# re-select the packages by YaST otherwise they would be unselected by the next solver run
pkgs = Pkg.ResolvableProperties("", :package, "")
pkgs = pkgs.select { |p| p["status"] == :selected && ids.include?(p["source"]) }

# set back the original solver flags
Pkg.SetSolverFlags(original_solver_flags)

pkgs.map { |p| p["name"] }
end

def select(repository_urls)
pkgs = find(repository_urls)
log.info "Preselected system packages: #{pkgs.inspect}"
pkgs.each { |p| Pkg.PkgInstall(p) }
end

private

#
# Create repository ID to URL mapping
#
# @return [Array<Integer>] List of reposit
#
def repo_ids(urls)
repo_ids = Yast::Pkg.SourceGetCurrent(true)
repo_ids.each_with_object([]) do |i, list|
list << i if urls.include?(Yast::Pkg.SourceGeneralData(i)["url"])
end
end
end
end

0 comments on commit 6696e64

Please sign in to comment.