# # Module to record our understanding of everything on disk. # # A DVD root holds one directory per disc. The concrete video files # (.mp4, .mkv, ...) live in that directory. Each Plex library holds # symlinks to those files, not copies. # # A directory may be retitled by a symlink beside it in the same root: # the directory is "abc" and "Superman" is a symlink to it. The title # is that symlink's name when one exists, otherwise the directory name. # # VIDEO_TS is the disc data. When that name is a symlink, the data has # already been moved to the archive: the disc is offloaded. We only # detect that. Nothing here moves files. # # asyncinotify keeps this picture current. # import asyncio import errno import logging import os from pathlib import Path import asyncinotify _LOGGER = logging.getLogger(__name__) # One below INFO. While main.py is reading the discs already on disk, the # disk logger sits at INFO, so these stay quiet. After that it sits at # DEBUG, and the same messages show for later changes. SETUP = logging.INFO - 1 logging.addLevelName(SETUP, 'SETUP') # The video extensions we'll look for. VIDEO_EXT = { '.mp4', '.m4v', '.mkv', } # Flags for changes on the disk. _DEPARTED = ( asyncinotify.Mask.DELETE | asyncinotify.Mask.MOVED_FROM ) _ARRIVED = ( asyncinotify.Mask.CREATE | asyncinotify.Mask.MOVED_TO ) class DVDRoot: def __init__(self, rootdir, plex): rootdir = Path(rootdir) assert rootdir.is_absolute() self.rootdir = rootdir self.plex = plex self.dvds = { } # path: DVDDirectory self.by_real = { } # realpath: DVDDirectory self.title_links = { } # symlink name -> DVDDirectory, or a real path if the disc is not here yet self.inotify = asyncinotify.Inotify() self.watches = { } # path: asyncinotify.Watch # The rescan runner creates the Event once it is inside the loop. self.rescan = None # Watch the root video directory, to see new DVDs arriving. self.watches[rootdir] = self.inotify.add_watch(rootdir, _DEPARTED | _ARRIVED) _LOGGER.log(SETUP, f'Watching "{rootdir}" for changes') pending = [ ] for entry in self.rootdir.iterdir(): # Dot-directories, such as .METADATA, are not discs. if entry.name.startswith('.'): continue # A symlink is a title for a directory, not a directory of its own. if entry.is_symlink(): pending.append(entry) continue # follow_symlinks arrived on Path.is_dir() in 3.12. Production is # 3.10, and the check above has already set symlinks aside. if entry.is_dir(): self._add_dvd(entry.name, entry) for entry in pending: self._note_symlink(entry.name, entry) _LOGGER.log(SETUP, f'Watching {len(self.dvds)} DVD subdirs for changes') def _add_dvd(self, dirname, path): dvd = DVDDirectory(dirname, path, self.plex) real = path.resolve() self.dvds[path] = dvd self.by_real[real] = dvd self.watches[path] = self.inotify.add_watch(path, _DEPARTED | _ARRIVED) # A title symlink may have arrived before this directory. for name, target in list(self.title_links.items()): if target == real: self._attach_title(name, dvd) async def rescan_runner(self): "Recompute each disc's reason. Callers set self.rescan to start a pass." self.rescan = asyncio.Event() while True: await self.rescan.wait() self.rescan.clear() for dvd in self.dvds.values(): dvd.reconsider() def _note_symlink(self, name, path): "A title symlink exists. Remember which disc it names." path = Path(path) try: target = path.resolve() except OSError: return dvd = self.by_real.get(target) if dvd: self._attach_title(name, dvd) else: self.title_links[name] = target def _attach_title(self, name, dvd): previous = self.title_links.get(name) if previous is not dvd and isinstance(previous, DVDDirectory): previous.drop_title(name) self.title_links[name] = dvd dvd.add_title(name) def _forget_symlink(self, name): previous = self.title_links.pop(name, None) if isinstance(previous, DVDDirectory): previous.drop_title(name) def _drop_dvd(self, path): dvd = self.dvds.pop(path) real = path.resolve() self.by_real.pop(real, None) # The symlink is still on disk. Remember it as unresolved so a # directory that reappears under the same path picks the title up. for name, owner in list(self.title_links.items()): if owner is dvd: self.title_links[name] = real dvd.dvd_removed() return dvd async def watch_dirs(self): "Run forever, to watch for changes in the root and DVD dirs." rootwatch = self.watches[self.rootdir] ### hack in a little bit of exception handling. Fix upstream ### to better handle exceptions. try: with self.inotify: async for event in self.inotify: path = event.path _LOGGER.debug(f'Event {event.mask:x} at: "{path}"') if event.mask & asyncinotify.Mask.IGNORED: # We don't need to see these; messes with the logic. continue name = path.name #print('WATCH PATH:', repr(event.watch.path)) #print('ROOTDIR:', repr(self.rootdir)) #print('WATCHER:', event.watch) if event.watch is rootwatch: # Change in the root directory (the set of DVDs). if event.mask & _ARRIVED: # One lstat: inotify has no symlink bit. if path.is_symlink(): self._note_symlink(name, path) continue if not (event.mask & asyncinotify.Mask.ISDIR): _LOGGER.warning(f'Non-directory appeared in DVDROOT: "{name}"') continue _LOGGER.info(f'New DVD: "{name}"') self._add_dvd(name, path) elif event.mask & _DEPARTED: if path not in self.dvds: # A title symlink left, or a file we do not track. self._forget_symlink(name) continue _LOGGER.info(f'DVD directory removed: "{name}"') self._drop_dvd(path) # NOTE: no need to call .rm_watch() as that happens # automatically when the directory is deleted. else: # Change within a DVD's directory. dirname = path.parent dvd = self.dvds.get(dirname) if not dvd: _LOGGER.error(f'where did "{dirname}" go??') #print('EVENT:', event) continue if event.mask & _ARRIVED: dvd.name_arrive(name) elif event.mask & _DEPARTED: dvd.name_depart(name) except Exception as e: #print('EXCEPTION:', repr(e)) import traceback traceback.print_exception(e) _LOGGER.error(f'EXIT: watch_dirs() for "{self.rootdir}"') class DVDDirectory: def __init__(self, dirname, dirpath, plex): self.dirname = dirname # the directory's own name self.titles = set() # sibling symlinks that retitle this disc self.dvddir = dirpath self.plex = plex self.lib = plex.default_lib # unless one library marker is present self.streams = set() # video filenames found here self.offloaded = False # VIDEO_TS is a symlink to the archive # One reason, or empty. A rescan replaces it from the names on disk. self.reason = '' # The library marker is applied before any video, so the links # are created in that library rather than the default. fnames = {entry.name for entry in self.dvddir.iterdir()} markers = sorted(name for name in fnames if name in self.plex.libs) if len(markers) == 1: self.lib = markers[0] for fname in fnames: if fname not in self.plex.libs: self.name_arrive(fname) # name_arrive keeps the latest reason. The listing can hold more # than one, so the priority pass replaces that. self.reconsider() def add_title(self, name): self.titles.add(name) def drop_title(self, name): self.titles.discard(name) @property def name(self): "Name to show: a retitling symlink, or the directory name." return sorted(self.titles)[0] if self.titles else self.dirname @property def flagged(self): "True when a reason is stored. The media filter reads this." return bool(self.reason) def reconsider(self): "Set the one reason from the names in this directory right now." # Multiple libraries outrank a stray file. A departure is not a # reason: the listing no longer contains the name that left. fnames = {entry.name for entry in self.dvddir.iterdir()} markers = sorted(name for name in fnames if name in self.plex.libs) if len(markers) > 1: self.reason = 'multiple libraries: ' + ', '.join(markers) return stray = sorted( name for name in fnames if name not in self.plex.libs and name != 'VIDEO_TS' and not is_video(self.dvddir / name) ) self.reason = f'stray file: {stray[0]}' if stray else '' def name_arrive(self, name): "A new name has arrived in this Directory." if name in self.plex.libs: # Replacing the default is the explicit choice. A second # marker, different from the one already chosen, is an error. if self.lib != self.plex.default_lib and name != self.lib: self.reason = f'second library marker: {name}' return self.lib = name self.plex.library_added(self, name) elif is_video(self.dvddir / name): self.streams.add(name) self.plex.stream_added(self, name) elif name == 'VIDEO_TS': # A symlink means the disc data already lives on the archive. self.offloaded = (self.dvddir / name).is_symlink() else: # There is a stray file here. A later rescan may replace this # with a higher-priority reason. self.reason = f'stray file: {name}' def name_depart(self, name): "A name has departed from this Directory." # Not a reason. A rescan cannot see a name that has already left, # and the Plex symlink is the record of a video that left. if name == 'VIDEO_TS': self.offloaded = False def dvd_removed(self): "The DVD directory has been removed, so remove all streams." ### this probably doesn't work. The DVDDirectory is being ### deleted. ... think more whether to bother, or just ### let this object disappear. ### maybe log a message in our activity stream? def is_video(fname): return Path(fname).suffix in VIDEO_EXT def create_symlink(stream, link): "Create LINK pointing to STREAM, retaining mtime." stream = Path(stream) link = Path(link) stat = stream.stat() link.symlink_to(stream) # Path has no utime. follow_symlinks=False sets the link's own times. os.utime(link, ns=(stat.st_atime_ns, stat.st_mtime_ns), follow_symlinks=False) class PlexLibraries: # Status values for a symlink in a Plex library. MISSING = 'missing' # nothing is present MATCHES = 'matches' # a link pointing to expected target DIFFERS = 'differs' # a link pointing to unexpected target NOTLINK = 'notlink' # something besides a link is present def __init__(self, rootdir, libs): # Parent directory for each library directory. rootdir = Path(rootdir) assert rootdir.is_absolute() self.rootdir = rootdir # Stash away the library information. self.default_lib = libs[0] self.libs = set(libs) _LOGGER.info(f'Known libraries: {libs}') def scrub_libs(self, dvdroots): # Review each of the Plex libraries for crappy. _LOGGER.debug('DISABLED: no scrubbing.') ### maybe flag some DVDDirectories for review? def clean_lib(self, lib, dvdroots): "Clean out LIB for anything not in DVDROOTS." _LOGGER.debug('DISABLED: no cleaning.') ### maybe flag some DVDDirectories for review? def path_status(self, lib, name, expect): expect = Path(expect) assert expect.is_absolute() path = self.rootdir / lib / name try: target = path.readlink() except FileNotFoundError: return path, self.MISSING except OSError as e: if e.errno == errno.EINVAL: _LOGGER.warning(f'Not a symlink: "{lib}/{name}"') return path, self.NOTLINK raise # EXPECT is absolute. Ensure that TARGET is absolute. target = (path.parent / target).resolve() if target == expect: return path, self.MATCHES _LOGGER.warning(f'Unexpected: "{lib}/{name}" should point to "{expect}"') return path, self.DIFFERS def library_added(self, dvd, lib): "In DVD, the LIB was added." _LOGGER.log(SETUP, f'DVD[{dvd.dvddir}] added to {lib}') # Add all streams into the target LIB. for sname in dvd.streams: expect = dvd.dvddir / sname plexpath, status = self.path_status(lib, sname, expect) # Other status values are okay: logged or matching. if status != self.MISSING: continue # Create the symlink: PLEXPATH -> EXPECT create_symlink(expect, plexpath) _LOGGER.log(SETUP, f'Added: PLEX/{plexpath.relative_to(self.rootdir)} -> .../{sname}') def library_removed(self, dvd, lib): "In DVD, the LIB was removed." _LOGGER.info(f'DISABLED: DVD[{dvd.dvddir}] removal from {lib}') def stream_added(self, dvd, stream): "In DVD, a new STREAM was added." expect = dvd.dvddir / stream # _LOGGER.info(f'New stream at "{expect}"') # Add symlink to EXPECT in the DVD's LIB. plexpath, status = self.path_status(dvd.lib, stream, expect) # Other status values are okay: DIFFERS and NOTLINK are logged, # or the link MATCHES. Only add MISSING symlinks. if status == self.MISSING: # Create the symlink: PLEXPATH -> EXPECT create_symlink(expect, plexpath) _LOGGER.log(SETUP, f'Added: PLEX/{plexpath.relative_to(self.rootdir)} -> .../{stream}') def stream_removed(self, dvd, stream): "In DVD, a STREAM was removed." expect = dvd.dvddir / stream _LOGGER.info(f'DISABLED: Removal of stream at "{expect}"') if __name__ == '__main__': import sys # argv: dvd-root plex-root lib [lib...] plex = PlexLibraries(sys.argv[2], sys.argv[3:]) root = DVDRoot(sys.argv[1], plex)