Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
120 lines (97 loc) · 3.43 KB
/
Copy pathclient.py
File metadata and controls
120 lines (97 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import logging
import platform
import sys
from urllib.parse import urlencode, urljoin
from urllib.request import urlopen
def save_to_speedcenter(url=None, project=None, commitid=None, executable=None,
benchmark=None, result_value=None, **kwargs):
"""Save a benchmark result to your speedcenter server
Mandatory:
:param url:
Codespeed server endpoint
(e.g. `http://codespeed.example.org/result/add/`)
:param project:
Project name
:param commitid:
VCS identifier
:param executable:
The executable name
:param benchmark:
The name of this particular benchmark
:param float result_value:
The benchmark result
Optional:
:param environment:
System description
:param date revision_date:
Optional, default will be either VCS commit, if available, or the
current date
:param date result_date:
Optional
:param float std_dev:
Optional
:param float max:
Optional
:param float min:
Optional
"""
data = {
'project': project,
'commitid': commitid,
'executable': executable,
'benchmark': benchmark,
'result_value': result_value,
}
data.update(kwargs)
if not data.get('environment', None):
data['environment'] = platform.platform(aliased=True)
f = urlopen(url, urlencode(data).encode('utf-8'))
response = f.read()
status = f.getcode()
f.close()
if status == 202:
logging.debug("Server %s: HTTP %s: %s", url, status, response)
else:
raise IOError("Server %s returned HTTP %s" % (url, status))
if __name__ == "__main__":
from optparse import OptionParser
parser = OptionParser()
parser.add_option("--benchmark")
parser.add_option("--commitid")
parser.add_option("--environment",
help="Use a custom Codespeed environment")
parser.add_option("--executable")
parser.add_option("--max", type="float")
parser.add_option("--min", type="float")
parser.add_option("--project")
parser.add_option("--branch")
parser.add_option("--result-date")
parser.add_option("--result-value", type="float")
parser.add_option("--revision_date")
parser.add_option("--std-dev", type="float")
parser.add_option("--url",
help="URL of your Codespeed server (e.g. http://codespeed.example.org)")
(options, args) = parser.parse_args()
if args:
parser.error("All arguments must be provided as command-line options")
# Yes, the optparse manpage has a snide comment about "required options"
# being gramatically dubious. Yes, it's still wrong about not needing to
# do this.
required = ('url', 'environment', 'project', 'commitid', 'executable',
'benchmark', 'result_value')
if not all(getattr(options, i) for i in required):
parser.error("The following parameters must be provided:\n\t%s" % "\n\t".join(
"--%s".replace("_", "-") % i for i in required))
kwargs = {}
for k, v in options.__dict__.items():
if v is not None:
kwargs[k] = v
kwargs.setdefault('branch', 'default')
if not kwargs['url'].endswith("/result/add/"):
kwargs['url'] = urljoin(kwargs['url'], '/result/add/')
try:
save_to_speedcenter(**kwargs)
sys.exit(0)
except Exception as e:
logging.error("Error saving results: %s", e)
sys.exit(1)