Skip to content

Commit

Permalink
Documentation, github actions and a lot more added.
Browse files Browse the repository at this point in the history
  • Loading branch information
hasansezertasan committed Sep 16, 2023
1 parent ba55473 commit bda9df8
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 50 deletions.
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms
github: [hasansezertasan]
# patreon: # Replace with a single Patreon username
# open_collective: # Replace with a single Open Collective username
# ko_fi: # Replace with a single Ko-fi username
# tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
# community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
# liberapay: # Replace with a single Liberapay username
# issuehunt: # Replace with a single IssueHunt username
# otechie: # Replace with a single Otechie username
# lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
# custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "monthly"
33 changes: 33 additions & 0 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Github Pages
on:
workflow_dispatch:
release:
types: [ published ]
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Check out repository.
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Install dependencies
run: poetry install --no-interaction
- name: Build Docs
run: poetry run mkdocs build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
if: github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./site
28 changes: 28 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Latest Release
on:
workflow_dispatch:
release:
types: [ published ]
jobs:
publish-release:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Install dependencies
run: poetry install --no-interaction
- name: Publish to PyPI
shell: bash
run: |
poetry config pypi-token.pypi ${{ secrets.PYPI_TOKEN }}
poetry publish --build --no-interaction
3 changes: 0 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
repos:
# https://github.com/pre-commit/pre-commit-hooks#pre-commit-hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
# https://pycqa.github.io/isort/docs/configuration/pre-commit.html
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
Expand All @@ -23,7 +21,6 @@ repos:
language_version: python3.11
exclude: ^(\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|_build|buck-out|build|dist|alembic)/
args: [--config=pyproject.toml]
# Check compliance with Ruff.
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.287
hooks:
Expand Down
15 changes: 1 addition & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Version 0.1.0 - 2023-09-08

### Added

* Project Structure
* Hello World CLI Tool.
* Pre Commit Hooks
* `CHANGELOG.md`
* `README.md`
* `LICENSE`
Please check [this page in the documentation](https://hasansezertasan.github.io/chrome-version/changelog/).
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<h1 align="center">
<strong>Chrome Version</strong>
</h1>
<p align="center">
<em>Get the version of Chrome installed on Windows, Linux, Mac. Cross-platform using Python, native OS detection, does not require Selenium.</em>
</p>
<p align="center">
<a href="https://github.com/hasansezertasan/chrome-version" target="_blank">
<img src="https://img.shields.io/github/last-commit/hasansezertasan/chrome-version" alt="Latest Commit">
Expand All @@ -11,12 +14,74 @@
<a href="https://pypi.org/project/chrome-version" target="_blank">
<img src="https://img.shields.io/pypi/v/chrome-version" alt="Package version">
</a>
<img src="https://img.shields.io/pypi/pyversions/chrome-version">
<a href="https://pypi.org/project/chrome-version" target="_blank">
<img src="https://img.shields.io/pypi/pyversions/chrome-version">
</a>
<img src="https://img.shields.io/github/license/hasansezertasan/chrome-version">
</p>

## Installation

- To use it as a module:

``` bash
pip install chrome-version
```

- To use it as a CLI:

``` bash
pip install chrome-version[console]
```

## Usage

Module:

```python
>>> import chrome_version
>>> print(chrome_version.get_chrome_version())
>>> '103.0.5060.114'
```

CLI:

```bash
chrome-version
103.0.5060.114
```

## Why?

At first,

I needed to get the Chrome version for a project I was working on, which was using [Undetected Chromedriver](https://github.com/ultrafunkamsterdam/undetected-chromedriver). I found the gist below.

When I used it in a variaty of projects, I decided to turn it into a module so I'll be free of copy/paste. It only have one functionality: getting the chrome version.

Then,

I decided to use it to learn more about:

- Using [Poetry](https://python-poetry.org/) for packaging and dependency management.
- Using typer for CLI.
- Versioning.
- License management.
- Documentation.
- Publishing a module on PyPI.
- Using GitHub Actions to automate the publishing process.
- ...

Finally,

Now it's kind of a playground for me to learn more...

## Motivation

- It might be useful for someone.
- It's easier to pip install a module than copy/paste a gist.
- Educational Purposes: A simple module is a good practice for me to learn how to build modules and publish them on PyPI and show others how easy it is.

## Disclaimer

This module based on [Detect the version of Chrome installed on Windows, Linux, Mac. Cross-platform using Python, native OS detection, does not require Selenium.](https://gist.github.com/primaryobjects/d5346bf7a173dbded1a70375ff7461b4) gist. I'm not the author of the gist, I just made it into a module and coded a CLI wrapping it. All credits to the author of the gist.
1 change: 1 addition & 0 deletions chrome_version/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from chrome_version.resolver import get_chrome_version
29 changes: 20 additions & 9 deletions chrome_version/__main__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import typer
try:
import typer
except ImportError:
# First Way: Raise the error if Typer is not installed.
# raise ImportError(
# "Typer is not installed, please install it using `pip install typer` and try again."
# )
# Second Way: Exit with a non-zero exit code to indicate a failure
assert (
False
), "Typer is not installed, please install it using `pip install typer` and try again."
from chrome_version.resolver import get_chrome_version

app = typer.Typer(
name="Chrome Version",
)

app = typer.Typer()


@app.callback(
name="Hello World",
help="Hello World.",
short_help="Hello World.",
@app.command(
name="Get Chrome Version",
help="Get the version of Chrome.",
short_help="Get the version of Chrome.",
)
def main():
"""
Get the version of Chrome.
"""
typer.echo("Hello World.")
version = get_chrome_version()
typer.echo(version)


if __name__ == "__main__":
Expand Down
79 changes: 79 additions & 0 deletions chrome_version/resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os
import re
from sys import platform


def extract_version_registry(output):
"""Extracts the Chrome version from the registry output."""
try:
google_version = ""
for letter in output[output.rindex("DisplayVersion REG_SZ") + 24 :]:
if letter != "\n":
google_version += letter
else:
break
return google_version.strip()
except TypeError:
return


def extract_version_folder():
"""Extracts the Chrome version from the folder name.
Check if the Chrome folder exists in the x32 or x64 Program Files folders.
"""
for i in range(2):
path = (
"C:\\Program Files"
+ (" (x86)" if i else "")
+ "\\Google\\Chrome\\Application"
)
if os.path.isdir(path):
paths = [f.path for f in os.scandir(path) if f.is_dir()]
for path in paths:
filename = os.path.basename(path)
pattern = "\d+\.\d+\.\d+\.\d+"
match = re.search(pattern, filename)
if match and match.group():
# Found a Chrome version.
return match.group(0)

return None


def get_chrome_version():
"""Gets the Chrome version."""
version = None
install_path = None

try:
if platform == "linux" or platform == "linux2":
# linux
install_path = "/usr/bin/google-chrome"
elif platform == "darwin":
# OS X
install_path = (
"/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
)
elif platform == "win32":
# Windows...
try:
# Try registry key.
stream = os.popen(
'reg query "HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome"'
)
output = stream.read()
version = extract_version_registry(output)
except Exception as ex:
# Try folder path.
version = extract_version_folder()
except Exception as ex:
print(ex)

version = (
os.popen(f"{install_path} --version").read().strip("Google Chrome ").strip()
if install_path
else version
)

return version
45 changes: 45 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning].

## [Unreleased]

### Added

- Module itself with poetry.
- CLI Tool wrapping the module with typer.
- Optional dependency `typer` for CLI Tool.
- MkDocs for documentation.
- GitHub Actions:
- Generate Documentation.
- Publish to PyPI.
- Dependabot for dependency management.
- Funding options for GitHub Sponsors.

### Changed

- `README.md` to include installation and usage instructions.
- Dependencies to fit the project.

## [0.1.0] - 2023-09-08

- initial release

### Added

- Project Structure
- Hello World CLI Tool.
- Pre Commit Hooks
- `CHANGELOG.md`
- `README.md`
- `LICENSE`

<!-- Links -->
[keep a changelog]: https://keepachangelog.com/en/1.1.0/
[semantic versioning]: https://semver.org

<!-- Versions -->
[unreleased]: https://github.com/hasansezertasan/chrome-version/compare/0.1.0...HEAD
[0.1.0]: https://github.com/hasansezertasan/chrome-version/releases/tag/0.1.0
Loading

0 comments on commit bda9df8

Please sign in to comment.