Search code examples
pippatchpypdfidempotentrequirements.txt

Idempotent Bash Script for a Patch and requirements file


I have a python project with its own requirements file. The project also has its own virtualenv, with one of the packages being 'pyPdf'. The library has a bug and I wrote a patch to fix the bug.

--- venv/local/lib/python2.7/site-packages/pyPdf/pdf.py 2014-07-17 17:04:57.000000000 +0530
+++ pypdf_fixer.py  2014-07-19 01:19:53.176877332 +0530
@@ -1726,7 +1726,10 @@
     m.update(p_entry)
     # 5. Pass the first element of the file's file identifier array to the MD5
     # hash function.
-    m.update(id1_entry)
+    if isinstance(id1_entry, str):
+        m.update(id1_entry)
+    else:
+        m.update(id1_entry.original_bytes)
     # 6. (Revision 3 or greater) If document metadata is not being encrypted,
     # pass 4 bytes with the value 0xFFFFFFFF to the MD5 hash function.
     if rev >= 3 and not metadata_encrypt:

Lets call this file as pypdf.patch. I run the patch using the Linux patch command.

$ patch venv/local/lib/python2.7/site-packages/pyPdf/pdf.py < pypdf.patch

I need to write a bash script to do 2 things with Idempotent property(meaning running the script multiple times should be the same as running it a single time)

1) Run through the requirements file with various library requirements(example Flask==0.10.1 etc, including a git+ssh requirement) and do a pip install for all the libraries.

2) Apply the patch to the pypdf library.

Appreciate any leads on this front. Kindly take note of 'Idempotency'.

PS: My requirements.txt file looks like this.(I do a pip install - r requirements.txt under the virtualenv to install the libraries)

Flask==0.10.1
Jinja2==2.7.3
...
pyPdf==1.13
git+ssh://[email protected]/some-production-library.git

Thanks


Solution

  • For those who are looking out for similar script. Save the script in projectfolder/bin/

    #!/bin/bash -e
    
    BASEDIR=`dirname $0`/..
    
    if [ ! -d "$BASEDIR/venv" ]; then
        virtualenv $BASEDIR/venv
        echo "Virtualenv created with name venv."
    fi
    
    source $BASEDIR/venv/bin/activate
    
    if [ ! -f "$BASEDIR/venv/updated" -o $BASEDIR/requirements.txt -nt $BASEDIR/venv/updated ]; then
        pip install -r $BASEDIR/requirements.txt
        touch $BASEDIR/venv/updated
        echo "Requirements installed."
    fi
    
    patch $BASEDIR/venv/local/lib/python2.7/site-packages/pyPdf/pdf.py -p0 -N --dry-run --silent < $BASEDIR/pypdf.patch
    if [ $? -eq 0 ];
    then
        echo "applying patch"
        #apply the patch
        patch $BASEDIR/venv/local/lib/python2.7/site-packages/pyPdf/pdf.py -p0 -N < $BASEDIR/pypdf.patch
    fi