Skip to content

Commit

Permalink
Added pre-commit, reformatted all code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Knucklessg1 committed May 9, 2024
1 parent e261b6f commit aea4208
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 63 deletions.
69 changes: 69 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
default_language_version:
python: python3
exclude: 'dotnet'
ci:
autofix_prs: true
autoupdate_commit_msg: '[pre-commit.ci] pre-commit suggestions'
autoupdate_schedule: 'monthly'

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-yaml
- id: check-toml
- id: check-json
- id: check-byte-order-marker
exclude: .gitignore
- id: check-merge-conflict
- id: detect-private-key
- id: trailing-whitespace
- id: end-of-file-fixer
- id: no-commit-to-branch
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: ruff
types_or: [ python, pyi, jupyter ]
args: ["--fix", "--ignore=E402"]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
args: ["-L", "ans,linar,nam,tread,ot,"]
exclude: |
(?x)^(
pyproject.toml |
website/static/img/ag.svg |
website/yarn.lock |
website/docs/tutorial/code-executors.ipynb |
website/docs/topics/code-execution/custom-executor.ipynb |
website/docs/topics/non-openai-models/cloud-gemini.ipynb |
notebook/.*
)$
# See https://jaredkhan.com/blog/mypy-pre-commit
- repo: local
hooks:
- id: mypy
name: mypy
entry: "./scripts/pre-commit-mypy-run.sh"
language: python
# use your preferred Python version
# language_version: python3.8
additional_dependencies: []
types: [python]
# use require_serial so that script
# is only called once per commit
require_serial: true
# Print the number of files as a sanity-check
verbose: true
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
hooks:
- id: nbqa-black
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ 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.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions repository_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# coding: utf-8
from repository_manager.version import __version__, __author__, __credits__
from repository_manager.repository_manager import Git, main

"""
report-manager
Expand All @@ -11,3 +12,5 @@
__version__ = __version__
__author__ = __author__
__credits__ = __credits__

__all__ = ["Git", "main"]
103 changes: 66 additions & 37 deletions repository_manager/repository_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ def __init__(self):
def git_action(self, command, directory=None):
if directory is None:
directory = self.repository_directory
pipe = subprocess.Popen(command,
shell=True,
cwd=directory,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
pipe = subprocess.Popen(
command,
shell=True,
cwd=directory,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
(out, error) = pipe.communicate()
result = f"{str(out, 'utf-8')}{str(error, 'utf-8')}"
pipe.wait()
Expand All @@ -33,7 +35,9 @@ def set_repository_directory(self, repository_directory):
if os.path.exists(repository_directory):
self.repository_directory = repository_directory.replace(os.sep, "/")
else:
print(f'Path specified does not exist: {repository_directory.replace(os.sep, "/")}')
print(
f'Path specified does not exist: {repository_directory.replace(os.sep, "/")}'
)

def set_git_projects(self, git_projects):
self.git_projects = git_projects
Expand All @@ -47,10 +51,14 @@ def set_threads(self, threads):
if threads > 0 or threads < os.cpu_count():
self.threads = threads
else:
print(f"Did not recognize {threads} as a valid value, defaulting to CPU Count: {os.cpu_count()}")
print(
f"Did not recognize {threads} as a valid value, defaulting to CPU Count: {os.cpu_count()}"
)
self.threads = os.cpu_count()
except Exception as e:
print(f"Did not recognize {threads} as a valid value, defaulting to CPU Count: {os.cpu_count()}\nError: {e}")
print(
f"Did not recognize {threads} as a valid value, defaulting to CPU Count: {os.cpu_count()}\nError: {e}"
)
self.threads = os.cpu_count()

def append_git_project(self, git_project):
Expand All @@ -68,36 +76,46 @@ def pull_projects_in_parallel(self):
pool.map(self.pull_project, os.listdir(self.repository_directory))

def pull_project(self, git_project):
print(f'Scanning: {self.repository_directory}/{git_project}\n'
f'Pulling latest changes for {git_project}\n'
f'{self.git_action(command="git pull", directory=os.path.normpath(os.path.join(self.repository_directory, git_project)))}')
print(
f"Scanning: {self.repository_directory}/{git_project}\n"
f"Pulling latest changes for {git_project}\n"
f'{self.git_action(command="git pull", directory=os.path.normpath(os.path.join(self.repository_directory, git_project)))}'
)
if self.set_to_default_branch:
default_branch = self.git_action("git symbolic-ref refs/remotes/origin/HEAD",
directory=f"{self.repository_directory}/{git_project}")
default_branch = self.git_action(
"git symbolic-ref refs/remotes/origin/HEAD",
directory=f"{self.repository_directory}/{git_project}",
)
default_branch = re.sub("refs/remotes/origin/", "", default_branch).strip()
print(f"Checking out default branch ",
self.git_action(f'git checkout "{default_branch}"',
directory=f"{self.repository_directory}/{git_project}"))
print(
"Checking out default branch ",
self.git_action(
f'git checkout "{default_branch}"',
directory=f"{self.repository_directory}/{git_project}",
),
)


def usage():
print(f"Usage: \n"
f"-h | --help [ See usage for script ]\n"
f"-b | --default-branch [ Checkout default branch ]\n"
f"-c | --clone [ Clone projects specified ]\n"
f"-d | --directory [ Directory to clone/pull projects ]\n"
f"-f | --file [ File with repository links ]\n"
f"-p | --pull [ Pull projects in parent directory ]\n"
f"-r | --repositories [ Comma separated Git URLs ]\n"
f"-t | --threads [ Number of parallel threads - Default 4 ]\n"
f"\n"
f"repository-manager \n\t"
f"--clone \n\t"
f"--pull \n\t"
f"--directory '/home/user/Downloads'\n\t"
f"--file '/home/user/Downloads/repositories.txt' \n\t"
f"--repositories 'https://github.com/Knucklessg1/media-downloader,https://github.com/Knucklessg1/genius-bot'\n\t"
f"--threads 8")
print(
"Usage: \n"
"-h | --help [ See usage for script ]\n"
"-b | --default-branch [ Checkout default branch ]\n"
"-c | --clone [ Clone projects specified ]\n"
"-d | --directory [ Directory to clone/pull projects ]\n"
"-f | --file [ File with repository links ]\n"
"-p | --pull [ Pull projects in parent directory ]\n"
"-r | --repositories [ Comma separated Git URLs ]\n"
"-t | --threads [ Number of parallel threads - Default 4 ]\n"
"\n"
"repository-manager \n\t"
"--clone \n\t"
"--pull \n\t"
"--directory '/home/user/Downloads'\n\t"
"--file '/home/user/Downloads/repositories.txt' \n\t"
"--repositories 'https://github.com/Knucklessg1/media-downloader,https://github.com/Knucklessg1/genius-bot'\n\t"
"--threads 8"
)


def repository_manager(argv):
Expand All @@ -111,9 +129,20 @@ def repository_manager(argv):
repositories = None
threads = os.cpu_count()
try:
opts, args = getopt.getopt(argv, "hbcpd:f:r:t:",
["help", "default-branch", "clone", "pull", "directory=", "file=", "repositories=",
"threads="])
opts, args = getopt.getopt(
argv,
"hbcpd:f:r:t:",
[
"help",
"default-branch",
"clone",
"pull",
"directory=",
"file=",
"repositories=",
"threads=",
],
)
except getopt.GetoptError:
usage()
sys.exit(2)
Expand Down Expand Up @@ -147,7 +176,7 @@ def repository_manager(argv):

# Verify file with repositories exists
if os.path.exists(file):
file_repositories = open(file, 'r')
file_repositories = open(file, "r")
for repository in file_repositories:
projects.append(repository)
else:
Expand Down
6 changes: 3 additions & 3 deletions repository_manager/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# coding: utf-8

__version__ = '0.8.2'
__author__ = 'Audel Rouhi'
__credits__ = 'Audel Rouhi'
__version__ = "0.8.2"
__author__ = "Audel Rouhi"
__credits__ = "Audel Rouhi"
15 changes: 15 additions & 0 deletions scripts/pre-commit-mypy-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

# taken from: https://jaredkhan.com/blog/mypy-pre-commit

# A script for running mypy,
# with all its dependencies installed.

set -o errexit

# Change directory to the project root directory.
cd "$(dirname "$0")"/..

pip install -q -e .[types]

mypy
50 changes: 28 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,45 @@
from pip._internal.network.session import PipSession
from pip._internal.req import parse_requirements

readme = Path('README.md').read_text()
readme = Path("README.md").read_text()
version = __version__
requirements = parse_requirements(os.path.join(os.path.dirname(__file__), 'requirements.txt'), session=PipSession())
requirements = parse_requirements(
os.path.join(os.path.dirname(__file__), "requirements.txt"), session=PipSession()
)
readme = re.sub(r"Version: [0-9]*\.[0-9]*\.[0-9][0-9]*", f"Version: {version}", readme)
with open("README.md", "w") as readme_file:
readme_file.write(readme)
description = 'Manage your git projects'
description = "Manage your git projects"

setup(
name='repository-manager',
name="repository-manager",
version=f"{version}",
description=description,
long_description=f'{readme}',
long_description_content_type='text/markdown',
url='https://github.com/Knuckles-Team/repository-manager',
long_description=f"{readme}",
long_description_content_type="text/markdown",
url="https://github.com/Knuckles-Team/repository-manager",
author=__author__,
author_email='knucklessg1@gmail.com',
license='MIT',
packages=['repository_manager'],
author_email="knucklessg1@gmail.com",
license="MIT",
packages=["repository_manager"],
include_package_data=True,
install_requires=[str(requirement.requirement) for requirement in requirements],
py_modules=['repository_manager'],
package_data={'repository_manager': ['repository_manager']},
py_modules=["repository_manager"],
package_data={"repository_manager": ["repository_manager"]},
classifiers=[
'Development Status :: 5 - Production/Stable',
'License :: Public Domain',
'Environment :: Console',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
"Development Status :: 5 - Production/Stable",
"License :: Public Domain",
"Environment :: Console",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
entry_points={'console_scripts': ['repository-manager = repository_manager.repository_manager:main']},
entry_points={
"console_scripts": [
"repository-manager = repository_manager.repository_manager:main"
]
},
)

0 comments on commit aea4208

Please sign in to comment.