Skip to content

Commit

Permalink
Update files for Binder
Browse files Browse the repository at this point in the history
  • Loading branch information
kwankyu authored and alice committed Dec 22, 2024
0 parents commit 8a6b2f1
Show file tree
Hide file tree
Showing 11 changed files with 2,665 additions and 0 deletions.
57 changes: 57 additions & 0 deletions .github/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Multi-stage Dockerfile for slim Sage
# https://docs.docker.com/build/building/multi-stage

FROM ghcr.io/sagemath/sage/sage-ubuntu-jammy-minimal-with-targets:10.5 AS target

# Resolve symbolic links to recreate them later
RUN readlink /sage/prefix >> /sage/prefix_link
RUN readlink /sage/venv >> /sage/venv_link

WORKDIR /sage

# Strip executables in /sage
RUN LC_ALL=C find local src pkgs -type f -exec strip '{}' ';' 2>&1 | grep -v "file format not recognized" || true

# Clean up before copying in the next stage
RUN rm -rf local/share/doc local/share/*/doc local/share/*/examples local/share/singular/html

# Remove all __pycache__ directories
RUN find /sage -type d -name "__pycache__" -exec rm -rf {} +
# Remove all .c .cpp files corresponding to .pyx files
RUN find /sage -name "*.pyx" | sed 's/\(.*\)[.]pyx$/\1.c \1.cpp/' | xargs rm -f +

FROM ghcr.io/sagemath/sage/sage-ubuntu-jammy-minimal-with-system-packages:10.5

USER root

# Remove warnings
ENV DEBIAN_FRONTEND noninteractive
ENV DEBCONF_NOWARNINGS="yes"

# Install JupyterLab to the system
RUN apt-get update
RUN apt-get install -y --no-install-recommends python3-pip
RUN python3 -m pip install --no-warn-script-location --no-cache-dir notebook jupyterlab ipywidgets

# Disable annoying popup for Jupyter news
RUN jupyter labextension disable "@jupyterlab/apputils-extension:announcements"

# Install /sage from target
COPY --from=target /sage/local /sage/local
COPY --from=target /sage/src/bin /sage/src/bin
COPY --from=target /sage/src/sage /sage/src/sage
COPY --from=target /sage/sage /sage/sage
COPY --from=target /sage/pkgs/sage-conf /sage/pkgs/sage-conf

# Recreate symbolic links
COPY --from=target /sage/prefix_link /sage/prefix_link
COPY --from=target /sage/venv_link /sage/venv_link
RUN ln -s $(cat /sage/prefix_link) /sage/prefix && rm /sage/prefix_link
RUN ln -s $(cat /sage/venv_link) /sage/venv && rm /sage/venv_link

# Configure Sage library
RUN /sage/sage -pip install --root-user-action=ignore /sage/pkgs/sage-conf

# Remove problematic lines!
RUN sed -i '/^__requires__/d' /sage/venv/bin/sage-venv-config
RUN sed -i '/^__import__/d' /sage/venv/bin/sage-venv-config
Binary file added .github/icons/blue.ico
Binary file not shown.
Binary file added .github/icons/orange.ico
Binary file not shown.
311 changes: 311 additions & 0 deletions .github/workflows/create-wsl-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
name: Build docker image and export for WSL

on:
schedule:
- cron: '0 2 * * *' # Run every day
workflow_dispatch:

jobs:
build-and-export:
runs-on: ubuntu-latest
outputs:
upload_url: ${{ steps.create_release.outputs.upload_url }}
version: ${{ steps.extract_version.outputs.version }}
steps:

# Create WSL export (a tar file) of the minimal Sage install

- name: Prepare Dockerfile
run: |
curl -o Dockerfile https://raw.githubusercontent.com/sagemath/sage-binder-env/master/Dockerfile
sed -i '/COPY notebooks/d' Dockerfile # remove the line from Dockerfile
- name: Extract version from Dockerfile
id: extract_version
run: |
VERSION=$(grep '^FROM ghcr.io/sagemath/sage-binder-env:' Dockerfile | sed 's/^FROM ghcr.io\/sagemath\/sage-binder-env://')
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Build and export Docker image
run: |
docker build -t sagemath-container .
export CONTAINER_ID=$(docker create sagemath-container)
docker export $CONTAINER_ID -o sagemath-$VERSION-wsl.tar
- name: Upload tar file as artifact
id: upload-artifact
uses: actions/upload-artifact@v4
with:
name: sagemath-${{ env.VERSION }}-wsl
path: sagemath-${{ env.VERSION }}-wsl.tar

# Make a release

- name: Get latest release version
id: get_latest_release
run: |
LATEST_VERSION=$(curl -s https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r '.tag_name // "0.0.0"' | sed 's/v//')
echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV
- name: Zip the tar file
if: ${{ env.VERSION != env.LATEST_VERSION }}
run: |
zip sagemath-${{ env.VERSION }}-wsl.zip sagemath-${{ env.VERSION }}-wsl.tar
- name: Create GitHub Release
if: ${{ env.VERSION != env.LATEST_VERSION }}
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ env.VERSION }}
release_name: Run SageMath ${{ env.VERSION }} on WSL
body: |
We recommend you check if WSL is enabled before running the installer.
The .ps1 installer downloads the .zip file and imports a minimized SageMath ${{ env.VERSION }} into WSL.
After downloading the installer, right-click it to open the context menu and run it with PowerShell.
You may also open the installer as a text file, and copy and paste all lines into a PowerShell window.
The installer should create two shortcuts on your desktop: one for launching SageMath
and another to start the Jupyter server for SageMath.
The minimized SageMath is the same as the one used in the Binder environment, meaning no bells and whistles.
Notably the Sage build system is not included.
draft: false
prerelease: false

- name: Upload release asset
if: ${{ env.VERSION != env.LATEST_VERSION }}
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./sagemath-${{ env.VERSION }}-wsl.zip
asset_name: sagemath-${{ env.VERSION }}-wsl.zip
asset_content_type: application/zip

# Generate a Windows PowerShell script

- name: Checkout repo
uses: actions/checkout@v3

- name: Generate PowerShell Script
run: |
cat << EOF > download_and_import_sagemath_to_wsl.ps1
# ----------------------------------------------------
# Copy and paste all lines into the Windows PowerShell
# ----------------------------------------------------
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
try {
Write-Host "Checking WSL installation."
wsl --install --no-distribution
if (\$LASTEXITCODE -ne 0) {
Write-Host "WSL installation failed."
Read-Host -Prompt "Press any key to exit"; exit
}
} catch {
Write-Host "Could not check WSL installation, proceeding with fingers crossed."
}
# Check if SageMath already exists in WSL
\$sagemathWSL = "SageMath-$VERSION"
if ((wsl -l -q) -split '`n' -contains \$sagemathWSL) {
# Ask user if they want to reinstall SageMath
\$response = Read-Host "\$sagemathWSL is already installed. Do you want to reinstall it? (y/n)"
if (\$response -eq "y") {
Write-Host "Uninstalling \$sagemathWSL."
# Unregister the existing SageMath WSL distribution
wsl --unregister \$sagemathWSL
} else {
Read-Host "OK. Press any key to exit"; exit
}
}
\$artifactUrl = "https://github.com/sagemath/sage-binder-env/releases/download/v$VERSION/sagemath-$VERSION-wsl.zip"
\$zipFilePath = "\$PWD\\artifact.zip"
\$dataPath = "\$HOME\\AppData\\Local\\SageMath"
\$tarFilePath = "\$dataPath\\sagemath-$VERSION-wsl.tar"
# Ensure the path exists
if (-Not (Test-Path \$dataPath)) { New-Item -Path \$dataPath -ItemType Directory > \$null }
# Skip downloading and extracting if the tar file already exists
if (-Not (Test-Path \$tarFilePath)) {
Write-Host "Downloading \$sagemathWSL..."
Start-BitsTransfer -Source \$artifactUrl -Destination \$zipFilePath
Write-Host "Extracting..."
Expand-Archive -Path \$zipFilePath -DestinationPath \$dataPath -Force
if (Test-Path \$tarFilePath) { Remove-Item \$zipFilePath }
} else {
Write-Host "\$sagemathWSL was already downloaded."
}
# Import the WSL image
Write-Host "Start importing..."
wsl --import SageMath-$VERSION \$dataPath \$tarFilePath
# Check if importing succeeded
if (-Not ((wsl -l -q) -split '`n' -contains \$sagemathWSL)) {
Write-Host "Importing \$sagemathWSL into WSL failed."
Write-Host "If WSL was freshly installed by this installer, try again after rebooting the computer."
Read-Host "Press any key to exit"; exit
}
Write-Host "Creating shortcults at:"
# Download icons for shortcuts
\$iconUrlOrange = "https://raw.githubusercontent.com/sagemath/sage-binder-env/refs/heads/master/.github/icons/orange.ico"
\$iconUrlBlue = "https://raw.githubusercontent.com/sagemath/sage-binder-env/refs/heads/master/.github/icons/blue.ico"
\$iconPathOrange = "\$dataPath\\orange.ico"
\$iconPathBlue = "\$dataPath\\blue.ico"
# Download the icon if it doesn't exist
if (-Not (Test-Path \$iconPathBlue)) {
Start-BitsTransfer -Source \$iconUrlBlue -Destination \$iconPathBlue
}
if (-Not (Test-Path \$iconPathOrange)) {
Start-BitsTransfer -Source \$iconUrlOrange -Destination \$iconPathOrange
}
# Create shortcuts
\$ShortcutFile = "\$([Environment]::GetFolderPath('Desktop'))\\Run Sage.lnk"
\$TargetPath = "C:\\Windows\\System32\\wsl.exe"
\$Arguments = "-d \$sagemathWSL --user user --cd ~ /sage/sage"
\$WScriptShell = New-Object -ComObject WScript.Shell
\$Shortcut = \$WScriptShell.CreateShortcut(\$ShortcutFile)
\$Shortcut.TargetPath = \$TargetPath
\$Shortcut.Arguments = \$Arguments
\$Shortcut.IconLocation = \$iconPathBlue
\$Shortcut.Save()
Write-Host "\$ShortcutFile"
# Create a shortcut that starts Jupyter on Desktop
\$ShortcutFile = "\$([Environment]::GetFolderPath('Desktop'))\\Run Jupyter.lnk"
\$TargetPath = "C:\\Windows\\System32\\wsl.exe"
\$Arguments = "-d \$sagemathWSL --user user --cd ~ jupyter lab --no-browser"
\$WScriptShell = New-Object -ComObject WScript.Shell
\$Shortcut = \$WScriptShell.CreateShortcut(\$ShortcutFile)
\$Shortcut.TargetPath = \$TargetPath
\$Shortcut.Arguments = \$Arguments
\$Shortcut.IconLocation = \$iconPathOrange
\$Shortcut.Save()
Write-Host "\$ShortcutFile"
Read-Host "\`n\$sagemathWSL installation succeeded!\`nPress any key to exit"
EOF
cat << EOF > remove_sagemath_from_wsl.ps1
# ----------------------------------------------------
# Copy and paste all lines into the Windows PowerShell
# ----------------------------------------------------
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
try {
Write-Host "Checking WSL installation."
wsl --install --no-distribution
if (\$LASTEXITCODE -ne 0) {
Write-Host "WSL installation failed."
Read-Host -Prompt "Press any key to exit"; exit
}
} catch {
Write-Host "Could not check WSL installation, proceeding with fingers crossed."
}
# Check if SageMath already exists in WSL
\$sagemathWSL = "SageMath-$VERSION"
\$dataPath = "\$HOME\\AppData\\Local\\SageMath"
\$iconPathOrange = "\$dataPath\\orange.ico"
\$iconPathBlue = "\$dataPath\\blue.ico"
if ((wsl -l -q) -split '`n' -contains \$sagemathWSL) {
# Ask user if they want to reinstall SageMath
\$response = Read-Host "Do you want to uninstall \$(\$sagemathWSL) from WSL? (y/n)"
if (\$response -eq "y") {
Write-Host "Uninstalling \$sagemathWSL..."
try {
wsl --unregister \$sagemathWSL
} catch {
Write-Host "Failed: wsl --unregister \$sagemathWSL"
}
try {
rm -recurse \$dataPath
} catch {
Write-Host "Failed: rm -recurse \$dataPath"
}
try {
\$ShortcutFile = "\$([Environment]::GetFolderPath('Desktop'))\\Run Sage.lnk"
rm \$ShortcutFile
} catch {
Write-Host "Failed: rm \$ShortcutFile"
}
try {
\$ShortcutFile = "\$([Environment]::GetFolderPath('Desktop'))\\Run Jupyter.lnk"
rm \$ShortcutFile
} catch {
Write-Host "Failed: rm \$ShortcutFile"
}
} else {
Read-Host "OK. Press any key to exit"; exit
}
} else {
Write-Host "\$sagemathWSL is not found."
Read-Host -Prompt "Press any key to exit"; exit
}
Read-Host "\`n\$sagemathWSL uninstallation succeeded!\`nPress any key to exit"
EOF
shell: bash

- name: Upload installation script as artifact
uses: actions/upload-artifact@v4
with:
name: download_and_import_sagemath_to_wsl
path: download_and_import_sagemath_to_wsl.ps1

- name: Upload uninstallation script as artifact
uses: actions/upload-artifact@v4
with:
name: remove_sagemath_from_wsl
path: remove_sagemath_from_wsl.ps1

- name: Upload installation script as release asset
if: ${{ env.VERSION != env.LATEST_VERSION }}
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./download_and_import_sagemath_to_wsl.ps1
asset_name: sagemath-${{ env.VERSION }}-wsl-installer.ps1
asset_content_type: application/octet-stream

- name: Upload uninstallation script as release asset
if: ${{ env.VERSION != env.LATEST_VERSION }}
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./remove_sagemath_from_wsl.ps1
asset_name: sagemath-${{ env.VERSION }}-wsl-uninstaller.ps1
asset_content_type: application/octet-stream

# build-exe:
# runs-on: windows-latest
# needs: build-and-export
# if: ${{ contains(needs.build-and-export.outputs.upload_url || '', 'http') }}
# steps:
# - name: Checkout repository
# uses: actions/checkout@v3
#
# - name: Download PowerShell Script artifact
# uses: actions/download-artifact@v4
# with:
# name: download_and_import_sagemath_to_wsl
#
# - name: Install PS2EXE
# run: |
# Install-Module -Name ps2exe -Force
# Import-Module ps2exe
# Invoke-ps2exe -inputFile .\download_and_import_sagemath_to_wsl.ps1 -outputFile .\sagemath_installer.exe
# shell: powershell
#
# - name: Upload EXE to GitHub Release
# uses: actions/upload-release-asset@v1
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# with:
# upload_url: ${{ needs.build-and-export.outputs.upload_url }}
# asset_path: ./sagemath_installer.exe
# asset_name: sagemath-${{ needs.build-and-export.outputs.version }}-wsl-installer.exe
# asset_content_type: application/octet-stream
Loading

0 comments on commit 8a6b2f1

Please sign in to comment.