summaryrefslogtreecommitdiffstats
path: root/pre_commit
diff options
context:
space:
mode:
Diffstat (limited to 'pre_commit')
-rw-r--r--pre_commit/all_languages.py2
-rw-r--r--pre_commit/commands/install_uninstall.py3
-rw-r--r--pre_commit/languages/haskell.py56
-rw-r--r--pre_commit/xargs.py11
4 files changed, 69 insertions, 3 deletions
diff --git a/pre_commit/all_languages.py b/pre_commit/all_languages.py
index 2bed706..476bad9 100644
--- a/pre_commit/all_languages.py
+++ b/pre_commit/all_languages.py
@@ -9,6 +9,7 @@ from pre_commit.languages import docker_image
from pre_commit.languages import dotnet
from pre_commit.languages import fail
from pre_commit.languages import golang
+from pre_commit.languages import haskell
from pre_commit.languages import lua
from pre_commit.languages import node
from pre_commit.languages import perl
@@ -31,6 +32,7 @@ languages: dict[str, Language] = {
'dotnet': dotnet,
'fail': fail,
'golang': golang,
+ 'haskell': haskell,
'lua': lua,
'node': node,
'perl': perl,
diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py
index 5ff6cba..d19e0d4 100644
--- a/pre_commit/commands/install_uninstall.py
+++ b/pre_commit/commands/install_uninstall.py
@@ -103,8 +103,7 @@ def _install_hook_script(
hook_file.write(before + TEMPLATE_START)
hook_file.write(f'INSTALL_PYTHON={shlex.quote(sys.executable)}\n')
- # TODO: python3.8+: shlex.join
- args_s = ' '.join(shlex.quote(part) for part in args)
+ args_s = shlex.join(args)
hook_file.write(f'ARGS=({args_s})\n')
hook_file.write(TEMPLATE_END + after)
make_executable(hook_path)
diff --git a/pre_commit/languages/haskell.py b/pre_commit/languages/haskell.py
new file mode 100644
index 0000000..76442eb
--- /dev/null
+++ b/pre_commit/languages/haskell.py
@@ -0,0 +1,56 @@
+from __future__ import annotations
+
+import contextlib
+import os.path
+from typing import Generator
+from typing import Sequence
+
+from pre_commit import lang_base
+from pre_commit.envcontext import envcontext
+from pre_commit.envcontext import PatchesT
+from pre_commit.envcontext import Var
+from pre_commit.errors import FatalError
+from pre_commit.prefix import Prefix
+
+ENVIRONMENT_DIR = 'hs_env'
+get_default_version = lang_base.basic_get_default_version
+health_check = lang_base.basic_health_check
+run_hook = lang_base.basic_run_hook
+
+
+def get_env_patch(target_dir: str) -> PatchesT:
+ bin_path = os.path.join(target_dir, 'bin')
+ return (('PATH', (bin_path, os.pathsep, Var('PATH'))),)
+
+
+@contextlib.contextmanager
+def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
+ envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
+ with envcontext(get_env_patch(envdir)):
+ yield
+
+
+def install_environment(
+ prefix: Prefix,
+ version: str,
+ additional_dependencies: Sequence[str],
+) -> None:
+ lang_base.assert_version_default('haskell', version)
+ envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
+
+ pkgs = [*prefix.star('.cabal'), *additional_dependencies]
+ if not pkgs:
+ raise FatalError('Expected .cabal files or additional_dependencies')
+
+ bindir = os.path.join(envdir, 'bin')
+ os.makedirs(bindir, exist_ok=True)
+ lang_base.setup_cmd(prefix, ('cabal', 'update'))
+ lang_base.setup_cmd(
+ prefix,
+ (
+ 'cabal', 'install',
+ '--install-method', 'copy',
+ '--installdir', bindir,
+ *pkgs,
+ ),
+ )
diff --git a/pre_commit/xargs.py b/pre_commit/xargs.py
index 31be6f3..a7493c0 100644
--- a/pre_commit/xargs.py
+++ b/pre_commit/xargs.py
@@ -25,6 +25,14 @@ TRet = TypeVar('TRet')
def cpu_count() -> int:
try:
+ # On systems that support it, this will return a more accurate count of
+ # usable CPUs for the current process, which will take into account
+ # cgroup limits
+ return len(os.sched_getaffinity(0))
+ except AttributeError:
+ pass
+
+ try:
return multiprocessing.cpu_count()
except NotImplementedError:
return 1
@@ -170,7 +178,8 @@ def xargs(
results = thread_map(run_cmd_partition, partitions)
for proc_retcode, proc_out, _ in results:
- retcode = max(retcode, proc_retcode)
+ if abs(proc_retcode) > abs(retcode):
+ retcode = proc_retcode
stdout += proc_out
return retcode, stdout