Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Keyupdate - Script for updating keywords in ebuilds
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Gentoo on AMD64
View previous topic :: View next topic  
Author Message
Camoes
Tux's lil' helper
Tux's lil' helper


Joined: 09 Jan 2003
Posts: 125
Location: Munich, Germany

PostPosted: Mon Feb 23, 2004 2:15 am    Post subject: Keyupdate - Script for updating keywords in ebuilds Reply with quote

Update: :!: Removed severe bug, get version below, or the script could break your ebuilds, sorry :(
Update: Added new functionality(multiple keywords, load lists from file)

Hi,
for all of you who are tired of manually updating your keywords in the ebuilds, i wrote a little ruby script which does this automatically.

It's written in ruby, so you might do a
Code:
emerge ruby
before you can use it. The usage is quite simple. Either Copy/Paste the script bellow in file called keyupdate for example, and make this file executable.
You can also download the file here
Then run it with
Code:
./keyupdate directory_to_update

the script will then update _all_ ebuilds under that path. You also can update single files or multiple arguments like
Code:
./keyupdate dir file dir dir

Note, also all subdirs of dirs etc. will be updated

after an update keyupdate will ask you if it shall store the list of files updated (if you provided the -s option). If you do this, next time you run keyupdate you can load this list with
Code:
keyupdate -f file

then it will also update this files again. This comes in handy if you have some files that you update regulary (for example, if portage overwrites it again ;))

Code:
keyupdate -s
tells keyupdate to save the list after the update

you can also add different use flags with
Code:
keyupdate -k keyword

if you don't specify a keyword, the script will assume ~amd64 by default !!!

of course all of these options can be combined in every way you like.

Well enough talking, here is the script
And of course, use it on your own risk ! Playing around with portage can break your system

Code:

#!/usr/bin/ruby
#Author: max at unmuted.de
#Desc: Script used to update ebuilds with new keywords
########################################################

#Available Keywords and standard keyword
Keywords = ["~amd64", "~x86", "~ppc", "~sparc", "~mips" , "~alpha", "~arm", "~hppa", "~ia64"]
@keyword = "~amd64"

#update a given file with new keyword, if keyword is not allready present in file
def update(file)
  printf "Updating " + file + " for #{@keyword}..."
  sed = "sed -e '/KEYWORDS=\".*#{@keyword}/bexit
  s:^KEYWORDS=\":KEYWORDS=\"#{@keyword} :
  :exit' -i " + file
  result = `#{sed}`
  printf "  done\n"
end

#run recursivly through all subdirs and store all ebuilds in @files
def search_dirs(dir)
  printf "checking directory " + dir + "\n"
  portage_check = File.expand_path(dir)
  if portage_check == "/usr/portage" then
    printf "Dont run this script over /usr/portage, it will break your system !\n"
    return
  end
  tocheck = Dir.entries(dir)
  tocheck.each {
    |current|
    if current == "." or current == ".." then
      next
    else
      current = dir + "/" + current
      if File.directory?(current)
        search_dirs(current)
      else
        parts = current.split('.')
   if parts.last.to_s == "ebuild" then
          @files << current
   end
      end
    end
  }
end

#tell script to get all ebuilds if dir is given and update them, or update single file directly
def gather(locations)
  @files = Array.new
  locations.each{
    |file|
    if File.exist?(file) then
      if File.directory?(file) then
        printf "\nDirectory update\n"
        search_dirs(file)
        @files.each {
          |current|
     update(current)
        }
      else
        @files << file
        printf "Single file update\n"
        update(file)
      end
    else
      printf "\nError: file specified does not exist !\n"
      exit
    end
  }
end

#ask if ebuild list should be saved, if yes ask for path and save it there
def check_save
  printf "\nShall i save the updated files ? (y/n) "
  input = gets
  if input.chomp! == "y" then
    printf "Enter filename (#{@filename}): "
    filename = gets
    if filename != "\n" then
      @filename = filename.chomp!
    end
    File.open(@filename, File::CREAT|File::APPEND|File::RDWR){
      |file|
      existing = file.readlines
      existing.each{ |line| line.chomp! }
      @files = @files - existing
      @files = @files.join("\n")
      file.write(@files)
    }
    printf "Stored list in " + @filename + "\n"
  end
end

#print usage message
def usage
  if @error then
    printf @error + "\n"
  end
  printf "update64 usage: update64 [OPTIONS] <file>|<dir> [<file>|<dir>|...]\n"
  printf "\nOptions:\n"
  printf "   -k <keyword>              keyword to update\n"
  printf "   -f <file>         file where list is stored which files shall be updated\n"
  printf "   -s             save updated list to file\n\n"
  printf "Available Keywords:\n"
  printf "   " + Keywords.join("\n   ") + "\n"
end


#### main() ###########################################################################################
printf "keyupdate - Ruby script for updating keywords in ebuilds\n"
if !ARGV[0] or ARGV[0] == '' then
  usage
else
  if ARGV.include?("-k") then
    index = ARGV.index("-k")
    if Keywords.include?(ARGV[index+1].to_s) then
      printf "Using keyword " + ARGV[index+1] + "\n"
      @keyword = ARGV[index+1].to_s
      ARGV.delete_at(index)
      ARGV.delete(@keyword)
      @list = ARGV
    else
      @error = "Error: Keyword not available"
      usage
      exit
    end
  end
  if ARGV.include?("-f") then
    index = ARGV.index("-f")
    if File.exist?(ARGV[index+1].to_s) then
      @filename = ARGV[index+1]
      printf "Reading from file " + @filename + "\n"
      File.open(@filename , File::RDONLY) {
        |file|
   touse = file.readlines
   touse.each{ |line| line.chomp! }
        ARGV.delete_at(index)
        ARGV.delete(@filename)
   @list = ARGV + touse
      }
    else
      @error = "Error: File does not exist"
      usage
      exit
    end
  end
  if ARGV.include?("-s") then
    ARGV.delete("-s")
    @save = true
  end
  if !@list or @list.size == 0 then
    @list = ARGV
  end
  @list.uniq!
  gather(@list)
  ARGV.clear
  check_save if @save
end



for critics and bug report, either post here or mail to max at unmuted.de
_________________
e = pi = 3
breakmygentoo


Last edited by Camoes on Wed Feb 25, 2004 4:01 pm; edited 8 times in total
Back to top
View user's profile Send private message
ectospasm
l33t
l33t


Joined: 19 Feb 2003
Posts: 711
Location: Mobile, AL, USA

PostPosted: Mon Feb 23, 2004 3:01 am    Post subject: Reply with quote

This will probably come in handy when I get my new dual-Opteron system up and running. Thanks a lot!
_________________
Join the adopt an unanswered post initiative today
Join the EFF!
Join the Drug Policy Alliance!
Back to top
View user's profile Send private message
NewBlackDak
Guru
Guru


Joined: 02 Nov 2003
Posts: 512
Location: Utah County, UT

PostPosted: Mon Feb 23, 2004 3:03 am    Post subject: Reply with quote

I know you have your little dislaimer in there about not running it on /usr/portage, but you might want fix it up so that it WONT run on /usr/portage . I know it could be taken out easily, but it would keep the knuckleheads from completely roasting their system.
_________________
Gentoo systems.
X2 4200+@2.6 - Athy
X2 3600+ - Myth
UltraSparc5 440 - sparcy
Back to top
View user's profile Send private message
Camoes
Tux's lil' helper
Tux's lil' helper


Joined: 09 Jan 2003
Posts: 125
Location: Munich, Germany

PostPosted: Mon Feb 23, 2004 3:34 am    Post subject: Reply with quote

Ok added the check over /usr/portage. You were right there.

Btw, the script is now hardcoded to add ~amd64 to the keywords. I did this cause of my lazyness, as i don't like to add the keyword everytime to the script.
But it would be no deal to change this to flexible keywords via command-line.
If you want this, just shout :)

Edit:
Another update: if the script runs throug dirs, it checks just files with the suffix .ebuild. If you point it directly to a file, it doesnt care
_________________
e = pi = 3
breakmygentoo
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Gentoo on AMD64 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