How to use calculate_crc32 method in localstack

Best Python code snippet using localstack_python

rcrc32.py

Source:rcrc32.py Github

copy

Full Screen

...27 **kwargs28 )29def fcrc32(value):30 return '{:0>8X}'.format(value)31def calculate_crc32(path):32 logger.debug('Calculating the CRC32 of "%s"', path)33 csum = 034 with open(path, 'rb') as f:35 while True:36 chunk = f.read(1 << 16)37 if not chunk:38 break39 csum = zlib.crc32(chunk, csum)40 return csum & 0xffffffff41def extract_crc32(file_name):42 match = CRC32_PATTERN.search(file_name)43 return None if match is None else int(match.group(1), 16)44def list_files(paths):45 result = []46 for path in paths:47 _list_files(path, os.stat(path), result)48 return result49def _list_files(path, st, result):50 if stat.S_ISDIR(st.st_mode):51 with os.scandir(path) as it:52 for entry in it:53 _list_files(entry.path, entry.stat(), result)54 else:55 root, fname = os.path.split(path)56 result.append((root, fname, st.st_size))57def filter_randomly(files, percentage):58 size_sum = sum(f[2] for f in files)59 logger.debug("The total file size is %d", size_sum)60 size_limit = max(int(size_sum * (percentage / 100.0)), 1)61 result_size = 062 result = []63 while files and result_size < size_limit:64 i = random.randrange(len(files))65 logger.debug("Adding the file '%s/%s' with size %d", files[i][0], files[i][1], files[i][2])66 result_size += files[i][2]67 result.append(files[i])68 files[i] = files[-1]69 files.pop()70 logger.debug("The result file size is %d of %d (%f%%)", result_size, size_sum, 100.0 * result_size / size_sum)71 return result72def filter_extensions(files, extensions):73 result = []74 for f in files:75 name, ext = os.path.splitext(f[1])76 if ext.lower() in extensions:77 result.append(f)78 return result79def filter_name_crc32(files):80 return [f for f in files if extract_crc32(f[1]) is not None]81def create_arg_parser():82 p = ArgumentParser(description='A recursive CRC32',83 formatter_class=ArgumentDefaultsHelpFormatter)84 p.add_argument('paths', nargs='*',85 help='the paths to analize, it could be a file or a directory')86 p.add_argument('--log-level', default='INFO',87 choices=('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'),88 help='logger level')89 p.add_argument('--add', action='store_true',90 help='add the CRC32 to the files without it')91 p.add_argument('--check', action='store_true',92 help='calculate only the CRC32 of the files with it in the name')93 p.add_argument('--only-corrupt', action='store_true',94 help='show only the corrupt files')95 p.add_argument('--random', type=type_random, default=None,96 help='apply randomly to this percentage of the files size')97 p.add_argument('--extension', '-e', nargs='*', default=('.mp4', '.avi', '.mkv'),98 help='apply only to files with this extensions')99 return p100def type_random(x):101 try:102 return max(0.0, min(float(x), 100.0))103 except ValueError:104 raise ArgumentTypeError("The random percentage must be a valid number")105def main():106 parser = create_arg_parser()107 args = parser.parse_args()108 logging.basicConfig(level=args.log_level)109 logger.debug("Listing files")110 files = list_files(args.paths)111 extensions = set(x.lower() for x in args.extension if x)112 if extensions:113 logger.debug("Filtering files by extension")114 files = filter_extensions(files, extensions)115 if not args.add:116 logger.debug("Filtering files with name CRC32")117 files = filter_name_crc32(files)118 if args.random is not None:119 logger.debug("Filtering files randomly")120 files = filter_randomly(files, args.random)121 corrupted_file = False122 error_adding_crc32 = False123 logger.debug("Doing operations")124 files.sort()125 for root, fname, _ in files:126 path = os.path.join(root, fname)127 name_crc32 = extract_crc32(fname)128 if name_crc32 is None and args.add:129 crc32 = calculate_crc32(path)130 name, ext = os.path.splitext(fname)131 new_fname = "{} [{}]{}".format(name, fcrc32(crc32), ext)132 new_path = os.path.join(root, new_fname)133 logger.debug("Renaming the file '%s' to '%s'", path, new_path)134 try:135 os.rename(path, new_path)136 except OSError:137 logger.exception("Can't rename '%s' to '%s'", path, new_path)138 error_adding_crc32 = True139 continue140 cprint('{_bold}{_lblue}NEW{_reset} {} {}', fcrc32(crc32), new_path)141 elif name_crc32 is not None and args.check:142 crc32 = calculate_crc32(path)143 if crc32 != name_crc32:144 cprint('{_bold}{_lred}CORRUPT{_reset} {} {}', fcrc32(crc32), path)145 corrupted_file = True146 elif not args.only_corrupt:147 cprint('{_bold}{_lgreen}HEALTHY{_reset} {} {}', fcrc32(crc32), path)148 if corrupted_file:149 return 1150 if error_adding_crc32:151 return 2152 return 0153if __name__ == "__main__":...

Full Screen

Full Screen

test_checksum_crc32_iso_hdlc.py

Source:test_checksum_crc32_iso_hdlc.py Github

copy

Full Screen

...33 ),34 ]35 for crc_entry in crc_data_list:36 # check expected result from first execution37 test_result = crc32_handler.calculate_crc32(crc_entry.test_data)38 test_result_str = convert_int_to_hex_string(test_result)39 assert test_result == crc_entry.expected_int40 assert test_result_str == crc_entry.expected_str41 # check if result is the same after second execution42 test_result = crc32_handler.calculate_crc32(crc_entry.test_data)43 test_result_str = convert_int_to_hex_string(test_result)44 assert test_result == crc_entry.expected_int45 assert test_result_str == crc_entry.expected_str46@pytest.mark.unittest47def test_checksum_calculate_crc32_iso_hdlc_to_match_zlib_result():48 test_data_array = [49 b"",50 b" ",51 b"123456789",52 b"123",53 b"Hello Python",54 b"Secret123@123#!",55 b"\xAA\xBB",56 b"\x00\x01\x02\x03",57 b"1",58 b"abcdefgh",59 b"\n",60 b"!@#$%^&*()",61 fake.binary(length=100),62 fake.binary(length=5000),63 ]64 for test_data in test_data_array:65 test_result = crc32_handler.calculate_crc32(test_data)66 zlib_result = zlib.crc32(test_data)67 test_result_str = convert_int_to_hex_string(test_result)68 zlib_result_str = convert_int_to_hex_string(zlib_result)69 assert test_result == zlib_result...

Full Screen

Full Screen

sys573_config_tool.py

Source:sys573_config_tool.py Github

copy

Full Screen

1import argparse23def decrypt_data_internal(data, key):4 def calculate_crc32(input):5 crc = -167 for c in bytearray(input, encoding='ascii'):8 crc ^= c << 24910 for _ in range(8):11 if crc & 0x80000000:12 crc = (crc << 1) ^ 0x4C11DB713 else:14 crc <<= 11516 return crc1718 decryption_key = calculate_crc32(key)1920 for i in range(len(data)):21 data[i] ^= (decryption_key >> 8) & 0xff # This 8 can be variable it seems, but it usually is 8?2223 return data242526def main():27 parser = argparse.ArgumentParser()2829 parser.add_argument('--insert', help='Input configuration text into output file', default=False, action='store_true')3031 args = parser.parse_args()32 ...

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 localstack 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