Best Python code snippet using localstack_python
update.py
Source:update.py
1#!/usr/bin/env python2# coding: utf-83# Copyright 2015 The Chromium Authors. All rights reserved.4# Use of this source code is governed by a BSD-style license that can be5# found in the LICENSE file.6from __future__ import print_function7import argparse8import os9import pipes10import re11import subprocess12import sys13import tempfile14import textwrap15if sys.version_info[0] < 3:16 input = raw_input17IS_WINDOWS = sys.platform.startswith('win')18def SubprocessCheckCall0Or1(args):19 """Like subprocss.check_call(), but allows a return code of 1.20 Returns True if the subprocess exits with code 0, False if it exits with21 code 1, and re-raises the subprocess.check_call() exception otherwise.22 """23 try:24 subprocess.check_call(args, shell=IS_WINDOWS)25 except subprocess.CalledProcessError as e:26 if e.returncode != 1:27 raise28 return False29 return True30def GitMergeBaseIsAncestor(ancestor, descendant):31 """Determines whether |ancestor| is an ancestor of |descendant|.32 """33 return SubprocessCheckCall0Or1(34 ['git', 'merge-base', '--is-ancestor', ancestor, descendant])35def main(args):36 parser = argparse.ArgumentParser(37 description='Update the in-tree copy of an imported project')38 parser.add_argument(39 '--repository',40 default='https://chromium.googlesource.com/crashpad/crashpad',41 help='The imported project\'s remote fetch URL',42 metavar='URL')43 parser.add_argument(44 '--subtree',45 default='third_party/crashpad/crashpad',46 help='The imported project\'s location in this project\'s tree',47 metavar='PATH')48 parser.add_argument(49 '--update-to',50 default='FETCH_HEAD',51 help='What to update the imported project to',52 metavar='COMMITISH')53 parser.add_argument(54 '--fetch-ref',55 default='HEAD',56 help='The remote ref to fetch',57 metavar='REF')58 parser.add_argument(59 '--readme',60 help='The README.chromium file describing the imported project',61 metavar='FILE',62 dest='readme_path')63 parser.add_argument(64 '--exclude',65 default=['codereview.settings'],66 action='append',67 help='Files to exclude from the imported copy',68 metavar='PATH')69 parsed = parser.parse_args(args)70 original_head = (71 subprocess.check_output(['git', 'rev-parse', 'HEAD'],72 shell=IS_WINDOWS).rstrip())73 # Read the README, because thatâs what itâs for. Extract some things from74 # it, and save it to be able to update it later.75 readme_path = (parsed.readme_path or76 os.path.join(os.path.dirname(__file__ or '.'),77 'README.chromium'))78 readme_content_old = open(readme_path, 'rb').read().decode('utf-8')79 project_name_match = re.search(80 r'^Name:\s+(.*)$', readme_content_old, re.MULTILINE)81 project_name = project_name_match.group(1)82 # Extract the original commit hash from the README.83 revision_match = re.search(r'^Revision:\s+([0-9a-fA-F]{40})($|\s)',84 readme_content_old,85 re.MULTILINE)86 revision_old = revision_match.group(1)87 subprocess.check_call(['git', 'fetch', parsed.repository, parsed.fetch_ref],88 shell=IS_WINDOWS)89 # Make sure that parsed.update_to is an ancestor of FETCH_HEAD, and90 # revision_old is an ancestor of parsed.update_to. This prevents the use of91 # hashes that are known to git but that donât make sense in the context of92 # the update operation.93 if not GitMergeBaseIsAncestor(parsed.update_to, 'FETCH_HEAD'):94 raise Exception('update_to is not an ancestor of FETCH_HEAD',95 parsed.update_to,96 'FETCH_HEAD')97 if not GitMergeBaseIsAncestor(revision_old, parsed.update_to):98 raise Exception('revision_old is not an ancestor of update_to',99 revision_old,100 parsed.update_to)101 # git-filter-branch needs a ref to update. Itâs not enough to just tell it102 # to operate on a range of commits ending at parsed.update_to, because103 # parsed.update_to is a commit hash that canât be updated to point to104 # anything else.105 subprocess.check_call(['git', 'update-ref', 'UPDATE_TO', parsed.update_to],106 shell=IS_WINDOWS)107 # Filter the range being updated over to exclude files that ought to be108 # missing. This points UPDATE_TO to the rewritten (filtered) version.109 # git-filter-branch insists on running from the top level of the working110 # tree.111 toplevel = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'],112 shell=IS_WINDOWS).rstrip()113 subprocess.check_call(114 ['git',115 'filter-branch',116 '--force',117 '--index-filter',118 'git rm --cached --ignore-unmatch ' +119 ' '.join(pipes.quote(path) for path in parsed.exclude),120 revision_old + '..UPDATE_TO'],121 cwd=toplevel,122 shell=IS_WINDOWS)123 # git-filter-branch saved a copy of the original UPDATE_TO at124 # original/UPDATE_TO, but this isnât useful because it refers to the same125 # thing as parsed.update_to, which is already known.126 subprocess.check_call(127 ['git', 'update-ref', '-d', 'refs/original/UPDATE_TO'],128 shell=IS_WINDOWS)129 filtered_update_range = revision_old + '..UPDATE_TO'130 unfiltered_update_range = revision_old + '..' + parsed.update_to131 # This cherry-picks each change in the window from the filtered view of the132 # upstream project into the current branch.133 assisted_cherry_pick = False134 try:135 if not SubprocessCheckCall0Or1(['git',136 'cherry-pick',137 '--keep-redundant-commits',138 '--strategy=subtree',139 '-Xsubtree=' + parsed.subtree,140 '-x',141 filtered_update_range]):142 assisted_cherry_pick = True143 print("""144Please fix the errors above and run "git cherry-pick --continue".145Press Enter when "git cherry-pick" completes.146You may use a new shell for this, or ^Z if job control is available.147Press ^C to abort.148""", file=sys.stderr)149 input()150 except:151 # ^C, signal, or something else.152 print('Aborting...', file=sys.stderr)153 subprocess.call(['git', 'cherry-pick', '--abort'], shell=IS_WINDOWS)154 raise155 # Get an abbreviated hash and subject line for each commit in the window,156 # sorted in chronological order. Use the unfiltered view so that the commit157 # hashes are recognizable.158 log_lines = subprocess.check_output(159 ['git',160 '-c',161 'core.abbrev=12',162 'log',163 '--abbrev-commit',164 '--pretty=oneline',165 '--reverse',166 unfiltered_update_range],167 shell=IS_WINDOWS).decode('utf-8').splitlines(False)168 if assisted_cherry_pick:169 # If the user had to help, count the number of cherry-picked commits,170 # expecting it to match.171 cherry_picked_commits = int(subprocess.check_output(172 ['git', 'rev-list', '--count', original_head + '..HEAD'],173 shell=IS_WINDOWS))174 if cherry_picked_commits != len(log_lines):175 print('Something smells fishy, aborting anyway...', file=sys.stderr)176 subprocess.call(['git', 'cherry-pick', '--abort'], shell=IS_WINDOWS)177 raise Exception('not all commits were cherry-picked',178 len(log_lines),179 cherry_picked_commits)180 # Make a nice commit message. Start with the full commit hash.181 revision_new = subprocess.check_output(182 ['git', 'rev-parse', parsed.update_to],183 shell=IS_WINDOWS).decode('utf-8').rstrip()184 new_message = u'Update ' + project_name + ' to ' + revision_new + '\n\n'185 # Wrap everything to 72 characters, with a hanging indent.186 wrapper = textwrap.TextWrapper(width=72, subsequent_indent = ' ' * 13)187 for line in log_lines:188 # Strip trailing periods from subjects.189 if line.endswith('.'):190 line = line[:-1]191 # If any subjects have what look like commit hashes in them, truncate192 # them to 12 characters.193 line = re.sub(r'(\s)([0-9a-fA-F]{12})([0-9a-fA-F]{28})($|\s)',194 r'\1\2\4',195 line)196 new_message += '\n'.join(wrapper.wrap(line)) + '\n'197 # Update the README with the new hash.198 readme_content_new = re.sub(199 r'^(Revision:\s+)([0-9a-fA-F]{40})($|\s.*?$)',200 r'\g<1>' + revision_new,201 readme_content_old,202 1,203 re.MULTILINE)204 # If the in-tree copy has no changes relative to the upstream, clear the205 # âLocal Modificationsâ section of the README.206 has_local_modifications = True207 if SubprocessCheckCall0Or1(['git',208 'diff-tree',209 '--quiet',210 'UPDATE_TO',211 'HEAD:' + parsed.subtree]):212 has_local_modifications = False213 if not parsed.exclude:214 modifications = 'None.\n'215 elif len(parsed.exclude) == 1:216 modifications = (217 ' - %s has been excluded.\n' % parsed.exclude[0])218 else:219 modifications = (220 ' - The following files have been excluded:\n')221 for excluded in sorted(parsed.exclude):222 modifications += ' - ' + excluded + '\n'223 readme_content_new = re.sub(r'\nLocal Modifications:\n.*$',224 '\nLocal Modifications:\n' + modifications,225 readme_content_new,226 1,227 re.DOTALL)228 # The UPDATE_TO ref is no longer useful.229 subprocess.check_call(['git', 'update-ref', '-d', 'UPDATE_TO'],230 shell=IS_WINDOWS)231 # This soft-reset causes all of the cherry-picks to show up as staged, which232 # will have the effect of squashing them along with the README update when233 # committed below.234 subprocess.check_call(['git', 'reset', '--soft', original_head],235 shell=IS_WINDOWS)236 # Write the new README.237 open(readme_path, 'wb').write(readme_content_new.encode('utf-8'))238 # Commit everything.239 subprocess.check_call(['git', 'add', readme_path], shell=IS_WINDOWS)240 try:241 commit_message_name = None242 with tempfile.NamedTemporaryFile(mode='wb',243 delete=False) as commit_message_f:244 commit_message_name = commit_message_f.name245 commit_message_f.write(new_message.encode('utf-8'))246 subprocess.check_call(['git',247 'commit', '--file=' + commit_message_name],248 shell=IS_WINDOWS)249 finally:250 if commit_message_name:251 os.unlink(commit_message_name)252 if has_local_modifications:253 print('Remember to check the Local Modifications section in ' +254 readme_path, file=sys.stderr)255 return 0256if __name__ == '__main__':...
Tools.py
Source:Tools.py
...16 os.remove(self.__filename)17class PlatformHolder(type):18 PLATFORM_NAME = platform.system()19 @staticmethod20 def is_windows():21 return 'Windows' in PlatformHolder.PLATFORM_NAME22 @staticmethod23 def is_mac():24 return 'Darwin' in PlatformHolder.PLATFORM_NAME25 @staticmethod26 def is_linux():27 return 'Linux' in PlatformHolder.PLATFORM_NAME28def set_title(title):29 if PlatformHolder.is_mac():30 os.system("osascript -e 'tell application \"Terminal\" to set custom title of front window to \"%s\"'" % title)31def get_title():32 if PlatformHolder.is_mac():33 name = call("osascript -e 'tell application \"Terminal\" to get custom title of front window'")34 if len(name) > 0:35 name = str(name[0]).strip()36 return name37def close_terminal_window(title):38 if PlatformHolder.is_mac():39 os.system("osascript -e 'tell application \"Terminal\" to close (every window whose custom title is \"%s\")'" % title)40def close_process(pid):41 if PlatformHolder.is_windows():42 pass43 else:44 os.system('kill -9 %s' % pid)45def call_terminal(arg):46 if PlatformHolder.is_windows():47 subprocess.call('start cmd /K %s' % arg, shell=True)48 else:49 os.system('open -a /Applications/Utilities/Terminal.app %s' % arg)50def call(*args):51 try:52 return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()53 except Exception as exc:54 return exc55def get_parent_id(pid):56 if not PlatformHolder.is_windows():57 result = call('ps -p %s -o ppid=' % pid)58 if len(result) > 0:59 return str(result[0]).strip()60def get_terminal_id():61 if not PlatformHolder.is_windows():62 return get_parent_id(get_parent_id(get_parent_id(os.getppid())))63def get_address_local():64 for interface_name in get_interfaces():65 interface = get_interface(interface_name)66 if PlatformHolder.is_windows():67 net = interface.get('IP Address')68 elif PlatformHolder.is_mac():69 net = interface.get('inet')70 else:71 net = interface.get('inet addr')72 if net and net.startswith('127.'):73 return net74def get_address():75 net = None76 for interface_name in get_interfaces():77 interface = get_interface(interface_name)78 if PlatformHolder.is_windows():79 net = interface.get('IP Address')80 elif PlatformHolder.is_mac():81 if interface.get('status') == 'active':82 net = interface.get('inet')83 else:84 i = interface.get('inet addr')85 if i and not i.startswith('127.'):86 net = interface.get('inet addr')87 if net:88 return net89def get_interfaces():90 if PlatformHolder.is_windows():91 interfaces = subprocess.check_output('netsh interface show interface', shell=True).split('\n')[3:]92 return [item.split(' ')[-1].strip() for item in interfaces if item.strip() != '']93 elif PlatformHolder.is_mac():94 return subprocess.check_output('ifconfig -lu', shell=True).split(' ')95 else:96 interfaces = subprocess.check_output('ifconfig -s', shell=True).split('\n')[1:]97 return [item.split(' ')[0].strip() for item in interfaces]98def get_interface(interface_name):99 interface = {'name': interface_name}100 if PlatformHolder.is_windows():101 items = subprocess.check_output('netsh interface ip show addresses "%s"' % interface_name, shell=True).split('\n')102 items = [item.strip().split(':') for item in items[2:] if item.strip() != '']103 for item in items:104 try:105 interface.update({item[0].strip(): item[1].strip()})106 except IndexError:107 pass108 elif PlatformHolder.is_mac():109 for line in subprocess.check_output('ifconfig %s' % interface_name, shell=True).split('\n'):110 if '\t' in line:111 line = line.replace('\t', '')112 line = line.split(' ', 1)113 if len(line) > 1:114 items = str(line[1]).split(' ')115 interface.update({line[0].replace(':', '').strip(): items[0].strip()})116 count = 0117 for index in range(0, len(items)):118 try:119 interface.update({items[count].strip(): items[count + 1].strip()})120 except IndexError:121 pass122 count += 2123 else:124 items = subprocess.check_output('ifconfig %s | grep "Link\|inet"' % interface_name, shell=True).split('\n')125 items = [item.strip().split(':', 1) for item in items]126 for item in items:127 if item[0] and item[-1]:128 interface[item[0]] = item[-1].split(' ')[0].strip()129 return interface130def route_path():131 """132 Change the os directory to the current site.133 """134 os.chdir(up_path(get_path()))135def new_window_path():136 return '%s/new-window.command' % up_path(up_path(get_path()))137def up_path(path):138 return os.path.abspath(os.path.join(path, os.pardir))139def get_path():140 return os.path.realpath(inspect.getfile(inspect.currentframe()))141def clear_screen():142 """143 Clear the terminal/cmd screen.144 """145 if PlatformHolder.is_windows():146 os.system('cls')147 else:148 os.system('clear')149def terminate():150 """151 Destroy all pythons.152 """153 if PlatformHolder.is_windows():154 os.system('taskkill -f -im python.exe')155 else:156 os.system('sudo pkill -9 Python')157def get_epoch(date):158 return int(time.mktime(time.strptime(str(date), '%Y-%m-%d %H:%M:%S')))159def human_time(epoch=time.time()):160 """161 Return a human readable epoch.162 :param epoch:163 """164 return time.strftime("%a, %d %b %Y %I:%M:%S %p", time.localtime(epoch))165def human_gmt_time(epoch=time.time()):166 """167 Return a human readable epoch.168 :param epoch:169 """170 return time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(epoch))171def split_extension(filename):172 """173 Returns the name, ext of the given filename.174 :param filename: filename to split175 """176 name, ext = os.path.splitext(filename)177 ext = ext.replace(".", "")178 return name, ext179def get_files(path):180 """181 Parses through a directory and finds all files.182 :rtype : list183 :param path: path to parent of the files184 """185 try:186 return [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) and not f.startswith('.')]187 except OSError:188 return []189def applications():190 if os.path.isdir("/Applications"):191 return os.listdir("/Applications")192 return []193def open_in_browser(browser, url):194 if PlatformHolder.is_mac():195 if not browser:196 browser = 'Safari'197 for app in applications():198 if app.lower().find(browser.lower()) != -1:199 subprocess.call(["open", "-a", app, url])200 elif PlatformHolder.is_windows():201 if not browser:202 browser = 'iexplore'...
genericgrader.py
Source:genericgrader.py
1import argparse2import json3import platform4import shlex5import subprocess6import os7def _section_match(s1, s2):8 return str(s1) == str(s2) or (s1 is None and s2 is None)9def _row_match(r1, r2):10 return str(r1) == str(r2) or (r1 is None and r2 is None)11def grade_match(match, verbose=False):12 e_sid = match["expected"]["section_id"]13 e_rid = match["expected"]["row_id"]14 e_valid = match["expected"]["valid"]15 o_sid = match["output"]["section_id"]16 o_rid = match["output"]["row_id"]17 o_valid = match["output"]["valid"]18 i_s = match["input"]["section"]19 i_r = match["input"]["row"]20 # if expected is valid...21 sm = _section_match(e_sid, o_sid)22 rm = _row_match(e_rid, o_rid)23 vm = e_valid == o_valid24 pts = 025 if e_valid:26 if sm and rm and vm:27 if verbose:28 print(".. ok")29 pts = 130 if not vm:31 if verbose:32 print(33 f".. {i_s}:{i_r} marked invalid, should be {e_sid}:{e_rid}")34 pts = 035 if vm and (not sm or not rm):36 if verbose:37 print(38 f".. {i_s}:{i_r} WRONG!, marked {o_sid}:{o_rid}, should be {e_sid}:{e_rid}")39 pts = -540 if not e_valid:41 if not o_valid:42 if verbose:43 print(".. ok")44 pts = 145 else:46 if verbose:47 print(48 f".. {i_s}:{i_r} WRONG! Marked valid, should be invalid")49 pts = -550 return pts51def parse_output(k):52 stdout_output = k[0]53 lines = stdout_output.splitlines()54 valid_lines = []55 for line in lines:56 try:57 data = json.loads(line.strip())58 if "expected" in data and "output" in data and "input" in data:59 valid_lines.append(data)60 except:61 pass62 return valid_lines63def escape(filepath):64 return filepath.replace(" ", "\\ ")65def grade(path_to_manifest, path_to_input, path_to_executable, verbose=False, is_windows=False):66 _manifest = os.path.abspath(path_to_manifest)67 _input = os.path.abspath(path_to_input)68 _executable = os.path.abspath(path_to_executable)69 cmd = "{} --manifest {} --input {}".format(70 escape(_executable), escape(_manifest), escape(_input))71 if is_windows:72 p = subprocess.Popen(73 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)74 else:75 args = shlex.split(cmd)76 p = subprocess.Popen(77 args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)78 k = p.communicate()79 valid_lines = parse_output(k)80 total_pts = 081 max_pts = 082 for match in valid_lines:83 pts = grade_match(match, verbose=verbose)84 max_pts += 185 total_pts += pts86 print(f"{total_pts} / {max_pts}")87 return total_pts, max_pts88def grade_python(path_to_manifest, path_to_input, verbose=False, is_windows=False):89 executable = "python/normalize.cmd" if is_windows else "python/normalize"90 grade(path_to_manifest, path_to_input, executable,91 verbose=verbose, is_windows=is_windows)92def grade_ruby(path_to_manifest, path_to_input, verbose=False, is_windows=False):93 executable = "ruby/normalize.cmd" if is_windows else "ruby/normalize"94 grade(path_to_manifest, path_to_input, executable,95 verbose=verbose, is_windows=is_windows)96def grade_csharp(path_to_manifest, path_to_input, verbose=False, is_windows=False):97 executable = "csharp/normalize.cmd" if is_windows else "csharp/normalize"98 grade(path_to_manifest, path_to_input, executable,99 verbose=verbose, is_windows=is_windows)100def grade_java(path_to_manifest, path_to_input, verbose=False, is_windows=False):101 executable = "java/normalize.cmd" if is_windows else "java/normalize"102 grade(path_to_manifest, path_to_input, executable,103 verbose=verbose, is_windows=is_windows)104def grade_php(path_to_manifest, path_to_input, verbose=False, is_windows=False):105 executable = "php/normalize.cmd" if is_windows else "php/normalize"106 grade(path_to_manifest, path_to_input, executable,107 verbose=verbose, is_windows=is_windows)108if __name__ == "__main__":109 parser = argparse.ArgumentParser(110 description="grader for SeatGeek SectionNormalization code test")111 parser.add_argument("--manifest", default=None,112 help="path to manifest file")113 parser.add_argument("--input", default=None, help="path to input file")114 parser.add_argument("--lang", default="python")115 parser.add_argument("--verbose", action="store_true", default=False)116 args = parser.parse_args()117 is_windows = platform.system() == "Windows"118 assert args.lang in ("python3", "ruby", "c#", "java", "php")119 assert args.manifest and args.input120 if args.lang == "python3":121 grade_python(args.manifest, args.input, args.verbose, is_windows)122 if args.lang == "ruby":123 grade_ruby(args.manifest, args.input, args.verbose, is_windows)124 if args.lang == "c#":125 grade_csharp(args.manifest, args.input, args.verbose, is_windows)126 if args.lang == "java":127 grade_java(args.manifest, args.input, args.verbose, is_windows)128 if args.lang == "php":...
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!!