From bb0f0eeff57638df397d2c1b86dfd0410de065e9 Mon Sep 17 00:00:00 2001 From: Sacha Manson-Smith Date: Tue, 29 Mar 2022 20:16:33 +0100 Subject: [PATCH 1/8] Optionally preserve file stats on copy --- mkdocs_simple_plugin/plugin.py | 15 +++++++++++++-- requirements.txt | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/mkdocs_simple_plugin/plugin.py b/mkdocs_simple_plugin/plugin.py index b3de6b52..84ca2aa9 100644 --- a/mkdocs_simple_plugin/plugin.py +++ b/mkdocs_simple_plugin/plugin.py @@ -286,6 +286,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 +384,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 +415,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 +433,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 From 6b17904c54a146c1203ab1ab209e7a6252e0c58c Mon Sep 17 00:00:00 2001 From: Sacha Manson-Smith Date: Tue, 29 Mar 2022 21:00:11 +0100 Subject: [PATCH 2/8] Add missing config --- mkdocs_simple_plugin/plugin.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mkdocs_simple_plugin/plugin.py b/mkdocs_simple_plugin/plugin.py index 84ca2aa9..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. From c6fa48b53bbd882eaabd04c0d348da4a30d7855b Mon Sep 17 00:00:00 2001 From: Sacha Manson-Smith Date: Tue, 29 Mar 2022 21:43:08 +0100 Subject: [PATCH 3/8] Add root_dir option --- mkdocs_simple_plugin/plugin.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mkdocs_simple_plugin/plugin.py b/mkdocs_simple_plugin/plugin.py index a28782f1..ae68557c 100644 --- a/mkdocs_simple_plugin/plugin.py +++ b/mkdocs_simple_plugin/plugin.py @@ -122,6 +122,11 @@ class SimplePlugin(BasePlugin): # (eg last modified time) are preserved. ("preserve_file_stats", config_options.Type(bool, default=False)), + # ### root_dir + # If true, when files are copied to local directories the file stats + # (eg last modified time) are preserved. + ("root_dir", config_options.Type(str, default=".")), + # ### include_extensions # Any file in the searched directories whose name contains a string in # this list will simply be copied to the generated documentation. @@ -292,6 +297,7 @@ def on_config(self, config, **kwargs): self.include_folders = self.config['include_folders'] self.ignore_folders = self.config['ignore_folders'] self.preserve_file_stats = self.config["preserve_file_stats"] + self.root_dir = self.config["root_dir"] self.ignore_hidden = self.config['ignore_hidden'] self.include_extensions = utils.markdown_extensions + \ self.config['include_extensions'] @@ -367,7 +373,7 @@ def get_config_site_dir(self, config_file_path): def build_docs(self): """Build the docs directory from workspace files.""" paths = [] - for root, directories, files in os.walk("."): + for root, directories, files in os.walk(self.root_dir): if self.in_include_directory(root): document_root = self.build_docs_dir + root[1:] for f in files: From 9335af036b146da4eaed26e15fe81def1c3e406a Mon Sep 17 00:00:00 2001 From: Sacha Manson-Smith Date: Tue, 29 Mar 2022 21:49:02 +0100 Subject: [PATCH 4/8] Some logging --- mkdocs_simple_plugin/plugin.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mkdocs_simple_plugin/plugin.py b/mkdocs_simple_plugin/plugin.py index ae68557c..fbb82053 100644 --- a/mkdocs_simple_plugin/plugin.py +++ b/mkdocs_simple_plugin/plugin.py @@ -374,6 +374,8 @@ def build_docs(self): """Build the docs directory from workspace files.""" paths = [] for root, directories, files in os.walk(self.root_dir): + utils.log.info("mkdocs-simple-plugin: root is: {}".format( + root)) if self.in_include_directory(root): document_root = self.build_docs_dir + root[1:] for f in files: From af6db241484d3e029bb1f4a22f896a4bd5fd7cb6 Mon Sep 17 00:00:00 2001 From: Sacha Manson-Smith Date: Tue, 29 Mar 2022 21:58:52 +0100 Subject: [PATCH 5/8] More logging --- mkdocs_simple_plugin/plugin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mkdocs_simple_plugin/plugin.py b/mkdocs_simple_plugin/plugin.py index fbb82053..0b8af825 100644 --- a/mkdocs_simple_plugin/plugin.py +++ b/mkdocs_simple_plugin/plugin.py @@ -377,6 +377,7 @@ def build_docs(self): utils.log.info("mkdocs-simple-plugin: root is: {}".format( root)) if self.in_include_directory(root): + utils.log.info("mkdocs-simple-plugin: found match") document_root = self.build_docs_dir + root[1:] for f in files: copied = self.copy_file(root, f, document_root) From 6655b1a3d0f2c241bd623dc7a1f7b8606223e349 Mon Sep 17 00:00:00 2001 From: Sacha Manson-Smith Date: Mon, 4 Apr 2022 10:57:57 +0100 Subject: [PATCH 6/8] Remove extra logging --- mkdocs_simple_plugin/plugin.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/mkdocs_simple_plugin/plugin.py b/mkdocs_simple_plugin/plugin.py index 0b8af825..ae68557c 100644 --- a/mkdocs_simple_plugin/plugin.py +++ b/mkdocs_simple_plugin/plugin.py @@ -374,10 +374,7 @@ def build_docs(self): """Build the docs directory from workspace files.""" paths = [] for root, directories, files in os.walk(self.root_dir): - utils.log.info("mkdocs-simple-plugin: root is: {}".format( - root)) if self.in_include_directory(root): - utils.log.info("mkdocs-simple-plugin: found match") document_root = self.build_docs_dir + root[1:] for f in files: copied = self.copy_file(root, f, document_root) From 9993ec359db2ba90ea3592ab6a9ac68d993b3209 Mon Sep 17 00:00:00 2001 From: Sacha Manson-Smith Date: Mon, 4 Apr 2022 17:57:41 +0100 Subject: [PATCH 7/8] Remove root_dir --- mkdocs_simple_plugin/plugin.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/mkdocs_simple_plugin/plugin.py b/mkdocs_simple_plugin/plugin.py index ae68557c..0e9c23f7 100644 --- a/mkdocs_simple_plugin/plugin.py +++ b/mkdocs_simple_plugin/plugin.py @@ -122,11 +122,6 @@ class SimplePlugin(BasePlugin): # (eg last modified time) are preserved. ("preserve_file_stats", config_options.Type(bool, default=False)), - # ### root_dir - # If true, when files are copied to local directories the file stats - # (eg last modified time) are preserved. - ("root_dir", config_options.Type(str, default=".")), - # ### include_extensions # Any file in the searched directories whose name contains a string in # this list will simply be copied to the generated documentation. @@ -297,7 +292,6 @@ def on_config(self, config, **kwargs): self.include_folders = self.config['include_folders'] self.ignore_folders = self.config['ignore_folders'] self.preserve_file_stats = self.config["preserve_file_stats"] - self.root_dir = self.config["root_dir"] self.ignore_hidden = self.config['ignore_hidden'] self.include_extensions = utils.markdown_extensions + \ self.config['include_extensions'] @@ -373,7 +367,7 @@ def get_config_site_dir(self, config_file_path): def build_docs(self): """Build the docs directory from workspace files.""" paths = [] - for root, directories, files in os.walk(self.root_dir): + for root, directories, files in os.walk(.): if self.in_include_directory(root): document_root = self.build_docs_dir + root[1:] for f in files: From f55e19b4a56d2f17ac4c4bc372942b9fe4533890 Mon Sep 17 00:00:00 2001 From: Sacha Manson-Smith Date: Mon, 4 Apr 2022 18:03:54 +0100 Subject: [PATCH 8/8] Fix typo --- mkdocs_simple_plugin/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs_simple_plugin/plugin.py b/mkdocs_simple_plugin/plugin.py index 0e9c23f7..a28782f1 100644 --- a/mkdocs_simple_plugin/plugin.py +++ b/mkdocs_simple_plugin/plugin.py @@ -367,7 +367,7 @@ def get_config_site_dir(self, config_file_path): def build_docs(self): """Build the docs directory from workspace files.""" paths = [] - for root, directories, files in os.walk(.): + for root, directories, files in os.walk("."): if self.in_include_directory(root): document_root = self.build_docs_dir + root[1:] for f in files: