forked from cms-sw/topic-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externals
executable file
·89 lines (81 loc) · 2.82 KB
/
externals
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
#!/usr/bin/env python26
import cgi, os, tempfile, shutil
from ws_sso_content_reader import getContent
from BeautifulSoup import BeautifulSoup
from common import debug, jsonReply, error, getRequestCached
from os.path import join, exists, dirname
from secrets import cern_secrets as config
from urllib2 import urlopen
import re
if config.get("debug", False):
import cgitb
cgitb.enable()
from os import fdopen
try:
from json import dumps
from json import loads
except ImportError:
from cjson import encode as dumps
from cjson import decode as loads
try:
from hashlib import sha1
except:
from sha import new as sha1
GITHUB_CMSDIST_REPO="https://raw.github.com/cms-sw/cmsdist/IB/%s/stable/config.map"
# Get TC url and calculate sha1sum with the result.
# Look if there is already a transformed object which uses the same content as
# input
# - If yes, simply return it.
# - If no, transform the result, save it and return it.
def getGitHubCached(url, transformation, transformation_id=""):
try:
html = urlopen(url).read()
except:
error(404, "Not Found")
contentHash = sha1(html).hexdigest()
hash = sha1(url + transformation_id).hexdigest()
cachedTransform = join(config["tccache"], hash[0:2], hash[2:], contentHash)
if exists(cachedTransform):
return file(cachedTransform).read()
result = transformation(html)
(tmpfile, tmppath) = tempfile.mkstemp(prefix='tmp', dir=config["tccache"])
f = fdopen(tmpfile, "w")
f.write(result)
f.close()
try:
os.makedirs(dirname(cachedTransform))
except OSError:
pass
try:
shutil.move(tmppath, cachedTransform)
except OSError:
pass
return result
def listQueues():
def doListQueues(txt):
branches = loads(txt)
matchStableBranch = "refs/heads/IB/(CMSSW_[0-9]+_[0-9]+_X)/stable"
return dumps([{"ref": re.sub(matchStableBranch, "\\1", x["ref"])} for x in branches if re.match(matchStableBranch, x["ref"])])
return getRequestCached("/repos/cms-sw/cmsdist/git/refs/heads",
transformation=doListQueues, transformation_id="CMSDIST/Queues")
if __name__ == "__main__":
if not os.environ.get("ADFS_LOGIN"):
print 'Status: 403 Forbidden\r\n\r\n\r\n';
exit(0)
requestMethod = os.environ.get("REQUEST_METHOD")
pathInfo = os.environ.get("PATH_INFO", "").strip("/")
if requestMethod == "GET":
if not pathInfo:
jsonReply(listQueues())
if not "/" in pathInfo:
def getExternals(data):
lines = [x.strip().split(";") for x in data.split("\n") if x.strip()]
results = []
for line in lines:
parts = dict(x.split("=") for x in line)
results.append(dict(parts))
return dumps(results)
release_name = pathInfo
externals = getGitHubCached(GITHUB_CMSDIST_REPO % pathInfo, getExternals, "Externals/V3")
jsonReply(externals)
error(404, "Not Found")