Files

215 lines
9.1 KiB
Markdown

# dotnuke
A small cross-platform service that keeps a directory free of macOS metadata clutter. It deletes
files as soon as they appear, and also cleans up whatever is already there when it starts.
Files removed:
| Pattern | What it is |
|-------------|-----------------------------------------------------------------------------------|
| `._*` | AppleDouble files. macOS writes these to non-HFS/APFS volumes (SMB/NFS shares, exFAT/FAT32/NTFS drives) to store resource forks and extended attributes. |
| `.DS_Store` | Finder's per-folder view settings (icon positions, sort order, etc.). |
This is most useful on network shares, external drives, or servers (Linux, Windows) that are
accessed from Macs, where these files otherwise accumulate and show up as junk to other systems.
Matching is by file name only and is case-sensitive. Only files are removed, never directories.
## Supported platforms
| OS | Supported | Notes |
|---------|-----------|-----------------------------------------------------------------------|
| macOS | Yes | `fswatch` uses the native FSEvents backend. |
| Linux | Yes | `fswatch` uses inotify (or another available backend). |
| Windows | Yes | `fswatch` uses the native Windows backend; see the Windows notes below. |
The script itself is plain Python and uses only portable standard-library calls; the only
platform-specific piece is installing `fswatch`.
## Dependencies
1. **Python 3.6 or newer.** Standard library only; no `pip install` needed.
2. **[fswatch](https://github.com/emcrisostomo/fswatch)** available on your `PATH`. It is the
program that reports filesystem events to the script.
Installing `fswatch`:
| OS | Command |
|---------|---------------------------------------------------------------------------------|
| macOS | `brew install fswatch` |
| Debian/Ubuntu | `sudo apt install fswatch` |
| Fedora | `sudo dnf install fswatch` |
| Arch | `sudo pacman -S fswatch` |
| Other Linux | Build from source (see the fswatch repository) |
| Windows | Install through [MSYS2](https://www.msys2.org/) (`pacman -S fswatch`) or Cygwin, then add its `bin` directory to `PATH` |
Verify the installation with `fswatch --version`.
## Setting up a virtual environment
Using a virtual environment keeps the build tooling (PyInstaller) separate from your system
Python. The script itself needs no third-party packages, so the venv is only strictly needed for
building the executable.
```sh
# macOS / Linux
python3 -m venv .venv
source .venv/bin/activate
# Windows (Command Prompt)
py -m venv .venv
.venv\Scripts\activate.bat
# Windows (PowerShell)
py -m venv .venv
.venv\Scripts\Activate.ps1
```
Once activated, `python` refers to the venv interpreter and the prompt shows `(.venv)`. Run the
script with `python dotnuke.py`. Leave the environment with `deactivate`.
Note that `fswatch` is not a Python package; it is still installed system-wide as described above.
## Building a portable executable (PyInstaller)
You can bundle the script and the Python interpreter into one self-contained file, so the target
machine does not need Python installed. (`fswatch` must still be installed there.)
1. Activate the virtual environment (see above) and install PyInstaller:
```sh
pip install pyinstaller
```
2. Build a single-file executable:
```sh
pyinstaller --onefile --name dotnuke dotnuke.py
```
3. Find the result in `dist/`: `dist/dotnuke` (macOS/Linux) or `dist\dotnuke.exe`
(Windows).
4. Place `dotnuke.ini` in the **same folder as the executable**, then run it:
```sh
./dotnuke # macOS / Linux
dotnuke.exe # Windows
```
The executable looks for its config next to itself (not in PyInstaller's temporary extraction
folder), so the file can be edited without rebuilding. You can still pass an explicit config path
as the first argument. Copy the executable and ini file together to move them to another machine.
Notes:
- PyInstaller does not cross-compile. Build on each OS (and CPU architecture) you target.
- Build artifacts (`build/`, `dist/`, `dotnuke.spec`) can be deleted or git-ignored.
- On macOS, an unsigned binary copied from another machine may be blocked by Gatekeeper; clear it
with `xattr -d com.apple.quarantine dotnuke`.
## Configuration
Settings are read from an ini file, by default `dotnuke.ini` in the same folder as the
script (or as the executable, when running the PyInstaller build):
```ini
[watch]
dir = /Volumes/omniplex/dek-charlie
```
| Section | Option | Required | Description |
|---------|--------|----------|------------------------------------------------------------------|
| `watch` | `dir` | Yes | Directory to clean and monitor. Subdirectories are included. |
Path examples for each platform:
```ini
; macOS
dir = /Volumes/omniplex/dek-charlie
; Linux
dir = /mnt/omniplex/dek-charlie
; Windows (forward slashes or backslashes both work; no escaping needed)
dir = D:\omniplex\dek-charlie
dir = //server/share/dek-charlie
```
Only one `dir` should be active at a time. A leading `~` is expanded to the home directory, and
surrounding double quotes are ignored.
## Usage
Run from the folder containing the script:
```sh
# macOS / Linux
python3 dotnuke.py
# Windows (Command Prompt or PowerShell)
py dotnuke.py
```
To use a different config file, pass its path as the only argument:
```sh
python3 dotnuke.py /path/to/other.ini
```
Stop the service with `Ctrl+C`. If you built the executable, run it instead of the `python`
command; arguments work the same way.
### Running in the background
The script runs in the foreground and logs to standard output. To keep it running permanently,
use your platform's service manager:
- **macOS:** a `launchd` agent in `~/Library/LaunchAgents`.
- **Linux:** a `systemd` unit (or user unit) running `python3 /path/to/dotnuke.py`.
- **Windows:** Task Scheduler with the trigger "At log on" or "At startup".
## How it works
1. **Initial sweep.** If the configured directory exists, the script walks it recursively, counts
the matching files, deletes them, and prints how many were purged. If the directory does not
exist yet (for example, a network volume that is not mounted), the sweep is skipped and the
script continues to the next step.
2. **Watch mode.** The script starts `fswatch` for the directory, subscribed to file *creation*
events. Each time `fswatch` reports a batch of events, the script rescans the whole tree and
deletes any matching files. Rescanning (rather than parsing individual event paths) keeps the
logic simple and handles bursts of events reliably.
Deletion failures (a file already gone, in use, or read-only) are ignored, so one problem file
never stops the service. The file is retried on the next event.
## Sample output
```text
Starting AppleDouble (._) and .DS_Store auto-cleanup service...
Target folder: /Volumes/omniplex/dek-charlie
Running initial cleaning sweep to remove existing ._ and .DS_Store files...
Initial cleanup complete. Purged 12 legacy file(s).
Entering active watch mode. Listening for new filesystem events...
```
## Troubleshooting
| Symptom | Likely cause and fix |
|---------------------------------------------------|-----------------------------------------------------------------|
| `fswatch not found. Install it ...` | `fswatch` is not installed or not on `PATH`. See Dependencies. |
| `Config file not found or unreadable: ...` | The ini file is missing; create it or pass its path as an argument. |
| `Config file ... must contain [watch] with a 'dir' option.` | The section or option name is misspelled or absent. |
| "Target directory does not exist yet..." message | The path is wrong or the volume is not mounted. Mount the volume or fix `dir`, then restart the script (`fswatch` may exit if the path does not exist). |
| New files are not removed on a network share | Some network filesystems do not deliver change events. Run the script on the machine that hosts the share, or on a local mount of it. |
| Permission errors (silent) | The user running the script needs delete permission on the directory. |
## Notes
- The script deletes files permanently; they do not go to the Trash or Recycle Bin. Double-check
`dir` before running.
- Only the `._*` and `.DS_Store` patterns are matched. To add more, edit the `PATTERNS` tuple near
the top of `dotnuke.py`.
- Cross-platform behavior beyond macOS follows from the portable Python code and `fswatch`'s
per-OS backends; it has not been exercised on every OS/filesystem combination.