[buildserver] Fix buildserver and make python2 compatible
[youtube-dl] / devscripts / buildserver.py
1 #!/usr/bin/python3
2
3 import argparse
4 import ctypes
5 import functools
6 import shutil
7 import subprocess
8 import sys
9 import tempfile
10 import threading
11 import traceback
12 import os.path
13
14 sys.path.insert(0, os.path.dirname(os.path.dirname((os.path.abspath(__file__)))))
15 from youtube_dl.compat import (
16     compat_http_server,
17     compat_str,
18     compat_urlparse,
19 )
20
21 # These are not used outside of buildserver.py thus not in compat.py
22
23 try:
24     import winreg as compat_winreg
25 except ImportError:  # Python 2
26     import _winreg as compat_winreg
27
28 try:
29     import socketserver as compat_socketserver
30 except ImportError:  # Python 2
31     import SocketServer as compat_socketserver
32
33 try:
34     compat_input = raw_input
35 except NameError:  # Python 3
36     compat_input = input
37
38
39 class BuildHTTPServer(compat_socketserver.ThreadingMixIn, compat_http_server.HTTPServer):
40     allow_reuse_address = True
41
42
43 advapi32 = ctypes.windll.advapi32
44
45 SC_MANAGER_ALL_ACCESS = 0xf003f
46 SC_MANAGER_CREATE_SERVICE = 0x02
47 SERVICE_WIN32_OWN_PROCESS = 0x10
48 SERVICE_AUTO_START = 0x2
49 SERVICE_ERROR_NORMAL = 0x1
50 DELETE = 0x00010000
51 SERVICE_STATUS_START_PENDING = 0x00000002
52 SERVICE_STATUS_RUNNING = 0x00000004
53 SERVICE_ACCEPT_STOP = 0x1
54
55 SVCNAME = 'youtubedl_builder'
56
57 LPTSTR = ctypes.c_wchar_p
58 START_CALLBACK = ctypes.WINFUNCTYPE(None, ctypes.c_int, ctypes.POINTER(LPTSTR))
59
60
61 class SERVICE_TABLE_ENTRY(ctypes.Structure):
62     _fields_ = [
63         ('lpServiceName', LPTSTR),
64         ('lpServiceProc', START_CALLBACK)
65     ]
66
67
68 HandlerEx = ctypes.WINFUNCTYPE(
69     ctypes.c_int,     # return
70     ctypes.c_int,     # dwControl
71     ctypes.c_int,     # dwEventType
72     ctypes.c_void_p,  # lpEventData,
73     ctypes.c_void_p,  # lpContext,
74 )
75
76
77 def _ctypes_array(c_type, py_array):
78     ar = (c_type * len(py_array))()
79     ar[:] = py_array
80     return ar
81
82
83 def win_OpenSCManager():
84     res = advapi32.OpenSCManagerW(None, None, SC_MANAGER_ALL_ACCESS)
85     if not res:
86         raise Exception('Opening service manager failed - '
87                         'are you running this as administrator?')
88     return res
89
90
91 def win_install_service(service_name, cmdline):
92     manager = win_OpenSCManager()
93     try:
94         h = advapi32.CreateServiceW(
95             manager, service_name, None,
96             SC_MANAGER_CREATE_SERVICE, SERVICE_WIN32_OWN_PROCESS,
97             SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
98             cmdline, None, None, None, None, None)
99         if not h:
100             raise OSError('Service creation failed: %s' % ctypes.FormatError())
101
102         advapi32.CloseServiceHandle(h)
103     finally:
104         advapi32.CloseServiceHandle(manager)
105
106
107 def win_uninstall_service(service_name):
108     manager = win_OpenSCManager()
109     try:
110         h = advapi32.OpenServiceW(manager, service_name, DELETE)
111         if not h:
112             raise OSError('Could not find service %s: %s' % (
113                 service_name, ctypes.FormatError()))
114
115         try:
116             if not advapi32.DeleteService(h):
117                 raise OSError('Deletion failed: %s' % ctypes.FormatError())
118         finally:
119             advapi32.CloseServiceHandle(h)
120     finally:
121         advapi32.CloseServiceHandle(manager)
122
123
124 def win_service_report_event(service_name, msg, is_error=True):
125     with open('C:/sshkeys/log', 'a', encoding='utf-8') as f:
126         f.write(msg + '\n')
127
128     event_log = advapi32.RegisterEventSourceW(None, service_name)
129     if not event_log:
130         raise OSError('Could not report event: %s' % ctypes.FormatError())
131
132     try:
133         type_id = 0x0001 if is_error else 0x0004
134         event_id = 0xc0000000 if is_error else 0x40000000
135         lines = _ctypes_array(LPTSTR, [msg])
136
137         if not advapi32.ReportEventW(
138                 event_log, type_id, 0, event_id, None, len(lines), 0,
139                 lines, None):
140             raise OSError('Event reporting failed: %s' % ctypes.FormatError())
141     finally:
142         advapi32.DeregisterEventSource(event_log)
143
144
145 def win_service_handler(stop_event, *args):
146     try:
147         raise ValueError('Handler called with args ' + repr(args))
148         TODO
149     except Exception as e:
150         tb = traceback.format_exc()
151         msg = str(e) + '\n' + tb
152         win_service_report_event(service_name, msg, is_error=True)
153         raise
154
155
156 def win_service_set_status(handle, status_code):
157     svcStatus = SERVICE_STATUS()
158     svcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS
159     svcStatus.dwCurrentState = status_code
160     svcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP
161
162     svcStatus.dwServiceSpecificExitCode = 0
163
164     if not advapi32.SetServiceStatus(handle, ctypes.byref(svcStatus)):
165         raise OSError('SetServiceStatus failed: %r' % ctypes.FormatError())
166
167
168 def win_service_main(service_name, real_main, argc, argv_raw):
169     try:
170         # args = [argv_raw[i].value for i in range(argc)]
171         stop_event = threading.Event()
172         handler = HandlerEx(functools.partial(stop_event, win_service_handler))
173         h = advapi32.RegisterServiceCtrlHandlerExW(service_name, handler, None)
174         if not h:
175             raise OSError('Handler registration failed: %s' %
176                           ctypes.FormatError())
177
178         TODO
179     except Exception as e:
180         tb = traceback.format_exc()
181         msg = str(e) + '\n' + tb
182         win_service_report_event(service_name, msg, is_error=True)
183         raise
184
185
186 def win_service_start(service_name, real_main):
187     try:
188         cb = START_CALLBACK(
189             functools.partial(win_service_main, service_name, real_main))
190         dispatch_table = _ctypes_array(SERVICE_TABLE_ENTRY, [
191             SERVICE_TABLE_ENTRY(
192                 service_name,
193                 cb
194             ),
195             SERVICE_TABLE_ENTRY(None, ctypes.cast(None, START_CALLBACK))
196         ])
197
198         if not advapi32.StartServiceCtrlDispatcherW(dispatch_table):
199             raise OSError('ctypes start failed: %s' % ctypes.FormatError())
200     except Exception as e:
201         tb = traceback.format_exc()
202         msg = str(e) + '\n' + tb
203         win_service_report_event(service_name, msg, is_error=True)
204         raise
205
206
207 def main(args=None):
208     parser = argparse.ArgumentParser()
209     parser.add_argument('-i', '--install',
210                         action='store_const', dest='action', const='install',
211                         help='Launch at Windows startup')
212     parser.add_argument('-u', '--uninstall',
213                         action='store_const', dest='action', const='uninstall',
214                         help='Remove Windows service')
215     parser.add_argument('-s', '--service',
216                         action='store_const', dest='action', const='service',
217                         help='Run as a Windows service')
218     parser.add_argument('-b', '--bind', metavar='<host:port>',
219                         action='store', default='localhost:8142',
220                         help='Bind to host:port (default %default)')
221     options = parser.parse_args(args=args)
222
223     if options.action == 'install':
224         fn = os.path.abspath(__file__).replace('v:', '\\\\vboxsrv\\vbox')
225         cmdline = '%s %s -s -b %s' % (sys.executable, fn, options.bind)
226         win_install_service(SVCNAME, cmdline)
227         return
228
229     if options.action == 'uninstall':
230         win_uninstall_service(SVCNAME)
231         return
232
233     if options.action == 'service':
234         win_service_start(SVCNAME, main)
235         return
236
237     host, port_str = options.bind.split(':')
238     port = int(port_str)
239
240     print('Listening on %s:%d' % (host, port))
241     srv = BuildHTTPServer((host, port), BuildHTTPRequestHandler)
242     thr = threading.Thread(target=srv.serve_forever)
243     thr.start()
244     compat_input('Press ENTER to shut down')
245     srv.shutdown()
246     thr.join()
247
248
249 def rmtree(path):
250     for name in os.listdir(path):
251         fname = os.path.join(path, name)
252         if os.path.isdir(fname):
253             rmtree(fname)
254         else:
255             os.chmod(fname, 0o666)
256             os.remove(fname)
257     os.rmdir(path)
258
259
260 class BuildError(Exception):
261     def __init__(self, output, code=500):
262         self.output = output
263         self.code = code
264
265     def __str__(self):
266         return self.output
267
268
269 class HTTPError(BuildError):
270     pass
271
272
273 class PythonBuilder(object):
274     def __init__(self, **kwargs):
275         python_version = kwargs.pop('python', '3.4')
276         try:
277             key = compat_winreg.OpenKey(
278                 compat_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Python\PythonCore\%s\InstallPath' % python_version)
279             try:
280                 self.pythonPath, _ = compat_winreg.QueryValueEx(key, '')
281             finally:
282                 compat_winreg.CloseKey(key)
283         except Exception:
284             raise BuildError('No such Python version: %s' % python_version)
285
286         super(PythonBuilder, self).__init__(**kwargs)
287
288
289 class GITInfoBuilder(object):
290     def __init__(self, **kwargs):
291         try:
292             self.user, self.repoName = kwargs['path'][:2]
293             self.rev = kwargs.pop('rev')
294         except ValueError:
295             raise BuildError('Invalid path')
296         except KeyError as e:
297             raise BuildError('Missing mandatory parameter "%s"' % e.args[0])
298
299         path = os.path.join(os.environ['APPDATA'], 'Build archive', self.repoName, self.user)
300         if not os.path.exists(path):
301             os.makedirs(path)
302         self.basePath = tempfile.mkdtemp(dir=path)
303         self.buildPath = os.path.join(self.basePath, 'build')
304
305         super(GITInfoBuilder, self).__init__(**kwargs)
306
307
308 class GITBuilder(GITInfoBuilder):
309     def build(self):
310         try:
311             subprocess.check_output(['git', 'clone', 'git://github.com/%s/%s.git' % (self.user, self.repoName), self.buildPath])
312             subprocess.check_output(['git', 'checkout', self.rev], cwd=self.buildPath)
313         except subprocess.CalledProcessError as e:
314             raise BuildError(e.output)
315
316         super(GITBuilder, self).build()
317
318
319 class YoutubeDLBuilder(object):
320     authorizedUsers = ['fraca7', 'phihag', 'rg3', 'FiloSottile']
321
322     def __init__(self, **kwargs):
323         if self.repoName != 'youtube-dl':
324             raise BuildError('Invalid repository "%s"' % self.repoName)
325         if self.user not in self.authorizedUsers:
326             raise HTTPError('Unauthorized user "%s"' % self.user, 401)
327
328         super(YoutubeDLBuilder, self).__init__(**kwargs)
329
330     def build(self):
331         try:
332             proc = subprocess.Popen([os.path.join(self.pythonPath, 'python.exe'), 'setup.py', 'py2exe'], stdin=subprocess.PIPE, cwd=self.buildPath)
333             proc.wait()
334             #subprocess.check_output([os.path.join(self.pythonPath, 'python.exe'), 'setup.py', 'py2exe'],
335             #                        cwd=self.buildPath)
336         except subprocess.CalledProcessError as e:
337             raise BuildError(e.output)
338
339         super(YoutubeDLBuilder, self).build()
340
341
342 class DownloadBuilder(object):
343     def __init__(self, **kwargs):
344         self.handler = kwargs.pop('handler')
345         self.srcPath = os.path.join(self.buildPath, *tuple(kwargs['path'][2:]))
346         self.srcPath = os.path.abspath(os.path.normpath(self.srcPath))
347         if not self.srcPath.startswith(self.buildPath):
348             raise HTTPError(self.srcPath, 401)
349
350         super(DownloadBuilder, self).__init__(**kwargs)
351
352     def build(self):
353         if not os.path.exists(self.srcPath):
354             raise HTTPError('No such file', 404)
355         if os.path.isdir(self.srcPath):
356             raise HTTPError('Is a directory: %s' % self.srcPath, 401)
357
358         self.handler.send_response(200)
359         self.handler.send_header('Content-Type', 'application/octet-stream')
360         self.handler.send_header('Content-Disposition', 'attachment; filename=%s' % os.path.split(self.srcPath)[-1])
361         self.handler.send_header('Content-Length', str(os.stat(self.srcPath).st_size))
362         self.handler.end_headers()
363
364         with open(self.srcPath, 'rb') as src:
365             shutil.copyfileobj(src, self.handler.wfile)
366
367         super(DownloadBuilder, self).build()
368
369
370 class CleanupTempDir(object):
371     def build(self):
372         try:
373             rmtree(self.basePath)
374         except Exception as e:
375             print('WARNING deleting "%s": %s' % (self.basePath, e))
376
377         super(CleanupTempDir, self).build()
378
379
380 class Null(object):
381     def __init__(self, **kwargs):
382         pass
383
384     def start(self):
385         pass
386
387     def close(self):
388         pass
389
390     def build(self):
391         pass
392
393
394 class Builder(PythonBuilder, GITBuilder, YoutubeDLBuilder, DownloadBuilder, CleanupTempDir, Null):
395     pass
396
397
398 class BuildHTTPRequestHandler(compat_http_server.BaseHTTPRequestHandler):
399     actionDict = {'build': Builder, 'download': Builder}  # They're the same, no more caching.
400
401     def do_GET(self):
402         path = compat_urlparse.urlparse(self.path)
403         paramDict = dict([(key, value[0]) for key, value in compat_urlparse.parse_qs(path.query).items()])
404         action, _, path = path.path.strip('/').partition('/')
405         if path:
406             path = path.split('/')
407             if action in self.actionDict:
408                 try:
409                     builder = self.actionDict[action](path=path, handler=self, **paramDict)
410                     builder.start()
411                     try:
412                         builder.build()
413                     finally:
414                         builder.close()
415                 except BuildError as e:
416                     self.send_response(e.code)
417                     msg = compat_str(e).encode('UTF-8')
418                     self.send_header('Content-Type', 'text/plain; charset=UTF-8')
419                     self.send_header('Content-Length', len(msg))
420                     self.end_headers()
421                     self.wfile.write(msg)
422                 except HTTPError as e:
423                     self.send_response(e.code, str(e))
424             else:
425                 self.send_response(500, 'Unknown build method "%s"' % action)
426         else:
427             self.send_response(500, 'Malformed URL')
428
429 if __name__ == '__main__':
430     main()