blob: a5c325ed5df8d22aff718504c087b41e925c123c [file] [log] [blame]
Sergiusz Bazanski902d8602019-07-18 17:05:26 +02001import configparser
2import os
3import subprocess
4import tempfile
5
6import bazel_tools
7import bazel_tools.tools.python
8
Serge Bazanski4e8622d2020-09-25 20:23:28 +00009from elftools.elf.elffile import ELFFile
10from elftools.elf.segments import InterpSegment
11
Sergiusz Bazanski902d8602019-07-18 17:05:26 +020012from bazel_tools.tools.python.runfiles import runfiles
13r = runfiles.Create()
14
Bartosz Stebelcae27ec2020-07-03 07:05:15 +020015uwsgi = r.Rlocation("pydeps_pypi__uWSGI_2_0_18/uWSGI-2.0.18.data/scripts/uwsgi")
Sergiusz Bazanski5f9b1ec2019-09-22 02:19:18 +020016settings = r.Rlocation("hscloud/personal/q3k/djtest/djtest/settings.py")
Serge Bazanski4e8622d2020-09-25 20:23:28 +000017
18# uwsgi from runfiles is non-chmodded, we have to run it through its interpreter
19ld = None
20with open(uwsgi, 'rb') as f:
21 elffile = ELFFile(f)
22 for segment in elffile.iter_segments():
23 if isinstance(segment, InterpSegment):
24 ld = segment.get_interp_name()
25if ld is None:
26 raise Exception("could not find interpreter/ld.so path in uwsgi - failing")
Sergiusz Bazanski902d8602019-07-18 17:05:26 +020027
28apppath = os.path.dirname(settings)
29sitepath = os.path.dirname(apppath)
30
31pythonpath = os.environ['PYTHONPATH']
32
33# Make UWSGI ini config file
34cfgf = tempfile.NamedTemporaryFile(mode='w', delete=False)
35
36config = configparser.ConfigParser()
37config['uwsgi'] = {}
38config['uwsgi']['master'] = '1'
39config['uwsgi']['chdir'] = sitepath
40config['uwsgi']['module'] = 'djtest.wsgi'
41config['uwsgi']['env'] = 'DJANGO_SETTINGS_MODULE=djtest.settings'
42config['uwsgi']['http'] = '127.0.0.1:8080'
43config['uwsgi']['pythonpath'] = pythonpath
44
45config.write(cfgf)
46cfgf.close()
47
48args = [
Serge Bazanski4e8622d2020-09-25 20:23:28 +000049 ld,
Sergiusz Bazanski902d8602019-07-18 17:05:26 +020050 uwsgi,
51 '--ini', cfgf.name,
52]
53
54subprocess.call(args)
55
56os.unlink(cfgf.name)