#!/usr/bin/env python
#
# Creates entries on the tigris file manager for a release, using the
# contents of:
#   md5sums
#
# Note: this file is not guaranteed to work with Python 3.  In fact, it
# probably doesn't.  Since the RM is really the only guy who uses and 
# develops this script, and he currently has Python 2.x, let's forgo worrying
# about Python 3 compatibility until it becomes a real problem.  It makes
# the script simpler to develop and maintain.  -hkw


usage = '''\
post-to-tigris.py <username> <password> <folderId> <release>
    username - Your tigris.org username
    password - Your tigris.org password
    folderId - the folderId of the place to post the files.  This can be
      gleaned from the URL in the file manager.  For example:
      http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=1079
      has a folderId of 1079
    release - the full name of the release, such as 1.5.0-beta1
'''

import sys, cookielib, urllib2, urllib, re


def login(username, password, folderId):
    '''Login to tigris.org, using the provided username and password.
       Return the OpenDirector object for future use.'''
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

    folderURL = 'http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=%d' % folderId,
    params = {
        'detour' : folderURL,
        'loginID' : username,
        'password' : password,
      }
    request = urllib2.Request('http://www.tigris.org/servlets/TLogin',
                               urllib.urlencode(params))
    # We open the above request, grabbing the appropriate credentials for
    # future interactions.
    opener.open(request)

    return opener


def get_md5sums():
    "Open 'md5sums', and return a filename->checksum hash of the contents"
    f = open('md5sums')
    sums = {}

    for line in f:
        line_parts = line.split()
        sums[line_parts[1]] = line_parts[0]

    return sums


def add_items(opener, folderId, release_name):
    "Add the 12(!) items for a release to the given folder"
    folder_add_url = 'http://subversion.tigris.org/servlets/ProjectDocumentAdd?folderID=%d&action=Add%%20document' % folderId

    if re.match('^\d*\.\d*\.\d*$', release_name):
      status = 'Stable'
    else:
      status = 'Draft'

    md5sums = get_md5sums()

    for ext in ['.zip', '.tar.gz', '.tar.bz2']:
        for deps in ['', '-deps']:
            filename = 'subversion%s-%s%s' % (deps, release_name, ext)
            desc = 'Subversion %s' % release_name
            if deps:
                desc = desc + ' Dependencies'

            params = {
                'name' : filename,
                'status' : status,
                'description' : '%s (MD5: %s)' % (desc, md5sums[filename]),
                'type': 'link',
                'url': 'http://subversion.tigris.org/downloads/%s' % filename,
                'maxDepth': '',
            }

            # Add file
            request = urllib2.Request(folder_add_url,
                                      urllib.urlencode(params))
            opener.open(request)

            # Add signature
            filename = filename + '.asc'
            params['name'] = filename
            params['description'] = 'PGP signatures for %s' % desc
            params['url'] = 'http://subversion.tigris.org/downloads/%s' % \
                                                                      filename
            request = urllib2.Request(folder_add_url,
                                      urllib.urlencode(params))
            opener.open(request)


def main():
    if len(sys.argv) < 5:
        print(usage)
        sys.exit(-1)

    folderId = int(sys.argv[3])
    opener = login(sys.argv[1], sys.argv[2], folderId)
    add_items(opener, folderId, sys.argv[4])


if __name__ == '__main__':
    main()

@@ -1980,16 +1980,363 @@
  sums%0A%0A%0A
+def encode_multipart_form(params, boundary):%0A    %22Encode the given params to be output as a multipart POST%22%0A    lines = %5B%5D%0A    for key in params:%0A        lines.append('--' + boundary)%0A        lines.append('Content-Disposition: form-data; name=%22%25s%22' %25 key)%0A        lines.append('')%0A        lines.append(params%5Bkey%5D)%0A    return '%5Cr%5Cn'.join(lines)%0A%0A%0A
 def add_
@@ -2559,16 +2559,66 @@
 folderId
+%0A    boundary = '----------A_boundary_goes_here_$'
 %0A%0A    if
@@ -3332,16 +3332,52 @@
 h': '',%0A
+                'Button': 'Submit',%0A
         
@@ -3399,18 +3399,19 @@
     
-# Add file
+headers = %7B
 %0A   
@@ -3423,49 +3423,185 @@
     
-request = urllib2.Request(folder_add_url,
+    'Referer' : 'http://subversion.tigris.org/servlets/ProjectDocumentAdd?folderID=%25s' %25 folderId,%0A                'Content-Type' : 'multipart/form-data; boundary=%25s' %25 boundary
 %0A   
@@ -3603,32 +3603,35 @@
 y%0A              
+%7D%0A%0A
                 
@@ -3618,32 +3618,43 @@
  %7D%0A%0A            
+# Add file%0A
             urll
@@ -3649,39 +3649,189 @@
         
-urllib.urlencode(params
+data = encode_multipart_form(params, boundary)%0A            request = urllib2.Request(folder_add_url, data, headers)%0A            request.add_header('Content-Length', len(data
 ))%0A     
@@ -4204,111 +4204,181 @@
     
-request = urllib2.Request(folder_add_url,%0A                                      urllib.urlencode(params
+data = encode_multipart_form(params, boundary)%0A            request = urllib2.Request(folder_add_url, data, headers)%0A            request.add_header('Content-Length', len(data
 ))%0A 

Update the post-to-tigris script to account for the new format of form submission on s.t.o. This still doesn't work, however.