You are not logged in.

#1 2003-12-14 18:31:54

nelson
Member
From: Argentina
Registered: 2003-10-08
Posts: 10

Pacman cache

Hi,
here is a ruby script to generate a custom.db.tar.gz from the archive directory (/var/cache/pacman/pkg) and not use the abs source.
this way custom.db.tar.gz have only what are in the archive directory and not the full list, and pacman know what are in that repository.
i use it to have a cache directory to populate other computers in the local network, and pacman download from a local store if found it or from the net if not.
this script is in testing yet, not have help and command line options, if you need to change somethings, in the first lines are the paths that the script use.
if you don't trust this script, changing the 'destdir' variable to some user access directory, a non root user can execute this script an generate the custom.db.tar.gz

i hope this be helpfull for someone...  blames, patch, sugestion are welcome....

require 'pathname'
require 'fileutils'

class GenCache
    public      #----------------------------------------------------------
    
    attr_reader :cache_dir, :tmp_dir, :infoFile, :destdir
    attr_writer :cache_dir, :tmp_dir, :infoFile, :destdir
    
    def initialize()
        @cache_dir  = '/var/cache/pacman/pkg/'
        @tmp_dir    = '/tmp/gencache/'
        @infofile   = '.PKGINFO'
        @destdir    = '/var/cache/pacman/pkg/'
        @cache_name = 'custom.db.tar.gz'
    end
    
    def generateCache()
        if protec( "createTmp", @tmp_dir, nil ) { |par1,par2| Dir.mkdir( par1 ) }
            begin
                # move to temp dir
                Dir.chdir(@tmp_dir)                                
                # generate filesystem structure
                loopdir()
                # tar filesystem structure
                protec( "tar", @cache_name, "*" ) { |par1,par2| system( "tar czf "+ par1 + " " + par2 ) == 0 }
                # move to repository
                protec( "move", @cache_name, @destdir+@cache_name ) { |par1,par2| FileUtils.mv( par1, par2 ) }
            rescue => err
                # eat the error
            ensure
                # clean filesystem structure
                protec( "cleanTmp", @tmp_dir, nil ) { |par1,par2| FileUtils.rm_r( par1 ) }
            end
        end
    end

    private     #----------------------------------------------------------
    
    def doMd5( hashfile )
        hash = ""
        open("|md5sum "+hashfile ) { |i| hash = i.gets.gsub(/[ t].*/,'') }
        hash = "n%MD5SUM%n" + hash
        return hash
    end

    def protec( method, param1, param2 )
        rc = false
        begin
            if yield param1, param2
                rc = true
            end
        rescue => err
            puts "nError in GenCache::protec("+ method +") [" + err.message + "]"
            raise
        end
        return rc
    end
         
    def writedata( datafile, package, fullname )
        fst_groups = fst_replace = fst_depend = fst_conflict = fst_provides = true
        have_md5 = false
        desc = ""
        depends = ""
        datafile.each do |rc|
            if rc[0,1] != "#"
                if rc.strip.index("pkgname") == 0
                    desc += "%NAME%n"
                    desc += rc.gsub(/^.*=[ t]/,'')
                elsif rc.strip.index("pkgver") == 0
                    desc += "n%VERSION%n"
                    desc += rc.gsub(/^.*=[ t]/,'')
                elsif rc.strip.index("pkgdesc") == 0
                    desc += "n%DESC%n"
                    desc += rc.gsub(/^.*=[ t]/,'')
                elsif rc.strip.index("md5sum") == 0
                    desc += "n%MD5SUM%n"
                    desc += rc.gsub(/^.*=[ t]/,'')
                    have_md5 = true
                elsif rc.strip.index("group") == 0
                    if not have_md5
                        desc += doMd5( fullname )
                        have_md5 = true
                    end
                    if fst_groups
                        desc += "n%GROUPS%n"
                        fst_groups = false
                    end
                    desc += rc.gsub(/^.*=[ t]/,'') 
                elsif rc.strip.index("replaces") == 0
                    if fst_replace
                        desc += "n%REPLACES%n"
                        fst_replace = false
                    end
                    desc += rc.gsub(/^.*=[ t]/,'')
                elsif rc.strip.index("depend") == 0
                    if fst_depend
                        depends += "%DEPENDS%n"
                        fst_depend = false
                    end
                    depends += rc.gsub(/^.*=[ t]/,'')
                elsif rc.strip.index("conflicts") == 0
                    if fst_conflict
                        depends += "n%CONFLICTS%n"
                        fst_conflict = false
                    end
                    depends += rc.gsub(/^.*=[ t]/,'') 
                elsif rc.strip.index("provides") == 0
                    if fst_provides
                        depends += "n%PROVIDES%n"
                        fst_provides = false
                    end
                    depends += rc.gsub(/^.*=[ t]/,'')
                end
            end
        end
        if not have_md5
            desc += doMd5( fullname )
            have_md5 = true
        end
        
        begin
            f = File.new( @tmp_dir+package+"desc", File::CREAT|File::TRUNC|File::RDWR, 0644)
            f.write( desc )
            f.close()
            f = File.new( @tmp_dir+package+"depends", File::CREAT|File::TRUNC|File::RDWR, 0644)
            f.write( depends )
            f.close()
        rescue => err
            print "nerror creating generating control files ["+err.message+"]n"
            raise
        end
    end
            
    def loopdir()
        loop=1
        pth = Pathname.new( @cache_dir )
        pth.dir_foreach do |i|
            unless i.directory?()
                if ( i.to_s.index( "db.tar.gz" ) == nil )
                    if protec( "untar", @cache_dir+i, @infofile ) { |par1,par2| system( "tar xzf "+ par1 + " " + par2 ) == 0 }
                        puts "Error untaring " + @cache_dir+i + " : " + $?.to_s
                        exit 1
                    end
                    package = i.to_s.gsub(/.pkg.tar.gz/,'') + "/"
                    Dir.mkdir( @tmp_dir + package )
                    writedata( IO.readlines( @infofile ), package, @cache_dir + i )
                    loop = loop + 1
                    print "."
                end
            end
        end
        puts "n" + loop.to_s + " files - "
    end

end

#---------------------------------------------
cache = GenCache.new()
cache.generateCache()

Offline

Board footer

Powered by FluxBB