Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Make "disable i18n" logic optional #899

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Callables or other Python objects have to be passed in `setup.py` (via the `use_
Setuptools will still normalize it to create the final distribution,
so as to stay compliant with the python packaging standards.

`disable_i18n`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having it a config options is fundamentally wrong as everyone affected would have to enable it individually

: A boolean flag indicating if the values `LC_ALL="C" LANGUAGE=""` should be
injected into the environment when running commands such as `git` and `hg`.
Defaults to `True`.

## environment variables

Expand Down
1 change: 1 addition & 0 deletions src/setuptools_scm/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Configuration:
dist_name: str | None = None
version_cls: type[_VersionT] = _Version
search_parent_directories: bool = False
disable_i18n: bool = True

parent: _t.PathT | None = None

Expand Down
39 changes: 27 additions & 12 deletions src/setuptools_scm/_run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import warnings
from typing import Callable
from typing import Mapping
from typing import Optional
from typing import overload
from typing import Sequence
from typing import TYPE_CHECKING
from typing import TypeVar

from . import _log
from . import _types as _t
from . import Configuration

if TYPE_CHECKING:
BaseCompletedProcess = subprocess.CompletedProcess[str]
Expand Down Expand Up @@ -128,25 +130,35 @@ def run(
trace: bool = True,
timeout: int = 20,
check: bool = False,
config: Configuration | None = None,
) -> CompletedProcess:
if isinstance(cmd, str):
cmd = shlex.split(cmd)
else:
cmd = [os.fspath(x) for x in cmd]
cmd_4_trace = " ".join(map(_unsafe_quote_for_display, cmd))
log.debug("at %s\n $ %s ", cwd, cmd_4_trace)

run_env = dict(
avoid_pip_isolation(no_git_env(os.environ)),
# os.environ,
HGPLAIN="1",
)
if not config or (config and config.disable_i18n):
run_env.update(
{
# try to disable i18n
"LC_ALL": "C",
"LANGUAGE": "",
}
)
log.debug("Shell environment to be used for command: %s", run_env)

res = subprocess.run(
cmd,
capture_output=True,
cwd=os.fspath(cwd),
env=dict(
avoid_pip_isolation(no_git_env(os.environ)),
# os.environ,
# try to disable i18n
LC_ALL="C",
LANGUAGE="",
HGPLAIN="1",
),
env=run_env,
text=True,
timeout=timeout,
)
Expand All @@ -171,10 +183,13 @@ def _unsafe_quote_for_display(item: _t.PathT) -> str:


def has_command(
name: str, args: Sequence[str] = ["version"], warn: bool = True
name: str,
args: Sequence[str] = ["version"],
warn: bool = True,
config: Configuration | None = None,
) -> bool:
try:
p = run([name, *args], cwd=".", timeout=5)
p = run([name, *args], cwd=".", timeout=5, config=config)
except OSError as e:
log.warning("command %s missing: %s", name, e)
res = False
Expand All @@ -189,6 +204,6 @@ def has_command(
return res


def require_command(name: str) -> None:
if not has_command(name, warn=False):
def require_command(name: str, config: Configuration | None = None) -> None:
if not has_command(name, warn=False, config=config):
raise OSError(f"{name!r} was not found")
22 changes: 15 additions & 7 deletions src/setuptools_scm/hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import datetime
import logging
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
from typing import TYPE_CHECKING

from . import Configuration
Expand All @@ -22,13 +24,18 @@
log = logging.getLogger(__name__)


@dataclass()
class HgWorkdir(Workdir):
config: Configuration | None = None

@classmethod
def from_potential_worktree(cls, wd: _t.PathT) -> HgWorkdir | None:
res = _run(["hg", "root"], wd)
def from_potential_worktree(
cls, wd: _t.PathT, config: Configuration
) -> HgWorkdir | None:
res = _run(["hg", "root"], wd, config=config)
if res.returncode:
return None
return cls(Path(res.stdout))
return cls(path=Path(res.stdout), config=config)

def get_meta(self, config: Configuration) -> ScmVersion | None:
node: str
Expand All @@ -47,6 +54,7 @@ def get_meta(self, config: Configuration) -> ScmVersion | None:
["hg", "id", "-T", "{branch}\n{if(dirty, 1, 0)}\n{date|shortdate}"],
cwd=self.path,
check=True,
config=config,
).stdout.split("\n")
dirty = bool(int(dirty_str))
node_date = datetime.date.fromisoformat(dirty_date if dirty else node_date_str)
Expand Down Expand Up @@ -109,7 +117,7 @@ def get_meta(self, config: Configuration) -> ScmVersion | None:
def hg_log(self, revset: str, template: str) -> str:
cmd = ["hg", "log", "-r", revset, "-T", template]

return _run(cmd, cwd=self.path, check=True).stdout
return _run(cmd, cwd=self.path, check=True, config=self.config).stdout

def get_latest_normalizable_tag(self) -> str | None:
# Gets all tags containing a '.' (see #229) from oldest to newest
Expand Down Expand Up @@ -143,9 +151,9 @@ def check_changes_since_tag(self, tag: str | None) -> bool:


def parse(root: _t.PathT, config: Configuration) -> ScmVersion | None:
_require_command("hg")
_require_command("hg", config=config)
if os.path.exists(os.path.join(root, ".hg/git")):
res = _run(["hg", "path"], root)
res = _run(["hg", "path"], root, config=config)
if not res.returncode:
for line in res.stdout.split("\n"):
if line.startswith("default ="):
Expand All @@ -158,7 +166,7 @@ def parse(root: _t.PathT, config: Configuration) -> ScmVersion | None:
if wd_hggit:
return _git_parse_inner(config, wd_hggit)

wd = HgWorkdir.from_potential_worktree(config.absolute_root)
wd = HgWorkdir.from_potential_worktree(config.absolute_root, config=config)

if wd is None:
return None
Expand Down
Loading