Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/mercurial/tests/data/build.py
#!/usr/bin/env python | #!/usr/bin/env python | ||||
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
""" | """ | ||||
This build script purpose is to create example repositories from bash scripts | This build script purpose is to create example repositories from bash scripts | ||||
and extract assertion data from them into json files. | and extract assertion data from them into json files. | ||||
Advantages: | Advantages: | ||||
Show All 11 Lines | |||||
Here is a minimal working example: | Here is a minimal working example: | ||||
#!/usr/bin/env bash | #!/usr/bin/env bash | ||||
# Setup bash in strict mode | # Setup bash in strict mode | ||||
set -euo pipefail | set -euo pipefail | ||||
# Allow direct call to call the script: `./myscript.sh repository-name` | # Allow direct call to call the script: `./myscript.sh repository-name` | ||||
if [ ! -z "$1" ]; then | if [ -n "$1" ]; then | ||||
HG_REPO="$1" | HG_REPO="$1" | ||||
fi | fi | ||||
# Prepare the repository | # Prepare the repository | ||||
hg init "$HG_REPO" | hg init "$HG_REPO" | ||||
cd "$HG_REPO" | cd "$HG_REPO" | ||||
cat > .hg/hgrc << EOL | cat > .hg/hgrc << EOL | ||||
[ui] | [ui] | ||||
username = Full Name<full.name@domain.tld> | username = Full Name<full.name@domain.tld> | ||||
EOL | EOL | ||||
# Populate the repository | # Populate the repository | ||||
touch README.md | touch README.md | ||||
hg add README.md | hg add README.md | ||||
hg commit -m "Add README" | hg commit -m "Add README" | ||||
Then build the repository and the associated json file which containing | Then build the repository and the associated json file which containing | ||||
Show All 24 Lines | |||||
Extract json from existing repository | Extract json from existing repository | ||||
------------------------------------- | ------------------------------------- | ||||
For existing repository without build scripts it is possible to extract | For existing repository without build scripts it is possible to extract | ||||
the corresponding json file by running `./build.py json repository.tgz` | the corresponding json file by running `./build.py json repository.tgz` | ||||
""" | """ | ||||
from datetime import datetime | |||||
import json | import json | ||||
from pathlib import Path | |||||
import shutil | import shutil | ||||
import subprocess | import subprocess | ||||
from datetime import datetime | |||||
from pathlib import Path | |||||
import click | import click | ||||
def abort(message): | def abort(message): | ||||
"""Abort the script with a message.""" | """Abort the script with a message.""" | ||||
click.echo(message, err=True) | click.echo(message, err=True) | ||||
click.get_current_context().abort() | click.get_current_context().abort() | ||||
Show All 19 Lines | if script_path.suffix != ".sh": | ||||
abort(f"Wrong suffix: {script_path.suffix!r}. Expected: '.sh'") | abort(f"Wrong suffix: {script_path.suffix!r}. Expected: '.sh'") | ||||
repository_path = script_path.with_suffix("") | repository_path = script_path.with_suffix("") | ||||
if repository_path.exists(): | if repository_path.exists(): | ||||
backup(repository_path) | backup(repository_path) | ||||
click.echo(f"Running build script: {str(script_path)!r}") | click.echo(f"Running build script: {str(script_path)!r}") | ||||
subprocess.call( | subprocess.run( | ||||
["bash", "-euo", "pipefail", script_path], env={"HG_REPO": str(repository_path)} | ["bash", "-eo", "pipefail", script_path, repository_path.stem], | ||||
env={"HG_REPO": str(repository_path), "HGPLAIN": "", "HGRCPATH": ""}, | |||||
) | ) | ||||
return repository_path | return repository_path | ||||
def _build_json(source: str) -> Path: | def _build_json(source: str) -> Path: | ||||
if source.endswith(".tgz"): | if source.endswith(".tgz"): | ||||
archive_path = Path(source).absolute() | archive_path = Path(source).absolute() | ||||
▲ Show 20 Lines • Show All 136 Lines • Show Last 20 Lines |