Sergiusz Bazanski | 902d860 | 2019-07-18 17:05:26 +0200 | [diff] [blame] | 1 | import configparser |
| 2 | import os |
| 3 | import subprocess |
| 4 | import tempfile |
| 5 | |
| 6 | import bazel_tools |
| 7 | import bazel_tools.tools.python |
| 8 | |
Serge Bazanski | 4e8622d | 2020-09-25 20:23:28 +0000 | [diff] [blame] | 9 | from elftools.elf.elffile import ELFFile |
| 10 | from elftools.elf.segments import InterpSegment |
| 11 | |
Sergiusz Bazanski | 902d860 | 2019-07-18 17:05:26 +0200 | [diff] [blame] | 12 | from bazel_tools.tools.python.runfiles import runfiles |
| 13 | r = runfiles.Create() |
| 14 | |
Bartosz Stebel | cae27ec | 2020-07-03 07:05:15 +0200 | [diff] [blame] | 15 | uwsgi = r.Rlocation("pydeps_pypi__uWSGI_2_0_18/uWSGI-2.0.18.data/scripts/uwsgi") |
Sergiusz Bazanski | 5f9b1ec | 2019-09-22 02:19:18 +0200 | [diff] [blame] | 16 | settings = r.Rlocation("hscloud/personal/q3k/djtest/djtest/settings.py") |
Serge Bazanski | 4e8622d | 2020-09-25 20:23:28 +0000 | [diff] [blame] | 17 | |
| 18 | # uwsgi from runfiles is non-chmodded, we have to run it through its interpreter |
| 19 | ld = None |
| 20 | with 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() |
| 25 | if ld is None: |
| 26 | raise Exception("could not find interpreter/ld.so path in uwsgi - failing") |
Sergiusz Bazanski | 902d860 | 2019-07-18 17:05:26 +0200 | [diff] [blame] | 27 | |
| 28 | apppath = os.path.dirname(settings) |
| 29 | sitepath = os.path.dirname(apppath) |
| 30 | |
| 31 | pythonpath = os.environ['PYTHONPATH'] |
| 32 | |
| 33 | # Make UWSGI ini config file |
| 34 | cfgf = tempfile.NamedTemporaryFile(mode='w', delete=False) |
| 35 | |
| 36 | config = configparser.ConfigParser() |
| 37 | config['uwsgi'] = {} |
| 38 | config['uwsgi']['master'] = '1' |
| 39 | config['uwsgi']['chdir'] = sitepath |
| 40 | config['uwsgi']['module'] = 'djtest.wsgi' |
| 41 | config['uwsgi']['env'] = 'DJANGO_SETTINGS_MODULE=djtest.settings' |
| 42 | config['uwsgi']['http'] = '127.0.0.1:8080' |
| 43 | config['uwsgi']['pythonpath'] = pythonpath |
| 44 | |
| 45 | config.write(cfgf) |
| 46 | cfgf.close() |
| 47 | |
| 48 | args = [ |
Serge Bazanski | 4e8622d | 2020-09-25 20:23:28 +0000 | [diff] [blame] | 49 | ld, |
Sergiusz Bazanski | 902d860 | 2019-07-18 17:05:26 +0200 | [diff] [blame] | 50 | uwsgi, |
| 51 | '--ini', cfgf.name, |
| 52 | ] |
| 53 | |
| 54 | subprocess.call(args) |
| 55 | |
| 56 | os.unlink(cfgf.name) |