<commit_before>#!/usr/bin/env python

# Copyright (c) 2012 - 2014 <NAME>, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.

from __future__ import print_function

import sys, os, argparse, tempfile

jenkins_cli_jar = 'jenkins-cli.jar'
hudson_cli_jar = 'hudson-cli.jar'


def cli_jar_info(direct_url):
    base_url = os.environ.get('JENKINS_URL')
    cli_jar = jenkins_cli_jar

    if base_url is None:
        base_url = os.environ.get('HUDSON_URL')
        cli_jar = hudson_cli_jar

    base_url = base_url + '/' if base_url is not None and base_url[-1] != '/' else base_url
    return cli_jar, direct_url or base_url, base_url if base_url != direct_url else None


def download_cli(cli_jar, base_url, public_base_url):
    import urllib2

    cli_url = base_url + 'jnlpJars/' + cli_jar
    if public_base_url:
        public_cli_url = public_base_url + 'jnlpJars/' + cli_jar
        print("INFO: Downloading cli:", repr(public_cli_url), " (using direct url: ", cli_url, ')')
    else:
        print("INFO: Downloading cli:", repr(cli_url))
    response = urllib2.urlopen(cli_url)
    with open(cli_jar, 'w') as ff:
        ff.write(response.read())
    print("INFO: Download finished:", repr(cli_jar))


def set_build_result(username, password, result, direct_url=None, java='java'):
    """Change the result of a Jenkins job.

    Note: set_build_result can only be done from within the job, not after the job has finished.

    Note: Only available if URL is set in `Jenkins <http://jenkins-ci.org/>`_ system configuration.

    This command uses the Jenkins `cli` to change the result. It requires a java executable to run the cli.

    Args:
        username (str): Name of jenkins user with access to the job
        password (str): Password of <PASSWORD> job
        result (str): The result to set. Should probably be 'unstable'
        direct-url (str): Jenkins URL. Default is JENKINS_URL env var value. Use this argument if JENKINS_URL is a proxy.
        java (str): Alternative `java` executable. Use this if you don't wish to use the java in the PATH.
    """

    print("INFO: Setting job result to", repr(result))

    cli_jar, base_url, public_base_url = cli_jar_info(direct_url)
    if base_url is None:
        raise Exception("Could not get env variable JENKINS_URL or HUDSON_URL. Don't know whether to use " +
                        jenkins_cli_jar + " or " + hudson_cli_jar + " for setting result! " +
                        "You must set 'Jenkins Location' in Jenkins setup for JENKINS_URL to be exported. " +
                        "You must set 'Hudson URL' in Hudson setup for HUDSON_URL to be exported.")

    import subprocess

    def set_res():
        command = [java, '-jar', cli_jar, '-s', base_url, 'set-build-result', result]
        if username or password:
            assert password and username, "You must specify both username and password if any"
            fname = None
            try:
                fhandle, fname = tempfile.mkstemp()
                fhandle = os.fdopen(fhandle, 'w')
                fhandle.write(password)
                fhandle.close()
                subprocess.check_call(command + ['--username', username, '--password-file', fname])
            finally:
                try:<commit_msg>Use subprocess32 instead of subprocess<commit_after>#!/usr/bin/env python

# Copyright (c) 2012 - 2014 <NAME>, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.

from __future__ import print_function

import sys, os, argparse, tempfile

jenkins_cli_jar = 'jenkins-cli.jar'
hudson_cli_jar = 'hudson-cli.jar'


def cli_jar_info(direct_url):
    base_url = os.environ.get('JENKINS_URL')
    cli_jar = jenkins_cli_jar

    if base_url is None:
        base_url = os.environ.get('HUDSON_URL')
        cli_jar = hudson_cli_jar

    base_url = base_url + '/' if base_url is not None and base_url[-1] != '/' else base_url
    return cli_jar, direct_url or base_url, base_url if base_url != direct_url else None


def download_cli(cli_jar, base_url, public_base_url):
    import urllib2

    cli_url = base_url + 'jnlpJars/' + cli_jar
    if public_base_url:
        public_cli_url = public_base_url + 'jnlpJars/' + cli_jar
        print("INFO: Downloading cli:", repr(public_cli_url), " (using direct url: ", cli_url, ')')
    else:
        print("INFO: Downloading cli:", repr(cli_url))
    response = urllib2.urlopen(cli_url)
    with open(cli_jar, 'w') as ff:
        ff.write(response.read())
    print("INFO: Download finished:", repr(cli_jar))


def set_build_result(username, password, result, direct_url=None, java='java'):
    """Change the result of a Jenkins job.

    Note: set_build_result can only be done from within the job, not after the job has finished.

    Note: Only available if URL is set in `Jenkins <http://jenkins-ci.org/>`_ system configuration.

    This command uses the Jenkins `cli` to change the result. It requires a java executable to run the cli.

    Args:
        username (str): Name of jenkins user with access to the job
        password (str): <PASSWORD> <PASSWORD> the job
        result (str): The result to set. Should probably be 'unstable'
        direct-url (str): Jenkins URL. Default is JENKINS_URL env var value. Use this argument if JENKINS_URL is a proxy.
        java (str): Alternative `java` executable. Use this if you don't wish to use the java in the PATH.
    """

    print("INFO: Setting job result to", repr(result))

    cli_jar, base_url, public_base_url = cli_jar_info(direct_url)
    if base_url is None:
        raise Exception("Could not get env variable JENKINS_URL or HUDSON_URL. Don't know whether to use " +
                        jenkins_cli_jar + " or " + hudson_cli_jar + " for setting result! " +
                        "You must set 'Jenkins Location' in Jenkins setup for JENKINS_URL to be exported. " +
                        "You must set 'Hudson URL' in Hudson setup for HUDSON_URL to be exported.")

    import subprocess32 as subprocess

    def set_res():
        command = [java, '-jar', cli_jar, '-s', base_url, 'set-build-result', result]
        if username or password:
            assert password and username, "You must specify both username and password if any"
            fname = None
            try:
                fhandle, fname = tempfile.mkstemp()
                fhandle = os.fdopen(fhandle, 'w')
                fhandle.write(password)
                fhandle.close()
                subprocess.check_call(command + ['--username', username, '--password-file', fname])
            finally:
                try:
{'fraction_non_alphanumeric': 0.09545317675398976, 'fraction_numerical': 0.003914483589280337, 'mean_word_length': 2.7467569091934574, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7515545', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2028, 'n_words': 689}