Wordpress: Automatic plugin upgrade with python
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.
#!/usr/bin/python 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: ./pyWordpressUpgrade.py <url> <username> <password>' print ' Where <url> is similar to: http://www.example.com/wp-admin' if __name__ == '__main__': try: host = sys.argv[1] username = sys.argv[2] password = sys.argv[3] except: usage() sys.exit() #plugin name name_regex = re.compile(r'plugin=(?P<name>[\w-]*)') #success? success_regex = re.compile(r'Plugin upgraded successfully') #ftp connection required ftp_regex = re.compile(r'FTP connection information is required') br = Browser() br.set_handle_robots(False) #br.set_debug_http(True) try: br.open(host) br.select_form(name="loginform") except Exception, e: print 'Unable to log into: %s'%host print e usage() sys.exit() 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 = name_regex.search( link.url ) if m: print 'Updating: %s' % m.group('name') r = br.follow_link( url=link.url ) html = r.read() soup = BeautifulStoneSoup(html) result = str(soup.findAll('div', {'class':'wrap'})[0]) # strip the html result = strip_tags(result) m = ftp_regex.search ( result ) if m: print ' ++ FAILURE ++' print "Sorry, updating plugins via ftp not supported" break # search for the success string m = success_regex.search(result) if m: print ' ==> success!' else: print ' ++ FAILURE ++' print result print ' +++++++++++++' br.back() print "Done!"
Add A Comment