tests/get-with-headers.py
author A. S. Budden <abudden@gmail.com>
Fri, 30 Mar 2012 22:08:46 +0100
branchstable
changeset 1057 66d2754c6f30
parent 1053 ede95d3a1f27
child 1106 a02b1025536c
permissions -rwxr-xr-x
record: allow splitting of hunks by manually editing patches It is possible that unrelated changes in a file are on sequential lines. The current record extension does not allow these to be committed independently. An example use case for this is in software development for deeply embedded real-time systems. In these environments, it is not always possible to use a debugger (due to time-constraints) and hence inline UART-based printing is often used. When fixing a bug in a module, it is often convenient to add a large number of 'printf's (linked to the UART via a custom fputc) to the module in order to work out what is going wrong. printf is a very slow function (and also variadic so somewhat frowned upon by the MISRA standard) and hence it is highly undesirable to commit these lines to the repository. If only a partial fix is implemented, however, it is desirable to commit the fix without deleting all of the printf lines. This is also simplifies removal of the printf lines as once the final fix is committed, 'hg revert' does the rest. It is likely that the printf lines will be very near the actual fix, so being able to split the hunk is very useful in this case. There were two alternatives I considered for the user interface. One was to manually edit the patch, the other to allow a hunk to be split into individual lines for consideration. The latter option would require a significant refactor of the record module and is less flexible. While the former is potentially more complicated to use, this is a feature that is likely to only be used in certain exceptional cases (such as the use case proposed above) and hence I felt that the complexity would not be a considerable issue. I've also written a follow-up patch that refactors the 'prompt' code to base everything on the choices variable. This tidies up and clarifies the code a bit (removes constructs like 'if ret == 7' and removes the 'e' option from the file scope options as it's not relevant there. It's not really a necessity, so I've excluded it from this submission for now, but I can send it separately if there's a desire and it's on bitbucket (see below) in the meantime. Possible future improvements include: * Tidying up the 'prompt' code to base everything on the choices variable. This would allow entries to be removed from the prompt as currently 'e' is offered even for entire file patches, which is currently unsupported. * Allowing the entire file (or even multi-file) patch to be edited manually: this would require quite a large refactor without much benefit, so I decided to exclude it from the initial submission. * Allow the option to retry if a patch fails to apply (this is what Git does). This would require quite a bit of refactoring given the current 'hg record' implementation, so it's debatable whether it's worth it. Output is similar to existing record user interface except that an additional option ('e') exists to allow manual editing of the patch. This opens the user's configured editor with the patch. A comment is added to the bottom of the patch explaining what to do (based on Git's one). A large proportion of the changeset is test-case changes to update the options reported by record (Ynesfdaq? instead of Ynsfdaq?). Functional changes are in record.py and there are some new test cases in test-record.t. [ original upstream message ]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
513
90b51a0fe1f0 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     1
#!/usr/bin/env python
90b51a0fe1f0 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     2
567
fd52c78e1aa7 tests: fix doc string in get-with-headers.py
Martin Geisler <mg@lazybytes.net>
parents: 566
diff changeset
     3
"""This does HTTP GET requests given a host:port and path and returns
513
90b51a0fe1f0 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     4
a subset of the headers plus the body of the result."""
90b51a0fe1f0 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     5
1051
5b75af0ca224 pylint, pyflakes: remove unused or duplicate imports
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 717
diff changeset
     6
import httplib, sys
515
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
     7
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
     8
try:
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
     9
    import msvcrt, os
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    10
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    11
    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    12
except ImportError:
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    13
    pass
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    14
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    15
twice = False
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    16
if '--twice' in sys.argv:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    17
    sys.argv.remove('--twice')
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    18
    twice = True
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    19
1053
ede95d3a1f27 tests: fix incompatibility with python-2.4 in test-hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1052
diff changeset
    20
reasons = {'Not modified': 'Not Modified'} # python 2.4
ede95d3a1f27 tests: fix incompatibility with python-2.4 in test-hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1052
diff changeset
    21
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    22
tag = None
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    23
def request(host, path, show):
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    24
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    25
    global tag
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    26
    headers = {}
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    27
    if tag:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    28
        headers['If-None-Match'] = tag
514
06b69a53ab0f hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 513
diff changeset
    29
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    30
    conn = httplib.HTTPConnection(host)
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    31
    conn.request("GET", path, None, headers)
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    32
    response = conn.getresponse()
1053
ede95d3a1f27 tests: fix incompatibility with python-2.4 in test-hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1052
diff changeset
    33
    print response.status, reasons.get(response.reason, response.reason)
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    34
    for h in [h.lower() for h in show]:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    35
        if response.getheader(h, None) is not None:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    36
            print "%s: %s" % (h, response.getheader(h))
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    37
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    38
    print
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    39
    data = response.read()
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    40
    sys.stdout.write(data)
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    41
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    42
    if twice and response.getheader('ETag', None):
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    43
        tag = response.getheader('ETag')
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    44
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    45
    return response.status
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    46
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    47
status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    48
if twice:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    49
    status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    50
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    51
if 200 <= status <= 305:
514
06b69a53ab0f hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 513
diff changeset
    52
    sys.exit(0)
06b69a53ab0f hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 513
diff changeset
    53
sys.exit(1)