From 9954a21e3fbcc2bb959bd4ecbc2327ab01f08289 Mon Sep 17 00:00:00 2001 From: Joey Territo Date: Sat, 22 Jul 2023 15:17:14 -0500 Subject: [PATCH] Follow symlinks when resolving a templated file --- dotdrop/templategen.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index 8e75f9d..eb3d784 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -179,8 +179,7 @@ class Templategen: """add a comment usually in the header of a dotfile""" return f'{prepend}{utils.header()}' - def _handle_file(self, src): - """generate the file content from template""" + def _get_filetype(self, src): try: # pylint: disable=C0415 import magic @@ -188,10 +187,22 @@ class Templategen: self.log.dbg('using \"magic\" for filetype identification') except ImportError: # fallback - _, filetype = utils.run(['file', '-b', '--mime-type', src], + _, filetype = utils.run(['file', '-L', '-b', '--mime-type', src], debug=self.debug) self.log.dbg('using \"file\" for filetype identification') filetype = filetype.strip() + if filetype == 'inode/symlink': + dst = os.readlink(src) + if dst[0] != '/': + # Canonicalize relative path + dst = os.path.join(os.path.dirname(src), dst) + return self._get_filetype(dst) + else: + return filetype + + def _handle_file(self, src): + """generate the file content from template""" + filetype = self._get_filetype(src) istext = self._is_text(filetype) self.log.dbg(f'filetype \"{src}\": {filetype}') self.log.dbg(f'is text \"{src}\": {istext}')