class Archlinux::DB

Attributes

config[RW]
file[RW]

Public Class Methods

db_file?(name) click to toggle source
# File lib/aur/db.rb, line 10
def self.db_file?(name)
        case name
        when Pathname
                true
        when String
                if name.include?('/') or name.match(/\.db(\..*)?$/)
                        true
                else
                        false #Usually we assume this is a repo name
                end
        end
end
new(file, config: Archlinux.config) click to toggle source
# File lib/aur/db.rb, line 24
def initialize(file, config: Archlinux.config)
        @orig_file=Pathname.new(file)
        @file=@orig_file.realdirpath rescue @orig_file.abs_path
        @config=config
end

Public Instance Methods

add(*files, cmd: :'repo-add', default_opts:[], sign: @config&.use_sign?(:db), force_sign: false, **opts) click to toggle source
# File lib/aur/db.rb, line 162
def add(*files, cmd: :'repo-add', default_opts:[], sign: @config&.use_sign?(:db), force_sign: false, **opts)
        default_opts+=['-s', '-v'] if sign
        default_opts+=['--key', sign] if sign.is_a?(String)
        dir.chdir do
                files.map! {|f| Pathname.new(f)}
                existing_files=files.select {|f| f.file?}
                missing_files = files-existing_files
                SH.logger.warn "In #{cmd}, missing files: #{missing_files.join(', ')}" unless missing_files.empty?
                unless existing_files.empty?
                        sign_files = @config&.use_sign?(:package)
                        PackageFiles.new(*existing_files, config: @config).sign(sign_name: sign_files, force: force_sign) if sign_files
                        call(cmd, path, *existing_files, default_opts: default_opts, **opts)
                        @packages=nil #we need to refresh the list
                end
        end
        files
end
add_to_db(pkgs, update: true, op: :cp) click to toggle source

move/copy files to db and add them if update==true, only add more recent packages pkgs should be a PackageFiles or a PackageList or a list of files

# File lib/aur/db.rb, line 284
def add_to_db(pkgs, update: true, op: :cp)
        if update
                pkgs=PackageFiles.create(pkgs).packages unless pkgs.is_a? PackageList
                up=self.packages.check_updates(pkgs)
                pkgs=up.select {|_k, u| u[:op]==:upgrade or u[:op]==:install}.map do |_k,v|
                        pkgs[v[:out_pkg]].path
                end
        else
                pkgs=pkgs.map {|_k,v| v.path } if pkgs.is_a?(PackageList)
        end
        SH.logger.mark "Updating #{pkgs} in #{self}"
        cp_pkgs=move_to_db(*pkgs, op: op)
        add(*cp_pkgs)
end
bsdcat_list() click to toggle source

a db is a tar.gz archive of packages/desc, like yay-8.998-1/descr

# File lib/aur/db.rb, line 59
def bsdcat_list
        res= SH.run_simple("bsdcat #{@file.shellescape}", chomp: :lines) {return nil}
        list=[]; pkg={}; mode=nil
        flush = lambda do
                # catch old deps files which don't specify the full infos
                unless pkg[:name].nil? and pkg[:base].nil?
                        pkg[:repo]||=path
                        list << pkg 
                end
        end
        res.each do |l|
                next if l.empty? or l.match(/^\u0000*$/)
                if (m=l.match(/(\u0000+.*\u0000+)?%([A-Z0-9]*)%$/))
                        mode=m[2].downcase.to_sym
                        if m[1] #new db entry
                                flush.call #store old db entry
                                pkg={}
                        end
                else
                        l=l.to_i if mode==:csize or mode==:isize
                        l=Time.at(l.to_i) if mode==:builddate
                        Archlinux.add_to_hash(pkg, mode, l)
                end
        end
        flush.call #don't forget the last one
        list
end
call(*args, **opts) click to toggle source
# File lib/aur/db.rb, line 131
def call(*args, **opts)
        @config.launch(*args, **opts) do |*a, **o|
                dir.chdir do
                        SH.sh(*a, **o)
                end
        end
end
check() click to toggle source
# File lib/aur/db.rb, line 252
def check
        packages.same?(package_files)
end
check_update(other=dir_packages) click to toggle source
# File lib/aur/db.rb, line 256
def check_update(other=dir_packages)
        self.packages.check_updates(other)
        # yield up if block_given?
        # refresh=up.select {|_k, u| u[:op]==:upgrade or u[:op]==:downgrade}
        # add=up.select {|_k, u| u[:op]==:install}
        # remove=up.select {|_k, u| u[:op]==:obsolete}
        # return {refresh: refresh, add: add, remove: remove}
end
clean(dry_run: true) click to toggle source

In clean, we clean the dir packages which are newer or not present in the db. This is like the reverse of `update`. In particular be careful that this will delete newer versions or added versions

# File lib/aur/db.rb, line 316
def clean(dry_run: true)
        dir_pkgs=dir_packages_cls
        dir_files=dir_pkgs.files
        db_files=files
        to_remove=dir_files - db_files
        if dry_run
                to_remove
        else
                PackageFiles.rm_files(*to_remove, dir: self.dir)
        end
end
clean_dir(dry_run: true) click to toggle source

in clean_dir, we clean the dir packages which have a newer version (in the dir).

# File lib/aur/db.rb, line 308
def clean_dir(dry_run: true)
        files=dir_packages_cls
        files.clean(dry_run: dry_run)
end
create() click to toggle source
# File lib/aur/db.rb, line 46
def create
        mkpath
        unless @file.exist?
                call(:'repo-add', path)
        end
        self
end
dir() click to toggle source

def files

packages.l.map {|pkg| dir+Pathname.new(pkg[:filename])}

end

# File lib/aur/db.rb, line 124
def dir
        # # we memoize this because if we get called again in a dir.chdir
        # # call, then the realpath will fail
        # @dir ||= @file.dirname.realpath
        @file.dirname
end
dir_packages() click to toggle source
# File lib/aur/db.rb, line 199
def dir_packages
        dir_packages_cls.packages
end
dir_packages_cls() click to toggle source
# File lib/aur/db.rb, line 196
def dir_packages_cls
        PackageFiles.from_dir(dir, config: @config)
end
files(absolute=true) click to toggle source
# File lib/aur/db.rb, line 114
def files(absolute=true)
        list.map do |pkg|
                file=Pathname.new(pkg[:filename])
                absolute ? dir + file : file
        end
end
list() click to toggle source
# File lib/aur/db.rb, line 87
def list
        res= SH.run_simple("bsdtar -xOf #{@file.shellescape} '*/desc'", chomp: :lines) {return nil}
        list=[]; pkg={}; mode=nil
        flush = lambda do
                unless pkg.empty?
                        pkg[:repo]||=path
                        list << pkg
                end
        end
        res.each do |l|
                next if l.empty?
                if (m=l.match(/^%([A-Z0-9]*)%$/))
                        mode=m[1].downcase.to_sym
                        if mode==:filename #new db entry
                                flush.call #store old db entry
                                pkg={}
                        end
                else
                        l=l.to_i if mode==:csize or mode==:isize
                        l=Time.at(l.to_i) if mode==:builddate
                        Archlinux.add_to_hash(pkg, mode, l)
                end
        end
        flush.call #don't forget the last one
        list
end
mkpath() click to toggle source
# File lib/aur/db.rb, line 30
def mkpath
        @file.dirname.mkpath
end
move_to_db(*files, op: :mv) click to toggle source
# File lib/aur/db.rb, line 139
def move_to_db(*files, op: :mv)
        files=files.map {|f| Pathname.new(f).realpath}
        dir=self.dir
        SH.logger.verbose "#{op}: #{files.join(', ')} to #{dir}"
        files.map do |f|
                if f.dirname == dir
                        SH.logger.verbose2 "! #{f} already exists in #{dir}"
                        f
                else
                        new=dir+f.basename
                        SH.logger.verbose2 "-> #{op} #{f} to #{new}"
                        f.send(op, new)
                        sig=Pathname.new(f.to_s+".sig") #mv .sig too
                        if sig.exist?
                                newsig=dir+sig.basename
                                SH.logger.verbose2 "-> #{op} #{sig} to #{newsig}"
                                sig.send(op, newsig)
                        end
                        new
                end
        end
end
package_files() click to toggle source
# File lib/aur/db.rb, line 206
def package_files
        package_files_cls.packages
end
package_files_cls() click to toggle source
# File lib/aur/db.rb, line 203
def package_files_cls
        PackageFiles.new(*files, config: @config)
end
packages(refresh=false) click to toggle source
# File lib/aur/db.rb, line 190
def packages(refresh=false)
        @packages=nil if refresh
        # @packages||=PackageList.new(list, config: @config)
        @packages||=@config.to_packages(list)
end
path() click to toggle source
# File lib/aur/db.rb, line 34
def path
        if file.exist?
                file.realpath
        else
                file
        end
end
remove(*pkgnames, cmd: :'repo-remove', default_opts:[], sign: @config&.use_sign?(:db), **opts) click to toggle source
# File lib/aur/db.rb, line 180
def remove(*pkgnames, cmd: :'repo-remove', default_opts:[], sign: @config&.use_sign?(:db), **opts)
        default_opts+=['-s', '-v'] if sign
        default_opts+=['--key', sign] if sign.is_a?(String)
        dir.chdir do
                call(cmd, path, *pkgnames, default_opts: default_opts, **opts)
                @packages=nil #we need to refresh the list
        end
        pkgnames
end
repo_name() click to toggle source
# File lib/aur/db.rb, line 42
def repo_name
        @file.basename.to_s.sub(/\.db(\..*)?$/,'')
end
resign(**opts) click to toggle source

if we missed some signatures, resign them (and add them back to the db to get the signatures in the db). This override the sign:false config options.

# File lib/aur/db.rb, line 246
def resign(**opts)
        signed_files=sign_files(**opts)
        add(*signed_files) #note that this may try to sign again, but since the signature exists it won't launch gpg
        sign_db(**opts) #idem, normally the db will be signed by repo-add -v, but in case the signature was turned off in the config, this forces the signature
end
rm_from_db(*pkgs) click to toggle source

remove from db and also remove the package files

# File lib/aur/db.rb, line 300
def rm_from_db(*pkgs)
        to_rm=packages.get_packages(*pkgs)
        PackageFiles.rm_files(*to_rm.map {|pkg| pkg.path}, dir: self.dir)
        remove(*pkgs)
end
show_updates(other=dir_packages, **showopts) click to toggle source
# File lib/aur/db.rb, line 265
def show_updates(other=dir_packages, **showopts)
        c=check_update(other)
        packages.show_updates(c, **showopts)
        c
end
sign_db(sign_name: :db, **opts) click to toggle source
# File lib/aur/db.rb, line 215
def sign_db(sign_name: :db, **opts)
        @config&.sign(@file, sign_name: sign_name, **opts) if @file.file?
end
sign_files(sign_name: :package, **opts) click to toggle source

sign the files in the db (return the list of signed file, by default unless force: true is passed this won't resign already signed files)

# File lib/aur/db.rb, line 212
def sign_files(sign_name: :package, **opts)
        @config&.sign(*files, sign_name: sign_name, **opts)
end
to_s() click to toggle source
# File lib/aur/db.rb, line 54
def to_s
        @file.to_s
end
update(other=dir_packages, add_for: %i(upgrade downgrade install), rm_for: %i(obsolete), **showopts) click to toggle source
# File lib/aur/db.rb, line 271
def update(other=dir_packages, add_for: %i(upgrade downgrade install), rm_for: %i(obsolete), **showopts)
        c=show_updates(other, **showopts)
        to_add=c.select {|_k, u| add_for.include?(u[:op])}
        to_rm=c.select {|_k, u| rm_for.include?(u[:op])}
        add(*to_add.map { |_k,v| other[v[:out_pkg]].path })
        # remove(*(r[:remove].map {|_k,v| packages[v[:in_pkg]].file.shellescape}))
        remove(* to_rm.map {|_k,v| Query.strip(v[:in_pkg])})
        c
end
verify_sign_db() click to toggle source
# File lib/aur/db.rb, line 219
def verify_sign_db
        @config&.verify_sign(@file)
end
verify_sign_files() click to toggle source
# File lib/aur/db.rb, line 222
def verify_sign_files
        @config&.verify_sign(*files)
end
verify_sign_pkgs(*pkgs) click to toggle source

check the inline signatures

# File lib/aur/db.rb, line 226
def verify_sign_pkgs(*pkgs)
        packages.get_packages(*pkgs).map do |pkg|
                pgpsign=pkg[:pgpsig]
                if pgpsign
                        require 'base64'
                        sig=Base64.decode64(pgpsign)
                        filename=dir+pkg[:filename]
                        @config.launch(:gpg, "--enable-special-filenames", "--verify", "-", filename, mode: :capture, stdin_data: sig) do |*args|
                                suc, _r=SH.sh(*args)
                                [pkg.name, suc]
                        end
                else
                        [pkg.name, false]
                end
        end.to_h
end