Best Python code snippet using localstack_python
build-bisect.py
Source:build-bisect.py  
1#!/usr/bin/env python2# This Source Code Form is subject to the terms of the Mozilla Public3# License, v. 2.0. If a copy of the MPL was not distributed with this file,4# You can obtain one at http://mozilla.org/MPL/2.0/.5import argparse6import errno7import hashlib8import math9import os10import platform11import requests12import re13import shutil14import subprocess15import sys16import json17from io import StringIO18from lib.config import get_env_var19from lib.github import GitHub20from lib.helpers import *21from lib.util import download, execute, tempdir, extract_zip22tag_names = []23releases = {}24is_mac_os = True25def parse_args():26    parser = argparse.ArgumentParser(description='create PRs for all branches given branch against master')27    parser.add_argument('--bad',28                        help='optional version which is known to be bad',29                        default=None)30    parser.add_argument('--good',31                        help='optional version which is known to be good',32                        default=None)33    parser.add_argument('--branch',34                        help='optional branch where the problem is occurring',35                        default=None)36    parser.add_argument('-v', '--verbose', action='store_true',37                        help='extra logging')38    parser.add_argument('--real-profile', action='store_true',39                        help='if true, use your real profile (instead of a fresh one). \40                              can\'t be combined with `--use-profile`.')41    parser.add_argument('--use-profile',42                        help='url of a zipped profile to unzip/use for each install',43                        default=None)44    parser.add_argument('--channel',45                        help='narrow down to a specific release channel. nightly/dev/beta/release',46                        default=None)47    parser.add_argument('--demo-mode', action='store_true',48                        help='if true, don\'t actually perform download/install')49    return parser.parse_args()50def get_releases(repo):51    global releases52    global tag_names53    releases = {}54    tag_names = []55    page = 156    done = False57    draft_count = 058    print('fetching releases from GitHub...')59    while not done:60        # for more info, see: https://developer.github.com/v3/guides/traversing-with-pagination/61        get_data = {62            'page': page,63            'per_page': 10064        }65        # get all the releases and index them66        response = repo.releases.get(params=get_data)67        if len(response) == 0:68            done = True69            break70        for release in response:71            # skip releases in draft status72            if release['draft']:73                draft_count = draft_count + 174                continue75            tag_name = str(release['tag_name'].strip().replace('v', ''))76            # skip "android" releases and others not matching version format77            if not tag_name.replace('.', '').isdigit():78                continue79            tag_names.append(tag_name)80            releases[tag_name] = release81        page = page + 182    print('fetch complete; ' + str(len(tag_names)) + ' versions found (excluding ' + str(draft_count) + ' drafts)')83def filter_releases(args):84    global tag_names85    filtered_tag_names = []86    print('filtering out versions which are not relevant...')87    # find all unique MINOR versions88    for tag in tag_names:89        version = tag.split('.')90        if len(version) != 3:91            continue92        major_version = str(version[0])93        minor_version = str(version[1])94        branch_version = major_version + '.' + minor_version + '.x'95        # remove entries which don't match optional branch (if present)96        if args.branch is not None and branch_version != args.branch:97            print(' - skipping "' + tag + '" (' + branch_version + ' != ' + args.branch + ')')98            continue99        # remove entries which don't have installer binary100        if not get_release_asset(tag, False):101            print(' - skipping "' + tag + '" (installer not found)')102            continue103        if args.channel:104            channel = get_release_channel(tag)105            if args.channel != channel:106                print(' - skipping "' + tag + '" (not in channel "' + args.channel + '")')107                continue108        filtered_tag_names.append(tag)109    print('filtering complete (' + str(len(tag_names) - len(filtered_tag_names)) + ' versions removed)')110    tag_names = filtered_tag_names111def get_release_asset(version, verbose=True):112    global releases113    global is_mac_os114    release_id = releases[version]['id']115    if verbose:116        print('getting installer for  "' + version + '" (release id ' + str(release_id) + ')...')117    # find correct asset for platform118    for asset in releases[version]['assets']:119        if str(asset['name']).endswith('.dmg') and is_mac_os:120            if verbose:121                print('- binary found: ' + asset['browser_download_url'])122            return asset123    if verbose:124        print('- binary not found')125    return None126def get_release_channel(version):127    global releases128    full_title = releases[version]['name']129    if full_title.startswith('Nightly'):130        return 'nightly'131    if full_title.startswith('Developer'):132        return 'dev'133    if full_title.startswith('Beta'):134        return 'beta'135    if full_title.startswith('Release'):136        return 'release'137    return None138def install(download_dir, path):139    global is_mac_os140    if is_mac_os:141        print('- installing binary from DMG')142        print('-> mounting "' + path + '"')143        result = execute(['hdiutil', 'attach', path])144        # parse out the mounted volume145        volume = None146        result_lines = result.splitlines()147        for x in result_lines:148            x = x.strip()149            index = x.find('/Volumes/Brave')150            if index > -1:151                volume = x[index:]152                break153        if volume is None:154            raise Exception('[ERROR] did not find "/Volumes/Brave" sub-string in mount list!\n \155                             Full response from "hdiutil":\n' + result)156        print('-> mounted as "' + volume + '"')157        # in case volumes are already mounted, remove trailing " 1" or " 2" (etc)158        binary_name = volume.replace("/Volumes/", "")159        binary_name = re.sub("^\\d+\\s|\\s\\d+\\s|\\s\\d+$", "", binary_name) + '.app'160        volume_path = os.path.join(volume, binary_name)161        # copy binary to a temp folder162        print('-> copying "' + volume_path + '" to "' + download_dir + '"')163        result = execute(['cp', '-rp', volume_path, download_dir])164        print('-> copy complete')165        print('-> unmounting "' + volume + '"')166        result = execute(['hdiutil', 'detach', volume])167        return os.path.join(download_dir, binary_name)168def setup_profile_directory(args):169    global is_mac_os170    if is_mac_os:171        print('- processing changes for profile directory')172        if args.real_profile:173            print('-> using real profile (`--real-profile` passed in)')174            return None175        profile_dir = tempdir('build-bisect-profile_')176        if args.use_profile:177            print('-> downloading profile: "' + args.use_profile + '"')178            try:179                filename = os.path.basename(args.use_profile)180                query_string_index = filename.find('?')181                if query_string_index > -1:182                    filename = filename[0:query_string_index]183                download_path = os.path.join(profile_dir, filename)184                download('profile', args.use_profile, download_path)185                if filename.endswith('.zip'):186                    print('-> unzipping to ' + profile_dir)187                    extract_zip(download_path, profile_dir)188            except Exception as e:189                print('whoops- ' + str(e))190        print('-> using profile directory: "' + profile_dir + '"')191        return profile_dir192def get_run_cmd(install_path, profile_dir):193    global is_mac_os194    if is_mac_os:195        run_cmd = ['open', '-a', install_path]196        run_params = []197        if profile_dir:198            run_params = ['--args', '--user-data-dir=' + profile_dir]199        return run_cmd + run_params200def test_version(args, attempt, tag):201    global tag_names202    # get the OS specific installer203    asset = None204    while len(tag_names) > 0 and not asset:205        print('\nattempt ' + str(attempt) + '] getting installer for  "' + tag +206              '" (release id ' + str(releases[tag]['id']) + ')')207        asset = get_release_asset(tag)208        if not asset:209            return False210    if not args.demo_mode:211        download_dir = tempdir('build-bisect_')212        download_path = os.path.join(download_dir, asset['name'])213        print('- downloading to ' + download_path)214        download(tag, asset['browser_download_url'], download_path)215        install_path = install(download_dir, download_path)216        profile_dir = setup_profile_directory(args)217        print('- running binary')218        run_cmd = get_run_cmd(install_path, profile_dir)219        execute(run_cmd)220    first = True221    while True:222        if not first:223            print('please type either `y` for yes or `n` for no!')224        answer = raw_input('Did this version work?: y/n\n')225        first = False226        if answer == 'y' or answer == 'n':227            break228    return answer == 'y'229def get_github_token():230    github_token = get_env_var('GITHUB_TOKEN')231    if len(github_token) == 0:232        result = execute(['npm', 'config', 'get', 'BRAVE_GITHUB_TOKEN']).strip()233        if result == 'undefined':234            raise Exception('`BRAVE_GITHUB_TOKEN` value not found!')235        return result236    else:237        return github_token238def get_nearest_index(version, index_to_get, default):239    global tag_names240    try:241        return tag_names.index(version)242    except Exception as e:243        print('- value "' + version + '" not found. ' + str(e))244        versions = version.split('.')245        if len(versions) != 3:246            return default247        versions.pop()248        results = [i for i in tag_names if i.startswith('.'.join(versions) + '.')]249        if len(results) == 0:250            return default251        print('-> using "' + results[index_to_get])252        return tag_names.index(results[index_to_get])253def find_first_broken_version(args):254    global tag_names255    print('bisecting: total of ' + str(len(tag_names)) + ' versions in search set')256    left_index = 0257    right_index = len(tag_names) - 1258    if left_index >= right_index:259        raise Exception('[ERROR] Not enough versions to perform search')260    if args.good:261        left_index = get_nearest_index(args.good, 0, left_index)262        if left_index == 0:263            args.good = None264        else:265            print('- starting at ' + tag_names[left_index])266    if args.bad:267        right_index = get_nearest_index(args.bad, -1, right_index)268        if right_index == len(tag_names) - 1:269            args.bad = None270        else:271            print('- ending at ' + tag_names[right_index])272    if args.good or args.bad:273        print('- search set narrowed down to ' + str(right_index - left_index) +274              ' versions using provided good/bad version(s)')275    attempt_number = 1276    # left should be working277    if not args.good:278        left_tag = tag_names[left_index]279        result = test_version(args, attempt_number, left_tag)280        attempt_number = attempt_number + 1281        if result is False:282            raise Exception('[ERROR] Version "' + left_tag + '" is expected to work but doesn\'t')283    # right should be NOT working284    if not args.bad:285        right_tag = tag_names[right_index]286        result = test_version(args, attempt_number, right_tag)287        attempt_number = attempt_number + 1288        if result is True:289            raise Exception('[ERROR] Version "' + right_tag + '" is expected to fail but doesn\'t')290    # perform search291    works_from = left_index292    fails_at = right_index293    while (fails_at - works_from) > 1:294        test_index = int(math.floor((left_index + right_index) / 2))295        test_tag = tag_names[test_index]296        if args.verbose:297            print('\n[DEBUG]' +298                  '\nworks_from=' + tag_names[works_from] + ' (' + str(works_from) + ')' +299                  '\nfails_at=' + tag_names[fails_at] + ' (' + str(fails_at) + ')' +300                  '\nleft_index=' + tag_names[left_index] + ' (' + str(left_index) + ')' +301                  '\nright_index=' + tag_names[right_index] + ' (' + str(right_index) + ')' +302                  '\ntest_index=' + tag_names[test_index] + ' (' + str(test_index) + ')' +303                  '\ngap=' + str(fails_at - works_from))304        result = test_version(args, attempt_number, test_tag)305        if left_index == right_index:306            if result:307                return tag_names[fails_at], attempt_number308            return test_tag, attempt_number309        if result:310            works_from = max(test_index, works_from)311            left_index = test_index + 1312        else:313            fails_at = min(test_index, fails_at)314            right_index = test_index - 1315        attempt_number = attempt_number + 1316    return tag_names[test_index], attempt_number317def main():318    global tag_names319    supported_platforms = ['Darwin']320    if platform.system() not in supported_platforms:321        print('Error: Platform \'{}\' not supported; acceptable platform(s): {}'322              .format(platform.system(), ", ".join(supported_platforms)))323        exit(1)324    args = parse_args()325    if args.real_profile and args.use_profile:326        print('[ERROR] you can\'t use both `--fresh-profile` AND `--use-profile` at the same time.')327        return 1328    github_token = get_github_token()329    repo = GitHub(github_token).repos(BRAVE_REPO)330    get_releases(repo)331    tag_names.sort(key=lambda s: map(int, s.split('.')))332    filter_releases(args)333    first_broken_version, attempts = find_first_broken_version(args)334    print('DONE: issue first appeared in "' + str(first_broken_version) + '" (found in ' + str(attempts) + ' attempts)')335    try:336        broken_index = tag_names.index(first_broken_version)337        if broken_index > 0:338            previous_release = tag_names[broken_index - 1]339            versions = 'v' + previous_release + '..v' + first_broken_version340            if args.verbose:341                print('[INFO] finding commits using "git log --pretty=oneline ' + versions + '"')342            commits = execute(['git', 'log', '--pretty=oneline', versions]).strip()343            commit_lines = commits.split('\n')344            print('Commits specific to tag "v' + first_broken_version + '" (' + str(len(commit_lines)) + ' commit(s)):')345            print(commits)346    except Exception as e:347        print('[ERROR] ' + str(e))348if __name__ == '__main__':349    import sys...setup.py
Source:setup.py  
1""" bot that takes produces a fingerprint with the current browser and . """2import os3from config import FIREFOX_VERSION4from config import GECKODRIVER_VERSION5from config import RESOURCE_FOLDER6from config import FIREFOX_DIR7from config import GECKODRIVER_DIR8from config import FIREFOX_URL9from config import GECKODRIVER_URL10from config import FIREFOX_BINARY11from config import GECKODRIVER_BINARY12from config import USE_NIGHTLY13from config import IS_MAC_OS14import pdb15def does_exist(file_with_path):16	"""Checks if a file exists """17	return os.path.isfile(file_with_path)18def set_up_firefox():19	""" Downloads Firefox """20	print("Downloading Firefox {}".format(FIREFOX_VERSION))21	22	OS_STRING = "mac" if IS_MAC_OS else "linux-x86_64"23	fx_url = FIREFOX_URL.format(FIREFOX_VERSION, OS_STRING)24	25	if USE_NIGHTLY and IS_MAC_OS:26		status = os.system("wget https://queue.taskcluster.net/v1/task/EPaShNEQTYaBrJYpULyxwg/runs/0/artifacts/public/build/target.dmg")27		if status != 0:28			raise Exception("Could not download Firefox Nightly.")29		os.system("hdiutil attach -nobrowse -mountpoint /Volumes/firefox-tmp target.dmg")30	elif USE_NIGHTLY:31		status = os.system("wget https://queue.taskcluster.net/v1/task/HYGMEM_UT06yMsOpWtHyVQ/runs/0/artifacts/public/build/target.tar.bz2")32		if status != 0:33			raise Exception("Could not download Firefox Nightly.")34		os.system("tar jxf target.tar.bz2")35	elif IS_MAC_OS:36		status = os.system("wget -O target.dmg {}".format(fx_url))37		if status != 0:38			raise Exception("Could not download Firefox.")39		os.system("hdiutil attach -nobrowse -mountpoint /Volumes/firefox-tmp target.dmg")40	else:41		status = os.system("wget -O target.tar.bz2 {}".format(fx_url))42		if status != 0:43			raise Exception("Could not download Firefox.")44		os.system("tar jxf target.tar.bz2")45	46	path = os.path.join(FIREFOX_DIR, FIREFOX_VERSION)47	os.system("mkdir {}".format(path))48	49	if USE_NIGHTLY and IS_MAC_OS:50		os.system("cp -r /Volumes/firefox-tmp/Nightly.app {}/Firefox.app".format(path))51		os.system("hdiutil detach /Volumes/firefox-tmp")52		os.system("rm target.dmg")53	elif USE_NIGHTLY:54		os.system("mv firefox {}/firefox".format(path))55		os.system("rm target.tar.bz2")56	elif IS_MAC_OS:57		os.system("cp -r /Volumes/firefox-tmp/Firefox.app {}/Firefox.app".format(path))58		os.system("hdiutil detach /Volumes/firefox-tmp")59		os.system("rm target.dmg")60	else:61		print(path)62		os.system("mv firefox {}/firefox".format(path))63		os.system("rm target.tar.bz2")64def set_up_geckodriver():65	""" Downloads the files Geckodriver """66	print("Downloading Geckodriver {}".format(GECKODRIVER_VERSION))67	OS_STRING = "macos" if IS_MAC_OS else "linux64"68	69	geckodriver_url = GECKODRIVER_URL.format(GECKODRIVER_VERSION, OS_STRING)70	status = os.system("wget -O geckodriver.tar.gz {}".format(geckodriver_url))71	#if status != 0:72	#	 raise Exception("Could not download Geckodriver.")73	74	75	path = os.path.join(GECKODRIVER_DIR, GECKODRIVER_VERSION)76	os.system("mkdir {}".format(path))77	os.system("tar -C {} -xzf geckodriver.tar.gz".format(path))78	# also introduce commad to move geckodriver to the correct path79	os.system("rm geckodriver.tar.gz")80def set_up_environment():81	if not does_exist(FIREFOX_BINARY):82		set_up_firefox()83	84	if not does_exist(GECKODRIVER_BINARY):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
