Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
script python para afinar "update world"
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Portuguese
View previous topic :: View next topic  
Author Message
mvc
Tux's lil' helper
Tux's lil' helper


Joined: 27 Nov 2002
Posts: 98
Location: Portugal

PostPosted: Sun Jul 06, 2003 1:55 am    Post subject: script python para afinar "update world" Reply with quote

Vou colocar aqui um script que fiz para tornar os "update world" mais versateis
(vêr https://forums.gentoo.org/viewtopic.php?t=65435) :

Code:

#!/usr/bin/env python2.2
# Copyright 2003 Marco Correia - mvc@netcabo.pt
# Distributed under the terms of the GNU General Public License v2
# Version 0.0000000001

# Some code was borrowed from the etcat script, by Alastair Tse

import portage,string,pprint,re,readline
from stat import *
from output import *

ASK='ask'
ALWAYS='always'
NEVER='never'
MAJOR='major'
MINOR='minor'
RELEASE='release'
USE_FILE='use-file'
NONE='not found'
UP_FILE=os.environ['HOME']+'/.upolicy'
OUT_FILE=os.environ['HOME']+'/.upkgs_tmp'

# Search for packages newer thant a given package       |
def get_newer_packages(pkg):

    matches = []
    pkg_catsplit = portage.catpkgsplit(pkg)
    pkg_matches = portage.portdb.cp_list(pkg_catsplit[0]+\
                                         "/"+pkg_catsplit[1])
    pkg_split = portage.pkgsplit(pkg)
    for package in pkg_matches:
        package_split = portage.pkgsplit(package)
        if portage.pkgcmp(package_split,pkg_split) == 1 :
            matches.append(package)
    return matches

# output the description for a package
def output_description(pkg_file):
       ebuild_file = pkg_file
       lines = open(ebuild_file).readlines()
       descre = re.compile("^DESCRIPTION")
       for ln in lines:
            if descre.match(ln):
                dtext = ln.split("\"")
                print "description: " + green(dtext[1])
                return

# output the latest entry in changelog
# (taken from the etcat script)
def output_log(lines, package_ver=""):
    # using simple rules that all changelog entries have a "*"
    # as the first char
    is_log = 0
    is_printed = 0

    for line in lines:
        if package_ver:
            start_entry = re.search("^[\s]*\*[\s]*(" + \
                package_ver + ")[\s]+.*(\(.*\))",line)
        else:
            start_entry = re.search("^[\s]*\*[\s]*(.*)[\s]+.*(\(.*\))",line)
        if not is_log and start_entry:
            is_printed = 1
            is_log = 1
            print green("*") + "  " + white(start_entry.group(1)) +\
                "  " + turquoise(start_entry.group(2)) + " :"
        elif is_log and re.search("^[\s]*\*[\s]*(.*)[\s]+.*(\(.*\))",line):
            break
        elif is_log:
            print line.rstrip()
        else:
            pass

    return is_printed

# print out the ChangeLog entries for package           |
# (based on the function with the same name in the etcat script)
def changes(pkg):

    matches = []
    printed = 0

    matches = get_newer_packages(pkg)
    tup = portage.catpkgsplit(pkg)

    if tup[3] != "r0":
        print "[ Changes after version " + green(tup[2]+"-"+tup[3])  + " ]"
    else:
        print "[ Changes after version " + green(tup[2])  + " ]"
    print

    changelog_file = portage.settings["PORTDIR"] + '/' + \
                           tup[0] + "/" + tup[1] + '/ChangeLog'
    if os.path.exists(changelog_file):
        for match in matches:
               output_log(open(changelog_file).readlines(),match.split('/')[1])
    else:
        print "Error: No Changelog for " + pkg
        return


# returns 1 if package pkgnew is to be updated assuming police updatewhen
# assumes that pkgnew is the same or newer version of pkginstalled
def to_update(pkginstalled,pkgnew,updatewhen):
           if pkginstalled == pkgnew or updatewhen == NEVER :
               return 0
           if updatewhen == ALWAYS:
               return 1

           pi = portage.pkgsplit(pkginstalled)
           pn = portage.pkgsplit(pkgnew)

           if updatewhen == ASK:
               print "installed: "+green(pi[1]+"-"+pi[2])
               print "latest: "+green(pn[1]+"-"+pn[2])
               asw=""
               while asw!="u" and asw!="U" and asw!="s" and asw!="S":   
                    asw = raw_input("[ "+red("U")+"pdate ] [ "+\
                                    red("S")+"kip ] [ see "+\
                                    red("C")+"hanges ] : ").lower()
                    if asw == "c":
                        changes(pkginstalled)
               print
               if asw == "s":
                   return 0
               else:
                   return 1

           vi = string.split(pi[1],".")           
           vn = string.split(pn[1],".")

           if vn[0] > vi[0]:
               return 1
           elif updatewhen == MAJOR:
               return 0

           if len(vn)==1 and len(vi)==1:
               return 0
               
           if len(vn)==1 or len(vi)==1:
               return 1

           if  vn[1] > vi[1]:
               return 1
           elif updatewhen == MINOR:
               return 0

           if len(vn)==2 and len(vi)==2:
               return 0
               
           if len(vn)==2 or len(vi)==2:
               return 1

           if vn[2] > vi[2]:
               return 1

           return 0

# get updating policy for package pkgname
# pkgname is without the version
def get_upolicy(pkgname,lines):
    for ln in lines:
        lnlist = ln.split()
        if lnlist[0]==pkgname:
            return lnlist[1]
    return NONE

# set updating policy for package pkgname
# pkgname is without the version
def set_policy(pkgname,upfile):
#    print "Package updating policy not found in "+green(UP_FILE)+". Please enter policy :"
    print "please choose updating policy for this package :"
    asw=""
    while asw!="m" and asw!="i" and asw!="r" and asw!="a" \
          and asw!="n" and asw!="k" and asw!="p" :   
       print "[ "+red("M")+"ajor ] [ m"+red("I")+"nor ] [ "+red("R")+"elease ]"
       asw = raw_input("[ "+red("A")+"lways ] [ "+red("N")+\
                       "ever ] [ as"+red("K")+" ] [ no "+\
                       red("P")+"olicy ] : ").lower()
    print
    if (asw == "m"):
        policy = MAJOR
    elif (asw == "i"):
        policy = MINOR
    elif (asw == "r"):
        policy = RELEASE
    elif (asw == "a"):
        policy = ALWAYS
    elif (asw == "n"):
        policy = NEVER
    elif (asw == "k"):
        policy = ASK
    upfile.seek(0,2)
    upfile.write(pkgname+" "+policy+"\n")
    return policy

# gets the path to the pkg ebuild
# note that pkg is a list
def find_ebuild(pkg):
    if pkg[3]!="r0":
        ebuild_name='/'+pkg[0]+"/"+pkg[1]+"/"+pkg[1]+"-"+pkg[2]+"-"+pkg[3]+'.ebuild'
    else:
        ebuild_name='/'+pkg[0]+"/"+pkg[1]+"/"+pkg[1]+"-"+pkg[2]+'.ebuild'

    if os.path.exists(portage.settings["PORTDIR"]+ebuild_name):
        file=portage.settings["PORTDIR"]+ebuild_name
    elif os.path.exists(portage.settings["PORTDIR_OVERLAY"]+ebuild_name):
        file=portage.settings["PORTDIR_OVERLAY"]+ebuild_name
    else:
        file=""
    return file   
       
# get list of packages to update
def get_update_list(lns,policy):
    up_file = file(UP_FILE,"a+")
    uplns = up_file.readlines()
    ebuildre = re.compile("^\[ebuild ")
    ulist=[]
    for ln in lns:
        if ebuildre.match(ln):
            lnlist = ln.split()
            if lnlist[1] == "U":
                newpkg = portage.catpkgsplit(lnlist[3])
                pkginstalled = newpkg[0]+"/"+newpkg[1]+"-"+lnlist[4][1:-1]
                if policy == USE_FILE:
                    pkg_policy = get_upolicy(newpkg[0]+"/"+newpkg[1],uplns)
                else:
                    pkg_policy = policy
                print "* updating policy for package ["+\
                    white(newpkg[0]+"/"+newpkg[1])+"] : "+blue(pkg_policy)
                if pkg_policy == NONE:
                    output_description(find_ebuild(newpkg))
                    print "installed: "+green(lnlist[4][1:-1])
                    print "latest: "+green(newpkg[2]+"-"+newpkg[3])

                    pkg_policy = set_policy(newpkg[0]+"/"+newpkg[1],up_file)
                if to_update(pkginstalled,lnlist[3],pkg_policy):
                    ulist.append(newpkg[0]+"/"+newpkg[1])
    return ulist


# Main

print "Generating list of all updates ..."
pp = os.popen("emerge --pretend --update --nodeps world","r")
policy=USE_FILE
extra_args=['/usr/bin/emerge','--update']
i=1
while i < len(sys.argv):
    if sys.argv[i]=="--policy":
        policy = sys.argv[i+1]
        i=i+1
    elif sys.argv[i]=="--help" or sys.argv[i]=="-h":
        print "Usage: "+sys.argv[0]+" options"
        print "options can be a combination of:"
        print "--policy (use-file | major | minor | release | always | ask)  :default = use_file: "
        print "-h --help"
        print "and any other options to insert after \"emerge --update\". (--pretend is a good one)"
        sys.exit(0)
    else:
        extra_args.append(sys.argv[i])
    i=i+1

if policy!=USE_FILE and policy!=MAJOR and policy!=MINOR and \
   policy!=RELEASE and policy!=ALWAYS and policy!=ASK:
    print "Unkown updating policy. Try --help"
    sys.exit(0)

lns = pp.readlines()
pp.close()
ulist = get_update_list(lns,policy)
out_file=open(OUT_FILE,"w")
if len(ulist) > 0:
    pkg_args = extra_args

    for pkg in ulist:
        pkg_args.append(pkg)

       
#        print "Updating "+white(pkg)+" :"
           
#       Don't know why none of these work ...
#       status = portage.spawn(string.join(pkg_args),0,1)
#        status = os.spawnv(os.P_WAIT,'/usr/bin/emerge', pkg_args)

#        if status != 0:
#            print "An error occurred when updating "+white(pkg)+". "+ \
#                      sys.argv[0]+" will exit."
#            sys.exit(0)         


        # Workaround -> write the strings to an executable text file
        out_file.write(string.join(pkg_args)+" && \\\n")
        pkg_args.pop()


    out_file.write("true")   
    out_file.close()



Deve ser copiado para um ficheiro (por exemplo c-update-world.py) e tornado executável. Depois para executar faz-se

Code:

c-update-world.py && sh ~/.upkgs_tmp
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portuguese All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum