Frias
Frias

Reputation: 11291

Check if the directory content has changed with shell script or python

I have a program that create files in a specific directory. When those files are ready, I run Latex to produce a .pdf file. So, my question is, how can I use this directory change as a trigger to call Latex, using a shell script or a python script?

Best Regards

Upvotes: 6

Views: 5685

Answers (7)

Dennis Williamson
Dennis Williamson

Reputation: 360355

inotify replaces dnotify.

Why?

...dnotify requires opening one file descriptor for each directory that you intend to watch for changes...

Additionally, the file descriptor pins the directory, disallowing the backing device to be unmounted, which causes problems in scenarios involving removable media. When using inotify, if you are watching a file on a file system that is unmounted, the watch is automatically removed and you receive an unmount event.

...and more.

More Why?

Unlike its ancestor dnotify, inotify doesn't complicate your work by various limitations. For example, if you watch files on a removable media these file aren't locked. In comparison with it, dnotify requires the files themselves to be open and thus really "locks" them (hampers unmounting the media).

Reference

Upvotes: 5

Emmanuel
Emmanuel

Reputation: 14209

You can use native python module hashlib which implements MD5 algorithm:

>>> import hashlib
>>> import os
>>> m = hashlib.md5()
>>> for root, dirs, files in os.walk(path):
    for file_read in files:
        full_path = os.path.join(root, file_read)
        for line in open(full_path).readlines():
            m.update(line)


>>> m.digest()
'pQ\x1b\xb9oC\x9bl\xea\xbf\x1d\xda\x16\xfe8\xcf'

You can save this result in a file or a variable, and compare it to the result of the next run. This will detect changes in any files, in any sub-directory.

This does not take into account file permission changes; if you need to monitor these change as well, this could be addressed via appending a string representing the permissions (accessible via os.stat for instance, attributes depend on your system) to the mvariable.

Upvotes: 0

Fred Nurk
Fred Nurk

Reputation: 14212

You not only need to check for changes, but need to know that all changes are complete before running LaTeX. For example, if you start LaTeX after the first file has been modified and while more changes are still pending, you'll be using partial data and have to re-run later.

Wait for your first program to complete:

#!/bin/bash
first-program &&
  run-after-changes-complete

Using && means the second command is only executed if the first completes successfully (a zero exit code). Because this simple script will always run the second command even if the first doesn't change any files, you can incorporate this into whatever build system you are already familiar with, such as make.

Upvotes: 3

user225312
user225312

Reputation: 131727

Python FAM is a Python interface for FAM (File Alteration Monitor)

You can also have a look at Pyinotify, which is a module for monitoring file system changes.

Upvotes: 2

xzyfer
xzyfer

Reputation: 14135

Not much of a python man myself. But in a pinch, assuming you're on linux, you could periodically shell out and "ls -lrt /path/to/directory" (get the directory contents and sort by last modified), and compare the results of the last two calls for a difference. If so, then there was a change. Not very detailed, but gets the job done.

Upvotes: 1

Make on unix systems is usually used to track by date what needs rebuilding when files have changed. I normally use a rather good makefile for this job. There seems to be another alternative around on google code too

Upvotes: 3

eumiro
eumiro

Reputation: 213005

Is dnotify what you need?

Upvotes: 3

Related Questions