Skip to content

Commit

Permalink
Remove all vestiges of python2 from khan-dotfiles. (#101)
Browse files Browse the repository at this point in the history
## Summary:
We no longer use python2 anywhere!  (Well, we still use it in one
place: `make topic-icons`, anyone who wants to run that will now need
to install python2 manually.)  Let's remove it from khan-dotfiles.
That simplifies the dotfiles a fair bit, and also simplifies the
users' dev experience since the virtualenv was causing problems.

I did this by running `git grep -e 'python[^3]' -e 'virtualenv'`
and deleting anything that looked no longer relevant.

Issue: https://khanacademy.atlassian.net/browse/INFRA-9771

## Test plan:
TODO -- will need to test on new installs of both mac and linux, and I
have no idea how to do that!

Author: csilvers

Reviewers: dbraley, csilvers, #infra-platform

Required Reviewers:

Approved By: dbraley

Checks:

Pull Request URL: #101
  • Loading branch information
csilvers authored Mar 6, 2024
1 parent f7d3b5d commit 6355e82
Show file tree
Hide file tree
Showing 15 changed files with 8 additions and 290 deletions.
32 changes: 0 additions & 32 deletions .profile.khan
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ if which brew >/dev/null 2>&1; then
mkdir -p "$KA_CONFIG_CACHE_DIR"
echo $BREW_PREFIX > $BREW_PREFIX_HINT_FILE
fi

# Add the unversioned binaries from our backported homebrew python@2 formula,
# if they have brew and have our python@2 formula installed
# See: https://github.com/Khan/homebrew-repo/blob/master/Formula/python%402.rb

# This will exist if native python2 is installed (usually only on intel)
PY2_BIN=$BREW_PREFIX
# On a M1 machine, we run python2 via rosetta out of /usr/local
[ -d "$PY2_BIN" ] || PY2_BIN="/usr/local/opt/python@2"
export PATH="$PY2_BIN/libexec/bin:$PATH"
fi

# Make sure git (and other apps) prefer 'vim' to 'vi'.
Expand Down Expand Up @@ -104,27 +94,5 @@ if [ `uname -s` = Darwin ]; then
alias timeout=gtimeout
fi

# If you do NOT want to have the python2 environment auto-activated,
# add the following to the top of your ~/.profile script:
# export DO_NOT_ACTIVATE_VIRTUALENV_KHAN27=1
#
if [ -z "$DO_NOT_ACTIVATE_VIRTUALENV_KHAN27" ]; then
# Activate Python2.7 virtualenv if present
# This is desirable if you're touching the python monolith
if [ -s ~/.virtualenv/khan27/bin/activate ]; then
. ~/.virtualenv/khan27/bin/activate
else
echo "[WARN] Could not find '~/.virtualenv/khan27/bin/activate'"\
"- All processes that depend on Python may be negatively impacted."\
"Please run 'make' from the dotfiles directory to fix this."
fi
fi

# Since we auto-activate the khan27 virtualenv, we don't want pipenv using any
# virtualenv it happens to find itself in, but instead to use its own.
# TODO(benkraft): If in the future we move to pipenv everywhere, we can
# dispense with the auto-activated virtualenvs, and get rid of this.
export PIPENV_IGNORE_VIRTUALENVS=1

# Source the generated profile, if it exists.
[ -f $HOME/.profile-generated.khan ] && source $HOME/.profile-generated.khan
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,3 @@ os-install:

common-install:
./setup.sh

virtualenv:
./bin/rebuild_virtualenv.sh

2 changes: 1 addition & 1 deletion bin/install-mac-homebrew.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _validate_and_install_homebrew(self, force_x86=False):
# unbeknownst!)
# subprocess.run(['brew', 'tap', 'brew/cask-versions'], check=True)

# This is where we store our own formula, including a python@2 backport
# This is where we store our own formulas.
subprocess.run(brew_runner + ["tap", "khan/repo"], check=True)

def validate_and_install_homebrew(self):
Expand Down
47 changes: 0 additions & 47 deletions bin/install-mac-python2.py

This file was deleted.

2 changes: 1 addition & 1 deletion bin/mac-uninstall-brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if [[ -e "/usr/local/bin/brew" ]]; then
sudo rm -rf /usr/local/var/postgresql*
fi

echo "Removing khan python2 virtualenv..."
echo "Removing the (obsolete) khan python2 virtualenv..."
rm -rf ~/.virtualenv/khan27

echo "Removing other things that may upgrade if user reinstalls..."
Expand Down
3 changes: 0 additions & 3 deletions bin/pip2

This file was deleted.

47 changes: 0 additions & 47 deletions bin/rebuild_virtualenv.sh

This file was deleted.

3 changes: 1 addition & 2 deletions bin/system_report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ tool_version java -version
# Python tooling
header "Python"
tool_version python --version
tool_version python2 --version
tool_version python3 --version
tool_version pip --version
kv "VIRTUAL_ENV" "$VIRTUAL_ENV"
kv "sys.path" "$(python2 -c 'import sys; print sys.path')"
kv "sys.path" "$(python3 -c 'import sys; print(sys.path)')"

# TODO(dbraley): (l) check software-properties-common
# TODO(dbraley): (l) check apt-trasport-https
Expand Down
16 changes: 0 additions & 16 deletions containers/kabuild/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,6 @@ ENV DESIRED_GO_VERSION=1.21
RUN apt-get install -y golang-${DESIRED_GO_VERSION}
RUN cp -sf /usr/lib/go-${DESIRED_GO_VERSION}/bin/* /usr/local/bin/

# Python2
ENV VENV=.virtualenv/khan27
RUN apt-get install -y python2-dev python-setuptools
RUN ln -s /usr/bin/python2 /usr/bin/python
WORKDIR /tmp
RUN curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
RUN python2 get-pip.py pip==19.3.1
RUN rm -f get-pip.py
WORKDIR /root
RUN pip install virtualenv==20.0.23
RUN pip install http://sourceforge.net/projects/pychecker/files/pychecker/0.8.19/pychecker-0.8.19.tar.gz/download
RUN virtualenv -q --python="$(which python2)" --always-copy "/root/${VENV}"
RUN /root/${VENV}/bin/pip install -U "pip<20" setuptools

RUN apt-get install -y \
libfreetype6 libfreetype6-dev libpng-dev libjpeg-dev \
imagemagick \
Expand Down Expand Up @@ -128,8 +114,6 @@ WORKDIR /home/${USER_NAME}
RUN mkdir -m 700 .ssh
RUN mkdir khan
RUN ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN virtualenv -q --python="$(which python2)" --always-copy "/home/${USER_NAME}/${VENV}"
RUN /home/${USER_NAME}/${VENV}/bin/pip install -U "pip<20" setuptools
COPY --chown=${USER_NAME}:${GROUP_NAME} .profile.khan .profile.khan
COPY --chown=${USER_NAME}:${GROUP_NAME} .bash_profile.khan .bash_profile.khan
COPY --chown=${USER_NAME}:${GROUP_NAME} .bashrc.khan .bashrc.khan
Expand Down
35 changes: 2 additions & 33 deletions linux-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ EOF
updated_apt_repo=yes
fi

# To get python3.8, later.
# To get the most recent python, later.
if ! ls /etc/apt/sources.list.d/ 2>&1 | grep -q deadsnakes; then
sudo add-apt-repository -y ppa:deadsnakes/ppa
updated_apt_repo=yes
Expand All @@ -151,41 +151,9 @@ EOF
# We are on python3.11 now
sudo apt-get install -y python3.11 python3.11-venv

# Python2 is needed for development. First try the Ubuntu 22.04+ packages, then
# the Ubuntu <22.04 packages if that fails.
sudo apt-get install -y python2-dev python-setuptools || sudo apt-get install -y python-dev python-mode python-setuptools

# This is needed for Ubuntu >=20, but not prior ones. It no longer exists
# as of Ubuntu 22.04.
sudo apt-get install -y python-is-python2 || true

# If we're on Ubuntu 22.04+, installing python-is-python2 didn't do anything, so
# we create the symlink ourselves.
if ! [ -f /usr/bin/python ]; then
sudo ln -s /usr/bin/python2 /usr/bin/python
fi

# Install curl for setup script usage
sudo apt-get install -y curl

# Install pip manually.
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
# Match webapp's version.
sudo python2 get-pip.py pip==19.3.1
# Delete get-pip.py after we're finish running it.
rm -f get-pip.py

# Install virtualenv and pychecker manually; ubuntu
# dropped support for them in ubuntu >=20 (since they're python2)
sudo pip install virtualenv==20.0.23
sudo pip install http://sourceforge.net/projects/pychecker/files/pychecker/0.8.19/pychecker-0.8.19.tar.gz/download

# get-pip.py will remove the system pip3 binary if it previously existed,
# but it won't remove the package, so installing the package again won't
# restore it. Here we remove the package if it exists, so that the next
# apt-get command will install it properly.
sudo apt-get remove -y python3-pip || true

# Needed to develop at Khan: git, node (js).
# lib{freetype6{,-dev},{png,jpeg}-dev} are needed for PIL
# imagemagick is needed for image resizing and other operations
Expand All @@ -212,6 +180,7 @@ EOF
jq \
libnss3-tools \
python3-dev python3-setuptools python3-pip python3-venv \
python-is-python3 \
cargo cargo-doc \
docker lsof uuid-runtime

Expand Down
2 changes: 1 addition & 1 deletion mac-setup-elevated.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ echo "This setup script needs your password to install things as root."
sudo sh -c 'echo Thanks'

if [[ $(uname -m) = "arm64" ]]; then
# install rosetta on M1 (required for openjdk, python2 and other things)
# install rosetta on M1 (required for openjdk and other things)
# This will work here, but it requires input and I'd rather just have it in docs
#sudo softwareupdate --install-rosetta

Expand Down
26 changes: 0 additions & 26 deletions mac-setup-normal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,6 @@ update_git() {
fi
}

install_python2() {
# We only do this if python2 == /usr/bin/python2, which means it's system python
if [ "$(which python2)" != "/usr/bin/python2" ]; then
success "Already running a non-system python2."
return
fi

info "Installing python2 from khan/repo. This may take a few minutes."
brew86 install khan/repo/python@2
}

install_node() {
if ! which node >/dev/null 2>&1; then
# Install node 16: It's LTS and the latest version supported on
Expand Down Expand Up @@ -302,12 +291,6 @@ install_jq() {

install_python_tools() {
# Python3 is needed to run the python services (e.g. ai-guide-core).
# We pin it at python3.8 at the moment, but will move it to python3.11 soon
# TODO(csilvers, GL-1195): remove python3.8 ai-guide-core is on python3.11
if ! which python3.8 >/dev/null 2>&1; then
info "Installing python 3.8\n"
brew install [email protected]
fi
if ! which python3.11 >/dev/null 2>&1; then
info "Installing python 3.11\n"
brew install [email protected]
Expand All @@ -318,13 +301,6 @@ install_python_tools() {
if ! brew ls pyenv >/dev/null 2>&1; then
info "Installing pyenv\n"
brew install pyenv
# At the moment, we depend on MacOS coming with python 2.7. If that
# stops, or we want to align the python versions with the linux
# dotfiles more effectively, we could do it with pyenv:
# `pyenv install 2.7.16 ; pyenv global 2.7.16`
# Because the linux dotfiles do not yet install pyenv, holding off on
# using pyenv to enforce python version until either that happens, or
# MacOs stops including python 2.7 by default.
else
success "pyenv already installed"
fi
Expand Down Expand Up @@ -366,8 +342,6 @@ install_openssl
install_jq
update_git

"$DEVTOOLS_DIR"/khan-dotfiles/bin/install-mac-python2.py

install_node
install_go

Expand Down
3 changes: 0 additions & 3 deletions setup-gcloud.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
# This is used to be in setup.sh: install_and_setup_gcloud()
# It was broken and is easier to debug separately

# TODO(ericbrown): Shell scripts hard to debug - rewrite in python
# shared-functions.sh are an issue

# Bail on any errors
set -e

Expand Down
27 changes: 1 addition & 26 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,6 @@ install_dotfiles() {
fi
done

# If users are using a shell other than bash, the updates we make won't
# get picked up. They'll have to activate the virtualenv in their shell
# config; if they haven't, the rest of the script will fail.
# TODO(benkraft): Add more specific instructions for other common shells,
# or just write dotfiles for them.
shell="`basename "$SHELL"`"
if [ -z "$VIRTUAL_ENV" ] && [ "$shell" != bash ] && [ "$shell" != zsh ]; then
add_fatal_error "Your default shell is $shell, not bash or zsh, so you'll" \
"need to update its config manually to activate our" \
"virtualenv. You can follow the instructions at" \
"khanacademy.org/r/virtualenvs to create a new" \
"virtualenv and then export its path in the" \
"VIRTUAL_ENV environment variable before trying again."
fi

# *.template files are also copied so the user can change them. Unlike the
# "default" files above, these do not include KA code, they are merely
# useful defaults we want to install if the user doesnt have anything
Expand Down Expand Up @@ -238,21 +223,11 @@ clone_repos() {
kaclone_repair_self
}

# Sets up virtualenv and pipenv
# Sets up pipenv
setup_python() {
echo "Installing python2 virtualenv"
install_python2_virtualenv

create_and_activate_virtualenv "$ROOT/.virtualenv/khan27"

# Used by various infra projects for managing python3 environment
echo "Installing pipenv for python3"

# WARNING: This overrwrites the python2 virtualenv
pip3_install -q pipenv

echo "Reinstall python2 virtualenv (will overwrite python3 version)"
install_python2_virtualenv
}

# Must have cloned the repos first.
Expand Down
Loading

0 comments on commit 6355e82

Please sign in to comment.