You are not logged in.

#1 2008-08-31 10:40:48

abhidg
Member
From: City of Kol
Registered: 2006-07-01
Posts: 184
Website

upgpkg: Bumps pkgver of a package, reset pkgrel to 1

Hi all,

I'm working on a script which will bump the pkgver of a package
and reset pkgrel to 1. It also starts the build and reverts
back to the old version if the build failed. However, I can't
find out a way to remove the md5sums of the old source,
so that it can be replaced.

Here's the current code:

#!/bin/sh

PKGDIR="/home/sol/arch/pkgbuilds"

err() {
    echo $1
    exit 1
}

if [ -z $1 ]; then echo "usage: upgpkg package-name newver"; exit; fi

if [ ! -d "$PKGDIR/$1" ]; then err "upgpkg: package $1 not in $PKGDIR."; fi
if [ -z $2 ]; then err "upgpkg: no new version specified for $1."; fi

cd "$PKGDIR/$1"
. PKGBUILD
if [ $(vercmp $2 $pkgver) -gt 0 ]; then
    sed -i "s/pkgver=.*$/pkgver=$2/g" PKGBUILD
    sed -i "s/pkgrel=.*$/pkgrel=1/g" PKGBUILD
else
    err "upgpkg: $1 - new version ($2) older than current $pkgver"
fi
makepkg -m
if [ $? -gt 0 ]; then
    sed -i "s/pkgver=.*$/pkgver=$pkgver/g" PKGBUILD
    sed -i "s/pkgrel=.*$/pkgrel=$pkgrel/g" PKGBUILD
    err "upgpkg: $1 - build failed for $2, reverting to $pkgver"
fi

Offline

#2 2008-08-31 18:49:29

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

You should be able to use sed with an address range... If you can guarantee that the md5sum will always be the last part of the pkgbuild it becomes simpler. I'm not sure if this will always work, but:

sed -n '/md5sum/,/)/ p' PKGBUILD
md5sums=('ff115b184d090deae11afc67f56094eb'
         'e93463bba9168624e7c8b9cf28bdc47b'
         'adc46727c22d790454b2137a8f3d2c88'
         '45050fc57c3cabf85debbfcbd4825b9e'
         '3788d4d6900bfaad04e97b47d0ac1b70'
         'e711a94744171b66ca41c8ad157fb4bd'
         '79e306084c11059d6b15bccc44557af8'
         '1e3b1c6efcca3d7ae64a85d12d769ffa'
         'b60b17497f9679ff05f19c6674f543e9'
         '0ec3dbb726830035d067c91e625dd5ed'
         '1d7fdb5a445fd9e9b1816872909c17a7'
         '3456bbd52daac40e771e7ab619e9378b')

So this should work...

 sed -i '/^md5sums=(/,/)/ d' PKGBUILD

Also, once you get this working, would you mind if I include the script in my pkgtools package?

Last edited by Daenyth (2008-08-31 18:50:16)

Offline

#3 2008-09-03 05:01:42

abhidg
Member
From: City of Kol
Registered: 2006-07-01
Posts: 184
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

Hi,
Thanks for the hint, though if the script has to be generally
applicable, it must be able to remove the md5sums array
from all PKGBUILDs (though for my PKGBUILDs it'll work).

I'll give it a try this weekend on my own PKGBUILD set, and
also on some others. Once the holes are ironed out, I think
this will go nicely with the other tools in your pkgtools package smile

Offline

#4 2008-09-03 11:14:04

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

I'm not sure, but Daenyth's suggestion may fail if the close ')' is on the same line as the 'md5sums=(' line.

I think you need to do something like this instead:

sed -r -e '/^\s*md5sums=/ { :a; /\)/d; n; ba; }'

In English: when you find a line starting with (optional whitespace) and md5sums=, then execute the following block:
{
  label a
  if the line contains a ), then delete this line and restart all processing on the next line (exiting this block)
  otherwise read the next line (if this results in the earlier lines being printed, then use "N" instead, which will append the new lines to the buffer rather than overwriting the buffer)
  and go to label a
}

The = after md5sums may need to be escaped, I'm not sure. I haven't debugged or tested this, but am just trying to convey the idea.

Last edited by Profjim (2008-09-03 11:16:00)

Offline

#5 2008-09-03 11:23:45

abhidg
Member
From: City of Kol
Registered: 2006-07-01
Posts: 184
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

@Daenyth:
The sed line you posted is giving a problem
when md5sums is only one line (it deletes the next line as well).

@Profjim:
Using your sed script, the first line of the md5sums is remaining.

I'll look into this further over the weekend. Thanks for the hints! smile

Offline

#6 2008-09-03 13:47:44

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

I'm not experienced enough to know if his will work, but it would be easy to check before running sed; grep -E '\s*md5sums=(.*)'

If his works as expected though, it's smarter than my solution.

Offline

#7 2008-09-04 00:02:08

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

OK, it's as I expected, the 'n' command is flushing the buffer to output before reading the next line. (I'd expect that every line before the terminating ')' gets printed, not just the initial md5sum= line.) Just change the 'n;' to 'N;' and it should work.

echo -e 'alpha\nbeta\ngamma\ndelta\n\epsilon' | sed -r '/beta/{:a; /delta/d; N; ba;}'

gives me this output:

alpha
epsilon

Offline

#8 2008-09-04 02:10:00

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

neat stuff Profjim.

this seems to work

cat PKGBUILD | sed -r '/md5sums[ ]?\=/{:a; /\)/d; N; ba;}'

"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#9 2008-09-04 17:32:01

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

Send me an  email or talk to me on irc when you think this is ready for me to include smile

If you want, I can give you direct git access.

Offline

#10 2008-09-21 12:15:15

Pierre
Developer
From: Bonn
Registered: 2004-07-05
Posts: 1,964
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

Does anybody know how I could replace the md5sums of a pkgbuild with makepkg -g instead of just removing them. I want to place them at the same location and not just adding them to the end.

If the md56sums are just one line the following works:

  md5=$(makepkg -g | cat)
  sed -r "s|md5sums=.*|${md5}|g" -i PKGBUILD

But I just don't get it working with several lines. Any ideas?

Offline

#11 2008-09-21 12:29:28

abhidg
Member
From: City of Kol
Registered: 2006-07-01
Posts: 184
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

I don't know how to replace the md5sums in place... but the md5sums
can be deleted and then regenerated by makepkg -g (even for
md5sums spanning multiple lines).

Anyway, here goes the upgpkg code. I've modified a bit to work nicely
with pkgtools, but it can be easily changed to make it standalone if
the user wants.

#!/bin/bash

# upgpkg: Upgrades package versions in PKGBUILD and starts build.
# Author: Abhishek Dasgupta <abhidg@gmail.com>
#         Thanks to cactus, profjim and daenyth for all the sed help!
# Requires: pkgtools.

# I place this script in the public domain.

MYVERSION=0.1
PROGNAME="upgpkg"

if [ -r /usr/share/pkgtools/functions ]; then
        source /usr/share/pkgtools/functions
else
        printf "$(gettext "upgpkg: Unable to source function file!\n")" >&2
        exit 1
fi

die() {
    local message="$1"
    shift
    printf "$message" "$@"
    exit 1
}

if [ -r /etc/pkgtools/newpkg.conf ]; then
        source /etc/pkgtools/newpkg.conf
fi
if [ -r "${HOME}/.pkgtools/newpkg.conf" ]; then
        source "${HOME}/.pkgtools/newpkg.conf"
fi

if [ -z $BASEDIR ]; then die "$(gettext "upgpkg: unable to locate BASEDIR in configuration.")"; fi

if [ -z $1 ]; then printf "upgpkg %s\n" "$MYVERSION"; printf "$(gettext "usage: upgpkg package-name newver\n")"; exit; fi

if [ ! -d "$BASEDIR/$1" ]; then die "$(gettext "upgpkg: package %s not in %s.\n")" "$1" "$BASEDIR"; fi
if [ -z $2 ]; then die "$(gettext "upgpkg: no new version specified for %s\n")" "$1"; fi

# Main code follows

cd "$BASEDIR/$1"
sed -ri '/md5sums[ ]?\=/{:a; /\)/d; N; ba;}' PKGBUILD || die "upgpkg: could not bump pkgver of $1\n"
source PKGBUILD

if [ $(vercmp $2 $pkgver) -gt 0 ]; then
    sed -i "s/pkgver=.*$/pkgver=$2/g" PKGBUILD
    sed -i "s/pkgrel=.*$/pkgrel=1/g" PKGBUILD
    makepkg -g >> PKGBUILD
else
    die "$(gettext "upgpkg: %s - new version (%s) older or equal to current %s\n")" "$1" "$2" "$pkgver"
fi
makepkg -m
if [ $? -gt 0 ]; then
    sed -i "s/pkgver=.*$/pkgver=$pkgver/g" PKGBUILD
    sed -i "s/pkgrel=.*$/pkgrel=$pkgrel/g" PKGBUILD
    die "$(gettext "upgpkg: %s - build failed for %s, reverting to %s\n")" "$1" "$2" "$pkgver"
fi

Offline

#12 2008-09-21 12:46:29

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

You could use grep to find the line number of the old md5sums and then used sed's insert function to add them in the same place. I think it's much more KISS to add it to the end.

Also, regarding pkgtools, I just want to make a few tweaks.. I think your gettext stuff isn't complete (no pot file, no domain specified), so I was thinking I'd gettextify all mine (luckily I designed my functions with i18n in mind), and I also wanted to have your script use my functions instead of the ones in there. It's on my todo list... don't know when I'll have the motivation and the time at the same moment.


EDIT: Just realized you are already using the functions.. haha. I think I'll add the gettext stuff there. (Domain, etc). I was wondering, why did you use a different die()?

Last edited by Daenyth (2008-09-21 12:48:47)

Offline

#13 2008-09-21 13:06:36

abhidg
Member
From: City of Kol
Registered: 2006-07-01
Posts: 184
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

Ah, I think I misunderstood the line

 warn "${warning:-Unknown error}" "$@"

Initially, I thought it would print "warning" before every error. It is
actually to print "-Unknown error" in case ${warning} is null, right?
In that case, my die() isn't required. smile

Offline

#14 2008-09-21 14:02:04

Pierre
Developer
From: Bonn
Registered: 2004-07-05
Posts: 1,964
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

This one is nearly working: sed -e '1h;1!H;${;g;s/md5sums=[^\)]*)/md5sums/g;p;}' -e "s/md5sums/$(echo $(makepkg -g))/g"  PKGBUILD

Offline

#15 2008-09-21 14:55:05

Pierre
Developer
From: Bonn
Registered: 2004-07-05
Posts: 1,964
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

OK, I think sed is not the right tool for this:

makepkg -g | php -n -r 'file_put_contents("PKGBUILD", preg_replace("/md5sums=\([^\)]+\)/s", file_get_contents("php://stdin"), file_get_contents("PKGBUILD")));'

;-)

Offline

#16 2008-09-21 16:04:28

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

abhidg wrote:

Initially, I thought it would print "warning" before every error. It is
actually to print "-Unknown error" in case ${warning} is null, right?
In that case, my die() isn't required. smile

Correct, it uses $warning if it has content, but if it does not, then it uses "Unknown error"

Pierre wrote:

OK, I think sed is not the right tool for this:

makepkg -g | php -n -r 'file_put_contents("PKGBUILD", preg_replace("/md5sums=\([^\)]+\)/s", file_get_contents("php://stdin"), file_get_contents("PKGBUILD")));'

;-)

I strongly recommend against that. I want to keep the dependencies to a minimum if I include this, so please stick to coreutils if possible, and if not that, then python or perl. Awk should be able to do this, by the way.

Offline

#17 2008-09-21 16:19:31

Pierre
Developer
From: Bonn
Registered: 2004-07-05
Posts: 1,964
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

Don't mind. It's just for my personal use. :-)

Offline

#18 2008-09-21 17:01:33

foutrelis
Developer
From: Athens, Greece
Registered: 2008-07-28
Posts: 705
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

Daenyth wrote:

Awk should be able to do this, by the way.

Did someone order two pounds of ugly code? tongue

{ rm PKGBUILD; awk '$0 ~ /^md5sums/ {i = 1; system("makepkg -g 2>/dev/null")}; !i {print}; $0 ~ /\)/ {i = 0}' > PKGBUILD; } < PKGBUILD

Last edited by foutrelis (2008-09-21 17:02:01)

Offline

#19 2008-09-21 18:26:43

Pierre
Developer
From: Bonn
Registered: 2004-07-05
Posts: 1,964
Website

Re: upgpkg: Bumps pkgver of a package, reset pkgrel to 1

Great one. I must say this is a little above of my awk knowledge. :-)

Offline

Board footer

Powered by FluxBB