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

Commit 708c736

Browse files
authored
Merge pull request #1 from stashfiler/dev
Initial 0.1 release
2 parents ace7de9 + 6eaf038 commit 708c736

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea/
2+
__pycache__/
3+
build/
4+
dist/
5+
docker-compose-wsl.spec

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
1-
# docker-compose-wsl
2-
Use Docker Compose on Windows using the Windows Subsystem for Linux (WSL) from PyCharm
1+
# Docker Compose WSL
2+
Use Docker Compose from your Windows Subsystem for Linux (WSL) distribution from PyCharm on Windows
3+
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.
5+
6+
## Download
7+
8+
The latest binary can be found on the [release page](https://github.com/stashfiler/docker-compose-wsl/releases)
9+
10+
## Usage in PyCharm
11+
12+
1. Download the [latest binary](https://github.com/stashfiler/docker-compose-wsl/releases)
13+
2. Move the binary to any directory accessible by the PyCharm IDE
14+
3. In PyCharm, go to `File > Settings...`
15+
4. In the `Settings` window, go to `Build, Execution, Deployment > Docker > Tools` and set the **Docker Compose executable** path to the **docker-compose-wsl** binary path
16+
17+
## Remarks
18+
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.
20+
21+
## Thanks
22+
23+
* Andy-5 for [WSLGit](https://github.com/andy-5/wslgit) which served as the inspiration for creating this tool

docker-compose-wsl.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyaml

0 commit comments

Comments
 (0)