|
| 1 | +#!/usr/bin/env python |
| 2 | +""" Refresh modification times on Cython generated C files |
| 3 | +
|
| 4 | +This is sometimes necessary on windows when the git checkout appears to |
| 5 | +sometimes checkout the C files with modification times earlier than the pyx |
| 6 | +files, triggering an attempt to rebuild the C files with Cython when running a |
| 7 | +build. |
| 8 | +""" |
| 9 | +from __future__ import with_statement |
| 10 | +import os |
| 11 | +from os.path import splitext, join as pjoin, isfile |
| 12 | +import sys |
| 13 | +import optparse |
| 14 | + |
| 15 | + |
| 16 | +# From http://stackoverflow.com/questions/1158076/implement-touch-using-python |
| 17 | +if sys.version_info[0] >= 3: |
| 18 | + def touch(fname, times=None, ns=None, dir_fd=None): |
| 19 | + with os.open(fname, os.O_APPEND, dir_fd=dir_fd) as f: |
| 20 | + os.utime(f.fileno() if os.utime in os.supports_fd else fname, |
| 21 | + times=times, ns=ns, dir_fd=dir_fd) |
| 22 | +else: |
| 23 | + def touch(fname, times=None): |
| 24 | + with file(fname, 'a'): |
| 25 | + os.utime(fname, times) |
| 26 | + |
| 27 | + |
| 28 | +def main(): |
| 29 | + parser = optparse.OptionParser(usage='%prog [<root_dir>]') |
| 30 | + (opts, args) = parser.parse_args() |
| 31 | + if len(args) > 1: |
| 32 | + parser.print_help() |
| 33 | + sys.exit(-1) |
| 34 | + elif len(args) == 1: |
| 35 | + root_dir = args[0] |
| 36 | + else: |
| 37 | + root_dir = os.getcwd() |
| 38 | + for dirpath, dirnames, filenames in os.walk(root_dir): |
| 39 | + for fn in filenames: |
| 40 | + if fn.endswith('.pyx'): |
| 41 | + froot, ext = splitext(fn) |
| 42 | + cfile = pjoin(dirpath, froot + '.c') |
| 43 | + if isfile(cfile): |
| 44 | + touch(cfile) |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == '__main__': |
| 48 | + main() |
0 commit comments