# Released under the MIT License. See LICENSE for details.#"""Functionality related to android builds."""from__future__importannotationsfromtypingimportTYPE_CHECKINGfromdataclassesimportdataclassifTYPE_CHECKING:pass
[docs]@dataclassclassGradleFilterSection:"""Filtered section of gradle file."""tag:strfirstline:intlastline:int
[docs]deffilter_gradle_file(buildfilename:str,enabled_tags:set[str])->None:"""Filter 'EFRO_IF' sections in a gradle file."""sections:list[GradleFilterSection]=[]withopen(buildfilename,encoding='utf-8')asinfile:original=infile.read()lines=original.splitlines()current_section:GradleFilterSection|None=Nonefori,lineinenumerate(lines):ifline.strip().startswith('// EFRO_IF'):ifcurrent_sectionisnotNone:raiseRuntimeError('Malformed gradle file')current_section=GradleFilterSection(tag=line.split()[2],firstline=i,lastline=i)elifline.strip().startswith('// EFRO_ENDIF'):ifcurrent_sectionisNone:raiseRuntimeError('Malformed gradle file')current_section.lastline=isections.append(current_section)current_section=Noneifcurrent_sectionisnotNone:raiseRuntimeError('Malformed gradle file')forsectioninsections:forlinenoinrange(section.firstline+1,section.lastline):enable=section.taginenabled_tagsline=lines[lineno]leading=''whileline.startswith(' '):leading+=' 'line=line[1:]ifnotenableandnotline.startswith('// '):line='// '+lineifenableandline.startswith('// '):line=line[3:]lines[lineno]=leading+line# Only write if its changed (potentially avoid triggering builds).out='\n'.join(lines)+'\n'ifout!=original:withopen(buildfilename,'w',encoding='utf-8')asoutfile:outfile.write(out)