Skip to content

Commit da9b3bc

Browse files
committed
.
1 parent 2bb0cf7 commit da9b3bc

3 files changed

Lines changed: 121 additions & 4 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM mcr.microsoft.com/devcontainers/python:1-3.12-bookworm
2+
3+
# [Optional] Allow the vscode user to pip install globally w/o sudo
4+
# ENV PIP_TARGET=/usr/local/pip-global
5+
# ENV PYTHONPATH=${PIP_TARGET}:${PYTHONPATH}
6+
# ENV PATH=${PIP_TARGET}/bin:${PATH}
7+
# RUN mkdir -p ${PIP_TARGET} \
8+
# && chown vscode:root ${PIP_TARGET} \
9+
# && echo "if [ \"\$(stat -c '%U' ${PIP_TARGET})\" != \"vscode\" ]; then chown -R vscode:root ${PIP_TARGET}; fi" \
10+
# | tee -a /root/.bashrc /home/vscode/.bashrc /root/.zshrc >> /home/vscode/.zshrc
11+
12+
# copy dependency sync script for session and package management
13+
COPY ./.devcontainer/sync_deps.py /workspaces/session/post-start/
14+
15+
# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
16+
# COPY requirements.txt /tmp/pip-tmp/
17+
# RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
18+
# && rm -rf /tmp/pip-tmp
19+
20+
# copy requirements list into layer of dev image
21+
COPY requirements.txt /workspaces/session/
22+
RUN pip --disable-pip-version-check --no-cache-dir install -r /workspaces/session/requirements.txt
23+
24+
# [Optional] Uncomment this section to install additional OS packages.
25+
# RUN apt-get update \
26+
# && export DEBIAN_FRONTEND=noninteractive \
27+
# && apt-get -y install --no-install-recommends <your-package-list-here>

.devcontainer/devcontainer.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
// This is the default
77
// "image": "mcr.microsoft.com/devcontainers/universal:2",
8-
9-
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
10-
//"image": "mcr.microsoft.com/devcontainers/python:1-${templateOption:imageVariant}",
118
//"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bookworm",
129

1310
// Using the prebuild
14-
"image": "ghcr.io/league-infrastructure/jtlpython:latest",
11+
// "image": "ghcr.io/league-infrastructure/jtlpython:latest",
12+
13+
"build": {
14+
"dockerfile": "Dockerfile",
15+
"context": ".."
16+
},
1517

1618
// Features to add to the dev container. More info: https://containers.dev/features.
1719
"features": {

.devcontainer/sync_deps.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/local/bin/python
2+
3+
#-----------------------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See LICENSE in the project root for license information.
6+
#-----------------------------------------------------------------------------------------
7+
import os
8+
import sys
9+
import difflib
10+
11+
orig_deps_file = './../session/requirements.txt'
12+
new_deps_file = './requirements.txt'
13+
14+
'''
15+
read package dependencies listed in requirements.txt files
16+
'''
17+
def read_dependencies():
18+
orig_deps = ""
19+
new_deps = ""
20+
if os.path.exists(orig_deps_file)and os.path.exists(new_deps_file):
21+
orig_deps = open(orig_deps_file, 'r')
22+
new_deps = open(new_deps_file, 'r')
23+
else:
24+
raise Exception("A requirements.txt file was not found")
25+
return orig_deps, new_deps
26+
27+
'''
28+
report the diff between two requirements.txt files
29+
'''
30+
def report_changes(orig_deps, new_deps):
31+
change_report = difflib.ndiff(orig_deps.readlines(), new_deps.readlines())
32+
return change_report
33+
34+
'''
35+
sync changes by detecting changes in requirements.txt to pip un-/install
36+
'''
37+
def sync_changes(change_report):
38+
changes_detected = False
39+
for change in change_report:
40+
if change.startswith('- '):
41+
dependency = change[2:].replace('\n','')
42+
auto_uninstall = "-y"
43+
print("Detected requirements.txt change for [{dep}] \n".format(dep=dependency))
44+
sys.stdout.flush()
45+
# Note: Uninstall will not remove site-packages/dist-info directories or resultant dangling dependencies
46+
os.system("sudo pip --no-cache-dir uninstall {uninstall} {dep}".format(uninstall=auto_uninstall, dep=dependency))
47+
changes_detected = True
48+
if change.startswith('+ '):
49+
dependency = change[2:].replace('\n','')
50+
print("Detected requirements.txt change for [{dep}] \n".format(dep=dependency))
51+
sys.stdout.flush()
52+
os.system("sudo pip --no-cache-dir install {dep}".format(dep=dependency))
53+
changes_detected = True
54+
return changes_detected
55+
56+
'''
57+
replace session requirements with newest dependencies
58+
'''
59+
def replace_session_deps(changes_detected):
60+
if not changes_detected:
61+
print("No changes detected \n")
62+
sys.stdout.flush()
63+
return
64+
65+
if os.path.exists(orig_deps_file) and os.path.exists(new_deps_file):
66+
os.remove(orig_deps_file )
67+
print("Updating session with latest requirements.txt \n")
68+
sys.stdout.flush()
69+
with open(new_deps_file) as new_deps:
70+
with open(orig_deps_file, "w") as orig_deps:
71+
for line in new_deps:
72+
orig_deps.write(line)
73+
else:
74+
raise Exception("A requirements.txt file was not found \n")
75+
76+
def main():
77+
try:
78+
print("main module: syncing requirements.txt deps for devcontainer session in [os:{os}]...\n".format(os=sys.platform))
79+
sys.stdout.flush()
80+
orig_deps, new_deps = read_dependencies()
81+
change_report = report_changes(orig_deps, new_deps)
82+
changes_detected = sync_changes(change_report)
83+
replace_session_deps(changes_detected)
84+
except Exception as ex:
85+
print(str(ex))
86+
87+
if __name__ == '__main__':
88+
sys.exit(main())

0 commit comments

Comments
 (0)