Archives

Categories

Creating WordPress Packages

deb http://www.coker.com.au wheezy wordpress

I maintain Debian packages of a number of WordPress themes and plugins for my personal use which I am not planning to upload to Debian due to the maintenance and security issues. Generally the way things work with WordPress packages (and apparently most things in PHP) is that new versions are released whenever the author feels like it with little documentation and often now way of determining whether it’s a security issue. When there is a security issue it’s often fixed in a new version that includes new features giving no good option for someone who was happy with the old functionality and just wants a secure system. This isn’t the way we like to do things in Debian.

The result of this is that I maintain a number of packages for my personal use (and for the benefit of any interested people on the Internet) that often get new updates. I’ve written the below script to create a new version of a Debian package. It searches my repository for the most recent .debian.tar.gz file for the package, applies that, runs dch -i to update the changelog, and then builds the package. So far this has only been tested on one package, I expect that I’ll have to put a sed command in there to cover the case where the zip file name doesn’t match what I want as the package name and I’ll probably find other bugs in future, but I think it’s good enough to publish now.

#!/bin/bash
set -e
REPOSITORY=/home/whatever
unzip $1
FILE=$(basename $1)
PACKAGE=$(echo $FILE | sed -e "s/\..*$//")
LEN=$(($(echo $PACKAGE | wc -c)+1))
VER=$(echo $FILE | cut -c ${LEN}-200 | sed -e s/.zip//)
DIRNAME=wordpress-${PACKAGE}-${VER}
mv $PACKAGE $DIRNAME
tar czf wordpress-${PACKAGE}_${VER}.orig.tar.gz ${DIRNAME}
cd $DIRNAME
tar xzf $(ls -tr ${REPOSITORY}/wordpress-${PACKAGE}_*.debian.tar.gz | tail -1)
dch -i
dpkg-buildpackage

Any suggestions for improvement will be welcome, I don’t claim to be the world’s greatest shell scripter. But please note that I generally aim to write shell scripts that can be understood by people who aren’t experts. So if you can replace the program with a single line of Perl I will be impressed but I won’t implement your solution.

3 comments to Creating WordPress Packages

  • Daniel

    PACKAGE=$(echo $FILE | sed -e "s/\..*$//")
    can be done without a fork directly in the shell with parameter expansion:
    PACKAGE=${FILE%%\.*}
    (Evil evil forks, stealing system time! :-) )

    (And FILE=$(basename $1) could in the same vein be FILE=${1##*/})

    LEN=$(($(echo $PACKAGE | wc -c)+1))
    gives the length of $PACKAGE + 2 since the newline from echo is counted as an extra character, and then you add 1. The shell already keeps track of the length of the variable, so it could be done as LEN=$((${#PACKAGE}+2)). There is another subtle issue here: wc -c counts bytes and will thus count unicode characters as “multiple bytes”. wc -m counts characters and takes care of this (the given parameter expansion also takes care of this).

    printf '%s' "${VARIABLE}" is often preferred programmatically before echo "${VARIABLE}" since printf is consistently defined across all environments, and also avoids all potential parameter problems (dual blank spaces collapsing, statements beginning with -, etc.).

    Without ending argument (200) “cut -c” cuts to end of line anyway.

    Right now the script will do unexpected things if the input argument contains spaces (just some quoting is needed to solve this) or begins with – (the “–” construct after options have been given to e.g. mv and such solves this), which might never be the case, but it is often good to have consistent behavior from the start.

    You also don’t use any Bash-specific functions, so a POSIX compliant #!/bin/sh works just as well.

    Implemented, this would read (with excessive comments littered all over the place, added preamble and a “never tested”-disclaimer):

    #!/bin/sh

    ###
    #Just a boilerplate shell script preamble that I use
    usage()
    {
    cat </dev/null

    If this is more readable or not (after stripping excessive comments – probably all of them) depends on whether the reader is used to parameter expansion or not, I guess, which is difficult to know without a crystal ball. Feel free to implement what you want, or nothing, of the above :-) .

  • Daniel

    Hehe, the WordPress setup did not like the script I just pasted and ate it all. See this paste at paste.debian.net instead, or this one at pastebin.com (nicely syntax highlighted).

  • At http://apt.singpolyma.net I maintain a whole bunch of WordPress packages that are all auto-generated from script. I can release the scripts if you are interested.