|
| 1 | +# Python Standard Libraries |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +import pathlib |
| 6 | + |
| 7 | +# 3rd party libraries |
| 8 | +# import yaml |
| 9 | + |
| 10 | +# Variable(s) |
| 11 | +pycharm_compose_override = sys.argv[4] if len(sys.argv) > 3 else '' |
| 12 | +default_config = { |
| 13 | + 'wsl_drive_map': { |
| 14 | + 'C': '/c' |
| 15 | + } |
| 16 | +} |
| 17 | +docker_compose_cli_args = ' '.join(sys.argv[1:]) |
| 18 | +bash_variables = '' |
| 19 | + |
| 20 | +# Function(s) |
| 21 | +def wsl_drive_map(string_to_map): |
| 22 | + ''' |
| 23 | + Replace all the instance of the drive(s) letter(s) to their corresponding WSL mount point |
| 24 | +
|
| 25 | + :param string_to_map: |
| 26 | + :return: |
| 27 | + ''' |
| 28 | + |
| 29 | + for win_drive, wsl_mount in default_config.get('wsl_drive_map').items(): |
| 30 | + win_drive = win_drive.upper() |
| 31 | + wsl_mount = '{0}/'.format(wsl_mount) if wsl_mount[-1] != '/' else wsl_mount |
| 32 | + |
| 33 | + string_to_map = string_to_map \ |
| 34 | + .replace('{0}:\\'.format(win_drive), wsl_mount) \ |
| 35 | + .replace('{0}:/'.format(win_drive), wsl_mount) |
| 36 | + |
| 37 | + string_to_map = string_to_map.replace('\\', '/') |
| 38 | + |
| 39 | + return string_to_map |
| 40 | + |
| 41 | +# Add the docker variable(s), defined in the Windows environment variable(s) to the docker-compose command |
| 42 | +for key, value in os.environ.items(): |
| 43 | + if key.startswith('DOCKER'): |
| 44 | + if isinstance(value, int): |
| 45 | + bash_variables += '{0}={1} '.format(key, value) |
| 46 | + else: |
| 47 | + bash_variables += '{0}="{1}"'.format(key, value) |
| 48 | + |
| 49 | +# Replace the PyCharm command line option(s) Windows path with WSL path(s) |
| 50 | +docker_compose_cli_args = wsl_drive_map(docker_compose_cli_args) |
| 51 | +cli_args = ['C:\\Windows\\System32\\bash.exe', '-c', '{0} docker-compose {1}'.format( |
| 52 | + bash_variables, |
| 53 | + docker_compose_cli_args |
| 54 | + ) |
| 55 | +] |
| 56 | + |
| 57 | +# Replace Windows path with WSL supported path |
| 58 | +if os.path.isfile(pycharm_compose_override): |
| 59 | + pycharm_override_content = pathlib.Path(pycharm_compose_override).read_text() |
| 60 | + new_pycharm_override = wsl_drive_map(pycharm_override_content) |
| 61 | + pathlib.Path(pycharm_compose_override).write_text(new_pycharm_override) |
| 62 | +else: |
| 63 | + sys.stderr.write('The PyCharm docker-compose override file "{0}" does not exists, exiting...'.format( |
| 64 | + pycharm_compose_override)) |
| 65 | + raise SystemExit(1) |
| 66 | + |
| 67 | +print('The docker-compose argument(s) were: {0}'.format(bash_variables)) |
| 68 | + |
| 69 | +# Run the docker-compose binary in bash on WSL with the appropriate argument(s) |
| 70 | +subprocess.run(cli_args, shell=True) |
0 commit comments