[docs]defunpack_batch_file(input_path:str,output_paths:list[str]):""" Converts a batch file into a list of files. It's the inverse of make_batch_file. Parameters ---------- input_path: str, path to the batch file output_paths: list of str, paths to the output files Returns output_paths ------- """withopen(input_path,"rb")asf:bb=f.read()separated_batch=[]offset=0whileoffset<len(bb):size=struct.unpack("I",bb[offset:offset+4])[0]offset+=4print(offset,size)offset+=sizeseparated_batch.append(bb[offset-size:offset])assertlen(output_paths)==len(separated_batch)foridx,output_bytesinenumerate(separated_batch):output_path=output_paths[idx]withopen(output_path,"wb")asf:f.write(output_bytes)returnoutput_paths
@cachedefget_aws_credentials(capitalize=False):config=get_config()ifnotconfig.ready:config.load()cred={"aws_access_key_id":config.get("aws_access_key_id"),"aws_secret_access_key":config.get("aws_secret_access_key"),"region_name":config.get("aws_region"),}ifcapitalize:cred={key.upper():valueforkey,valueincred.items()}returncreddefnew_s3_client():returnboto3.client("s3",**get_aws_credentials())defnew_s3_resource():returnboto3.resource("s3",**get_aws_credentials())defget_s3_bucket(bucket_name:str):# pylint: disable=no-memberresource=new_s3_resource()returnresource.Bucket(bucket_name)defsetup_bucket_for_presigned_urls(bucket_name,public_read=False):logger.info("Setting bucket CORSRules and policies...")s3_resource=new_s3_resource()bucket=s3_resource.Bucket(bucket_name)cors=bucket.Cors()config={"CORSRules":[{"AllowedHeaders":["*"],"AllowedMethods":["GET","PUT"],"AllowedOrigins":["*"],}]}cors.delete()cors.put(CORSConfiguration=config)ifpublic_read:bucket_policy=s3_resource.BucketPolicy(bucket_name)new_policy=json.dumps({"Version":"2008-10-17","Statement":[{"Sid":"AllowPublicRead","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":f"arn:aws:s3:::{bucket_name}/*",}],})bucket_policy.put(Policy=new_policy)defmake_bucket_public(bucket_name):logger.info("Verifying that the S3 bucket '%s' is correctly configured for public access...",bucket_name,)s3_resource=new_s3_resource()bucket=s3_resource.Bucket(bucket_name)bucket.Acl().put(ACL="public-read")cors=bucket.Cors()config={"CORSRules":[{"AllowedMethods":["GET"],"AllowedOrigins":["*"]}]}cors.delete()cors.put(CORSConfiguration=config)bucket_policy=s3_resource.BucketPolicy(bucket_name)new_policy=json.dumps({"Version":"2008-10-17","Statement":[{"Sid":"AllowPublicRead","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":f"arn:aws:s3:::{bucket_name}/*",}],})bucket_policy.put(Policy=new_policy)defrecode_wav(file_path):withtempfile.NamedTemporaryFile()astemp_file:shutil.copyfile(file_path,temp_file.name)withwave.open(temp_file.name,"rb")asin_wave:params=in_wave.getparams()withwave.open(file_path,"wb")asout_wave:out_wave.setparams(params)chunk_size=1024data=in_wave.readframes(chunk_size)whiledata:out_wave.writeframes(data)data=in_wave.readframes(chunk_size)