| Conditions | 13 |
| Total Lines | 72 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like noxfile.activate_virtualenv_in_precommit_hooks() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Nox sessions.""" |
||
| 39 | def activate_virtualenv_in_precommit_hooks(session: Session) -> None: |
||
| 40 | """Activate virtualenv in hooks installed by pre-commit. |
||
| 41 | |||
| 42 | This function patches git hooks installed by pre-commit to activate the |
||
| 43 | session's virtual environment. This allows pre-commit to locate hooks in |
||
| 44 | that environment when invoked from git. |
||
| 45 | |||
| 46 | Args: |
||
| 47 | session: The Session object. |
||
| 48 | """ |
||
| 49 | assert session.bin is not None # noqa: S101 |
||
| 50 | |||
| 51 | # Only patch hooks containing a reference to this session's bindir. Support |
||
| 52 | # quoting rules for Python and bash, but strip the outermost quotes so we |
||
| 53 | # can detect paths within the bindir, like <bindir>/python. |
||
| 54 | bindirs = [ |
||
| 55 | bindir[1:-1] if bindir[0] in "'\"" else bindir |
||
| 56 | for bindir in (repr(session.bin), shlex.quote(session.bin)) |
||
| 57 | ] |
||
| 58 | |||
| 59 | virtualenv = session.env.get("VIRTUAL_ENV") |
||
| 60 | if virtualenv is None: |
||
| 61 | return |
||
| 62 | |||
| 63 | headers = { |
||
| 64 | # pre-commit < 2.16.0 |
||
| 65 | "python": f"""\ |
||
| 66 | import os |
||
| 67 | os.environ["VIRTUAL_ENV"] = {virtualenv!r} |
||
| 68 | os.environ["PATH"] = os.pathsep.join(( |
||
| 69 | {session.bin!r}, |
||
| 70 | os.environ.get("PATH", ""), |
||
| 71 | )) |
||
| 72 | """, |
||
| 73 | # pre-commit >= 2.16.0 |
||
| 74 | "bash": f"""\ |
||
| 75 | VIRTUAL_ENV={shlex.quote(virtualenv)} |
||
| 76 | PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH" |
||
| 77 | """, |
||
| 78 | # pre-commit >= 2.17.0 on Windows forces sh shebang |
||
| 79 | "/bin/sh": f"""\ |
||
| 80 | VIRTUAL_ENV={shlex.quote(virtualenv)} |
||
| 81 | PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH" |
||
| 82 | """, |
||
| 83 | } |
||
| 84 | |||
| 85 | hookdir = Path(".git") / "hooks" |
||
| 86 | if not hookdir.is_dir(): |
||
| 87 | return |
||
| 88 | |||
| 89 | for hook in hookdir.iterdir(): |
||
| 90 | if hook.name.endswith(".sample") or not hook.is_file(): |
||
| 91 | continue |
||
| 92 | |||
| 93 | if not hook.read_bytes().startswith(b"#!"): |
||
| 94 | continue |
||
| 95 | |||
| 96 | text = hook.read_text() |
||
| 97 | |||
| 98 | if not any( |
||
| 99 | Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text |
||
| 100 | for bindir in bindirs |
||
| 101 | ): |
||
| 102 | continue |
||
| 103 | |||
| 104 | lines = text.splitlines() |
||
| 105 | |||
| 106 | for executable, header in headers.items(): |
||
| 107 | if executable in lines[0].lower(): |
||
| 108 | lines.insert(1, dedent(header)) |
||
| 109 | hook.write_text("\n".join(lines)) |
||
| 110 | break |
||
| 111 | |||
| 237 |