Skip to content

Commit

Permalink
Merge pull request #119 from desultory/dev
Browse files Browse the repository at this point in the history
add force_out, to ignore tmpdir for out_file/dir
  • Loading branch information
desultory authored Nov 14, 2024
2 parents 58d7934 + 38426e5 commit 762e7eb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Modules write to a shared config dict that is accessible by other modules.
* `find_libgcc` (true) Automatically locates libgcc using ldconfig -p and adds it to the initramfs.
* `out_dir` (initramfs_out) If relative, it will be placed under `tmpdir`, defines the output directory.
* `out_file` Sets the name of the output file, under `out_dir`.
* `force_out` (false) Forces out_file/dir to ignore the defined TMPDIR.
* `clean` (true) forces the build dir to be cleaned on each run.
* `old_count` (1) Sets the number of old file to keep when running the `_rotate_old` function.
* `binaries` - A list used to define programs to be pulled into the initrams. `which` is used to find the path of added entries, and `lddtree` is used to resolve dependendies.
Expand Down
10 changes: 8 additions & 2 deletions src/ugrd/base/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = "desultory"
__version__ = "3.10.1"
__version__ = "3.11.0"

from pathlib import Path
from typing import Union
Expand Down Expand Up @@ -190,10 +190,13 @@ def find_libgcc(self) -> None:

def _process_out_file(self, out_file: str) -> None:
"""Processes the out_file.
If set to the current directory, resolves and sets the out_dir to the current directory.
If it starts with './', resolves it to the current directory.
If it is a directory, sets the out_dir to the directory.
If out_file is a directory, sets the out_dir to the directory, stops processing.
If it's an absolute path, sets the out_dir to the parent directory.
otherise, force_out is enabled, sets the out_dir to the resolved parent directory.
"""
out_file = str(out_file)
if out_file == "./" or out_file == ".":
Expand All @@ -218,6 +221,9 @@ def _process_out_file(self, out_file: str) -> None:
self["out_dir"] = out_file.parent
self.logger.info("Resolved out_dir to: %s" % self["out_dir"])
out_file = out_file.name
elif self["force_out"]:
self["out_dir"] = out_file.parent.resolve()
out_file = out_file.name

self.data["out_file"] = out_file

Expand Down
3 changes: 2 additions & 1 deletion src/ugrd/base/core.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ paths = "NoDupFlatList" # Paths to be created in the initramfs
masks = "dict" # Imports to be masked in the initramfs
make_nodes = "bool" # If true, actual device nodes will be created in the build dir
out_dir = "Path" # The directory where the initramfs is packed/output. If no packer is used, this is the final output directory.
out_file = "str" # The name of the output file, if absolute, overrids out dir with the path, and sets out_file to the filename
out_file = "str" # The name of the output file, if absolute, overrides out dir with the path, and sets out_file to the filename
force_out = "bool" # If true, the out_dir will be interpreted literally, and the initramfs will be output there
old_count = "int" # The number of times to cycle old files before deleting
clean = "bool" # Add the clean property, used to define if the mounts should be cleaned up after boot
2 changes: 1 addition & 1 deletion src/ugrd/base/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_rootfs_name = 'ugrd-test-rootfs'
test_rootfs_build_dir = 'initramfs_test_rootfs'
test_image_size = 16

test_copy_config = ["mounts", "out_dir", "tmpdir", "clean", "test_image_size", "test_flag", "cryptsetup"]
test_copy_config = ["mounts", "force_out", "out_dir", "tmpdir", "clean", "test_image_size", "test_flag", "cryptsetup"]

test_memory = '256M'
test_cpu = 'host'
Expand Down
4 changes: 2 additions & 2 deletions src/ugrd/generator_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from zenlib.util import pretty_print

__version__ = "1.3.7"
__version__ = "1.3.8"
__author__ = "desultory"


Expand All @@ -25,7 +25,7 @@ class GeneratorHelpers:
def _get_out_path(self, path: Union[Path, str]) -> Path:
"""Takes a filename, if the out_dir is relative, returns the path relative to the tmpdir.
If the out_dir is absolute, returns the path relative to the out_dir."""
if self.out_dir.is_absolute():
if self.out_dir.is_absolute() or self.get("force_out"):
return get_subpath(self.out_dir, path)
return get_subpath(get_subpath(self.tmpdir, self.out_dir), path)

Expand Down
1 change: 1 addition & 0 deletions src/ugrd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def main():
"help": "Tests the image with qemu using a specific kernel file.",
},
{"flags": {"--livecd-label"}, "action": "store", "help": "Sets the label for the livecd"},
{"flags": ["--force-out"], "action": "store_true", "help": "Force set the output file/dir, do not use a tmpdir."},
{"flags": ["out_file"], "action": "store", "help": "set the output image location", "nargs": "?"},
]

Expand Down
14 changes: 14 additions & 0 deletions tests/test_output_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ def test_TMPDIR(self):
finally:
environ.pop("TMPDIR")

def test_force_out(self):
""" Tests force_out which ignores the tmpdir prefix """
out_file = "implied/relative/path/initrd"
generator = InitramfsGenerator(logger=self.logger, config="tests/fullauto.toml", out_file=out_file, force_out=True)
out_path = Path(out_file)
if out_path.exists():
self.fail(f"File {out_file} already exists")
generator.build()
self.assertTrue(out_path.exists())
out_path.unlink()
test_image = out_path.parent / generator["test_rootfs_name"]
self.assertTrue(test_image.exists())
test_image.unlink()


if __name__ == "__main__":
main()

0 comments on commit 762e7eb

Please sign in to comment.