Project

General

Profile

1
#!/usr/bin/env python
2
#
3
# Script to provide a wrapper around the ShrinkSafe "web service"
4
# <http://alex.dojotoolkit.org/shrinksafe/>
5
#
6

    
7
#
8
# We use this script for two reasons:
9
#
10
#  * This avoids having to install and configure Java and the standalone
11
#    ShrinkSafe utility.
12
#
13
#  * The current ShrinkSafe standalone utility was broken when we last
14
#    used it.
15
#
16

    
17
import sys
18

    
19
import urllib
20
import urllib2
21

    
22
URL_SHRINK_SAFE = "http://alex.dojotoolkit.org/shrinksafe/shrinksafe.php"
23

    
24
# This would normally be dynamically generated:
25
BOUNDARY_MARKER = "---------------------------72288400411964641492083565382"
26
                   
27
if __name__ == "__main__":
28
    ## Grab the source code
29
    try:
30
        sourceFilename = sys.argv[1]
31
    except:
32
        print "Usage: %s (<source filename>|-)" % sys.argv[0]
33
        raise SystemExit
34

    
35
    if sourceFilename == "-":
36
        sourceCode = sys.stdin.read()
37
        sourceFilename = "stdin.js"
38
    else:
39
        sourceCode = open(sourceFilename).read()
40
        
41
    ## Create the request replicating posting of the form from the web page
42
    request = urllib2.Request(url=URL_SHRINK_SAFE)
43
    request.add_header("Content-Type",
44
                       "multipart/form-data; boundary=%s" % BOUNDARY_MARKER)
45
    request.add_data("""
46
--%s
47
Content-Disposition: form-data; name="shrinkfile[]"; filename="%s"
48
Content-Type: application/x-javascript
49

    
50
%s
51
""" % (BOUNDARY_MARKER, sourceFilename, sourceCode))
52

    
53
    ## Deliver the result
54
    print urllib2.urlopen(request).read(),
(5-5/6)