Skip to content

Commit e5a6929

Browse files
Update (#278)
* better copy check and bigger delay * refactor * update dependencies * fix type
1 parent 64788d3 commit e5a6929

File tree

4 files changed

+430
-919
lines changed

4 files changed

+430
-919
lines changed

namer/watchdog.py

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""
22
A file watching service to rename movie files and move them
3-
to relevant locations after match the file against the porndb.
3+
to relevant locations after match the file against the theporndb.
44
"""
55

6-
from contextlib import suppress
76
import os
87
import shutil
98
import sys
109
import tempfile
1110
import time
11+
from contextlib import suppress
1212
from pathlib import Path
13+
from platform import system
1314
from queue import Queue
1415
from threading import Thread
1516
from typing import Optional
@@ -19,15 +20,45 @@
1920
from watchdog.events import EVENT_TYPE_MODIFIED, EVENT_TYPE_MOVED, FileSystemEvent, PatternMatchingEventHandler
2021
from watchdog.observers.polling import PollingObserver
2122

23+
from namer.command import Command, gather_target_files_from_dir, is_interesting_movie, make_command_relative_to, move_command_files
2224
from namer.configuration import NamerConfig
2325
from namer.configuration_utils import verify_configuration
24-
from namer.command import gather_target_files_from_dir, is_interesting_movie, make_command_relative_to, move_command_files, Command
2526
from namer.metadataapi import get_user_info
2627
from namer.name_formatter import PartialFormatter
2728
from namer.namer import process_file
2829
from namer.web.server import NamerWebServer
2930

3031

32+
def __is_file_in_use_windows(file: Path):
33+
try:
34+
file.rename(file)
35+
except PermissionError:
36+
return True
37+
else:
38+
return False
39+
40+
41+
def __is_file_in_use_unix(file: Path):
42+
try:
43+
# pylint: disable=consider-using-with
44+
buffered_reader = open(file, mode='rb') # noqa: SIM115
45+
buffered_reader.close()
46+
except PermissionError:
47+
return True
48+
else:
49+
return False
50+
51+
52+
def is_file_in_use(file: Optional[Path]):
53+
if not file or not file.exists():
54+
return False
55+
56+
if system() == 'Windows':
57+
return __is_file_in_use_windows(file)
58+
else:
59+
return __is_file_in_use_unix(file)
60+
61+
3162
def done_copying(file: Optional[Path]) -> bool:
3263
"""
3364
Determines if a file is being copied by checking its size in 2 second
@@ -36,14 +67,8 @@ def done_copying(file: Optional[Path]) -> bool:
3667
if not file or not file.exists():
3768
return False
3869

39-
while True:
40-
try:
41-
# pylint: disable=consider-using-with
42-
buffered_reader = open(file, mode='rb') # noqa: SIM115
43-
buffered_reader.close()
44-
break
45-
except PermissionError:
46-
time.sleep(0.2)
70+
while is_file_in_use(file):
71+
time.sleep(2)
4772

4873
return True
4974

@@ -109,7 +134,7 @@ def on_any_event(self, event: FileSystemEvent):
109134
return
110135

111136
relative_path = str(path.relative_to(self.__namer_config.watch_dir))
112-
if not self.__namer_config.ignored_dir_regex.search(relative_path) and done_copying(path) and is_interesting_movie(path, self.__namer_config):
137+
if not self.__namer_config.ignored_dir_regex.search(relative_path) and is_interesting_movie(path, self.__namer_config) and done_copying(path):
113138
logger.info('watchdog process called for {}', relative_path)
114139

115140
# Extra wait time in case other files are copies in as well.
@@ -259,7 +284,7 @@ def start(self):
259284
return
260285

261286
relative_path = str(file.relative_to(self.__namer_config.watch_dir))
262-
if not self.__namer_config.ignored_dir_regex.search(relative_path) and done_copying(file) and is_interesting_movie(file, self.__namer_config):
287+
if not self.__namer_config.ignored_dir_regex.search(relative_path) and is_interesting_movie(file, self.__namer_config) and done_copying(file):
263288
self.__event_handler.prepare_file_for_processing(file)
264289

265290
def stop(self):
@@ -334,4 +359,3 @@ def main(config: NamerConfig):
334359
logger.add(sys.stdout, format=config.console_format, level=level, diagnose=config.diagnose_errors)
335360

336361
create_watcher(config).run()
337-

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
"@popperjs/core": "^2.11.8",
1616
"bootstrap": "^5.3.6",
1717
"bootstrap-icons": "^1.13.1",
18-
"datatables.net": "^2.3.0",
19-
"datatables.net-bs5": "^2.3.0",
18+
"datatables.net": "^2.3.1",
19+
"datatables.net-bs5": "^2.3.1",
2020
"datatables.net-buttons": "^3.2.3",
2121
"datatables.net-buttons-bs5": "^3.2.3",
2222
"datatables.net-colreorder": "^2.1.0",
2323
"datatables.net-colreorder-bs5": "^2.1.0",
24-
"datatables.net-fixedheader": "^4.0.1",
25-
"datatables.net-fixedheader-bs5": "^4.0.1",
24+
"datatables.net-fixedheader": "^4.0.2",
25+
"datatables.net-fixedheader-bs5": "^4.0.2",
2626
"datatables.net-responsive": "^3.0.4",
2727
"datatables.net-responsive-bs5": "^3.0.4",
2828
"jquery": "^3.7.1",
@@ -32,26 +32,26 @@
3232
"@babel/core": "^7.27.1",
3333
"@babel/preset-env": "^7.27.2",
3434
"@eslint/eslintrc": "^3.3.1",
35-
"@eslint/js": "^9.26.0",
35+
"@eslint/js": "^9.27.0",
3636
"babel-loader": "^10.0.0",
3737
"copy-webpack-plugin": "^13.0.0",
3838
"css-loader": "^7.1.2",
3939
"css-minimizer-webpack-plugin": "^7.0.2",
40-
"eslint": "^9.26.0",
40+
"eslint": "^9.27.0",
4141
"eslint-config-standard": "^17.1.0",
4242
"file-loader": "^6.2.0",
4343
"globals": "^16.1.0",
4444
"html-minimizer-webpack-plugin": "^5.0.2",
4545
"husky": "^9.1.7",
46-
"lint-staged": "^15.5.2",
46+
"lint-staged": "^16.0.0",
4747
"mini-css-extract-plugin": "^2.9.2",
4848
"postcss": "^8.5.3",
4949
"postcss-loader": "^8.1.1",
5050
"postcss-preset-env": "^10.1.6",
51-
"sass": "^1.87.0",
51+
"sass": "^1.89.0",
5252
"sass-loader": "^16.0.5",
5353
"terser-webpack-plugin": "^5.3.14",
54-
"webpack": "^5.99.8",
54+
"webpack": "^5.99.9",
5555
"webpack-cli": "^6.0.1"
5656
},
5757
"engines": {

0 commit comments

Comments
 (0)