Changeset - 8092a1881a49
[Not reviewed]
0 1 0
Branko Majic (branko) - 9 months ago 2025-03-02 00:58:38
branko@majic.rs
[thebuggenie_hg_remote.py] Fixed linter errors:

- Mostly spacing-related.
- Use the range function instead of xrange (Python 2.7 -> Python 3.x).
- Script probably will not run correctly under Python 3.x, but at
least the linter will not get in the way when working on other
Python code in the project.
1 file changed with 13 insertions and 8 deletions:
0 comments (0 inline, 0 general)
hooks/thebuggenie_hg_remote.py
Show inline comments
 
@@ -50,24 +50,25 @@
 
#
 
# nosslverify
 
#     Specifies that the calling program should _not_ verify the certificate (if
 
#     the URL specified begins with https). This can be used in conjunction with
 
#     the self-signed certificates.
 
#
 

	
 
from urllib import quote
 
import subprocess
 

	
 
import mercurial.node
 

	
 

	
 
def call_program(program, arguments, url):
 
    """
 
    Calls the specified program, passing it the arguments. The URL is passed
 
    through stdin to the program.
 

	
 

	
 
    Arguments:
 

	
 
    program - Binary that should be called.
 

	
 
    arguments - Arguments that should be passed to the program.
 

	
 
@@ -86,24 +87,25 @@ def call_program(program, arguments, url):
 
    process = subprocess.Popen(arguments, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 

	
 
    # curl expects slightly different format when reading URL from stdin.
 
    if program == "curl":
 
        url = "url=%s" % url
 

	
 
    # Obtain the contents of stdout and stderr.
 
    stdout, stderr = process.communicate(url)
 

	
 
    # Return the exit code of process, and message that should be shown to user.
 
    return process.returncode, stdout, stderr
 

	
 

	
 
def extract_report(repo, context):
 
    """
 
    Extract the commit report from provided context.
 

	
 

	
 
    Arguments:
 

	
 
    repo - Mercurial repository object.
 

	
 
    context - Mercurial commit context for which the report should be extracted.
 

	
 

	
 
@@ -132,24 +134,25 @@ def extract_report(repo, context):
 
    files_changed_status = []
 
    for f in files_changed[0]:
 
        files_changed_status.append("U %s" % f)
 
    for f in files_changed[1]:
 
        files_changed_status.append("A %s" % f)
 
    for f in files_changed[2]:
 
        files_changed_status.append("D %s" % f)
 
    report['changed'] = "\n".join(files_changed_status)
 

	
 
    # Finally return the report.
 
    return report
 

	
 

	
 
def submit_report(commit, repo, ui, program, program_params, base_url, project_id, passkey):
 
    """
 
    Submits report to TBG for a single commit.
 

	
 
    Arguments:
 

	
 
    commit - Mercurial commit identifier.
 

	
 
    repo - Mercurial repository object.
 

	
 
    ui - Mercurial user interface object.
 

	
 
@@ -197,56 +200,57 @@ def submit_report(commit, repo, ui, program, program_params, base_url, project_i
 
        # Strip out the whitespaces and newlines from the end, and make sure we
 
        # have a single final newline.
 
        stderr.rstrip()
 
        stderr += "\n"
 
        ui.warn(stderr)
 
    else:
 
        # Strip out the whitespaces and newlines from the end, and make sure we
 
        # have a single final newline.
 
        stdout.rstrip()
 
        stdout += "\n"
 
        ui.warn(stdout)
 

	
 

	
 
def hook(ui, repo, hooktype, node=None, url=None, **kwargs):
 
    """
 
    Commit and changegroup hook for Mercurial.
 

	
 

	
 
    Arguments:
 

	
 
    ui - Mercurial user interface object.
 

	
 
    repo - Mercurial repository object.
 

	
 
    node - Mercurial revision ID.
 

	
 
    url - Mercurial path/URL.
 

	
 
    kwargs - Additional keyword arguments.
 
    """
 

	
 
    # Read the configuration options.
 
    config = dict((param, value) for param, value in ui.configitems('buggenie'))
 

	
 
    # Make sure the required settings were specified.
 
    if not "program" in config:
 
    if "program" not in config:
 
        ui.warn("TBG HG Hook: No program was specified. Check your settings.\n")
 
        return
 
    if not "url" in config:
 
    if "url" not in config:
 
        ui.warn("TBG HG Hook: No base URL was specified. Check your settings.\n")
 
        return
 
    if not "project" in config:
 
    if "project" not in config:
 
        ui.warn("TBG HG Hook: No project ID was specified. Check your settings.\n")
 
        return
 
    if not "passkey" in config:
 
    if "passkey" not in config:
 
        ui.warn("TBG HG Hook: No passkey was specified. Check your settings.\n")
 
        return
 

	
 
    # Set-up parameters to be passed onto program.
 
    if config["program"] == "wget":
 
        # Use dots for progress, output to stdout, read link from stdin.
 
        program_params = ["--progress=dot", "-O", "-", "-i", "-"]
 
        if config.get("nosslverify") == "true":
 
            program_params.append("--no-check-certificate")
 
    elif config["program"] == "curl":
 
        # Quiet, but show errors, fail on server error, and read link from stdin.
 
        program_params = ["-s", "-S", "-f", "-K", "-"]
 
@@ -255,19 +259,20 @@ def hook(ui, repo, hooktype, node = None, url = None, **kwargs):
 
    else:
 
        ui.warn("TBG HG Hook: Specified program '%s' is not supported. Check your settings.\n")
 
        return
 

	
 
    # Get the node hash.
 
    node_hash = mercurial.node.bin(node)
 

	
 
    # If the hook is called as a changegroup hook, process all related commits.
 
    if hooktype == "changegroup":
 
        start = repo.changelog.rev(node_hash)
 
        end = len(repo.changelog)
 

	
 
        for commit in xrange(start, end):
 
            submit_report(commit, repo, ui, config["program"], program_params, config["url"], config["project"], config["passkey"])
 
        for commit in range(start, end):
 
            submit_report(commit, repo, ui,
 
                          config["program"], program_params, config["url"], config["project"], config["passkey"])
 

	
 
    # If the hook is called as a commit, hook, process just the single commit.
 
    elif hooktype == "commit":
 
        submit_report(commit, repo, ui, config["program"], program_params, config["url"], config["project"], config["passkey"])
 

	
 
        submit_report(commit, repo, ui,
 
                      config["program"], program_params, config["url"], config["project"], config["passkey"])
0 comments (0 inline, 0 general)