Skip to content

Commit

Permalink
Allow handling version specifiers; ability to update programs
Browse files Browse the repository at this point in the history
  • Loading branch information
dvershinin committed Jan 12, 2021
1 parent 1a60692 commit bb0263b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ then run `source ~/.bashrc` to apply to current shell.

### CentOS/RHEL 7, 8

sudo yum install https://extras.getpagespeed.com/release-latest.rpm
sudo yum install pip-safe
sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
sudo yum -y install pip-safe

Using `pip-safe` command installs stuff using Python 3.

Expand Down Expand Up @@ -69,17 +69,18 @@ Ensure `virtualenv-3` is installed and `/usr/local/bin` is in your `PATH`, then:
## Usage

```
usage: pip-safe [-h] [-v] [-y] [--system] <command> [package-name]
Safely install and remove PyPi (pip) programs without breaking your system
positional arguments:
<command>
<command> Command to run, e.g. install, update, remove or list
package-name
optional arguments:
-h, --help show this help message and exit
-v, --verbose
-y, --assumeyes
--system
--system Install for all users
--version show program's version number and exit
```

### Installing a program
Expand All @@ -90,7 +91,7 @@ To see what's going on under the hood, pass `--verbose` flag.

There is limited support for installing directly from Git URLs, e.g.:

pip-safe install git+https://github.com/dvershinin/lastversion.git
pip-safe install git+https://github.com/dvershinin/lastversion.git

#### Global installation

Expand All @@ -105,6 +106,10 @@ so it's still safe :-)
### Removing a program

pip-safe remove <name>

### Updating a program

pip-safe update <name>

### Listing installed packages

Expand Down
2 changes: 1 addition & 1 deletion pip_safe/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.6"
__version__ = "0.0.7"
37 changes: 27 additions & 10 deletions pip_safe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def get_venv_dir(name, system_wide=False):
# as a dumb way to make result directory creatable
name = name.replace('https://', '')
name = name.replace('/', '_')
# sanitize version specifier if user installs, e.g. lastversion==1.2.4
name = name.split('==')[0]
venvs_dir = get_venvs_dir(system_wide=system_wide)
return os.path.join(venvs_dir, name)

Expand All @@ -59,7 +61,8 @@ def get_venv_executable_names(name, system_wide=False):
log.debug("Checking what was installed to virtualenv's bin")
bin_names = []
venv_pip = get_venv_pip(name, system_wide)
main_package_name = name
# sanitize version specifier if user installs, e.g. lastversion==1.2.4
main_package_name = name.split('==')[0]
# if the passed name was a URL, we have to figure out the name of the "main"
# package that was installed, by listing non-dependent packages and weeding
# out known stuff like wheel and pip itself
Expand Down Expand Up @@ -110,17 +113,22 @@ def get_current_version(name, system_wide=False):
return v


def install_package(name, system_wide=False):
def install_package(name, system_wide=False, upgrade=False):
# create and activate the virtual environment
bin_dir = get_bin_dir(system_wide=system_wide)

venv_dir = get_venv_dir(name, system_wide=system_wide)
make_sure_path_exists(venv_dir)

install_for = 'system-wide' if system_wide else 'for current user'
log.info(
'Installing {} {} ...'.format(name, install_for)
)
if not upgrade:
log.info(
'Installing {} {} ...'.format(name, install_for)
)
else:
log.info(
'Upgrading {} {} ...'.format(name, install_for)
)
log.debug('Creating virtualenv at {}'.format(venv_dir))
try:
virtualenv.create_environment(venv_dir)
Expand All @@ -131,8 +139,12 @@ def install_package(name, system_wide=False):
log.debug("Running virtualenv's pip install {}".format(name))
# call_subprocess here is used for convinience: since we already import
# this, why not :)
call_subprocess(
[venv_dir + '/bin/pip', 'install', name, '--quiet'])
args = [venv_dir + '/bin/pip', 'install']
if upgrade:
args.append('-U')
args.append(name)
args.append('--quiet')
call_subprocess(args)

pkg_bin_names = get_venv_executable_names(name, system_wide)
for bin_name in pkg_bin_names:
Expand Down Expand Up @@ -234,8 +246,8 @@ def main():
'without breaking your system',
prog='pip-safe')
parser.add_argument('command', metavar='<command>', default=None,
choices=['install', 'list', 'remove'],
help='Command to run, e.g. install, remove or list')
choices=['install', 'update', 'upgrade', 'list', 'remove'],
help='Command to run, e.g. install, update, remove or list')
parser.add_argument('package', metavar='package-name', nargs='?',
default=None)
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true')
Expand All @@ -255,7 +267,12 @@ def main():
log.basicConfig(format="%(levelname)s: %(message)s", level=log.INFO)

if args.command == 'install':
install_package(name=args.package, system_wide=args.system)
install_package(name=args.package,
system_wide=args.system)
elif args.command in ['update', 'upgrade']:
install_package(name=args.package,
system_wide=args.system,
upgrade=True)
elif args.command == 'list':
list_packages()
elif args.command == 'remove':
Expand Down

0 comments on commit bb0263b

Please sign in to comment.