Skip to content

Commit

Permalink
Merge pull request #37 from vkottler/dev/build-libraries
Browse files Browse the repository at this point in the history
Dev/build libraries
  • Loading branch information
vkottler authored Jul 17, 2023
2 parents e98d228 + 93518db commit 282c4ea
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
- run: |
mk python-release owner=vkottler \
repo=yambs version=1.10.5
repo=yambs version=2.0.0
if: |
matrix.python-version == '3.11'
&& matrix.system == 'ubuntu-latest'
Expand Down
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[DESIGN]
max-args=8
max-attributes=8

[MESSAGES CONTROL]
disable=too-few-public-methods
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.2
hash=7b491a6ae08910955ce5d652d62d907f
hash=002bf1ec758c597d37da825e56ca08c0
=====================================
-->

# yambs ([1.10.5](https://pypi.org/project/yambs/))
# yambs ([2.0.0](https://pypi.org/project/yambs/))

[![python](https://img.shields.io/pypi/pyversions/yambs.svg)](https://pypi.org/project/yambs/)
![Build Status](https://github.com/vkottler/yambs/workflows/Python%20Package/badge.svg)
Expand Down Expand Up @@ -157,13 +157,14 @@ commands:
```
$ ./venv3.11/bin/mbs dist -h
usage: mbs dist [-h] [-c CONFIG]
usage: mbs dist [-h] [-c CONFIG] [-s]
options:
-h, --help show this help message and exit
-c CONFIG, --config CONFIG
the path to the top-level configuration file (default:
'yambs.yaml')
-s, --sources set this flag to only capture source files
```

Expand Down
6 changes: 3 additions & 3 deletions local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 1
minor: 10
patch: 5
major: 2
minor: 0
patch: 0
entry: mbs
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "yambs"
version = "1.10.5"
version = "2.0.0"
description = "Yet another meta build-system."
readme = "README.md"
requires-python = ">=3.8"
Expand Down
13 changes: 13 additions & 0 deletions tests/commands/test_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ def test_dist_command_basic():
"""Test the 'dist' command."""

for scenario in ["sample", "native"]:
assert (
yambs_main(
[
PKG_NAME,
"-C",
str(resource("scenarios", scenario)),
"dist",
"-s",
]
)
== 0
)

assert (
yambs_main(
[
Expand Down
1 change: 1 addition & 0 deletions tests/data/valid/scenarios/native/extra/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
2 changes: 2 additions & 0 deletions tests/data/valid/scenarios/native/yambs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cflag_groups:
debug: [-Og, -g]
opt: [-O2]

extra_dist: [extra]

project:
name: yambs
github:
Expand Down
4 changes: 2 additions & 2 deletions yambs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.2
# hash=e45ca62e08fc5342a3dc3c43ca433ee4
# hash=8dd6e5c8fb283e23758f59bf4a0844de
# =====================================

"""
Expand All @@ -10,4 +10,4 @@

DESCRIPTION = "Yet another meta build-system."
PKG_NAME = "yambs"
VERSION = "1.10.5"
VERSION = "2.0.0"
52 changes: 26 additions & 26 deletions yambs/commands/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,47 @@
# built-in
from argparse import ArgumentParser as _ArgumentParser
from argparse import Namespace as _Namespace
from shutil import make_archive, rmtree
from pathlib import Path
from shutil import copytree, rmtree
from tempfile import TemporaryDirectory

# third-party
from vcorelib.args import CommandFunction as _CommandFunction
from vcorelib.paths import create_hex_digest
from vcorelib.paths.context import in_dir

# internal
from yambs.commands.common import add_config_arg
from yambs.config.common import CommonConfig
from yambs.dist import copy_source_tree, make_archives


def dist_cmd(args: _Namespace) -> int:
"""Execute the dist command."""

config = CommonConfig.load(path=args.config, root=args.dir)

# Remove and re-create the dist directory.
dist = config.dist_root
rmtree(dist, ignore_errors=True)
dist.mkdir()
# Prepare a temporary directory with project sources.
with TemporaryDirectory() as tmp:
path = Path(tmp)

slug = str(config.project)
src = config.src_root
archives = []
# Put everything under a directory named after the project.
base = path.joinpath(config.project.name)

for ext, kind in [
("tar.gz", "gztar"),
("tar.xz", "xztar"),
("zip", "zip"),
]:
out = src.joinpath(f"{slug}.{ext}")
out.unlink(missing_ok=True)
if args.sources:
copytree(config.src_root, base)
else:
base.mkdir()
copy_source_tree(config, base)

with in_dir(src):
make_archive(slug, kind)
# Remove and re-create the dist directory.
dist = config.dist_root
rmtree(dist, ignore_errors=True)
dist.mkdir()

assert out.is_file(), out

final = dist.joinpath(out.name)
out.rename(final)
archives.append(final)

print(f"Created '{final}'.")
make_archives(path, config)

# Produce a hex digest.
print(f"Wrote '{create_hex_digest(dist, slug)}'.")
print(f"Wrote '{create_hex_digest(dist, str(config.project))}'.")

return 0

Expand All @@ -60,4 +54,10 @@ def add_dist_cmd(parser: _ArgumentParser) -> _CommandFunction:
"""Add dist-command arguments to its parser."""

add_config_arg(parser)
parser.add_argument(
"-s",
"--sources",
action="store_true",
help="set this flag to only capture source files",
)
return dist_cmd
9 changes: 9 additions & 0 deletions yambs/config/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class CommonConfig(_YambsDictCodec, _BasicDictCodec):
ninja_root: Path
dist_root: Path

file: Optional[Path]

def directory(self, name: str, mkdir: bool = True) -> Path:
"""Get a configurable directory."""

Expand All @@ -97,6 +99,8 @@ def init(self, data: _JsonObject) -> None:
self.ninja_root = self.directory("ninja_out")
self.dist_root = self.directory("dist_out")

self.file = None

self.project = Project.create(data["project"]) # type: ignore

@classmethod
Expand All @@ -111,6 +115,8 @@ def load(
src_config = find_file(package_config, package=PKG_NAME)
assert src_config is not None

path = normalize(path)

data = merge(
_ARBITER.decode(
src_config,
Expand All @@ -125,6 +131,9 @@ def load(

result = cls.create(data)

if path.is_file():
result.file = path

if root is not None:
result.root = normalize(root)
result.root.mkdir(parents=True, exist_ok=True)
Expand Down
3 changes: 3 additions & 0 deletions yambs/data/native.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ includes:
- includes/common.yaml

cflag_groups:
# Compile position-independent to build shared objects.
pic: [-fPIC]

# Instrument builds for coverage and other analysis.
debug: [-Og, -g, --coverage]
opt: [-O2]
Expand Down
5 changes: 5 additions & 0 deletions yambs/data/schemas/Config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ properties:
patternProperties:
"^[a-zA-Z0-9-_.+]+$":
$ref: package://yambs/schemas/Architecture.yaml

extra_third_party:
type: array
items:
type: string
2 changes: 1 addition & 1 deletion yambs/data/schemas/config_common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ properties:
items:
type: string

extra_third_party:
extra_dist:
type: array
items:
type: string
Expand Down
3 changes: 3 additions & 0 deletions yambs/data/templates/native_rules.ninja.j2
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ ldflags ={% for flag in common_ldflags %} {{flag}}{% endfor %}
rule link
command = $ld $cflags $ldflags -Wl,-Map=$out.map $in -o $out

rule ar
command = ar rcs $out $in

build_dir = {{build_root}}/$variant
65 changes: 65 additions & 0 deletions yambs/dist/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
A module implementing interfaces for facilitating project distribution.
"""

# built-in
from pathlib import Path
from shutil import copy2, copytree, make_archive

# third-party
from vcorelib.paths.context import in_dir

# internal
from yambs.config.common import CommonConfig

ARCHIVES = [
("tar.gz", "gztar"),
("tar.xz", "xztar"),
("zip", "zip"),
]


def make_archives(tmp: Path, config: CommonConfig) -> None:
"""Create a distribution that only contains sources."""

slug = str(config.project)

for ext, kind in ARCHIVES:
out = tmp.joinpath(f"{slug}.{ext}")
out.unlink(missing_ok=True)

with in_dir(tmp):
make_archive(slug, kind)

assert out.is_file(), out

final = config.dist_root.joinpath(out.name)
copy2(out, final.resolve())
out.unlink()

print(f"Created '{final}'.")


def copy_source_tree(config: CommonConfig, dest: Path) -> None:
"""
Copy necessary parts of the project source tree to some destination
directory.
"""

root = config.root

for item in [
x
for x in [
config.src_root,
config.ninja_root,
root.joinpath("build.ninja"),
config.file,
]
+ [root.joinpath(x) for x in config.data.get("extra_dist", [])]
if x and x.exists()
]:
if item.is_dir():
copytree(item, dest.joinpath(item.name))
else:
copy2(item, dest.joinpath(item.name))
Loading

0 comments on commit 282c4ea

Please sign in to comment.