gmake_python_helpers

Helpers for using Python venv/pip-tools/black/pylama/... with GNU make
git clone https://ccx.te2000.cz/git/gmake_python_helpers
Log | Files | Refs | README

__main__.py (3091B)


      1 #!/usr/bin/env python
      2 # vim: fileencoding=utf-8 ft=python et sw=4 ts=4 sts=4 tw=79
      3 
      4 from __future__ import (
      5     generators, division, absolute_import, with_statement, print_function
      6 )
      7 import sys
      8 import os.path
      9 import stat
     10 
     11 try:
     12     import venv
     13 except ImportError:
     14     venv = None
     15 
     16 try:
     17     import piptools
     18 except ImportError:
     19     piptools = None
     20 
     21 try:
     22     import wheel
     23 except ImportError:
     24     wheel = None
     25 
     26 try:
     27     import pip
     28 except ImportError:
     29     pip = None
     30 
     31 if os.environ.get('gmake_python_helpers_ignore_system_modules', ''):
     32     piptools = None
     33     wheel = None
     34 
     35 from .platform import get_wheel_tag, PY2
     36 
     37 
     38 def find_in_path(program):
     39     for directory in os.environ['PATH'].split(os.pathsep):
     40         try:
     41             fullpath = os.path.join(directory, program)
     42             s = os.stat()
     43         except OSError:
     44             pass
     45         if stat.S_ISREG(s.st_mode) and s.st_mode & 0x49:
     46             # Is executable plain file
     47             return fullpath
     48     return None
     49 
     50 
     51 def write_mk(f, tag):
     52     def define(**kwargs):
     53         for k, v in sorted(kwargs.items()):
     54             f.write('%s:=%s\n' % (k, v))
     55 
     56     def lazy_define(**kwargs):
     57         for k, v in sorted(kwargs.items()):
     58             f.write('%s=%s\n' % (k, v))
     59 
     60     # TODO: add proper escaping whenever I feel like staring into the abyss
     61     f.write('PYTHON_IMPL:=%s\n' % tag)
     62 
     63     if venv is None:
     64         if find_in_path("virtualenv") is None:
     65             sys.stderr.write((
     66                 "fatal: could not find usable venv/virtualenv implementation"
     67                 " for %r (%s) - please install one\n"
     68             ) % (sys.executable, tag))
     69             sys.exit(1)
     70         define(PYTHON_VENV="virtualenv -p $(PYTHON_EXE)")
     71     else:
     72         define(PYTHON_VENV="$(PYTHON_EXE) -m venv")
     73 
     74     if piptools is None:
     75         # use separate venv for piptools pip-compile
     76         lazy_define(PIP_COMPILE="$(VENV_PIP)/bin/pip-compile")
     77         lazy_define(PIP_COMPILE_DEP="$(VENV_PIP)/bin/pip-compile")
     78     else:
     79         # use system pip-compile
     80         define(PIP_COMPILE="$(PYTHON_EXE) -m piptools compile")
     81         define(PIP_COMPILE_DEP="")
     82 
     83     if wheel is None or pip is None:
     84         # use separate venv for pip+wheel
     85         lazy_define(PIP_WHEEL="$(VENV_PIP)/bin/pip wheel")
     86         lazy_define(PIP_WHEEL_DEP="$(VENV_PIP)/bin/wheel")
     87     else:
     88         # use system pip+wheel
     89         define(PIP_WHEEL="$(PYTHON_EXE) -m pip wheel")
     90         define(PIP_WHEEL_DEP="")
     91 
     92     define(PYTHON_VENV_INSTALL=(
     93         "'setuptools<45.0.0' 'pip<20.3' 'pip-tools<6'"
     94         if PY2 else
     95         "'pip-tools'"
     96     ))
     97 
     98 def main():
     99     try:
    100         tag = '%s-%s-%s' % get_wheel_tag()
    101     except Exception as e:
    102         print("$(error Error determining the Python implementation: %r)" % (e,))
    103         raise
    104     try:
    105         mk_filename = 'python_vars_%s.mk' % tag
    106         with open(mk_filename, 'wt') as f:
    107             write_mk(f, tag)
    108         print("include %s" % mk_filename)
    109     except Exception as e:
    110         print("$(error Internal error resolving Python variables: %r)" % (e,))
    111         raise
    112 
    113 if __name__ == '__main__':
    114     main()