Hello all

May be it will interested to someone.

Tool to mirror distfiles (see attachment). Mirroring
based on information stored in portage. Written
in python and use portage python library.

P.S. This is my first program in python....

I have question:
Can author of emerge tool move class depgraph
from tool source to portage library?
I need to write tool to make "download list" for offline
gentoo usage:
the first tool make list of files and their urls
the second download files from urls

I need this class to make dependence list or may be
exists another way to make dependence list?

The next question:
Is there tool to clean distfiles (remove old files)?
or make such tool?

Thanks.

#!/usr/bin/python -O
# Distributed under the terms of the GNU General Public License v2

import os,sys,getopt,types,re
os.environ["PORTAGE_CALLER"]="emirroo"

sys.path=["/usr/lib/portage/pym"]+sys.path
import portage

def usage(fh=sys.stdout):
    fh.write("Usage: emirroo [option1] [option2] ... [optionN]\n"
             "\t--help or -h\tthis help message\n"
             "\t--all or -a\tmirror distfiles for all packages in portage\n"
             "\t--visible or -u\tmirror for all visible packages\n"
             "\t--best or -b\tmirror for latest visible packages only\n"
             "\t--verbose or -v\tmore spam\n"
             "\t--listonly or -L\tlist URIs only\n"
             "\t--exclude-regex or -X\texclude category/package by regex 
match\n"
    )


try:
    options,argz=getopt.getopt(sys.argv[1:], "aubvhLX:",
        ["all", "visible", "best", "verbose", "help", "version", "listonly", 
"exclude-regex="])
except getopt.GetoptError:
    sys.stderr.write("Invalid argument\n")
    usage(sys.stderr)
    sys.exit(13)

verbose=0
listuris=0
mirrortype=0 # all=1 visible=2 latest=3
excludes=[]
for o,a in options:
    if o=="--version":
        sys.stderr.write("emirroo 0.04\n\n"
                         "Distributed under the terms of the GNU General Public 
License v2\n")
        sys.exit()
    elif o in ("-h", "--help"):
        usage()
        sys.exit()
    elif o in ("-v", "--verbose"):
        verbose+=1
    elif o in ("-L", "--listonly"):
        listuris=1
    elif o in ("-X", "--exclude-regex"):
        excludes.append(re.compile(a))
    elif o in ("-a", "--all"):
        if mirrortype:
            sys.stderr.write("Error! Only one mirroring type avaible (--best, 
--visible or --all)\n")
            usage(sys.stderr)
            sys.exit(13)
        else:
            mirrortype=1
    elif o in ("-u", "--visible"):
        if mirrortype:
            sys.stderr.write("Error! Only one mirroring type avaible (--best, 
--visible or --all)\n")
            usage(sys.stderr)
            sys.exit(13)
        else:
            mirrortype=2
    elif o in ("-b", "--best"):
        if mirrortype:
            sys.stderr.write("Error! Only one mirroring type avaible (--best, 
--visible or --all)\n")
            usage(sys.stderr)
            sys.exit(13)
        else:
            mirrortype=3
    else:
        sys.stderr.write("Invalid argument\n")
        usage(sys.stderr)
        sys.exit(13)

psettings=portage.config(clone=portage.settings)
root=psettings["ROOT"]+"/"
pdbapi=portage.db[root]["porttree"].dbapi

if psettings.has_key("PORTAGE_NICENESS"):
        try:
                os.nice(int(psettings["PORTAGE_NICENESS"]))
        except SystemExit, e:
                raise # Needed else can't exit
        except Exception,e:
                print "!!! Failed to change nice value to 
'"+str(psettings["PORTAGE_NICENESS"])+"'"
                print "!!!",e

def mirrorall(cp):
    return pdbapi.cp_list(cp)

def mirrorvisible(cp):
    return pdbapi.match(cp)

def mirrorlast(cp):
    cpv=pdbapi.xmatch("bestmatch-visible", cp)
    if type(cpv)==types.StringType:
        cpv=[cpv]
    return cpv

fetcherrcnt=0

if mirrortype==1:
    mirrorer=mirrorall
elif mirrortype==2:
    mirrorer=mirrorvisible
elif mirrortype==3:
    mirrorer=mirrorlast
else:
    sys.stderr.write("Specify mirroring type (--best, --visible or --all)\n")
    usage(sys.stderr)
    sys.exit(13)

cplist=pdbapi.cp_all()
cptotal=len(cplist)
cpcount=0

if verbose>0:
    sys.stderr.write("Mirror "+str(cptotal)+" category/package\n")

for cp in cplist:
    skipit=0
    for exre in excludes:
        if exre.match(cp):
            skipit=1
            break
    if skipit:
        continue

    cpcount+=1
    if verbose>0:
        sys.stderr.write("Mirroring \""+cp+"\" 
("+str(cpcount)+"/"+str(cptotal)+") ...\n")

    cpvlist=mirrorer(cp)
    for cpv in cpvlist:
        if cpv:
            if verbose>0:
                sys.stderr.write("Mirroring "+cpv+"\n")
            ebuild=pdbapi.findname(cpv)
            haserr=portage.doebuild(ebuild, "fetch", root, psettings, 
fetchall=1, listonly=listuris)
            if haserr: fetcherrcnt+=1

if fetcherrcnt or verbose > 1:
    sys.stderr.write("While mirroring was "+str(fetcherrcnt)+" errors\n")


--
[email protected] mailing list

Reply via email to