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 20 insertions and 15 deletions:
0 comments (0 inline, 0 general)
hooks/thebuggenie_hg_remote.py
Show inline comments
 
@@ -22,13 +22,13 @@
 
# [buggenie]
 
# program = [curl|wget]
 
# url = tbg_url
 
# passkey = project_passkey
 
# project = project_id
 
# nosslverify = [true|false]
 
# 
 
#
 
# [hooks]
 
# changegroup.buggenie = python:buggenie.hook
 
#
 
# The following parameters are mandatory:
 
#
 
# program
 
@@ -56,12 +56,13 @@
 

	
 
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.
 

	
 

	
 
@@ -80,24 +81,25 @@ def call_program(program, arguments, url):
 
    """
 

	
 
    # Prefix the arguments with program.
 
    arguments.insert(0, program)
 

	
 
    # Set-up the subprocess for execution.
 
    process = subprocess.Popen(arguments, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
 
    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:
 
@@ -138,18 +140,19 @@ def extract_report(repo, context):
 
        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.
 

	
 
@@ -175,13 +178,13 @@ def submit_report(commit, repo, ui, program, program_params, base_url, project_i
 
    # Generate the URL.
 
    # Strip the trailing slash.
 
    base_url = base_url.rstrip("/")
 
    # Set-up the base link for project.
 
    url = "%s/vcs_integration/report/%s/?" % (base_url, project_id)
 
    # Pass the GET arguments.
 
    args = [ "%s=%s" % (quote(key), quote(value)) for key, value in report.iteritems()]
 
    args = ["%s=%s" % (quote(key), quote(value)) for key, value in report.iteritems()]
 
    url += "&".join(args)
 

	
 
    # Call the program.
 
    exit_code, stdout, stderr = call_program(program, program_params, url)
 

	
 
    # Since wget has a bit poor way of outputting error, filter out the URL used
 
@@ -203,13 +206,14 @@ 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.
 
        stdout.rstrip()
 
        stdout += "\n"
 
        ui.warn(stdout)
 

	
 
def hook(ui, repo, hooktype, node = None, url = None, **kwargs):
 

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

	
 

	
 
    Arguments:
 

	
 
@@ -225,34 +229,34 @@ def hook(ui, repo, hooktype, node = None, url = None, **kwargs):
 
    """
 

	
 
    # 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", "-" ]
 
        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", "-" ]
 
        program_params = ["-s", "-S", "-f", "-K", "-"]
 
        if config.get("nosslverify") == "true":
 
            program_params.append("--insecure")
 
    else:
 
        ui.warn("TBG HG Hook: Specified program '%s' is not supported. Check your settings.\n")
 
        return
 

	
 
@@ -261,13 +265,14 @@ def hook(ui, repo, hooktype, node = None, url = None, **kwargs):
 

	
 
    # 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)