aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/touch.py
blob: 16be77630e3f5481f111550e8d91ea8fb1ce238f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python

import optparse
import os
import sys
import time


def main(argv):
    parser = optparse.OptionParser(add_help_option=False)
    parser.disable_interspersed_args()

    parser.add_option('-?', '--help', dest='help', action='store_true', default=None, help='print help')
    parser.add_option('-t', dest='t', action='store', default=None)

    opts, argv_rest = parser.parse_args(argv)
    if getattr(opts, 'help', False):
        parser.print_help()
        return 0

    tspec = opts.t
    if tspec is None:
        times = None
    else:
        head, sep, tail = tspec.partition('.')
        if 8 > len(head):
            raise Exception("time spec must follow format [[CC]YY]MMDDhhmm[.SS]: " + tspec + '; ' + head)
        tfmt = ''
        if 12 == len(head):
            tfmt += '%Y'
        elif 10 == len(head):
            tfmt += '%y'
        tfmt += '%m%d%H%M'
        if 2 == len(tail):
            tfmt += '.%S'
        mtime = time.mktime(time.strptime(tspec, tfmt))
        times = (mtime, mtime)

    for file in argv_rest:
        try:
            os.utime(file, times)
        except:
            open(file, 'w').close()
            if times is not None:
                os.utime(file, times)


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))