mirror of
https://github.com/BigfootACA/arch-image-builder.git
synced 2024-09-21 14:00:55 +08:00
builder: build: filesystem.py: add source for copy local files to rootfs
Signed-off-by: BigfootACA <bigfoot@classfun.cn>
This commit is contained in:
parent
38447f1dd7
commit
8ed06d8f8b
@ -87,8 +87,8 @@ def add_file(ctx: ArchBuilderContext, file: dict):
|
|||||||
# at least path content
|
# at least path content
|
||||||
if "path" not in file:
|
if "path" not in file:
|
||||||
raise ArchBuilderConfigError("no path set in file")
|
raise ArchBuilderConfigError("no path set in file")
|
||||||
if "content" not in file:
|
if "content" not in file and "source" not in file:
|
||||||
raise ArchBuilderConfigError("no content set in file")
|
raise ArchBuilderConfigError("no content or source set in file")
|
||||||
root = ctx.get_rootfs()
|
root = ctx.get_rootfs()
|
||||||
path: str = file["path"]
|
path: str = file["path"]
|
||||||
if path.startswith("/"): path = path[1:]
|
if path.startswith("/"): path = path[1:]
|
||||||
@ -100,6 +100,9 @@ def add_file(ctx: ArchBuilderContext, file: dict):
|
|||||||
# follow symbolic links
|
# follow symbolic links
|
||||||
follow = file["follow"] if "follow" in file else True
|
follow = file["follow"] if "follow" in file else True
|
||||||
|
|
||||||
|
# source is a folder
|
||||||
|
folder = file["folder"] if "folder" in file else False
|
||||||
|
|
||||||
# files mode
|
# files mode
|
||||||
mode = int(file["mode"]) if "mode" in file else 0o0644
|
mode = int(file["mode"]) if "mode" in file else 0o0644
|
||||||
|
|
||||||
@ -111,15 +114,27 @@ def add_file(ctx: ArchBuilderContext, file: dict):
|
|||||||
# resolve to rootfs
|
# resolve to rootfs
|
||||||
real = os.path.join(root, path)
|
real = os.path.join(root, path)
|
||||||
|
|
||||||
if not follow and os.path.exists(real): os.remove(real)
|
if "content" in file:
|
||||||
log.debug(f"create file {real}")
|
if not follow and os.path.exists(real): os.remove(real)
|
||||||
with open(real, "wb") as f:
|
log.debug(f"create file {real}")
|
||||||
content: str = file["content"]
|
with open(real, "wb") as f:
|
||||||
log.debug(
|
content: str = file["content"]
|
||||||
"write to %s with %s",
|
log.debug(
|
||||||
real, content.strip()
|
"write to %s with %s",
|
||||||
)
|
real, content.strip()
|
||||||
f.write(content.encode(encode))
|
)
|
||||||
|
f.write(content.encode(encode))
|
||||||
|
elif "source" in file:
|
||||||
|
src: str = file["source"]
|
||||||
|
if not src.startswith("/"):
|
||||||
|
src = os.path.join(ctx.dir, src)
|
||||||
|
log.debug(f"copy {src} to {real}")
|
||||||
|
if folder:
|
||||||
|
shutil.copytree(src, real, symlinks=follow)
|
||||||
|
else:
|
||||||
|
shutil.copyfile(src, real, follow_symlinks=follow)
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
log.debug(f"chmod file {real} to {mode:04o}")
|
log.debug(f"chmod file {real} to {mode:04o}")
|
||||||
os.chmod(real, mode=mode)
|
os.chmod(real, mode=mode)
|
||||||
log.debug(f"chown file {real} to {uid}:{gid}")
|
log.debug(f"chown file {real} to {uid}:{gid}")
|
||||||
|
Loading…
Reference in New Issue
Block a user