-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaphubd.py
133 lines (83 loc) · 2.62 KB
/
saphubd.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
#!/usr/bin/env python
import argparse, io, os, shutil, subprocess, sys, tempfile, zipfile
try:
import argparse
except:
print("")
print("The module argparse is not availble on your machine:")
print("- upgrade to python 2.7 or higher, or")
print("- download and install argparse from https://pypi.python.org/pypi/argparse")
print("")
sys.exit()
try:
# python 3.x
from http.server import BaseHTTPServer
except:
# python 2.x
import BaseHTTPServer
try:
# python 3.x
from urllib.parse import parse_qs
except:
# python 2.x
from urlparse import parse_qs
parser = argparse.ArgumentParser(description='saphub process')
parser.add_argument('--port', type=int,
help='port')
parser.add_argument('--regi',
help='path to the regi executable')
args = parser.parse_args()
PORT = args.port
REGI = args.regi
def regibase(host, usr, pwd, cwd):
env = {'REGI_HOST': host, 'REGI_USER': usr, 'REGI_PASSWD': pwd}
def _regibase(cmd):
pro = subprocess.Popen([REGI] + cmd, cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = pro.communicate()
if stderr:
raise Exception('REGI %s' % stderr)
return stdout
return _regibase
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
protocol_version = "HTTP/1.0"
def do_PUT(self):
arg = parse_qs(self.path[2:])
HOST = arg.get("host", [None])[0]
USER = arg.get("usr", [None])[0]
PASS = arg.get("pwd", [None])[0]
PACK = arg.get("package", [None])[0]
print("REQUEST", HOST, USER, PASS, PACK)
# e.g. /var/folders/mh/nb413rbs6q959lqn0lcq1nb80000gn/T/tmpG5x0Bn
root = tempfile.mkdtemp()
# e.g. foo
pack = os.path.join(root, PACK)
print("----", pack)
try:
arc = io.BytesIO(self.rfile.read(int(self.headers['Content-Length'])))
zip = zipfile.ZipFile(arc, 'r')
regi = regibase(HOST, USER, PASS, root)
os.mkdir(pack)
print("----", regi(['create', 'workspace', '--force']))
print("----", regi(['track', PACK]))
print("----", regi(['checkout']))
print("----", regi(['rebase']))
print("----", regi(['resolve', 'package', PACK, '--with=local']))
shutil.rmtree(pack)
os.mkdir(pack)
zip.extractall(pack)
print("----", regi(['commit']))
print("----", regi(['activate']))
self.send_response(200)
self.end_headers()
self.wfile.write("OKAY")
self.wfile.close()
except Exception, e:
print(e)
self.send_response(400)
self.end_headers()
self.wfile.write(str(e))
self.wfile.close()
finally:
shutil.rmtree(root)
print("LISTENING", PORT)
BaseHTTPServer.HTTPServer(('0.0.0.0', PORT), MyHandler).serve_forever()