├── LICENSE ├── README.md ├── examples ├── Downloads.yaml └── Stuff.yaml ├── filemaid.py └── requirements.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Chris Braun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Filemaid 2 | 3 | Rule-based file- and folder-organization tool inspired by 4 | [Belvedere](https://lifehacker.com/341950/belvedere-automates-your-self-cleaning-pc) 5 | and similar programs such as [Hazel](https://www.noodlesoft.com/) and 6 | [DropIt](http://www.dropitproject.com/). 7 | 8 | *Be careful, this is a mostly untested script -- don't run it on 9 | important folders and files.* 10 | 11 | It is recommended to take a look at the source code (a single file) for 12 | now to get a better grasp on Filemaid's capabilities. 13 | 14 | 15 | ## Configuration 16 | Configuration is done through YAML files. Some configurations to get you 17 | started can be found in the examples folder. 18 | -------------------------------------------------------------------------------- /examples/Downloads.yaml: -------------------------------------------------------------------------------- 1 | - Pictures: 2 | condition: 3 | all: 4 | - mime: image/.* 5 | actions: 6 | - move: ~/Downloads/Pictures 7 | 8 | - Videos: 9 | condition: 10 | all: 11 | - mime: video/.* 12 | actions: 13 | - move: ~/Downloads/Videos 14 | 15 | - Audio: 16 | condition: 17 | all: 18 | - mime: audio/.* 19 | actions: 20 | - move: ~/Downloads/Audio 21 | 22 | - Other: 23 | condition: 24 | all: 25 | - mime: application/.* 26 | actions: 27 | - move: ~/Downloads/Other 28 | -------------------------------------------------------------------------------- /examples/Stuff.yaml: -------------------------------------------------------------------------------- 1 | - 1 month ago: 2 | condition: 3 | age: '>= 4 weeks' 4 | actions: 5 | - move: ~/Stuff/1 month ago 6 | 7 | - 3 weeks ago: 8 | condition: 9 | age: '>= 3 weeks' 10 | actions: 11 | - move: ~/Stuff/3 weeks ago 12 | 13 | - 2 weeks ago: 14 | condition: 15 | age: '>= 2 weeks' 16 | actions: 17 | - move: ~/Stuff/2 weeks ago 18 | 19 | - 1 week ago: 20 | condition: 21 | age: '>= 1 weeks' 22 | actions: 23 | - move: ~/Stuff/1 week ago 24 | -------------------------------------------------------------------------------- /filemaid.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import abc 4 | import argparse 5 | import functools 6 | import itertools 7 | import operator 8 | import os 9 | import re 10 | import shutil 11 | import sys 12 | import textwrap 13 | from datetime import datetime, timedelta 14 | 15 | import magic 16 | import yaml 17 | 18 | SUCCESS_EXIT_STATUS = 0 19 | FAILURE_EXIT_STATUS = 1 20 | ENCODING = 'UTF-8' 21 | 22 | argument_parser = argparse.ArgumentParser() 23 | argument_parser.add_argument('rules') 24 | argument_parser.add_argument('path') 25 | argument_parser.add_argument('--dry-run', '-d', action='store_true') 26 | argument_parser.add_argument('--recursive', '-r', action='store_true') 27 | argument_parser.add_argument('--follow-symlinks', '-s', action='store_true') 28 | 29 | 30 | class ConditionError(Exception): 31 | pass 32 | 33 | 34 | class ActionError(Exception): 35 | pass 36 | 37 | 38 | class Matchable(metaclass=abc.ABCMeta): 39 | @abc.abstractmethod 40 | def match(self, path): 41 | pass 42 | 43 | 44 | class Rule(Matchable): 45 | def __init__(self, name, condition, actions): 46 | self.name = name 47 | self.condition = condition 48 | self.actions = actions 49 | 50 | # Accumulate all action ignore paths to prevent Filemaid applying rules on action destination folders and such 51 | self.ignore_paths = set(itertools.chain(*(action.ignore_paths for action in actions))) 52 | 53 | def match(self, path): 54 | return self.condition.match(path) 55 | 56 | def apply(self, path): 57 | for action in self.actions: 58 | path = action.apply(path) or path 59 | 60 | return path 61 | 62 | def __repr__(self): 63 | condition_repr = repr(self.condition) 64 | actions_repr = repr(self.actions) 65 | return f'Rule(\n{textwrap.indent(condition_repr, " ")},\n{textwrap.indent(actions_repr, " ")}\n)' 66 | 67 | 68 | class BaseTermCondition(Matchable): 69 | def __init__(self, *condition_data): 70 | super().__init__() 71 | self.conditions = [make_condition(datum) for datum in condition_data] 72 | 73 | def __repr__(self): 74 | condition_reprs = ',\n'.join(repr(condition) for condition in self.conditions) 75 | return f'{self.__class__.__name__}(\n{textwrap.indent(condition_reprs, " ")}\n)' 76 | 77 | 78 | class AllCondition(BaseTermCondition): 79 | def match(self, path): 80 | return all(condition.match(path) for condition in self.conditions) 81 | 82 | 83 | class AnyCondition(BaseTermCondition): 84 | def match(self, path): 85 | return any(condition.match(path) for condition in self.conditions) 86 | 87 | 88 | class NotCondition(Matchable): 89 | def __init__(self, condition_datum): 90 | super().__init__() 91 | self.condition = make_condition(condition_datum) 92 | 93 | def match(self, path): 94 | return not self.condition.match(path) 95 | 96 | def __repr__(self): 97 | return f'NotCondition({repr(self.condition)})' 98 | 99 | 100 | class PathCondition(Matchable): 101 | def __init__(self, regex): 102 | super().__init__() 103 | self.regex = re.compile(regex) 104 | 105 | def match(self, path): 106 | return bool(self.regex.match(path)) 107 | 108 | def __repr__(self): 109 | return f'PathCondition({repr(self.regex.pattern)})' 110 | 111 | 112 | class MimeCondition(Matchable): 113 | def __init__(self, regex, ignore_case=True, magic_bytes=1024): 114 | super().__init__() 115 | self.regex = re.compile(regex, re.IGNORECASE if ignore_case else 0) 116 | self.magic_bytes = magic_bytes 117 | 118 | @functools.lru_cache(None) 119 | def match(self, path): 120 | if not os.path.isfile(path): 121 | return False 122 | 123 | with open(path, 'rb') as file: 124 | buffer = file.read(self.magic_bytes) 125 | mime = magic.from_buffer(buffer, mime=True) 126 | 127 | return bool(self.regex.match(mime)) 128 | 129 | def __repr__(self): 130 | return f'MimeCondition({repr(self.regex.pattern)}, {repr(self.magic_bytes)})' 131 | 132 | 133 | class AgeCondition(Matchable): 134 | UNITS = {'seconds', 'minutes', 'hours', 'days', 'weeks'} 135 | COMPARATORS = { 136 | '>': operator.gt, 137 | '>=': operator.ge, 138 | '=': operator.eq, 139 | '<=': operator.le, 140 | '<': operator.lt 141 | } 142 | 143 | def __init__(self, condition_string): 144 | self.condition_string = condition_string 145 | self.age_predicate = self.parse_age_condition(condition_string) 146 | 147 | def parse_age_condition(self, condition_string): 148 | comparator_string, size_string, unit_string = condition_string.split() 149 | compare = self.COMPARATORS[comparator_string] 150 | kwargs = {unit_string.lower(): int(size_string)} 151 | time_delta = timedelta(**kwargs) 152 | return lambda other: compare(datetime.now() - other, time_delta) 153 | 154 | def match(self, path): 155 | stat = os.stat(path) 156 | date_time = datetime.fromtimestamp(stat.st_mtime) 157 | return self.age_predicate(date_time) 158 | 159 | def __repr__(self): 160 | return f'AgeCondition({repr(self.condition_string)})' 161 | 162 | 163 | class SizeCondition(Matchable): 164 | UNITS = { 165 | 'b': 1, 166 | 'kb': 1024, 167 | 'mb': 1024 ** 2, 168 | 'gb': 1024 ** 3, 169 | 'tb': 1024 ** 4, 170 | 'kib': 1000, 171 | 'mib': 1000 ** 2, 172 | 'gib': 1000 ** 3, 173 | 'tib': 1000 ** 4 174 | } 175 | COMPARATORS = { 176 | '>': operator.gt, 177 | '>=': operator.ge, 178 | '=': operator.eq, 179 | '<=': operator.le, 180 | '<': operator.lt 181 | } 182 | 183 | def __init__(self, size): 184 | self.size_string = size 185 | self.size_predicate = self.parse_size_condition(size) 186 | 187 | def parse_size_condition(self, condition_string): 188 | comparator_string, size_string, unit_string = condition_string.split() 189 | size = self.UNITS[unit_string.lower()] * float(size_string) 190 | compare = self.COMPARATORS[comparator_string] 191 | return lambda other: compare(other, size) 192 | 193 | def match(self, path): 194 | stat = os.stat(path) 195 | return self.size_predicate(stat.st_size) 196 | 197 | def __repr__(self): 198 | return f'SizeCondition({repr(self.size_string)})' 199 | 200 | 201 | class BaseAction(metaclass=abc.ABCMeta): 202 | def __init__(self): 203 | self.ignore_paths = set() 204 | 205 | @abc.abstractmethod 206 | def apply(self, path): 207 | # return the new path if modified 208 | pass 209 | 210 | def __repr__(self): 211 | return f'{self.__class__.__name__}()' 212 | 213 | 214 | class MoveAction(BaseAction): 215 | def __init__(self, destination): 216 | super().__init__() 217 | self.destination = os.path.expanduser(destination) 218 | self.ignore_paths.add(self.destination) 219 | 220 | def apply(self, path): 221 | os.makedirs(self.destination, exist_ok=True) 222 | return shutil.move(path, self.destination) 223 | 224 | def __repr__(self): 225 | return f'MoveAction({repr(self.destination)})' 226 | 227 | 228 | class CopyAction(BaseAction): 229 | def __init__(self, destination): 230 | super().__init__() 231 | self.destination = os.path.expanduser(destination) 232 | self.ignore_paths.add(self.destination) 233 | 234 | def apply(self, path): 235 | os.makedirs(self.destination, exist_ok=True) 236 | shutil.copy2(path, self.destination) 237 | 238 | def __repr__(self): 239 | return f'CopyAction({repr(self.destination)})' 240 | 241 | 242 | class DeleteAction(BaseAction): 243 | def apply(self, path): 244 | os.remove(path) 245 | 246 | 247 | CONDITIONS = { 248 | 'all': AllCondition, 249 | 'any': AnyCondition, 250 | 'not': NotCondition, 251 | 'path': PathCondition, 252 | 'mime': MimeCondition, 253 | 'age': AgeCondition, 254 | 'size': SizeCondition 255 | } 256 | 257 | ACTIONS = { 258 | 'move': MoveAction, 259 | 'copy': CopyAction, 260 | 'delete': DeleteAction 261 | } 262 | 263 | 264 | def make_condition(data): 265 | if isinstance(data, dict): 266 | type_, data = list(data.items())[0] 267 | else: 268 | type_, data = data, [] 269 | 270 | class_ = CONDITIONS.get(type_) 271 | if not class_: 272 | raise ConditionError(f'unknown type: {type_}') 273 | 274 | # arguments are keyword-arguments 275 | if isinstance(data, dict): 276 | return class_(**data) 277 | 278 | # arguments are positonal arguments 279 | if isinstance(data, list): 280 | return class_(*data) 281 | 282 | # a single argument 283 | return class_(data) 284 | 285 | 286 | def make_actions(data): 287 | actions = [] 288 | for datum in data: 289 | if isinstance(datum, dict): 290 | type_, datum = list(datum.items())[0] 291 | else: 292 | type_, datum = datum, [] 293 | 294 | class_ = ACTIONS.get(type_) 295 | if not class_: 296 | raise ActionError(f'unknown type: {type_}') 297 | 298 | # arguments are keyword-arguments 299 | if isinstance(datum, dict): 300 | action = class_(**datum) 301 | # arguments are positonal arguments 302 | elif isinstance(datum, list): 303 | action = class_(*datum) 304 | # a single argument 305 | else: 306 | action = class_(datum) 307 | actions.append(action) 308 | 309 | return actions 310 | 311 | 312 | def make_rule(data): 313 | name, data = list(data.items())[0] 314 | condition = make_condition(data['condition']) 315 | actions = make_actions(data['actions']) 316 | return Rule(name, condition, actions) 317 | 318 | 319 | def load_rules(path): 320 | with open(path, encoding=ENCODING) as file: 321 | config = yaml.load(file) 322 | 323 | # Positional priority 324 | return [make_rule(data) for data in config] 325 | 326 | 327 | def find_paths(path, predicate=lambda path: True, recursive=True, follow_symlinks=False): 328 | for root, directories, file_names in os.walk(path, followlinks=follow_symlinks): 329 | for name in itertools.chain(directories, file_names, [root]): 330 | path = os.path.join(root, name) 331 | if predicate(path): 332 | yield path 333 | 334 | if not recursive: 335 | break 336 | 337 | 338 | def main(arguments): 339 | if not os.path.isfile(arguments.rules): 340 | print(f'No such file: {arguments.rules}', file=sys.stderr) 341 | return FAILURE_EXIT_STATUS 342 | 343 | if not os.path.isdir(arguments.path): 344 | print(f'No such folder: {arguments.path}', file=sys.stderr) 345 | return FAILURE_EXIT_STATUS 346 | 347 | rules = load_rules(arguments.rules) 348 | paths = find_paths(arguments.path, recursive=arguments.recursive, follow_symlinks=arguments.follow_symlinks) 349 | ignore_paths = {os.path.abspath(arguments.path)} 350 | ignore_paths.update(itertools.chain(*(rule.ignore_paths for rule in rules))) 351 | for path in (path for path in paths if path not in ignore_paths): 352 | for rule in rules: 353 | if rule.match(path): 354 | if arguments.dry_run: 355 | print(f'{rule.name}: {path}') 356 | continue 357 | 358 | # Potentially updated path, not used as of yet 359 | path = rule.apply(path) 360 | # Break after the first rule matched. Priority is given by order of appearance 361 | break 362 | 363 | return SUCCESS_EXIT_STATUS 364 | 365 | 366 | if __name__ == '__main__': 367 | arguments = argument_parser.parse_args() 368 | argument_parser.exit(main(arguments)) 369 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | python-magic==0.4.13 2 | PyYAML==3.12 3 | --------------------------------------------------------------------------------