My blog

My place to bitch!

WordPress: Automatic plugin upgrade with python

Posted by admin On December - 29 - 2008

Since I have been unable to find anything that automagically upgrades wordpress plugins from a script, I decided to write my own in python. This basically logs in and presses the ‘upgrade’ button for each of the plugins that have an upgrade available. The script does not support the FTP login for the upgrade process… so you’re out of luck if you need that.

I’ve really only tested this on wordpress 2.7.

Only basic error detection is done, so don’t blame me if this breaks something :)   Code follows…

The python modules mechanize and BeautifulSoup are required.


from mechanize import Browser, FormNotFoundError
from BeautifulSoup import BeautifulStoneSoup
import sys,re

def strip_tags(value):
    "Return the given HTML with all tags stripped."
    return re.sub(r'<[^>]*?>', '', value) 

def usage():
    print 'Usage: ./ <url> <username> <password>'
    print '   Where <url> is similar to:'

if __name__ == '__main__':
        host = sys.argv[1]
        username = sys.argv[2]
        password = sys.argv[3]

    #plugin name
    name_regex = re.compile(r'plugin=(?P<name>[\w-]*)')
    success_regex = re.compile(r'Plugin upgraded successfully')
    #ftp connection required
    ftp_regex = re.compile(r'FTP connection information is required')

    br = Browser()

    except Exception, e:
        print 'Unable to log into: %s'%host
        print e

    br["log"] = username
    br["pwd"] = password

    r = br.submit()

    # get to the plugin page
    r = br.follow_link(url="plugins.php")

    # get all the upgrade links into a list
    upgrade_list = [link for link in br.links(url_regex="update")]

    for link in upgrade_list:
        m = link.url )
        if m:
            print 'Updating: %s' %'name')
            r = br.follow_link( url=link.url )
            html =
            soup = BeautifulStoneSoup(html)
            result = str(soup.findAll('div', {'class':'wrap'})[0])
            # strip the html
            result = strip_tags(result)
            m = ( result )
            if m:
                print ' ++ FAILURE ++'
                print "Sorry, updating plugins via ftp not supported"
            # search for the success string
            m =
            if m:
                print '  ==> success!'
                print ' ++ FAILURE ++'
                print result
                print ' +++++++++++++'

    print "Done!"

Add A Comment