-
Notifications
You must be signed in to change notification settings - Fork 0
/
hscReleaseQuery.py
201 lines (164 loc) · 6.3 KB
/
hscReleaseQuery.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import json
import argparse
import urllib2
import time
import sys
import csv
import getpass
import os
import os.path
import re
import ssl
version = 20170216.1
args = None
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--user', '-u', required=True,
help='specify your account name')
parser.add_argument('--release-version', '-r', choices='pdr1'.split(), default='pdr1',
help='specify release version')
parser.add_argument('--delete-job', '-D', action='store_true',
help='delete the job you submitted after your downloading')
parser.add_argument('--format', '-f', dest='out_format', default='csv', choices=['csv', 'csv.gz', 'sqlite3', 'fits'],
help='specify output format')
parser.add_argument('--nomail', '-M', action='store_true',
help='suppress email notice')
parser.add_argument('--password-env', default='HSC_SSP_CAS_PASSWORD',
help='specify the environment variable that has STARS password as its content')
parser.add_argument('--preview', '-p', action='store_true',
help='quick mode (short timeout)')
parser.add_argument('--skip-syntax-check', '-S', action='store_true',
help='skip syntax check')
parser.add_argument('--api-url', default='https://hsc-release.mtk.nao.ac.jp/datasearch/api/catalog_jobs/',
help='for developers')
parser.add_argument('--download','-d', default=False, action='store_true', help='activate download')
parser.add_argument('sql-file', type=argparse.FileType('r'),
help='SQL file')
global args
args = parser.parse_args()
print args
# credential = {'account_name': args.user, 'password': getPassword()}
credential = {'account_name': args.user, 'password': 'r0/En5eV2tYN4HMbLAfftz+4SonfkqjLW5P1iNVp'} #REMOVE
sql = args.__dict__['sql-file'].read()
job = None
try:
if args.preview:
preview(credential, sql, sys.stdout)
else:
job = submitJob(credential, sql, args.out_format)
print 'Job submitted: ',sql
if args.download:
blockUntilJobFinishes(credential, job['id'])
print 'Downloading job',str(job['id'])
download(credential, job['id'], out = open('./'+str(job['id'])+'.'+args.out_format,"w"))
print 'Done!'
# if args.delete_job:
# deleteJob(credential, job['id'])
except urllib2.HTTPError, e:
if e.code == 401:
print >> sys.stderr, 'invalid id or password.'
if e.code == 406:
print >> sys.stderr, e.read()
else:
print >> sys.stderr, e
except QueryError, e:
print >> sys.stderr, e
except KeyboardInterrupt:
if job is not None:
jobCancel(credential, job['id'])
raise
else:
sys.exit(0)
sys.exit(1)
class QueryError(Exception):
pass
def httpJsonPost(url, data):
data['clientVersion'] = version
postData = json.dumps(data)
return httpPost(url, postData, {'Content-type': 'application/json'})
def httpPost(url, postData, headers):
req = urllib2.Request(url, postData, headers)
skipVerifying = None
try:
skipVerifying = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
except AttributeError:
pass
# if skipVerifying:
# res = urllib2.urlopen(req, context=skipVerifying)
# else:
# res = urllib2.urlopen(req)
res = urllib2.urlopen(req,context=context)
return res
def submitJob(credential, sql, out_format):
url = args.api_url + 'submit'
catalog_job = {
'sql' : sql,
'out_format' : out_format,
'include_metainfo_to_body': True,
'release_version' : args.release_version,
}
postData = {'credential': credential, 'catalog_job': catalog_job, 'nomail': args.nomail, 'skip_syntax_check': args.skip_syntax_check}
res = httpJsonPost(url, postData)
job = json.load(res)
return job
def jobStatus(credential, job_id):
url = args.api_url + 'status'
postData = {'credential': credential, 'id': job_id}
res = httpJsonPost(url, postData)
job = json.load(res)
return job
def jobCancel(credential, job_id):
url = args.api_url + 'cancel'
postData = {'credential': credential, 'id': job_id}
httpJsonPost(url, postData)
def preview(credential, sql, out):
url = args.api_url + 'preview'
catalog_job = {
'sql' : sql,
'release_version' : args.release_version,
}
postData = {'credential': credential, 'catalog_job': catalog_job}
res = httpJsonPost(url, postData)
result = json.load(res)
writer = csv.writer(out)
# writer.writerow(result['result']['fields'])
for row in result['result']['rows']:
writer.writerow(row)
if result['result']['count'] > len(result['result']['rows']):
raise QueryError, 'only top %d records are displayed !' % len(result['result']['rows'])
def blockUntilJobFinishes(credential, job_id):
max_interval = 5 * 60 # sec.
interval = 1
while True:
time.sleep(interval)
job = jobStatus(credential, job_id)
if job['status'] == 'error':
raise QueryError, 'query error: ' + job['error']
if job['status'] == 'done':
break
interval *= 2
if interval > max_interval:
interval = max_interval
def download(credential, job_id, out):
url = args.api_url + 'download'
postData = {'credential': credential, 'id': job_id}
res = httpJsonPost(url, postData)
bufSize = 64 * 1<<10 # 64k
while True:
buf = res.read(bufSize)
out.write(buf)
if len(buf) < bufSize:
break
def deleteJob(credential, job_id):
url = args.api_url + 'delete'
postData = {'credential': credential, 'id': job_id}
httpJsonPost(url, postData)
def getPassword():
password_from_envvar = os.environ.get(args.password_env, '')
if password_from_envvar != '':
return password_from_envvar
else:
return getpass.getpass('password? ')
if __name__ == '__main__':
main()