Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to preserve file stats #1

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions mkdocs_simple_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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))
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down