You are not logged in.

#1 2011-01-31 16:47:42

CHPE
Member
Registered: 2010-12-05
Posts: 32

pacso - a Concept For Generating PKGBUILD from Source Files Using Bash

Hello!

Since the tools of this manner are mostly outdated, I've decided to start writing a simple script to handle generating PKGBUILDs for source tarballs and installing them using Pacman. Following Arch's philosophy, I've created a simple Bash script, which you could try out here: https://sourceforge.net/projects/pacso/ … o/download . It features name and version generation,  downloading of the file from a remote HTTP server, PKGBUILD review and edition using external editor. It has a simple ./configure - make - make install script included, which is not enough, but can be easily edited to suit your needs.

This script is really limited to my knowledge in Bash and I'm sure there's plenty of mess in there, but I'm willing to learn and work on this project, keeping it simple yet functional.

Since it appears to be a just a concept right now, I'm greatly appreciating any tips and ideas on how to make this better.
Cheers.

Last edited by CHPE (2011-02-14 20:05:37)

Offline

#2 2011-01-31 17:16:03

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: pacso - a Concept For Generating PKGBUILD from Source Files Using Bash

I think you can call echo (or printf) just once to print the whole block of text. Instead of

echo >> PKGBUILD "# This PKGBUILD was generated by pacso, an open source tool for "
echo >> PKGBUILD "# compiling and installing source files using pacman, the packet"
echo >> PKGBUILD "# manager of Arch Linux."
echo >> PKGBUILD "#"
echo >> PKGBUILD "# pacso is released under the GPL."
echo >> PKGBUILD "#"
echo >> PKGBUILD "# pacso is provided as-is and comes with no warranty."
echo >> PKGBUILD "# Use only at your own risk!"
echo >> PKGBUILD "# "
echo >> PKGBUILD ""

do

echo "# This PKGBUILD was generated by pacso, an open source tool for
# compiling and installing source files using pacman, the packet
# manager of Arch Linux.
" >> PKGBUILD

and instead of

# Install script
echo >> PKGBUILD $(echo 'build() {')
echo >> PKGBUILD $(echo 'cd "$srcdir/$pkgname-$pkgver"')
echo >> PKGBUILD $(echo './configure')
echo >> PKGBUILD $(echo 'make PREFIX=/usr sysconfdir=/etc localstatedir=/var')
echo >> PKGBUILD $(echo '}')
echo >> PKGBUILD $(echo 'package ()')
echo >> PKGBUILD $(echo '{')
echo >> PKGBUILD $(echo 'cd "$srcdir/$pkgname-$pkgver"')
echo >> PKGBUILD $(echo 'make PREFIX=/usr sysconfdir=/etc localstatedir=/var DESTDIR=$pkgdir install')
echo >> PKGBUILD $(echo '}')
echo >> PKGBUILD md5sums=$(echo "('"$md5sum"')")

do

# Install script
echo "build() {
cd "$srcdir/$pkgname-$pkgver"
./configure
make PREFIX=/usr sysconfdir=/etc localstatedir=/var
}
package ()
{
cd "$srcdir/$pkgname-$pkgver"
make PREFIX=/usr sysconfdir=/etc localstatedir=/var DESTDIR=$pkgdir install
}
md5sums=('"$md5sum"')" >> PKGBUILD

Am I missing something?


I haven't tested it, but will this work for ftp:// and e.g. tar.bz2 files?

# We copy the file to our destination dir
if [[ $cd = "http://"* ]];
file=(*.tar.gz)
# Checking wether a configure fail is present
if ! tar -tvf $file | grep -q configure;

Last edited by karol (2011-01-31 17:45:48)

Offline

#3 2011-01-31 18:20:25

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: pacso - a Concept For Generating PKGBUILD from Source Files Using Bash

Since you're (implicitly) asking for bash tips, here's a "confirm" function i frequently use for asking Yes/No questions. Might help to clean up the main code a little...

I've added some comments to explain what's going on in detail:

#!/bin/bash

confirm () {    
    # ask the question and read the reply...

    printf "$1 (y|n) " # print out the first argument (the question)
    read -n1           # read only one character (no need to press return)
    echo               # line feed

    # the reply is now stored in $REPLY by default.

    # test the reply using a regexp... " =~ Y|y" means "match either y or Y"
    # we're using "short circuit logic" here (using &&, e.g. logical and) to return 
    # success or failure

    [[ "$REPLY" =~ Y|y ]] && return 0 
    [[ "$REPLY" =~ N|n ]] && return 1 

    # if we end up here, the user has typed something other than y or
    # n, so we ask the question again using recursion...

    printf 'Please answer y or n.\n'
    confirm "$1"
}

# now we can ask questions a little more elegantly in our main code. 
# Since the function returns either true (0) or false (1), we can use it 
# directly in an if statement:

if confirm "Directory does not exist. Do you want to create it?"; then
    printf "Creating directory...\n"
else
    printf "Not creating directory...\n"
fi

Offline

#4 2011-01-31 18:42:23

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: pacso - a Concept For Generating PKGBUILD from Source Files Using Bash

@Karol: Yes, you're missing something wink

In your multiline echo statement for the PKGBUILD, variables like $srcdir will be evaluated, which is probably not intended. You'll have to use escapes like \$srcdir to avoid that. Also, juggling with quotes can be avoided by using a "here document".

The common idiom for this is:

md5sum="af876..." 

# we want to put the $md5sum into the string, but 
# we want to have "$srcdir" and "$pkgdir" to appear literally, so we 
# need to quote those (e.g. "\$srcdir"...) 

(cat <<EOT
build() {
  cd "\$srcdir/\$pkgdir"
  ...
md5sums=('$md5sum')
EOT
) >> PKGBUILD

Note that the second EOT needs to be on a single line, without any leading or trailing whitespace.

Offline

#5 2011-02-14 20:04:24

CHPE
Member
Registered: 2010-12-05
Posts: 32

Re: pacso - a Concept For Generating PKGBUILD from Source Files Using Bash

Thanks for the help!

Here you go, some improvements:
* ftp:// servers and .tar.bz2 source support
* arguments:
   -s to regularly install a source file
   -c to check for configure script existence
* better pkgname and pkgver matching (somewhat...)
* added licence and makedepends to PKGBUILD
* tidied up some code...

If you find this tool useful, you may want to copy it to your bin:

# wget http://master.dl.sourceforge.net/project/pacso/pacso -O /usr/local/bin/pacso && chmod +x /usr/local/bin/pacso 

Last edited by CHPE (2011-02-14 20:08:14)

Offline

Board footer

Powered by FluxBB