From 268fe4409b1a8c0c2d28f296a43e946b4129f586 Mon Sep 17 00:00:00 2001 From: AlexMili Date: Wed, 22 May 2024 17:39:00 +0200 Subject: [PATCH] first commit --- .github/workflows/publish.yml | 33 +++++++++++++++++++++++++ .github/workflows/release.yml | 45 +++++++++++++++++++++++++++++++++++ .gitignore | 5 ++++ LICENSE | 21 ++++++++++++++++ README.md | 36 ++++++++++++++++++++++++++++ VERSION.md | 1 + main.py | 25 +++++++++++++++++++ pyproject.toml | 42 ++++++++++++++++++++++++++++++++ 8 files changed, 208 insertions(+) create mode 100644 .github/workflows/publish.yml create mode 100644 .github/workflows/release.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 VERSION.md create mode 100644 main.py create mode 100644 pyproject.toml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..8121a6f --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,33 @@ +name: Publish + +on: + workflow_dispatch: + release: + types: + - created + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + id-token: write + steps: + - name: Github actions init + uses: actions/checkout@v4 + with: + # To force fetching tags + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.8" + + - name: Install build dependencies + run: pip install build + + - name: Build distribution + run: python -m build + + - name: Publish + uses: pypa/gh-action-pypi-publish@v1.8.14 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..4705716 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,45 @@ +name: Release + +on: + workflow_dispatch: + +permissions: + contents: write + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Github actions init + uses: actions/checkout@v4 + with: + # To force fetching tags + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.8" + + - name: Install build dependencies + run: pip install build + + - name: Build + run: python -m build + + - name: Read VERSION file + id: getversion + run: echo "version=$(cat VERSION.md)" >> $GITHUB_OUTPUT + + - name: Changelog + run: git log $(git describe --tags --abbrev=0)..HEAD --format="%s %h" > LATEST-CHANGES.md + + - name: Release + uses: softprops/action-gh-release@v2 + with: + files: | + dist/*.whl + dist/*.tar.gz + tag_name: v${{ steps.getversion.outputs.version }} + body_path: LATEST-CHANGES.md + token: ${{ secrets.PAT_ISVIRTUAL }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ab3bf11 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.venv +__pycache__ +dist +*.egg-info +.pypirc diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a8fc851 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2da6f39 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +**isVirtual** is a very simple tool to detect if the current script is within a virtual environment. + +# Disclaimer + +The goal of this project was to play around with the process to publish to [Pypi](https://pypi.org). The code of this module is coming from this [stackoverflow thread](https://stackoverflow.com/questions/1871549/how-to-determine-if-python-is-running-inside-a-virtualenv). + +If you find use cases in which it doesn't work please open an [issue](https://github.com/AlexMili/isVirtual/issues). I intend to maintain this small package even if it can be seen as *"useless"*. + +# Install + +```bash +pip install isvirtual +``` + +# Usage + +Within a python script: +```python +from isvirtual import is_virtual_env + +if __name__ == "__main__": + if is_virtual_env() is True: + print("You are within a virtual environment") + else: + print("You are not in a virtual env") +``` + +CLI mode: +```console +$ isvirtual +Yes +``` + +# License + +This project is licensed under the terms of the MIT license. diff --git a/VERSION.md b/VERSION.md new file mode 100644 index 0000000..afaf360 --- /dev/null +++ b/VERSION.md @@ -0,0 +1 @@ +1.0.0 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..639b05a --- /dev/null +++ b/main.py @@ -0,0 +1,25 @@ +import os.path as osp +import sys + +version_path = osp.join(osp.dirname(__file__), "VERSION.md") + +if osp.exists(version_path): + with open(version_path, "r") as f: + __version__ = f.readline() + + +def is_virtual_env(): + # The check for sys.real_prefix covers virtualenv, the equality of + # non-empty sys.base_prefix with sys.prefix covers venv. + # note: Python versions before 3.3 don't have sys.base_prefix + # if you're not in virtual environment + return hasattr(sys, "real_prefix") or ( + hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix + ) + + +def is_virtual_env_cli(): + if is_virtual_env() is True: + print("Yes") + else: + print("No") diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5ce6318 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,42 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "isvirtual" +version = "1.0.0" +description = "Detect if your script is running inside a virtual environment" +readme = "README.md" +authors = [{ name = "Alex Mili" }] +license = { file = "LICENSE" } +requires-python = ">=3.3" +classifiers = [ + "License :: OSI Approved :: MIT License", + "Intended Audience :: Developers", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.3", + "Programming Language :: Python :: 3.4", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", +] +keywords = ["virtual", "env", "venv", "virtualenv", "pyenv", "environment"] +dependencies = [] + +[project.urls] +Homepage = "https://github.com/AlexMili/isVirtual" +Issues = "https://github.com/AlexMili/isVirtual/issues" +Repository = "https://github.com/AlexMili/isVirtual" +Documentation = "https://github.com/AlexMili/isVirtual" + +[project.scripts] +isvirtual = "main:is_virtual_env_cli" + +[tool.hatch.build.targets.wheel] +packages = ["."]