Skip to content

Commit

Permalink
feat: added set_perms_rw_for_g
Browse files Browse the repository at this point in the history
  • Loading branch information
Oskar Manhart committed Feb 17, 2023
1 parent 6cdb825 commit 67f70e9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
8 changes: 7 additions & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
description: sets odoo module version to <commit-id>@<branch>
entry: set-module-version
language: python
types: [python]
types: [python]

- id: set-perms-rw-for-g
name: set file perms to g+rw
description: change all files permission to 770
entry: set-perms-rw-for-g
language: python
23 changes: 23 additions & 0 deletions pre_commit_hooks/set_perms_rw_for_g.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import annotations

import argparse
from typing import Sequence
import elevate
from .util import try_chmod

def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv)

for filename in args.filenames:
if try_chmod(filename) == 1:
elevate.elevate()
if try_chmod(filename) == 1:
return 1

return 0


if __name__ == '__main__':
raise SystemExit(main())
11 changes: 10 additions & 1 deletion pre_commit_hooks/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import subprocess
import os
from typing import Any

def current_branch() -> str:
Expand Down Expand Up @@ -34,4 +35,12 @@ def cmd_output(*cmd: str, retcode: int | None = 0, **kwargs: Any) -> str:
stdout = stdout.decode()
if retcode is not None and proc.returncode != retcode:
raise RuntimeError(cmd, retcode, proc.returncode, stdout, stderr)
return stdout
return stdout

def try_chmod(filename) -> int:
try:
os.chmod(filename, 0o770)
except Exception:
return 1

return 0
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ classifiers =
packages = find:
install_requires =
black >= 23.1.0
elevate >= 0.1.3
python_requires = >=3.7

[options.entry_points]
console_scripts =
set-module-version = pre_commit_hooks.set_module_version:main
set-perms-rw-for-g = pre_commit_hooks.set_perms_rw_for_g:main

[bdist_wheel]
universal = True
Expand Down

0 comments on commit 67f70e9

Please sign in to comment.