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