You are not logged in.

#1 2007-09-17 03:26:58

Jessehk
Member
From: Toronto, Ontario, Canada
Registered: 2007-01-16
Posts: 152

compac -- a small bash script to compile a package in ABS. Any tips?

I'm a beginner in Bash scripting (though not to programming) and I thought I'd write a little script to first move a PKGBUILD and related files to a user specifed location and then compile them.

Basically,

compac vim

Which would copy all the files from wherever vim is in the abs tree into /var/abs/local/vim, and run a makepkg.
I know yaourt does this as well, but I had very specific behavior in mind and I thought it would be good practise.

So, does anyone have any tips that could help improve this script? I'm very happy to take all suggestions and criticisms.

# compac -- A script to automate the automatic compilation
# of a package through makepkg and abs.
#
# By Jessehk on Tuesday, September 11 2007
# Released into the public domain
#
# Currently tied to pacman's search output format.

MAKEPKG_OPTS="-s"
ABS_DIR=/var/abs
LOCAL_ABS_DIR=/var/abs/local

usage_and_exit() {
    echo Usage: $(basename $0) [package]
    exit 1
}

check_tree() {
    if [ -d $ABS_DIR ]; then
        cd $ABS_DIR
        # --- How could I check to see if the directory has the valid contents of an abs tree?
        #if [ -z $(ls) ]; then
        #    echo No abs tree found. Download a valid abs tree.
        #    usage_and_exit
        #else
            if [ ! -d $LOCAL_ABS_DIR ]; then
                echo Local package folder not found. Creating at $LOCAL_ABS_DIR
                sudo mkdir $LOCAL_ABS_DIR
            fi
        #fi
    fi
}

find_match() {
    match=$(find $ABS_DIR -name "$1" | grep -v "$LOCAL_ABS_DIR")
    
    if [ -z "$match" ]; then
        echo No packages match the name \"$1\"
        exit 1
    fi
    
    pkg_name=$(basename $match)
}

yesno() {
    printf "$@"
    read response
    
    if echo "$response" | grep "^[yY]" > /dev/null; then
        return 0
    else
        return 1
    fi
}

user_edit() {
    if yesno "Edit the PKGBUILD prior to compilation?: "; then
        if [ -z "$EDITOR" ]; then
            echo '$EDITOR variable has not been set. Exiting.'
            exit 1
        fi
        
        sudo $EDITOR $LOCAL_ABS_DIR/$pkg_name/PKGBUILD
    fi
}

check_existing() {
    if [ -d $LOCAL_ABS_DIR/$pkg_name ]; then
        echo $LOCAL_ABS_DIR/$pkg_name exists
        
        if yesno "Should its contents be deleted?: "; then
            sudo rm -r $LOCAL_ABS_DIR/$pkg_name/*
        else
            echo Contents of $LOCAL_ABS_DIR/$pkg_name will be unchanged
            exit 1
       fi
    else
        sudo mkdir $LOCAL_ABS_DIR/$pkg_name
    fi
}

if [ $# -lt 1 ]; then
    usage_and_exit
fi

check_tree
find_match "$1"
check_existing
    
sudo cp $match/* $LOCAL_ABS_DIR/$pkg_name
user_edit
cd $LOCAL_ABS_DIR/$pkg_name && sudo makepkg "$MAKEPKG_OPTS"

PS: Does anyone "get" the name? wink

Last edited by Jessehk (2007-09-17 03:27:23)

Offline

#2 2007-09-17 09:50:25

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: compac -- a small bash script to compile a package in ABS. Any tips?

Jessehk wrote:

PS: Does anyone "get" the name? wink

Didn't you already give the answer? smile

# compac -- A script to automate the automatic compilation
# of a package through makepkg and abs.


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#3 2007-09-17 16:42:34

wain
Member
From: France
Registered: 2005-05-01
Posts: 289
Website

Re: compac -- a small bash script to compile a package in ABS. Any tips?

I hate abs system but this is a nice little script smile

two  things:

1. I don't like "sudo makepkg" because package are built with root permissions (very bad). Maybe you can use a specific user with write permissions in /var/abs/.

2. The size of the /var/abs/ directory will grow too much. You should add something to clean /var/abs/  (src, pkg)

Last edited by wain (2007-09-17 19:46:13)

Offline

#4 2007-09-17 19:35:41

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: compac -- a small bash script to compile a package in ABS. Any tips?

I always recommend building packages under your $HOME, with fakeroot.

Offline

#5 2007-09-18 21:37:29

Jessehk
Member
From: Toronto, Ontario, Canada
Registered: 2007-01-16
Posts: 152

Re: compac -- a small bash script to compile a package in ABS. Any tips?

Thanks for all the tips guys. I'll make the suggested changes and post the finished result. smile

Offline

#6 2007-09-18 21:43:26

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

Re: compac -- a small bash script to compile a package in ABS. Any tips?

Cool. I like to see these kinds of "grass roots" projects.
It is a fun way to learn, and to play with something new.
smile


"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

#7 2008-01-06 02:27:35

Jessehk
Member
From: Toronto, Ontario, Canada
Registered: 2007-01-16
Posts: 152

Re: compac -- a small bash script to compile a package in ABS. Any tips?

I've changed the script around and renamed it to pacport.

#!/bin/bash

# pacport -- a small script to find, build, and install a package
# in the ABS tree.
# Released into the public domain.
# By Jessehk

# Make sure you the UID that runs
# this script can write to this directory!
LOCAL_ABS_DIR=/var/abs/local
MAKEPKG_OPTS="-bc"

get_abs_dir() {
    source /etc/abs/abs.conf
}

usage_and_exit() {
    echo "Usage: $(basename $0) [package]"
    exit 1
}

check_tree() {
    if [ -d $ABSROOT ]; then
        cd $ABSROOT
        
        if ls | grep -E "core|extra" > /dev/null ; then
            echo "==> Found valid abs tree at $ABSROOT"
        else
            echo "==> Unable to find "core" or "extra" repository in $ABSROOT"
            echo '==> Have you downloaded the abs tree?'
            exit 1
        fi
    else
        echo "==> abs dir $ABSROOT does not exist."
        exit 1
    fi
    
    if [ ! -d $LOCAL_ABS_DIR ]; then
        echo "==> Local abs dir does not exist."
        exit 1
    else
        echo "==> Found local abs directory at $LOCAL_ABS_DIR"
    fi
    
    return 0
}

find_match() {
    match=$(find $ABSROOT -name "$1" | grep -v "$LOCAL_ABS_DIR")
    
    if [ -z "$match" ]; then
        echo "==> No packages match the name '$1'."
        exit 1
    fi
    
    echo "==> Found $match"
    if yes_no "Build $match? [n]: "; then
        pkg_name=$(basename $match)
    else
        exit 0
    fi
}

yes_no() {
    printf "$@"
    read response
    
    return $(echo "$response" | grep "^[yY]" > /dev/null)
}

user_edit() {
    if yes_no "Edit the PKGBUILD prior to compilation? [no]: "; then
        if [ -z "$EDITOR" ]; then
            echo "==> '$EDITOR' has not been set. Quitting."
            exit 1
        fi
        
        $EDITOR $LOCAL_ABS_DIR/$pkg_name/PKGBUILD
    fi
}


check_already_installed() {
    if pacman -Q | grep $pkg_name > /dev/null; then
        echo "==> $pkg_name was previously installed with pacman."
        if yes_no "Upgrade $pkg_name to local version? [no]: "; then
            sudo pacman -U $pkg_name-*.pkg.tar.gz
        else
            exit 0
        fi
    fi
}

check_existing() {
    if [ -d $LOCAL_ABS_DIR/$pkg_name ]; then
        echo "==> $LOCAL_ABS_DIR/$pkg_name exists."
        
        if yes_no "Should its contents be deleted? [no]: "; then
            rm -r $LOCAL_ABS_DIR/$pkg_name/*
            echo "==> The contents of $LOCAL_ABS_DIR/$pkg_name were deleted."
            return 1
        else
            echo "==> Existing files will not be deleted."
            
            if [ -e $LOCAL_ABS_DIR/$pkg_name/*.pkg.tar.gz ]; then
                echo "==> A $pkg_name package exists in $LOCAL_ABS_DIR/$pkg_name"
                printf "==> Delete or install it? [delete]: "
                
                read response
                if echo $response | grep "^[iI]" > /dev/null; then
                    check_already_installed
                else
                    rm $LOCAL_ABS_DIR/$pkg_name/*.pkg.tar.gz
                    echo "==> Existing package deleted."
                fi
            fi
                  
            return 0
        fi
    else
        mkdir $LOCAL_ABS_DIR/$pkg_name
        return 1
    fi
}

if [ $# -lt 1 ]; then
    usage_and_exit
fi

get_abs_dir
check_tree
find_match "$1"
check_existing
cp $match/* $LOCAL_ABS_DIR/$pkg_name
user_edit
cd $LOCAL_ABS_DIR/$pkg_name
makepkg $MAKEPKG_OPTS || return 1

if yes_no "Install it? [no]: "; then
    check_already_installed
    sudo pacman -U $pkg_name-*.pkg.tar.gz
fi

exit 0

pacport in action:

$ pacport nano
==> Found valid abs tree at /var/abs/
==> Found local abs directory at /var/abs/local
==> Found /var/abs/core/base/nano
Build /var/abs/core/base/nano? [n]: y
==> /var/abs/local/nano exists.
Should its contents be deleted? [no]: n
==> Existing files will not be deleted.
Edit the PKGBUILD prior to compilation? [no]: n

### makepkg output snipped ###

Install it? [no]: y

### pacman output snipped ###

or

$ pacport foobar
==> Found valid abs tree at /var/abs/
==> Found local abs directory at /var/abs/local
==> No packages match the name 'foobar'.

I don't know if will be useful to anyone, but I certainly find it saves time. Plus, it was fun to write.
Thanks for looking. smile

Offline

#8 2009-04-06 06:08:22

Koonboi
Member
From: El Ghetto
Registered: 2008-08-14
Posts: 59

Re: compac -- a small bash script to compile a package in ABS. Any tips?

I just made a slightly edited version of the script for myself, that copies it to a file in my home directory instead of /var/abs, and some other things. Thank you for this code, it is a BIG time-saver.


"We're all mad here. I'm mad. You're mad."

Offline

#9 2009-04-10 02:13:21

vkumar
Member
Registered: 2008-10-06
Posts: 166

Re: compac -- a small bash script to compile a package in ABS. Any tips?

This is great!

Us lowly i686 users will be needing it soon, seeing as Arch is leaving us behind in order to move to bigger and better things big_smile

edit:
here's a suggestion; find a nicer place to put your script. If you update it, people won't find out unless they check this thread.

Google Docs will let you publish files (like this), for example. I'm sure there are other free options.
If you have a server, you could use that..

Last edited by vkumar (2009-04-10 02:18:11)


div curl F = 0

Offline

#10 2009-04-10 02:47:48

Ghost1227
Forum Fellow
From: Omaha, NE, USA
Registered: 2008-04-21
Posts: 1,422
Website

Re: compac -- a small bash script to compile a package in ABS. Any tips?

Take a look at my makeaur script (it's in the AUR). It should give you a few ideas.


.:[My Blog] || [My GitHub]:.

Offline

#11 2009-04-10 11:09:10

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: compac -- a small bash script to compile a package in ABS. Any tips?

vkumar wrote:

Us lowly i686 users will be needing it soon, seeing as Arch is leaving us behind in order to move to bigger and better things

Not sure if youre being serious here, vkumar, but just in case...
http://www.archlinux.org/news/441/  smile

Offline

Board footer

Powered by FluxBB