builder: build: filesystem.py: add source for copy local files to rootfs

Signed-off-by: BigfootACA <bigfoot@classfun.cn>
This commit is contained in:
BigfootACA 2024-07-21 17:25:00 +08:00
parent 38447f1dd7
commit 8ed06d8f8b

View File

@ -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,6 +114,7 @@ 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 "content" in file:
if not follow and os.path.exists(real): os.remove(real) if not follow and os.path.exists(real): os.remove(real)
log.debug(f"create file {real}") log.debug(f"create file {real}")
with open(real, "wb") as f: with open(real, "wb") as f:
@ -120,6 +124,17 @@ def add_file(ctx: ArchBuilderContext, file: dict):
real, content.strip() 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}")