23404eaead54cc19e945ac91bdcb17250c3efbe3
[pygrader.git] / pygrader / todo.py
1 # Copyright (C) 2012 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of pygrader.
4 #
5 # pygrader is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # pygrader is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # pygrader.  If not, see <http://www.gnu.org/licenses/>.
16
17 """List grading that still needs to be done.
18 """
19
20 import os as _os
21 import os.path as _os_path
22
23
24 def mtime(path, walk_directories=True):
25     if walk_directories and _os.path.isdir(path):
26         time = mtime(path, walk_directories=False)
27         for dirpath,dirnames,filenames in _os.walk(path):
28             for filename in filenames:
29                 t = mtime(_os_path.join(dirpath, filename))
30                 time = max(time, t)
31         return time
32     stat = _os.stat(path)
33     return stat.st_mtime
34
35 def newer(a, b):
36     """Return ``True`` if ``a`` is newer than ``b``.
37     """
38     return mtime(a) > mtime(b)
39
40 def todo(basedir, source, target):
41     """Yield ``source``\s in ``basedir`` with old/missing ``target``\s.
42     """
43     for dirpath,dirnames,filenames in _os.walk(basedir):
44         names = dirnames + filenames
45         if source in names:
46             s = _os_path.join(dirpath, source)
47             t = _os_path.join(dirpath, target)
48             if target in names:
49                 if newer(s, t):
50                     yield(s)
51             else:
52                 yield s
53
54 def print_todo(basedir, source, target):
55     """Print ``source``\s in ``basedir`` with old/missing ``target``\s.
56     """
57     for path in sorted(todo(basedir, source, target)):
58         print(path)