contrib/automation/automation.py
changeset 42024 b05a3e28cf24
child 43076 2372284d9457
equal deleted inserted replaced
42023:bf87d34a675c 42024:b05a3e28cf24
       
     1 #!/usr/bin/env python3
       
     2 #
       
     3 # automation.py - Perform tasks on remote machines
       
     4 #
       
     5 # Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com>
       
     6 #
       
     7 # This software may be used and distributed according to the terms of the
       
     8 # GNU General Public License version 2 or any later version.
       
     9 
       
    10 import os
       
    11 import pathlib
       
    12 import subprocess
       
    13 import sys
       
    14 import venv
       
    15 
       
    16 
       
    17 HERE = pathlib.Path(os.path.abspath(__file__)).parent
       
    18 REQUIREMENTS_TXT = HERE / 'requirements.txt'
       
    19 SOURCE_DIR = HERE.parent.parent
       
    20 VENV = SOURCE_DIR / 'build' / 'venv-automation'
       
    21 
       
    22 
       
    23 def bootstrap():
       
    24     venv_created = not VENV.exists()
       
    25 
       
    26     VENV.parent.mkdir(exist_ok=True)
       
    27 
       
    28     venv.create(VENV, with_pip=True)
       
    29 
       
    30     if os.name == 'nt':
       
    31         venv_bin = VENV / 'Scripts'
       
    32         pip = venv_bin / 'pip.exe'
       
    33         python = venv_bin / 'python.exe'
       
    34     else:
       
    35         venv_bin = VENV / 'bin'
       
    36         pip = venv_bin / 'pip'
       
    37         python = venv_bin / 'python'
       
    38 
       
    39     args = [str(pip), 'install', '-r', str(REQUIREMENTS_TXT),
       
    40             '--disable-pip-version-check']
       
    41 
       
    42     if not venv_created:
       
    43         args.append('-q')
       
    44 
       
    45     subprocess.run(args, check=True)
       
    46 
       
    47     os.environ['HGAUTOMATION_BOOTSTRAPPED'] = '1'
       
    48     os.environ['PATH'] = '%s%s%s' % (
       
    49         venv_bin, os.pathsep, os.environ['PATH'])
       
    50 
       
    51     subprocess.run([str(python), __file__] + sys.argv[1:], check=True)
       
    52 
       
    53 
       
    54 def run():
       
    55     import hgautomation.cli as cli
       
    56 
       
    57     # Need to strip off main Python executable.
       
    58     cli.main()
       
    59 
       
    60 
       
    61 if __name__ == '__main__':
       
    62     try:
       
    63         if 'HGAUTOMATION_BOOTSTRAPPED' not in os.environ:
       
    64             bootstrap()
       
    65         else:
       
    66             run()
       
    67     except subprocess.CalledProcessError as e:
       
    68         sys.exit(e.returncode)
       
    69     except KeyboardInterrupt:
       
    70         sys.exit(1)