Skip to content

Commit 03efac7

Browse files
devcontainer permissions changes
1 parent 401b4c6 commit 03efac7

File tree

4 files changed

+186
-25
lines changed

4 files changed

+186
-25
lines changed

.devcontainer/devcontainer.json

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@
3838
"ms-vscode.vscode-json"
3939
],
4040
"settings": {
41-
"python.defaultInterpreterPath": "/opt/venv/bin/python",
41+
"python.defaultInterpreterPath": "~/.venv/bin/python",
4242
"python.linting.enabled": true,
4343
"python.linting.pylintEnabled": true,
44-
"python.formatting.autopep8Path": "/opt/venv/bin/autopep8",
45-
"python.formatting.blackPath": "/opt/venv/bin/black",
46-
"python.formatting.yapfPath": "/opt/venv/bin/yapf",
47-
"python.linting.banditPath": "/opt/venv/bin/bandit",
48-
"python.linting.flake8Path": "/opt/venv/bin/flake8",
49-
"python.linting.mypyPath": "/opt/venv/bin/mypy",
50-
"python.linting.pycodestylePath": "/opt/venv/bin/pycodestyle",
51-
"python.linting.pydocstylePath": "/opt/venv/bin/pydocestyle",
52-
"python.linting.pylintPath": "/opt/venv/bin/pylint",
53-
"python.testing.pytestPath": "/opt/venv/bin/pytest",
44+
"python.formatting.autopep8Path": "~/.venv/bin/autopep8",
45+
"python.formatting.blackPath": "~/.venv/bin/black",
46+
"python.formatting.yapfPath": "~/.venv/bin/yapf",
47+
"python.linting.banditPath": "~/.venv/bin/bandit",
48+
"python.linting.flake8Path": "~/.venv/bin/flake8",
49+
"python.linting.mypyPath": "~/.venv/bin/mypy",
50+
"python.linting.pycodestylePath": "~/.venv/bin/pycodestyle",
51+
"python.linting.pydocstylePath": "~/.venv/bin/pydocestyle",
52+
"python.linting.pylintPath": "~/.venv/bin/pylint",
53+
"python.testing.pytestPath": "~/.venv/bin/pytest",
5454
"jupyter.askForKernelRestart": false,
5555
"jupyter.interactiveWindow.textEditor.executeSelection": true,
5656
"files.associations": {
@@ -60,11 +60,10 @@
6060
}
6161
},
6262
"containerEnv": {
63-
"PYTHONPATH": "/workspaces/Apim-Samples/shared/python:/workspaces/Apim-Samples",
64-
"VIRTUAL_ENV": "/opt/venv"
63+
"PYTHONPATH": "/workspaces/Apim-Samples/shared/python:/workspaces/Apim-Samples"
6564
},
6665
"initializeCommand": "echo 'Initializing dev container...'",
67-
"postCreateCommand": "bash .devcontainer/setup-simple.sh",
66+
"postCreateCommand": "bash .devcontainer/setup-codespaces.sh",
6867
"postStartCommand": "bash .devcontainer/post-start.sh",
6968
"forwardPorts": [
7069
8000,

.devcontainer/post-start.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ fi
4242
# ------------------------------
4343

4444
echo "✅ Verifying Python environment..."
45-
if [ -f "/opt/venv/bin/activate" ]; then
46-
source /opt/venv/bin/activate
45+
if [ -f "$HOME/.venv/bin/activate" ]; then
46+
source "$HOME/.venv/bin/activate"
4747
python --version
48+
echo "Using virtual environment at: $HOME/.venv"
49+
elif [ -f "/opt/venv/bin/activate" ]; then
50+
source "/opt/venv/bin/activate"
51+
python --version
52+
echo "Using virtual environment at: /opt/venv"
4853
else
4954
echo "⚠️ Virtual environment not found, using system Python"
5055
python --version

.devcontainer/setup-codespaces.sh

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/bin/bash
2+
3+
# ------------------------------
4+
# CODESPACES-COMPATIBLE SETUP
5+
# ------------------------------
6+
7+
set -e
8+
echo "🔧 Running Codespaces-compatible setup..."
9+
10+
# Basic environment setup
11+
export DEBIAN_FRONTEND=noninteractive
12+
13+
# ------------------------------
14+
# PYTHON VIRTUAL ENVIRONMENT
15+
# ------------------------------
16+
17+
echo "📦 Creating Python virtual environment in user space..."
18+
19+
# Create virtual environment in user home directory (no sudo needed)
20+
VENV_PATH="$HOME/.venv"
21+
22+
if [ ! -d "$VENV_PATH" ]; then
23+
echo "Creating virtual environment at $VENV_PATH"
24+
python3 -m venv "$VENV_PATH"
25+
else
26+
echo "Virtual environment already exists at $VENV_PATH"
27+
fi
28+
29+
# Activate virtual environment
30+
source "$VENV_PATH/bin/activate"
31+
32+
# Install packages in virtual environment
33+
echo "📦 Installing Python packages..."
34+
pip install --upgrade pip setuptools wheel
35+
pip install -r requirements.txt
36+
pip install pytest pytest-cov coverage
37+
38+
# ------------------------------
39+
# SHELL CONFIGURATION
40+
# ------------------------------
41+
42+
echo "🔧 Configuring shell environment..."
43+
44+
# Add virtual environment activation to bashrc
45+
if ! grep -q "source $VENV_PATH/bin/activate" ~/.bashrc; then
46+
cat >> ~/.bashrc << EOF
47+
48+
# Virtual environment activation
49+
export VIRTUAL_ENV="$VENV_PATH"
50+
export PATH="$VENV_PATH/bin:\$PATH"
51+
source $VENV_PATH/bin/activate
52+
53+
EOF
54+
fi
55+
56+
# Add virtual environment activation to zshrc (if it exists)
57+
if [ -f ~/.zshrc ]; then
58+
if ! grep -q "source $VENV_PATH/bin/activate" ~/.zshrc; then
59+
cat >> ~/.zshrc << EOF
60+
61+
# Virtual environment activation
62+
export VIRTUAL_ENV="$VENV_PATH"
63+
export PATH="$VENV_PATH/bin:\$PATH"
64+
source $VENV_PATH/bin/activate
65+
66+
EOF
67+
fi
68+
fi
69+
70+
# ------------------------------
71+
# PYTHON PATH SETUP
72+
# ------------------------------
73+
74+
echo "🔧 Setting up Python path..."
75+
python setup/setup_python_path.py --generate-env || echo "⚠️ Python path setup failed, continuing..."
76+
77+
# ------------------------------
78+
# AZURE CLI SETUP
79+
# ------------------------------
80+
81+
echo "☁️ Configuring Azure CLI..."
82+
az config set core.login_experience_v2=off 2>/dev/null || true
83+
84+
echo "📥 Installing Azure CLI extensions..."
85+
az extension add --name containerapp --only-show-errors 2>/dev/null || true
86+
az extension add --name front-door --only-show-errors 2>/dev/null || true
87+
88+
# ------------------------------
89+
# JUPYTER SETUP
90+
# ------------------------------
91+
92+
echo "📓 Setting up Jupyter..."
93+
python -m ipykernel install --user --name=apim-samples --display-name="APIM Samples Python" || echo "⚠️ Jupyter kernel setup failed, continuing..."
94+
95+
# ------------------------------
96+
# WORKSPACE CONFIGURATION
97+
# ------------------------------
98+
99+
echo "🛠️ Configuring workspace..."
100+
mkdir -p .vscode
101+
102+
cat > .vscode/settings.json << EOF
103+
{
104+
"python.terminal.activateEnvironment": true,
105+
"python.defaultInterpreterPath": "$VENV_PATH/bin/python",
106+
"python.analysis.extraPaths": [
107+
"/workspaces/Apim-Samples/shared/python"
108+
],
109+
"jupyter.kernels.filter": [
110+
{
111+
"path": "$VENV_PATH/bin/python",
112+
"type": "pythonEnvironment"
113+
}
114+
],
115+
"files.associations": {
116+
"*.bicep": "bicep"
117+
},
118+
"python.envFile": "\${workspaceFolder}/.env"
119+
}
120+
EOF
121+
122+
# ------------------------------
123+
# COMPLETION
124+
# ------------------------------
125+
126+
echo "✅ Creating completion marker..."
127+
echo "$(date): Codespaces-compatible setup completed" > .devcontainer/.prebuild-complete
128+
echo "Virtual environment path: $VENV_PATH" >> .devcontainer/.prebuild-complete
129+
130+
echo "🎉 Setup complete!"
131+
132+
# Simple verification
133+
echo "✅ Verification:"
134+
echo "Python version: $(python --version)"
135+
echo "Virtual env location: $VENV_PATH"
136+
echo "Packages installed: $(pip list | wc -l) packages"
137+
echo "Python executable: $(which python)"
138+
139+
echo "📋 Setup complete - virtual environment ready!"
140+
echo "💡 Virtual environment will be auto-activated in new terminal sessions"

.devcontainer/verify-venv.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,22 @@
3030

3131
def check_virtual_environment():
3232
"""Check if we're running in the expected virtual environment."""
33-
expected_venv = '/opt/venv'
33+
import os
34+
expected_venv_locations = [
35+
os.path.expanduser('~/.venv'),
36+
'/opt/venv'
37+
]
38+
3439
current_prefix = sys.prefix
3540

36-
if expected_venv in current_prefix:
37-
print(f"✅ Running in virtual environment: {current_prefix}")
38-
return True
39-
else:
40-
print(f"❌ Not running in expected virtual environment. Current: {current_prefix}")
41-
return False
41+
for expected_venv in expected_venv_locations:
42+
if expected_venv in current_prefix:
43+
print(f"✅ Running in virtual environment: {current_prefix}")
44+
return True
45+
46+
print(f"❌ Not running in expected virtual environment. Current: {current_prefix}")
47+
print(f"Expected one of: {expected_venv_locations}")
48+
return False
4249

4350
def check_package_imports():
4451
"""Check if all required packages can be imported."""
@@ -56,15 +63,25 @@ def check_package_imports():
5663

5764
def check_executables():
5865
"""Check if Python executables are correctly configured."""
66+
import os
5967
try:
6068
python_path = subprocess.check_output(['which', 'python'], text=True).strip()
6169
pip_path = subprocess.check_output(['which', 'pip'], text=True).strip()
6270

6371
print(f"✅ Python executable: {python_path}")
6472
print(f"✅ Pip executable: {pip_path}")
6573

66-
expected_venv = '/opt/venv'
67-
return expected_venv in python_path and expected_venv in pip_path
74+
expected_venv_locations = [
75+
os.path.expanduser('~/.venv'),
76+
'/opt/venv'
77+
]
78+
79+
for expected_venv in expected_venv_locations:
80+
if expected_venv in python_path and expected_venv in pip_path:
81+
return True
82+
83+
print(f"⚠️ Executables not in expected virtual environment locations: {expected_venv_locations}")
84+
return False
6885
except subprocess.CalledProcessError:
6986
print("❌ Failed to locate Python or pip executables")
7087
return False

0 commit comments

Comments
 (0)