diff --git a/mkdocs_simple_plugin/plugin.py b/mkdocs_simple_plugin/plugin.py index b3de6b52..a28782f1 100644 --- a/mkdocs_simple_plugin/plugin.py +++ b/mkdocs_simple_plugin/plugin.py @@ -117,6 +117,11 @@ class SimplePlugin(BasePlugin): # Otherwise, the build docs directory will be a temporary directory. ('build_docs_dir', config_options.Type(str, default='')), + # ### preserve_file_stats + # If true, when files are copied to local directories the file stats + # (eg last modified time) are preserved. + ("preserve_file_stats", config_options.Type(bool, default=False)), + # ### include_extensions # Any file in the searched directories whose name contains a string in # this list will simply be copied to the generated documentation. @@ -286,6 +291,7 @@ def on_config(self, config, **kwargs): config['docs_dir'] = self.build_docs_dir self.include_folders = self.config['include_folders'] self.ignore_folders = self.config['ignore_folders'] + self.preserve_file_stats = self.config["preserve_file_stats"] self.ignore_hidden = self.config['ignore_hidden'] self.include_extensions = utils.markdown_extensions + \ self.config['include_extensions'] @@ -383,7 +389,11 @@ def copy_file(self, from_directory, name, destination_directory): new_file = os.path.join(destination_directory, name) try: os.makedirs(destination_directory, exist_ok=True) - shutil.copy(original, new_file) + # If desired, use copy2 to preserve file stats + if self.preserve_file_stats: + shutil.copy2(original, new_file) + else: + shutil.copy(original, new_file) utils.log.debug("mkdocs-simple-plugin: {} --> {}".format( original, new_file)) return True @@ -410,6 +420,8 @@ def copy_docs_directory(self, root_source_directory, """Copy all files from source to destination directory.""" if sys.version_info >= (3, 8): # pylint: disable=unexpected-keyword-arg + # Note that copytree uses copy2 under the hood, and so by default + # file stats are preserved shutil.copytree(root_source_directory, root_destination_directory, dirs_exist_ok=True) @@ -426,7 +438,11 @@ def copy_docs_directory(self, root_source_directory, file_) if os.path.exists(destination_file): os.remove(destination_file) - shutil.copy(source_file, destination_directory) + # If desired, use copy2 to preserve file stats + if self.preserve_file_stats: + shutil.copy2(source_file, destination_directory) + else: + shutil.copy(source_file, destination_directory) utils.log.debug( "mkdocs-simple-plugin: {}/* --> {}/*".format( source_file, destination_file)) diff --git a/requirements.txt b/requirements.txt index deec1d71..807b2a09 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ flake8==4.0.1 mkdocs-awesome-pages-plugin==2.7.0 mkdocs-macros-plugin==0.6.4 mkdocs-material==8.2.6 -mkdocs==1.2.3 +mkdocs==1.2.4 mkdocstrings==0.18.1 pip-upgrader==1.4.15 pydocstyle==6.1.1