How to use md5sum method in fMBT

Best Python code snippet using fMBT_python

md5sum.py

Source:md5sum.py Github

copy

Full Screen

1# Copyright 2014 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4import os5import posixpath6import re7from devil import devil_env8from devil.android import device_errors9from devil.utils import cmd_helper10MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum'11MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + '/md5sum_bin'12_STARTS_WITH_CHECKSUM_RE = re.compile(r'^\s*[0-9a-fA-F]{32}\s+')13def CalculateHostMd5Sums(paths):14 """Calculates the MD5 sum value for all items in |paths|.15 Directories are traversed recursively and the MD5 sum of each file found is16 reported in the result.17 Args:18 paths: A list of host paths to md5sum.19 Returns:20 A dict mapping file paths to their respective md5sum checksums.21 """22 if isinstance(paths, basestring):23 paths = [paths]24 md5sum_bin_host_path = devil_env.config.FetchPath('md5sum_host')25 if not os.path.exists(md5sum_bin_host_path):26 raise IOError('File not built: %s' % md5sum_bin_host_path)27 out = cmd_helper.GetCmdOutput(28 [md5sum_bin_host_path] + [os.path.realpath(p) for p in paths])29 return _ParseMd5SumOutput(out.splitlines())30def CalculateDeviceMd5Sums(paths, device):31 """Calculates the MD5 sum value for all items in |paths|.32 Directories are traversed recursively and the MD5 sum of each file found is33 reported in the result.34 Args:35 paths: A list of device paths to md5sum.36 Returns:37 A dict mapping file paths to their respective md5sum checksums.38 """39 if not paths:40 return {}41 if isinstance(paths, basestring):42 paths = [paths]43 # Allow generators44 paths = list(paths)45 md5sum_dist_path = devil_env.config.FetchPath('md5sum_device', device=device)46 if os.path.isdir(md5sum_dist_path):47 md5sum_dist_bin_path = os.path.join(md5sum_dist_path, 'md5sum_bin')48 else:49 md5sum_dist_bin_path = md5sum_dist_path50 if not os.path.exists(md5sum_dist_path):51 raise IOError('File not built: %s' % md5sum_dist_path)52 md5sum_file_size = os.path.getsize(md5sum_dist_bin_path)53 # For better performance, make the script as small as possible to try and54 # avoid needing to write to an intermediary file (which RunShellCommand will55 # do if necessary).56 md5sum_script = 'a=%s;' % MD5SUM_DEVICE_BIN_PATH57 # Check if the binary is missing or has changed (using its file size as an58 # indicator), and trigger a (re-)push via the exit code.59 md5sum_script += '! [[ $(ls -l $a) = *%d* ]]&&exit 2;' % md5sum_file_size60 # Make sure it can find libbase.so61 md5sum_script += 'export LD_LIBRARY_PATH=%s;' % MD5SUM_DEVICE_LIB_PATH62 if len(paths) > 1:63 prefix = posixpath.commonprefix(paths)64 if len(prefix) > 4:65 md5sum_script += 'p="%s";' % prefix66 paths = ['$p"%s"' % p[len(prefix):] for p in paths]67 md5sum_script += ';'.join('$a %s' % p for p in paths)68 # Don't fail the script if the last md5sum fails (due to file not found)69 # Note: ":" is equivalent to "true".70 md5sum_script += ';:'71 try:72 out = device.RunShellCommand(73 md5sum_script, shell=True, check_return=True, large_output=True)74 except device_errors.AdbShellCommandFailedError as e:75 # Push the binary only if it is found to not exist76 # (faster than checking up-front).77 if e.status == 2:78 # If files were previously pushed as root (adbd running as root), trying79 # to re-push as non-root causes the push command to report success, but80 # actually fail. So, wipe the directory first.81 device.RunShellCommand(['rm', '-rf', MD5SUM_DEVICE_LIB_PATH],82 as_root=True, check_return=True)83 if os.path.isdir(md5sum_dist_path):84 device.adb.Push(md5sum_dist_path, MD5SUM_DEVICE_LIB_PATH)85 else:86 mkdir_cmd = 'a=%s;[[ -e $a ]] || mkdir $a' % MD5SUM_DEVICE_LIB_PATH87 device.RunShellCommand(mkdir_cmd, shell=True, check_return=True)88 device.adb.Push(md5sum_dist_bin_path, MD5SUM_DEVICE_BIN_PATH)89 out = device.RunShellCommand(90 md5sum_script, shell=True, check_return=True, large_output=True)91 else:92 raise93 return _ParseMd5SumOutput(out)94def _ParseMd5SumOutput(out):95 hash_and_path = (l.split(None, 1) for l in out96 if l and _STARTS_WITH_CHECKSUM_RE.match(l))...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful