<commit_before>import subprocess as sp
import os

def init_parser(parser):
    parser.add_argument('name', type=str, help='Cluster name.')
    parser.add_argument('service', type=str,
                        choices=['notebook', 'nb', 'spark-ui', 'ui', 'spark-ui1', 'ui1',
                                 'spark-ui2', 'ui2', 'spark-history', 'hist'],
                        help='Web service to launch.')
    parser.add_argument('--port', '-p', default='10000', type=str,
                        help='Local port to use for SSH tunnel to master node (default: %(default)s).')
    parser.add_argument('--zone', '-z', default='us-central1-b', type=str,
                        help='Compute zone for Dataproc cluster (default: %(default)s).')

def main(args, pass_through_args):
    print("Connecting to cluster '{}'...".format(args.name))

    # shortcut mapping
    shortcut = {
        'ui': 'spark-ui',
        'ui1': 'spark-ui1',
        'ui2': 'spark-ui2',
        'hist': 'history',
        'nb': 'notebook'
    }

    service = args.service
    if service in shortcut:
        service = shortcut[service]

    # Dataproc port mapping
    dataproc_ports = {
        'spark-ui': 4040,
        'spark-ui1': 4041,
        'spark-ui2': 4042,
        'spark-history': 18080,
        'notebook': 8123
    }
    connect_port = dataproc_ports[service]

    # open SSH tunnel to master node
    sp.check_call(
        ['gcloud',
         'compute',
         'ssh',
         '{}-m'.format(args.name),
         '--zone={}'.format(args.zone),
         '--ssh-flag=-D {}'.format(args.port),
         '--ssh-flag=-N',
         '--ssh-flag=-f',
         '--ssh-flag=-n'],
        stderr=sp.STDOUT
    )

    # open Chrome with SOCKS proxy configuration
    with open(os.devnull, 'w') as f:
        sp.Popen([
            r'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
            'http://localhost:{}'.format(connect_port),
            '--proxy-server=socks5://localhost:{}'.format(args.port),
            '--host-resolver-rules=MAP * 0.0.0.0 , EXCLUDE localhost',
            '--proxy-bypass-list=<-loopback>', # https://chromium.googlesource.com/chromium/src/+/da790f920bbc169a6805a4fb83b4c2ab09532d91
            '--user-data-dir=/tmp/'
        ], stdout=f, stderr=f)<commit_msg>Attempt to find chromium before defaulting to Chrome (#6280)<commit_after>import subprocess as sp
import os

from shutil import which

def init_parser(parser):
    parser.add_argument('name', type=str, help='Cluster name.')
    parser.add_argument('service', type=str,
                        choices=['notebook', 'nb', 'spark-ui', 'ui', 'spark-ui1', 'ui1',
                                 'spark-ui2', 'ui2', 'spark-history', 'hist'],
                        help='Web service to launch.')
    parser.add_argument('--port', '-p', default='10000', type=str,
                        help='Local port to use for SSH tunnel to master node (default: %(default)s).')
    parser.add_argument('--zone', '-z', default='us-central1-b', type=str,
                        help='Compute zone for Dataproc cluster (default: %(default)s).')

def main(args, pass_through_args):
    print("Connecting to cluster '{}'...".format(args.name))

    # shortcut mapping
    shortcut = {
        'ui': 'spark-ui',
        'ui1': 'spark-ui1',
        'ui2': 'spark-ui2',
        'hist': 'history',
        'nb': 'notebook'
    }

    service = args.service
    if service in shortcut:
        service = shortcut[service]

    # Dataproc port mapping
    dataproc_ports = {
        'spark-ui': 4040,
        'spark-ui1': 4041,
        'spark-ui2': 4042,
        'spark-history': 18080,
        'notebook': 8123
    }
    connect_port = dataproc_ports[service]

    # open SSH tunnel to master node
    sp.check_call(
        ['gcloud',
         'compute',
         'ssh',
         '{}-m'.format(args.name),
         '--zone={}'.format(args.zone),
         '--ssh-flag=-D {}'.format(args.port),
         '--ssh-flag=-N',
         '--ssh-flag=-f',
         '--ssh-flag=-n'],
        stderr=sp.STDOUT
    )

    for c in ['chromium', 'chromium-browser']:
        chrome = which(c)
        if chrome is not None:
            break
    if chrome is None:
        chrome = r'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'

    # open Chrome with SOCKS proxy configuration
    with open(os.devnull, 'w') as f:
        sp.Popen([
            chrome,
            'http://localhost:{}'.format(connect_port),
            '--proxy-server=socks5://localhost:{}'.format(args.port),
            '--host-resolver-rules=MAP * 0.0.0.0 , EXCLUDE localhost',
            '--proxy-bypass-list=<-loopback>', # https://chromium.googlesource.com/chromium/src/+/da790f920bbc169a6805a4fb83b4c2ab09532d91
            '--user-data-dir=/tmp/'
        ], stdout=f, stderr=f)
{'fraction_non_alphanumeric': 0.16614874663351978, 'fraction_numerical': 0.028589185829707892, 'mean_word_length': 2.0928891736066624, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6874015', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1531, 'n_words': 316}