|
3 | 3 | import sys |
4 | 4 | import subprocess |
5 | 5 | import pathlib |
6 | | - |
7 | | -# 3rd party libraries |
8 | | -# import yaml |
| 6 | +import re |
9 | 7 |
|
10 | 8 | # 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:]) |
| 9 | +compose_file_tmp_dir = os.path.join(os.path.expandvars('%userprofile%'), 'AppData', 'Local', 'Temp') |
| 10 | +cli_args = sys.argv[1:] |
| 11 | +docker_compose_cli_args = '' |
18 | 12 | bash_variables = '' |
19 | 13 |
|
20 | 14 | # Function(s) |
21 | 15 | def wsl_drive_map(string_to_map): |
22 | 16 | ''' |
23 | 17 | Replace all the instance of the drive(s) letter(s) to their corresponding WSL mount point |
24 | 18 |
|
25 | | - :param string_to_map: |
| 19 | + :param path: |
26 | 20 | :return: |
27 | 21 | ''' |
| 22 | + mapped_string = string_to_map |
| 23 | + |
| 24 | + paths_regex = re.compile(r'([A-Za-z]+:[\\|/|\\\\][^:]+)') |
| 25 | + for windows_path_match in re.finditer(paths_regex, string_to_map): |
| 26 | + windows_path = windows_path_match.group(1) |
28 | 27 |
|
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 |
| 28 | + if os.path.exists(windows_path): |
| 29 | + drives_regex = re.compile(r'([A-Za-z]+):[\\|/|\\\\]') |
32 | 30 |
|
33 | | - string_to_map = string_to_map \ |
34 | | - .replace('{0}:\\'.format(win_drive), wsl_mount) \ |
35 | | - .replace('{0}:/'.format(win_drive), wsl_mount) |
| 31 | + for drive in re.finditer(drives_regex, windows_path): |
| 32 | + windows_drive = drive.group(1) |
| 33 | + wsl_mount = '/{0}/'.format(windows_drive.lower()) |
36 | 34 |
|
37 | | - string_to_map = string_to_map.replace('\\', '/') |
| 35 | + mapped_string = re.sub(r'{0}:[\\|/|\\\\]'.format(windows_drive), wsl_mount, mapped_string) |
| 36 | + mapped_string = mapped_string.replace('\\', '/').replace('\\\\', '/') |
38 | 37 |
|
39 | | - return string_to_map |
| 38 | + return mapped_string |
40 | 39 |
|
41 | 40 | # Add the docker variable(s), defined in the Windows environment variable(s) to the docker-compose command |
42 | 41 | for key, value in os.environ.items(): |
43 | 42 | if key.startswith('DOCKER'): |
| 43 | + |
| 44 | + try: |
| 45 | + value = int(value) |
| 46 | + except ValueError: |
| 47 | + pass |
| 48 | + |
44 | 49 | if isinstance(value, int): |
45 | 50 | bash_variables += '{0}={1} '.format(key, value) |
46 | 51 | else: |
47 | | - bash_variables += '{0}="{1}"'.format(key, value) |
| 52 | + bash_variables += '{0}="{1}" '.format(key, value) |
| 53 | + |
| 54 | +# Replace Windows path with WSL supported path |
| 55 | +for idx, cli_arg in enumerate(cli_args): |
| 56 | + |
| 57 | + if idx != 0 and cli_args[idx - 1] == '-f': |
| 58 | + compose_file = cli_arg |
| 59 | + compose_file_name = os.path.basename(compose_file) |
| 60 | + tmp_compose_file = os.path.join(compose_file_tmp_dir, compose_file_name) |
48 | 61 |
|
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( |
| 62 | + if os.path.isfile(compose_file): |
| 63 | + compose_file_content = pathlib.Path(compose_file).read_text() |
| 64 | + new_compose_file_content = wsl_drive_map(compose_file_content) |
| 65 | + pathlib.Path(tmp_compose_file).write_text(new_compose_file_content) |
| 66 | + else: |
| 67 | + sys.stderr.write('The PyCharm docker-compose file "{0}" does not exists, exiting...'.format( |
| 68 | + compose_file)) |
| 69 | + raise SystemExit(1) |
| 70 | + |
| 71 | + compose_file_mapped = wsl_drive_map(tmp_compose_file) |
| 72 | + docker_compose_cli_args += '{0} '.format(compose_file_mapped) |
| 73 | + |
| 74 | + else: |
| 75 | + docker_compose_cli_args += '{0} '.format(cli_arg) |
| 76 | + |
| 77 | +docker_compose_cmd = ['C:\\Windows\\System32\\bash.exe', '-c', '{0} docker-compose {1}'.format( |
52 | 78 | bash_variables, |
53 | 79 | docker_compose_cli_args |
54 | 80 | ) |
55 | 81 | ] |
56 | 82 |
|
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)) |
| 83 | +print('The docker-compose bash variable(s) were: {0}'.format(bash_variables)) |
| 84 | +print('The docker-compose argument(s) were: {0}'.format(docker_compose_cli_args)) |
| 85 | +print('The docker-compose command was: {0}'.format(' '.join(docker_compose_cmd))) |
68 | 86 |
|
69 | 87 | # Run the docker-compose binary in bash on WSL with the appropriate argument(s) |
70 | | -subprocess.run(cli_args, shell=True) |
| 88 | +subprocess.run(docker_compose_cmd, shell=True) |
0 commit comments