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

feat: add ignore option #131

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ drive:
- "png"
- "jpg"
- "jpeg"
ignore:
- "node_modules"
- "*.md"
photos:
destination: "photos"
remove_obsolete: false
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
icloudpy==0.3.3
ruamel.yaml==0.16.12
python-magic==0.4.27
python-magic==0.4.27
requests~=2.28.1
pathspec~=0.11.0
27 changes: 21 additions & 6 deletions src/sync_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import re
import time
from pathspec import PathSpec
from pathlib import Path
from shutil import copyfileobj, rmtree, unpack_archive

Expand All @@ -14,10 +15,14 @@
from src import LOGGER, config_parser


def wanted_file(filters, file_path):
def wanted_file(filters, ignore, file_path):
"""Check if file is wanted."""
if not file_path:
return False
if ignore:
if PathSpec.from_lines("gitwildmatch", ignore).match_file(file_path):
LOGGER.debug(f"Skipping the unwanted file {file_path}")
return False
if not filters or len(filters) == 0:
return True
for file_extension in filters:
Expand All @@ -27,8 +32,11 @@ def wanted_file(filters, file_path):
return False


def wanted_folder(filters, root, folder_path):
def wanted_folder(filters, ignore, root, folder_path):
"""Check if folder is wanted."""
if ignore:
if PathSpec.from_lines("gitwildmatch", ignore).match_file(f"{folder_path}/foo.bar"):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f"{folder_path}/foo.bar" shouldn't this be folder_path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary 😕. If you remove /foo.bar the tests will not pass
If you have another suggestion, I can try it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like cpburnz/python-pathspec#65 is causing the tests to fail. Seems like pathspec isn't the right fit for our needs. 😕

It might be easier to use something like https://pymotw.com/3/glob/ or our own implementation to check if entries in ignore list matches the path and then return False to ignore it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glob is useful to find filenames, but do not seems helpful for testing path

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like glob internally uses fnmatch. fnmatch seems to be a good fit for this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just tried fnmatch it's the same problem

return False
if not filters or not folder_path or not root or len(filters) == 0:
# Nothing to filter, return True
return True
Expand Down Expand Up @@ -65,12 +73,12 @@ def wanted_parent_folder(filters, root, folder_path):
return False


def process_folder(item, destination_path, filters, root):
def process_folder(item, destination_path, filters, ignore, root):
"""Process the given folder."""
if not (item and destination_path and root):
return None
new_directory = os.path.join(destination_path, item.name)
if not wanted_folder(filters=filters, folder_path=new_directory, root=root):
if not wanted_folder(filters=filters, ignore=ignore, folder_path=new_directory, root=root):
LOGGER.debug(f"Skipping the unwanted folder {new_directory} ...")
return None
os.makedirs(new_directory, exist_ok=True)
Expand Down Expand Up @@ -188,12 +196,12 @@ def download_file(item, local_file):
return True


def process_file(item, destination_path, filters, files):
def process_file(item, destination_path, filters, ignore, files):
"""Process given item as file."""
if not (item and destination_path and files is not None):
return False
local_file = os.path.join(destination_path, item.name)
if not wanted_file(filters=filters, file_path=local_file):
if not wanted_file(filters=filters, ignore=ignore, file_path=local_file):
return False
files.add(local_file)
item_is_package = is_package(item=item)
Expand Down Expand Up @@ -236,6 +244,7 @@ def sync_directory(
root,
top=True,
filters=None,
ignore=None,
remove=False,
):
"""Sync folder."""
Expand All @@ -250,6 +259,7 @@ def sync_directory(
filters=filters["folders"]
if filters and "folders" in filters
else None,
ignore=ignore,
root=root,
)
if not new_folder:
Expand All @@ -264,6 +274,7 @@ def sync_directory(
root=root,
top=False,
filters=filters,
ignore=ignore,
)
)
except Exception:
Expand All @@ -284,6 +295,7 @@ def sync_directory(
filters=filters["file_extensions"]
if filters and "file_extensions" in filters
else None,
ignore=ignore,
files=files,
)
except Exception:
Expand All @@ -306,5 +318,8 @@ def sync_drive(config, drive):
filters=config["drive"]["filters"]
if "drive" in config and "filters" in config["drive"]
else None,
ignore=config["drive"]["ignore"]
if "drive" in config and "ignore" in config["drive"]
else None,
remove=config_parser.get_drive_remove_obsolete(config=config),
)
3 changes: 3 additions & 0 deletions tests/data/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ drive:
destination: "./drive"
remove_obsolete: true
sync_interval: -1
ignore:
- "*.psd"
- .git/
filters:
# File filters to be included in syncing iCloud drive content
folders:
Expand Down