Skip to content

Commit

Permalink
Use venv to manage dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lebr0nli committed Aug 31, 2023
1 parent e13dc1d commit c23144c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__/*
.venv/*
.gdb_history
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ And also, GEP has some awesome features already, you can directly use it!
Make sure you have GDB 8.0 or higher compiled with Python3.7+ bindings, then:

1. Install git and curl (or wget)
2. Install fzf: [Installation](https://github.com/junegunn/fzf#installation) (Optional, but GEP works better with fzf)
3. Install this plug-in by:
2. Make sure you have [virtualenv](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#installing-virtualenv) installed
3. Install fzf: [Installation](https://github.com/junegunn/fzf#installation) (Optional, but GEP works better with fzf)
4. Install this plug-in by:

```shell
# via the install script
Expand All @@ -38,23 +39,13 @@ $ bash -c "$(curl -fsSL https://raw.githubusercontent.com/lebr0nli/GEP/main/inst

## using wget
$ bash -c "$(wget https://raw.githubusercontent.com/lebr0nli/GEP/main/install.sh -O -)"

# manually
$ pip install --no-cache-dir prompt_toolkit
$ git clone https://github.com/lebr0nli/GEP.git ~/.local/share/GEP
$ printf '\nsource ~/.local/share/GEP/gdbinit-gep.py\n' >> ~/.gdbinit
```

3. Enjoy!

## How to update the version of GEP?

In your GDB, use `gep-update` command, or you can manually use `wget` or `curl` to download the [gdbinit-gep.py](./gdbinit-gep.py) again.

```shell
# e.g. using wget and your GEP is installed in ~/.local/share/GEP
wget https://raw.githubusercontent.com/lebr0nli/GEP/main/gdbinit-gep.py -O ~/.local/share/GEP/gdbinit-gep.py
```
You can re-run the install script to update the version of GEP.

## For more configuration

Expand Down
57 changes: 14 additions & 43 deletions gdbinit-gep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import os
import re
import shutil
import site
import sys
import tempfile
import threading
import traceback
from glob import glob
from shutil import which
from string import ascii_letters
from subprocess import PIPE
Expand All @@ -16,6 +18,18 @@
from typing import Tuple

import gdb

directory, file = os.path.split(__file__)
directory = os.path.expanduser(directory)
directory = os.path.abspath(directory)
sys.path.append(directory)
venv_path = os.path.join(directory, ".venv")
if not os.path.exists(venv_path):
print("You might need to reinstall GEP, please check the latest version on Github")
sys.exit(1)
site_pkgs_path = glob(os.path.join(venv_path, "lib/*/site-packages"))[0]
site.addsitedir(site_pkgs_path)

from prompt_toolkit import PromptSession
from prompt_toolkit import print_formatted_text
from prompt_toolkit.application import run_in_terminal
Expand Down Expand Up @@ -72,10 +86,6 @@
)

try:
directory, file = os.path.split(__file__)
directory = os.path.expanduser(directory)
directory = os.path.abspath(directory)
sys.path.append(directory)
from geprc import BINDINGS
from geprc import DONT_REPEAT as USER_DONT_REPEAT

Expand Down Expand Up @@ -436,45 +446,6 @@ def get_completions(self, document, complete_event):
yield Completion(completion, display=display, display_meta=display_meta)


class UpdateGEPCommand(gdb.Command):
"""
Update GEP to the latest version
"""

def __init__(self):
# we need save __file__ because somehow when gdb invoke this command, somehow __file__ will be not defined
self.__gep_location = __file__
super(UpdateGEPCommand, self).__init__("gep-update", gdb.COMMAND_NONE)

def invoke(self, arg, from_tty):
print_info("Updating GEP...")
try:
import urllib.request

remote_content = urllib.request.urlopen(
"https://raw.githubusercontent.com/lebr0nli/GEP/main/gdbinit-gep.py"
).read()
except Exception as e:
print(e)
print_warning("Failed to download GEP from Github")
return
with open(self.__gep_location, "r") as f:
content = f.read()
if content == remote_content.decode("utf-8"):
print_info("GEP is already the latest version.")
return
with open(self.__gep_location, "w") as f:
f.write(remote_content.decode("utf-8"))
print_info("GEP at %s is updated to the latest version." % self.__gep_location)
print_info("Please restart GDB to use the latest version of GEP.")
print_warning(
"You may need to check https://github.com/lebr0nli/GEP for more information about the new version."
)


UpdateGEPCommand()


def gep_prompt(current_prompt: str) -> None:
print_info("GEP is running now!")
UserParamater.gep_loaded = True
Expand Down
37 changes: 23 additions & 14 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
#!/bin/bash
set -ex

# install prompt_toolkit
if [ "$(which python3)" ]; then
python3 -m pip install --no-cache-dir prompt_toolkit
elif [ "$(which python)" ]; then
python -m pip install --no-cache-dir prompt_toolkit
elif [ "$(which pip)" ]; then
pip install --no-cache-dir prompt_toolkit
else
echo "Can't find pip in your env, please install it and run again"
exit 1
fi

# create a folder for GEP
INSTALL_PATH=${XDG_DATA_HOME:-$HOME/.local/share}/GEP
mkdir -p "$INSTALL_PATH"
GDBINIT_GEP_PY=$INSTALL_PATH/gdbinit-gep.py
echo "Installing GEP to $INSTALL_PATH ..."

# git clone the repo
git clone https://github.com/lebr0nli/GEP.git --depth=1 "$INSTALL_PATH"
if [ -d "$INSTALL_PATH/.git" ]; then
# git pull if exists
cd "$INSTALL_PATH"
git pull
else
# git clone the repo if not exists
git clone https://github.com/lebr0nli/GEP.git --depth=1 "$INSTALL_PATH"
fi

# find python path
PYVER=$(gdb -batch -q --nx -ex 'pi import platform; print(".".join(platform.python_version_tuple()[:2]))')
PYTHON=$(gdb -batch -q --nx -ex 'pi import sys; print(sys.executable)')
if ! uname -a | grep -q Darwin > /dev/null; then
PYTHON+="${PYVER}"
fi

# create venv and install prompt_toolkit
VENV_PATH=$INSTALL_PATH/.venv
echo "Creating virtualenv in path: ${VENV_PATH}"
"$PYTHON" -m venv "$VENV_PATH"
PYTHON=$VENV_PATH/bin/python
"$PYTHON" -m pip install -U pip
"$VENV_PATH/bin/pip" install --no-cache-dir -U prompt_toolkit

if [ -f ~/.gdbinit ]; then
# backup gdbinit if exists
Expand Down

0 comments on commit c23144c

Please sign in to comment.