# -*- coding: utf-8 -*-
import os
import socket
import subprocess
import time
import urllib
import urllib2

SqueakImage = "Squeak4.4-12327"

# You need to enter the real URL and have the server running
CODESPEED_URL = 'http://speed.bithug.org/'

# Executables (assumed to be in the local directory)
executables = ["targetimageloadingsmalltalk-c"]

# Arguments (inserted between executable and benchmark)
executable_arguments = ["%s.image" % SqueakImage, "-m"]

# Benchmarks (run for each executable)
benchmarks = ["tinyBenchmarks"]

def build_data(executable, benchmark, result):
    # Mandatory fields
    return {
        'commitid': get_commitid(),
        'branch': 'default',
        'project': 'lang-smalltalk',
        'executable': executable,
        'benchmark': benchmark,
        'environment': socket.gethostname(),
        'result_value': str(result),
    }
    # Optional fields
    # data.update({
    #     'std_dev': 1.11111, # Optional. Default is blank
    #     'max': 4001.6, # Optional. Default is blank
    #     'min': 3995.1, # Optional. Default is blank
    # })


def download_prerequesites():
    clean_workspace()
    import gzip, tarfile
    image = urllib2.urlopen("http://ftp.squeak.org/4.4/%s.tgz" % SqueakImage).read()
    sources = urllib2.urlopen('http://ftp.squeak.org/4.4/SqueakV41.sources.gz').read()
    with open("image.tgz", "w") as f:
        f.write(image)
    with gzip.open("image.tgz") as f:
        tar = f.read()
    with open("image.tar", "w") as f:
        f.write(tar)
    with tarfile.open("image.tar") as f:
        f.extractall(".")
    with open("sources.gz", "w") as f:
        f.write(sources)
    with gzip.open("sources.gz") as f:
        with open("SqueakV41.sources", "w") as s:
            s.write(f.read())

def clean_workspace():
    for f in ["image.tgz", "image.tar", "sources.gz",
              "SqueakV41.sources",
              "%s.image" % SqueakImage, "%s.changes" % SqueakImage]:
        try:
            os.remove(f)
        except:
            pass


def get_commitid():
    try:
        pipe = subprocess.Popen(
            ["hg", "log", "-l", "1", "--template", "{node}"],
            stdout=subprocess.PIPE
        )
        if pipe.wait() == 0:
            return pipe.stdout.read()
    except:
        pass
    try:
        pipe = subprocess.Popen(
            ["git", "log", "-1", "--pretty=%H"],
            stdout=subprocess.PIPE
        )
        if pipe.wait() == 0:
            return pipe.stdout.read()
    except:
        pass
    raise Exception("commitid not found. not a git or hg repo")


def add(executable, benchmark, result):
    print "Saving result %s for executable %s, benchmark %s" % (
        result, executable, benchmark)
    data = build_data(executable, benchmark, result)
    params = urllib.urlencode(data)
    response = "None"
    print "Saving result for executable %s, revision %s, benchmark %s" % (
        data['executable'], data['commitid'], data['benchmark'])
    try:
        f = urllib2.urlopen(CODESPEED_URL + 'result/add/', params)
    except urllib2.HTTPError as e:
        print str(e)
        print e.read()
        return
    response = f.read()
    f.close()
    print "Server (%s) response: %s\n" % (CODESPEED_URL, response)


def run():
    for executable in executables:
        for benchmark in benchmarks:
            start = time.time()
            pipe = subprocess.Popen(
                ["./%s" % executable] + executable_arguments + [benchmark]
            )
            pipe.wait()
            result = time.time() - start
            add(executable, benchmark, result)


if __name__ == "__main__":
    download_prerequesites()
    run()

@@ -59,16 +59,27 @@
 process%0A
+import sys%0A
 import t
@@ -1406,28 +1406,27 @@
 (image)%0A    
-with
+f =
  gzip.open(%22
@@ -1432,35 +1432,25 @@
 %22image.tgz%22)
- as f:%0A    
+%0A
     tar = f.
@@ -1448,32 +1448,46 @@
  tar = f.read()%0A
+    f.close()%0A
     with open(%22i
@@ -1533,20 +1533,19 @@
 ar)%0A    
-with
+f =
  tarfile
@@ -1562,27 +1562,18 @@
 ge.tar%22)
- as f:%0A    
+:%0A
     f.ex
@@ -1586,16 +1586,30 @@
 ll(%22.%22)%0A
+    f.close()%0A
     with
@@ -1668,20 +1668,19 @@
 es)%0A    
-with
+f =
  gzip.op
@@ -1695,27 +1695,17 @@
 ces.gz%22)
- as f:%0A    
+%0A
     with
@@ -1750,20 +1750,16 @@
         
-    
 s.write(
@@ -1767,16 +1767,30 @@
 .read())
+%0A    f.close()
 %0A%0Adef cl
@@ -3271,24 +3271,79 @@
 %0Adef run():%0A
+    suffix = %22.exe%22 if sys.platform == %22win32%22 else %22%22%0A
     for exec
@@ -3493,20 +3493,23 @@
   %5B%22./%25s
+%25s
 %22 %25 
+(
 executab
@@ -3510,16 +3510,25 @@
 ecutable
+, suffix)
 %5D + exec

fix benchmark script on windows