-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpackage_python.sh
More file actions
executable file
·106 lines (87 loc) · 2.89 KB
/
package_python.sh
File metadata and controls
executable file
·106 lines (87 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
set -ex
(/dockerstartup/kasm_default_profile.sh)
sudo update-ca-certificates
export PIP_CERT=/etc/ssl/certs/ca-certificates.crt >> ~/.bashrc
export PYTHON_CONFIGURE_OPTS="--enable-optimizations --with-lto"
export PYTHON_CFLAGS="-march=native -mtune=native"
source $HOME/.bashrc
BACKUP_TARGET=${BACKUP_TARGET:-/var/spool/python_versions/}
EXTRA_PACKAGES=${EXTRA_PACKAGES:-}
install_python() {
pyenv install $VERSION
pyenv global $VERSION
pip install --upgrade pip
if [ -n "$EXTRA_PACKAGES" ]; then
echo "Installing $EXTRA_PACKAGES"
pip install $EXTRA_PACKAGES
fi
}
# Function to backup pyenv versions
backup_pyenv() {
local versions_dir="$HOME/.pyenv/versions"
# Check if versions directory exists
if [ ! -d "$versions_dir" ]; then
echo "Error: PyEnv versions directory not found at $versions_dir"
exit 1
fi
# Get list of installed versions
local installed_versions=($(ls "$versions_dir"))
if [ ${#installed_versions[@]} -eq 0 ]; then
echo "No Python versions found in $versions_dir"
exit 1
fi
local backup_file="pyenv_version_${installed_versions}.tar.bz2"
echo "Found ${#installed_versions[@]} Python version(s)"
echo "Creating backup archive: $backup_file"
# Create tar.bz2 archive with proper directory structure
tar -C "$HOME/.pyenv" -cjf "$backup_file" versions/
if [ -n "$BACKUP_TARGET" ]; then
sudo mv $backup_file $BACKUP_TARGET
fi
echo "Backup completed successfully"
echo "Backup file: $backup_file"
}
# Function to restore pyenv versions
restore_pyenv() {
local backup_file=$(find "$BACKUP_TARGET" -maxdepth 1 -name "pyenv_version_${VERSION}.*.tar.bz2" | sort -r | head -n1)
if [ -z "$backup_file" ]; then
echo "Error: No backup file found for Python version $version in $BACKUP_TARGET"
exit 1
fi
echo "Found backup file: $backup_file"
local target_dir="$HOME/.pyenv/versions"
# Check if target directory exists, create if it doesn't
if [ ! -d "$target_dir" ]; then
echo "Creating target directory: $target_dir"
mkdir -p "$target_dir"
fi
echo "Restoring from backup: $backup_file"
# Extract the archive
tar -C "$HOME/.pyenv" -xjf "$backup_file"
pyenv global $VERSION
echo "Restore completed successfully"
}
# Usage examples:
# To backup: ./package_python.sh prepare 3.11 "numpy pandas"
# To restore: ./package_python.sh restore <backup_file>
case "$1" in
"prepare")
VERSION=$2
install_python
backup_pyenv
;;
"restore")
if [ -z "$2" ]; then
echo "Error: Please provide version to restore"
echo "Usage: $0 restore <version>"
exit 1
fi
VERSION=$2
restore_pyenv
;;
*)
echo "Usage: $0 {backup|restore <backup_file>}"
exit 1
;;
esac