Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 1 | """Bring time stamps of generated checked-in files into the right order |
| 2 | |
| 3 | A versioned configuration file .hgtouch specifies generated files, in the |
| 4 | syntax of make rules. |
| 5 | |
| 6 | output: input1 input2 |
| 7 | |
| 8 | In addition to the dependency syntax, #-comments are supported. |
| 9 | """ |
Benjamin Peterson | 0e1a5b4 | 2012-04-27 11:56:30 -0400 | [diff] [blame] | 10 | import errno |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 11 | import os |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 12 | import time |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 13 | |
| 14 | def parse_config(repo): |
Benjamin Peterson | 0e1a5b4 | 2012-04-27 11:56:30 -0400 | [diff] [blame] | 15 | try: |
| 16 | fp = repo.wfile(".hgtouch") |
| 17 | except IOError, e: |
| 18 | if e.errno != errno.ENOENT: |
| 19 | raise |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 20 | return {} |
| 21 | result = {} |
Benjamin Peterson | 0e1a5b4 | 2012-04-27 11:56:30 -0400 | [diff] [blame] | 22 | with fp: |
| 23 | for line in fp: |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 24 | # strip comments |
| 25 | line = line.split('#')[0].strip() |
| 26 | if ':' not in line: |
| 27 | continue |
| 28 | outputs, inputs = line.split(':', 1) |
| 29 | outputs = outputs.split() |
| 30 | inputs = inputs.split() |
| 31 | for o in outputs: |
| 32 | try: |
| 33 | result[o].extend(inputs) |
| 34 | except KeyError: |
| 35 | result[o] = inputs |
| 36 | return result |
| 37 | |
Georg Brandl | a814533 | 2014-01-27 08:22:49 +0100 | [diff] [blame] | 38 | def check_rule(ui, repo, modified, basedir, output, inputs): |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 39 | """Verify that the output is newer than any of the inputs. |
| 40 | Return (status, stamp), where status is True if the update succeeded, |
| 41 | and stamp is the newest time stamp assigned to any file (might be in |
Georg Brandl | a814533 | 2014-01-27 08:22:49 +0100 | [diff] [blame] | 42 | the future). |
| 43 | |
| 44 | If basedir is nonempty, it gives a directory in which the tree is to |
| 45 | be checked. |
| 46 | """ |
| 47 | f_output = repo.wjoin(os.path.join(basedir, output)) |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 48 | try: |
| 49 | o_time = os.stat(f_output).st_mtime |
| 50 | except OSError: |
| 51 | ui.warn("Generated file %s does not exist\n" % output) |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 52 | return False, 0 |
| 53 | youngest = 0 # youngest dependency |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 54 | backdate = None |
| 55 | backdate_source = None |
| 56 | for i in inputs: |
Georg Brandl | a814533 | 2014-01-27 08:22:49 +0100 | [diff] [blame] | 57 | f_i = repo.wjoin(os.path.join(basedir, i)) |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 58 | try: |
| 59 | i_time = os.stat(f_i).st_mtime |
| 60 | except OSError: |
| 61 | ui.warn(".hgtouch input file %s does not exist\n" % i) |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 62 | return False, 0 |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 63 | if i in modified: |
| 64 | # input is modified. Need to backdate at least to i_time |
| 65 | if backdate is None or backdate > i_time: |
| 66 | backdate = i_time |
| 67 | backdate_source = i |
| 68 | continue |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 69 | youngest = max(i_time, youngest) |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 70 | if backdate is not None: |
| 71 | ui.warn("Input %s for file %s locally modified\n" % (backdate_source, output)) |
| 72 | # set to 1s before oldest modified input |
| 73 | backdate -= 1 |
| 74 | os.utime(f_output, (backdate, backdate)) |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 75 | return False, 0 |
| 76 | if youngest >= o_time: |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 77 | ui.note("Touching %s\n" % output) |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 78 | youngest += 1 |
| 79 | os.utime(f_output, (youngest, youngest)) |
| 80 | return True, youngest |
| 81 | else: |
| 82 | # Nothing to update |
| 83 | return True, 0 |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 84 | |
Georg Brandl | a814533 | 2014-01-27 08:22:49 +0100 | [diff] [blame] | 85 | def do_touch(ui, repo, basedir): |
| 86 | if basedir: |
| 87 | if not os.path.isdir(repo.wjoin(basedir)): |
| 88 | ui.warn("Abort: basedir %r does not exist\n" % basedir) |
| 89 | return |
| 90 | modified = [] |
| 91 | else: |
| 92 | modified = repo.status()[0] |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 93 | dependencies = parse_config(repo) |
| 94 | success = True |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 95 | tstamp = 0 # newest time stamp assigned |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 96 | # try processing all rules in topological order |
| 97 | hold_back = {} |
| 98 | while dependencies: |
| 99 | output, inputs = dependencies.popitem() |
| 100 | # check whether any of the inputs is generated |
| 101 | for i in inputs: |
| 102 | if i in dependencies: |
| 103 | hold_back[output] = inputs |
| 104 | continue |
Georg Brandl | a814533 | 2014-01-27 08:22:49 +0100 | [diff] [blame] | 105 | _success, _tstamp = check_rule(ui, repo, modified, basedir, output, inputs) |
| 106 | success = success and _success |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 107 | tstamp = max(tstamp, _tstamp) |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 108 | # put back held back rules |
| 109 | dependencies.update(hold_back) |
| 110 | hold_back = {} |
Martin v. Löwis | bf52648 | 2013-09-30 16:09:44 +0200 | [diff] [blame] | 111 | now = time.time() |
| 112 | if tstamp > now: |
| 113 | # wait until real time has passed the newest time stamp, to |
| 114 | # avoid having files dated in the future |
| 115 | time.sleep(tstamp-now) |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 116 | if hold_back: |
| 117 | ui.warn("Cyclic dependency involving %s\n" % (' '.join(hold_back.keys()))) |
| 118 | return False |
| 119 | return success |
| 120 | |
Georg Brandl | a814533 | 2014-01-27 08:22:49 +0100 | [diff] [blame] | 121 | def touch(ui, repo, basedir): |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 122 | "touch generated files that are older than their sources after an update." |
Georg Brandl | a814533 | 2014-01-27 08:22:49 +0100 | [diff] [blame] | 123 | do_touch(ui, repo, basedir) |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 124 | |
| 125 | cmdtable = { |
Georg Brandl | a814533 | 2014-01-27 08:22:49 +0100 | [diff] [blame] | 126 | "touch": (touch, |
Georg Brandl | e46abb4 | 2014-03-09 10:22:10 +0100 | [diff] [blame] | 127 | [('b', 'basedir', '', 'base dir of the tree to apply touching')], |
Georg Brandl | a814533 | 2014-01-27 08:22:49 +0100 | [diff] [blame] | 128 | "hg touch [-b BASEDIR]") |
Martin v. Loewis | cfc1cc2 | 2012-04-27 16:10:21 +0200 | [diff] [blame] | 129 | } |