Best Python code snippet using autotest_python
util.py
Source:util.py
1from urllib import request2import os3import platform4import json5import re6import shutil7import subprocess8import tarfile9import zipfile10# Runs a go subprocess and get the installed version11def get_installed_go_version():12 out = subprocess.run(['go','version'], capture_output=True).stdout13 result = re.search('go(\d+)\.(\d+)\.?(\d+)?(\w+)*', out.decode('utf-8'))14 if result:15 return result[0]16# Get a list of go releases using Github api17def get_go_releases():18 URL = 'https://api.github.com/repos/golang/go/git/matching-refs/tags/go'19 req = request.urlopen(URL)20 jsonResponse = json.loads(req.read())21 tagCount = len(jsonResponse)22 if tagCount > 0:23 releases = [jsonResponse[x]['ref'] for x in range(tagCount)]24 releases = [x[x.rindex('/')+1:] for x in releases]25 return releases26# Returns the latest go version string available27# If allow_preview is True include betas and release candidates28def get_go_release(allow_preview=False):29 releases = get_go_releases()30 if releases:31 for version in releases[::-1]:32 ver_info = parse_version(version)33 if ver_info['is_beta'] or ver_info['is_release_candidate']:34 if allow_preview:35 return version36 else:37 return version38# Returns a dictionary with fields version, major, minor, patch, is_beta and is_release_candidate39def parse_version(ver):40 matches = re.search('(\d+)\.(\d+)\.?(\d+)?(\w+)*', ver)41 if matches:42 version = matches[0]43 major = matches[1]44 minor = matches[2]45 patch = matches[3] or 046 beta = None47 release_candidate = None48 if matches[4]:49 beta = True if matches[4].find('beta') >= 0 else False50 release_candidate = True if matches[4].find('rc') >= 0 else False51 return {52 'version': version,53 'major': int(major),54 'minor': int(minor),55 'patch': int(patch),56 'is_beta': beta,57 'is_release_candidate': release_candidate58 }59# Returns 0 if ver1 and ver2 match60# Returns a positive number if ver1 is newer than ver261# Returns a negative number if ver2 is newer than ver162def compare_versions(ver_info1, ver_info2):63 result = 064 result += 100000 * (ver_info1['major'] - ver_info2['major'])65 result += 10 * (ver_info1['minor'] - ver_info2['minor'])66 result += ver_info1['patch'] - ver_info2['patch']67 if ver_info1['is_beta']: result -= 168 if ver_info1['is_release_candidate']: result -= 269 if ver_info2['is_beta']: result += 170 if ver_info2['is_release_candidate']: result += 271 return result72# Returns True if ver2 is newer than ver1. Returns False otherwise.73# If allow_preview is True release canditates and betas will count as valid74# to replace ver1.75def should_update(ver1, ver2, allow_preview=False):76 ver_info1 = parse_version(ver1)77 ver_info2 = parse_version(ver2)78 result = compare_versions(ver_info1, ver_info2)79 if result < 0:80 is_preview = ver_info2['is_beta'] or ver_info2['is_release_candidate']81 if is_preview:82 return allow_preview83 else:84 return True85 return False86def get_update_version(allow_preview):87 try:88 installed_version = get_installed_go_version()89 releases = get_go_releases()90 # Compute only later versions91 index = releases.index(installed_version)92 candidates = releases[index+1:]93 # Find latest version that is a suitable replacement94 for candidate in candidates[::-1]:95 if should_update(installed_version, candidate, allow_preview):96 return candidate97 except json.JSONDecodeError as e:98 print('Could not get release info from Github')99 except subprocess.CalledProcessError as e:100 print('Could not get installed go version')101 except ValueError as e:102 print('Error interpreting go version scheme')103def build_release_file_name(version):104 system = platform.system().lower()105 arch = archMap[platform.machine()]106 extension = extensionMap[system]107 version = version if version.find('go') == 0 else 'go' + version108 name = version + '.' + system + '-' + arch + extension109 return name110# Donwload file and show progress report111def donwload_file(name):112 request.urlretrieve(113 'https://go.dev/dl/' + name,114 name,115 progress_report116 )117 print() # New line after donwload is finished118# Remove current go installtion119def remove_installation():120 go_location = shutil.which('go')121 if go_location:122 go_location = go_location[:go_location.rindex(os.sep + 'bin')]123 parent_location = go_location[:go_location.rindex(os.sep + 'go')]124 if go_location:125 shutil.rmtree(go_location)126 return parent_location127def extract_file(name, location):128 if name.endswith('.tar.gz'):129 compressed_file = tarfile.open(name, mode='r:gz')130 compressed_file.extractall(path=location)131 elif name.endswith('.zip'):132 compressed_file = zipfile.ZipFile(name)133 compressed_file.extractall(path=location)134def progress_report(count, blocksize, totalsize):135 print('\rProgress: ' + str(round((count * blocksize) * 100 / totalsize)) + '%', end='')136archMap = {137 'AMD64': 'amd64',138 'x86_64': 'amd64',139 'i386': '386',140 'i686': '386',141 '386': '386',142 'aarch64': 'arm64',143 'arm64': 'arm64',144 'arm': 'armv6l',145 'armv7l': 'armv6l',146 'armv6l': 'armv6l'147}148extensionMap = {149 'linux': '.tar.gz',150 'windows': '.zip',151 'darwin': '.pkg'152}153def update_go_version(allow_preview):154 # Check if there is an update available155 version = get_update_version(allow_preview)156 if version:157 print('Version ' + version + ' available')158 print('Downloading file...')159 # Build release name based on platform160 name = build_release_file_name(version)161 # Download go release162 donwload_file(name)163 # Remove current installtion164 print('Removing current installation...')165 go_location = remove_installation()166 # Extract new version167 print('Extracting file...')168 extract_file(name, go_location)169 # Remove downloaded file170 print('Removing temporary file...')171 os.remove(name)172 print('Go version updated')173 else:174 print('Your already have the latest version')175def install_go_version(install_path, version, allow_preview):176 target_version = version177 if not target_version:178 release = get_go_release(allow_preview)179 if release:180 target_version = release181 else:182 raise RuntimeError('Could not find a go release')183 if parse_version(target_version):184 name = build_release_file_name(target_version)185 print('Downloading go version ' + target_version)186 donwload_file(name)187 # Create directory if it does not exist188 if not os.path.isdir(install_path):189 os.mkdir(install_path)190 print('Extracting file...')191 extract_file(name, install_path)192 # Remove downloaded file193 print('Removing temporary file...')194 os.remove(name)195 print('Installation complete')196 bin_path = os.path.abspath(install_path + os.sep + 'go' + os.sep + 'bin')...
post_version.py
Source:post_version.py
...26 html,27)28def is_first_release(version: str) -> bool:29 return version.endswith('.0')30def is_release_candidate(version: str) -> bool:31 return '-rc' in version32def branch_name(version: str) -> str:33 if is_release_candidate(version):34 version = version.split('-')[0]35 (major, minor, _) = version.split('.')36 return f'{major}.{minor}'37def update_index(version: str) -> None:38 p = pathlib.Path(__file__).parent.parent / 'docs' / 'index.html'39 with p.open('rt') as f:40 tree = html.parse(f)41 news = tree.xpath('.//h1')[0]42 date = datetime.date.today()43 month = calendar.month_name[date.month]44 header = etree.Element('h2')45 header.text = f"{month} {date.day}, {date.year}"46 body = etree.Element('p')47 a = etree.SubElement(48 body, 'a', attrib={'href': f'relnotes/{version}.html'})49 a.text = f"Mesa {version}"50 if is_first_release(version):51 a.tail = (" is released. This is a new development release. "52 "See the release notes for more information about this release.")53 else:54 a.tail = " is released. This is a bug fix release."55 root = news.getparent()56 index = root.index(news) + 157 root.insert(index, body)58 root.insert(index, header)59 tree.write(p.as_posix(), method='html', pretty_print=True)60 subprocess.run(['git', 'add', p])61def update_release_notes(version: str) -> None:62 p = pathlib.Path(__file__).parent.parent / 'docs' / 'relnotes.html'63 with p.open('rt') as f:64 tree = html.parse(f)65 li = etree.Element('li')66 a = etree.SubElement(li, 'a', href=f'relnotes/{version}.html')67 a.text = f'{version} release notes'68 ul = tree.xpath('.//ul')[0]69 ul.insert(0, li)70 tree.write(p.as_posix(), method='html', pretty_print=True)71 subprocess.run(['git', 'add', p])72def update_calendar(version: str) -> None:73 p = pathlib.Path(__file__).parent.parent / 'docs' / 'release-calendar.html'74 with p.open('rt') as f:75 tree = html.parse(f)76 branch = branch_name(version)77 old = None78 new = None79 for tr in tree.xpath('.//tr'):80 if old is not None:81 new = tr82 break83 for td in tr.xpath('./td'):84 if td.text == branch:85 old = tr86 break87 assert old is not None88 assert new is not None89 old.getparent().remove(old)90 # rowspan is 1 based in html, but 0 based in lxml91 rowspan = int(td.get("rowspan")) - 192 if rowspan:93 td.set("rowspan", str(rowspan))94 new.insert(0, td)95 tree.write(p.as_posix(), method='html', pretty_print=True)96 subprocess.run(['git', 'add', p])97def main() -> None:98 parser = argparse.ArgumentParser()99 parser.add_argument('version', help="The released version.")100 args = parser.parse_args()101 update_calendar(args.version)102 done = 'update calendar'103 if not is_release_candidate(args.version):104 update_index(args.version)105 update_release_notes(args.version)106 done += ', add news item, and link releases notes'107 subprocess.run(['git', 'commit', '-m',108 f'docs: {done} for {args.version}'])109if __name__ == "__main__":...
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!!