Home > Uncategorized > Uploading files to (and removing files from) Redmine using scripts

Uploading files to (and removing files from) Redmine using scripts

At my work we are currently using Redmine and in particular we are using its wiki feature. Since we keep a lot of useful information on our wiki and since almost anyone that relates to our project have access to it, we though it would be nice to store the current release (nighly build) there too. Well, how hard can it be I though, asking my best friend google about it. It was not long until I found a guy who had solved the problem for me. He was using a python script to upload files to the redmine wiki. The script was leaning on a library called Mechanize and looked something like below:

import mechanize
import sys

if len(sys.argv) != 3:
  sys.exit("Usage: upload.py <directory> <filename>")
directory = sys.argv[1]
fileName = sys.argv[2]

url_fh = ‘http://redmine.corp.se/’
br = mechanize.Browser()
br.set_handle_robots(False)

# Fetching login page
page = br.open( url_fh + ‘login’ )

# Logging in
br.select_form( nr=1 )
br["username"]="buildagent¬-username"
br["password"]="buildagent-password"
#br["autologin"]=0
page = br.submit()

# Opening releases – New Files page…
page = br.open( url_fh + ‘projects/projectname/wiki/Releases’ )

# Registering the file
br.select_form( nr=1 )
#br["version_id"]= [ "44" ]
br.add_file( open( directory + fileName, ‘rb’ ), "binary", fileName )
page = br.submit()

Sweet! I installed python and mechanize on our build agent. Installing mechanize was not that straight forward as I had hoped (probably due to my lack of experience using python eggs etc) but that is another story. All was fine except that I was now uploading new files but never removing old ones. Well, someone must have solved that problem too I though, again turning to google for answers. However, this time I came back empty handed. Darn. Well, what can you do? I started poking around in the mechanize library trying to figure out how to write the script myself. The documentation was not giving me anything but luckily the source code was there and source code is also documentation, although slightly obfuscated. After a couple of attempts I came up with this solution:

import mechanize
import sys
import urllib, urllib2, time
from ClientForm import HTMLForm

url_fh = ‘http://sredmine.corp.se/’
br = mechanize.Browser()
br.set_handle_robots(False)

# Fetching login page
page = br.open( url_fh + ‘login’ )

# Logging in
br.select_form( nr=1 )
br["username"]=" buildagent¬-username"
br["password"]=" buildagent¬-password"
page = br.submit()

# Opening releases – New Files page…
page = br.open( url_fh + ‘projects/projectname/wiki/Releases’ )

# Remove files if above threshold value
msi_links = list(br.links(url_regex="destroy/\d"))
max_msi_links = 20
if len(msi_links) > max_msi_links:
    for i in range(len(msi_links)-max_msi_links):
       temp_link = "
http://redmine.corp.se" + msi_links[i].url      
       print "Removing " + temp_link
       try:
           br.form = HTMLForm(action=temp_link, method="POST")
           br.submit()
           br.refresh()
       except:
           # ignore exceptions, we always seems to get exceptions but the code works anyway.
           pass

Maybe not something that a python expert would be proud of but it certainly works and that was good enough for me. 

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: