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