Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.

Commit 1eb5d5b

Browse files
authored
Merge pull request #4 from stashfiler/dev
Add support for auto-translating path(s)
2 parents 3bbbf9f + c83c906 commit 1eb5d5b

File tree

3 files changed

+56
-39
lines changed

3 files changed

+56
-39
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Docker Compose WSL
2-
Use Docker Compose from your Windows Subsystem for Linux (WSL) distribution from PyCharm on Windows
2+
Use Docker Compose from your Windows Subsystem for Linux (WSL) distribution from an IDE that integrate Docker Compose like PyCharm on Windows
33

4-
This tool will translate Windows paths, used by the PyCharm IDE, to WSL paths, including the path(s) passed by PyCharm to the docker-compose binary, but also inside the PyCharm docker-compose override file.
4+
This tool will translate the Windows path(s) passed to the docker-compose binary, but also the one(s) inside the docker-compose file(s) passed as argument(s).
55

66
## Download
77

@@ -16,7 +16,7 @@ The latest binary can be found on the [release page](https://github.com/stashfil
1616

1717
## Remarks
1818

19-
The initial 0.1 release is rough and only support translating the C:\ drive to the /c path inside WSL, but I plan to add support for allowing any drive to be mapped to any WSL path(s), using a YAML configuration file.
19+
This tool should work with any IDE that use the docker-compose command-line tool, but was only tested with PyCharm
2020

2121
## Build from source
2222

docker-compose-wsl.py

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,86 @@
33
import sys
44
import subprocess
55
import pathlib
6-
7-
# 3rd party libraries
8-
# import yaml
6+
import re
97

108
# 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 = ''
1812
bash_variables = ''
1913

2014
# Function(s)
2115
def wsl_drive_map(string_to_map):
2216
'''
2317
Replace all the instance of the drive(s) letter(s) to their corresponding WSL mount point
2418
25-
:param string_to_map:
19+
:param path:
2620
:return:
2721
'''
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)
2827

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]+):[\\|/|\\\\]')
3230

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())
3634

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('\\\\', '/')
3837

39-
return string_to_map
38+
return mapped_string
4039

4140
# Add the docker variable(s), defined in the Windows environment variable(s) to the docker-compose command
4241
for key, value in os.environ.items():
4342
if key.startswith('DOCKER'):
43+
44+
try:
45+
value = int(value)
46+
except ValueError:
47+
pass
48+
4449
if isinstance(value, int):
4550
bash_variables += '{0}={1} '.format(key, value)
4651
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)
4861

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(
5278
bash_variables,
5379
docker_compose_cli_args
5480
)
5581
]
5682

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)))
6886

6987
# 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)

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
pyaml
21
pyinstaller

0 commit comments

Comments
 (0)