Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Fowlie committed Jul 3, 2023
1 parent 8e91a8a commit 75bf8a1
Show file tree
Hide file tree
Showing 7 changed files with 685 additions and 0 deletions.
161 changes: 161 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# venvmgr

_👀 Manage Python virtual environments_

A command line application to manage Python virtual envionrments (venvs).

Create a venv:

```bash
$ vm create example
```
Use it:

```bash
$ vm python --venv example code.py
```

The assoications between files and venvs is recorded, such that

```bash
$ vm python code.py
```

uses the `example` venv. We might want install packages in this venv, e.g.,

```bash
$ vm pip --venv example scipy
```

We now might want to check the venvs:

```bash
$ vm ls
example
created at 2023-07-03 12:07:48.872147
activate: source /home/user/.venvmgr/example/bin/activate
used by: /home/user/code.py
```

Lastll, we might want to activate this venv

```bash
$ vm activate example
```

## Install 💥

```bash
pipx install venvmgr
```
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "venvmgr"
version = "0.1.0"
dependencies = ["click"]

[project.scripts]
vm = "venvmgr.cli:commands"
5 changes: 5 additions & 0 deletions src/venvmgr/activate
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#!/bin/bash

source $HOME/.bashrc
source /home/andrew/.venvmgr/everyday/bin/activate
155 changes: 155 additions & 0 deletions src/venvmgr/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
"""
CLI for venv manager
====================
"""


import os
import click

from . import venvmgr
from . import config


__version__ = "0.1.0"


def get_venv_names(ctx, args, incomplete):
return os.listdir(config.VENV_DIR)


@click.group()
@click.version_option(__version__)
def commands():
pass


@click.command(context_settings=dict(ignore_unknown_options=True))
@click.argument('file', required=False, default=None,
type=click.Path(exists=True))
@click.option('--venv',
type=click.STRING,
default=None,
required=False,
shell_complete=get_venv_names,
help="Name of venv")
@click.argument('args', nargs=-1, type=click.UNPROCESSED)
def python(file, venv, args):
"""
Python though with a venv argument
If no VENV provided but a FILE is provided, uses a VENV inferred from FILE
"""
venvmgr.python(venv, file, *args)


@click.command()
@click.option('-l', '--long', type=bool, default=False,
is_flag=True, help='long listing format')
@click.argument('name', nargs=-1, required=False,
type=click.STRING, shell_complete=get_venv_names)
def ls(long, name):
"""
List information about venvmgr known venvs
"""
click.echo_via_pager("\n".join(venvmgr.ls(name, long)))


@click.command()
@click.argument('name', type=click.STRING, shell_complete=get_venv_names)
@click.argument('file', type=click.Path(exists=True), nargs=-1)
def associate(name, file):
"""
Associate venv NAME with FILE
"""
venvmgr.associate(name, file)


@click.command()
@click.argument('name', type=click.STRING, shell_complete=get_venv_names)
@click.option('-r', '--requirement',
type=click.Path(exists=True),
default=None,
required=False,
help="Install from the given requirements file")
def create(name, requirement):
"""
Create NAME venv
"""
venvmgr.create(name, requirement)


@click.command()
@click.argument('name', type=click.STRING, shell_complete=get_venv_names)
def rm(name):
"""
Remove NAME venv
"""
venvmgr.rm(name)


@click.command(context_settings=dict(ignore_unknown_options=True))
@click.option('--venv', type=click.STRING, required=True,
shell_complete=get_venv_names, help="Name of venv")
@click.argument('args', nargs=-1, type=click.UNPROCESSED)
def pip(venv, args):
"""
Pip though with venv argument
"""
venvmgr.pip(venv, *args)


@click.command()
@click.argument('dir', type=click.STRING)
def add(dir):
"""
Add a venv from DIR created outside of vm
"""
venvmgr.add(dir)


@click.command()
@click.argument('name', type=click.STRING, shell_complete=get_venv_names)
def annotate(name):
"""
Annotate NAME venv
"""
venvmgr.annotate(name)


@click.command()
def home():
"""
Location of venvmgr home directory
"""
click.echo(config.VENV_DIR)


@click.command()
def dotfile():
"""
Location of venvmgr configuration file
"""
click.echo(config.CONFIG_FILE)


@click.command()
@click.argument('name', type=click.STRING, shell_complete=get_venv_names)
def activate(name):
"""
Activate NAME venv
"""
venvmgr.activate(name)


commands.add_command(create)
commands.add_command(rm)
commands.add_command(python)
commands.add_command(ls)
commands.add_command(pip)
commands.add_command(add)
commands.add_command(annotate)
commands.add_command(associate)
commands.add_command(home)
commands.add_command(dotfile)
commands.add_command(activate)
Loading

0 comments on commit 75bf8a1

Please sign in to comment.