<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import sys <EOL> import difflib <EOL> from xml . etree import ElementTree as etree <EOL> from qisys import ui <EOL> import qisys . parsers <EOL> import qibuild <EOL> def guess_project_name ( source_dir ) : <EOL> """<STR_LIT>""" <EOL> res = None <EOL> qiproj_xml = os . path . join ( source_dir , "<STR_LIT>" ) <EOL> res = name_from_xml ( qiproj_xml ) <EOL> if res : <EOL> return res <EOL> cmakelists = os . path . join ( source_dir , "<STR_LIT>" ) <EOL> res = name_from_cmakelists ( cmakelists ) <EOL> if res : <EOL> return res <EOL> res = os . path . basename ( source_dir ) <EOL> return res <EOL> def name_from_xml ( xml_path ) : <EOL> """<STR_LIT>""" <EOL> mess = "<STR_LIT>" <EOL> mess += "<STR_LIT>" % xml_path <EOL> if not os . path . exists ( xml_path ) : <EOL> return None <EOL> tree = etree . ElementTree ( ) <EOL> try : <EOL> tree . parse ( xml_path ) <EOL> except Exception , e : <EOL> ui . fatal ( e ) <EOL> root = tree . getroot ( ) <EOL> if root . tag != "<STR_LIT>" : <EOL> mess += "<STR_LIT>" <EOL> ui . fatal ( mess ) <EOL> if root . get ( "<STR_LIT:version>" ) == "<STR_LIT:3>" : <EOL> project_elem = root . find ( "<STR_LIT>" ) <EOL> if not project_elem : <EOL> return <EOL> else : <EOL> project_elem = root <EOL> name = project_elem . get ( '<STR_LIT:name>' ) <EOL> if not name : <EOL> mess += "<STR_LIT>" <EOL> ui . fatal ( mess ) <EOL> return name <EOL> def name_from_cmakelists ( cmakelists ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( cmakelists ) : <EOL> return None <EOL> res = None <EOL> regexp = re . compile ( r'<STR_LIT>' , re . IGNORECASE ) <EOL> lines = list ( ) <EOL> with open ( cmakelists , "<STR_LIT:r>" ) as fp : <EOL> lines = fp . readlines ( ) <EOL> for line in lines : <EOL> match = re . match ( regexp , line ) <EOL> if match : <EOL> res = match . groups ( ) [ <NUM_LIT:0> ] <EOL> res = res . strip ( ) <EOL> return res <EOL> return res <EOL> def fix_root_cmake ( cmakelists , project_name , dry_run = True ) : <EOL> """<STR_LIT>""" <EOL> template = """<STR_LIT>""" <EOL> template = template . format ( project_name = project_name ) <EOL> if not os . path . exists ( cmakelists ) : <EOL> if not dry_run : <EOL> with open ( cmakelists , "<STR_LIT:w>" ) as fp : <EOL> fp . write ( template ) <EOL> return <EOL> with open ( cmakelists , "<STR_LIT:r>" ) as fp : <EOL> old_lines = fp . readlines ( ) <EOL> new_lines = list ( ) <EOL> seen_find_package_qibuild = False <EOL> for line in old_lines : <EOL> match = re . match ( "<STR_LIT>" , line ) <EOL> if match : <EOL> seen_find_package_qibuild = True <EOL> match = re . match ( "<STR_LIT>" , line ) <EOL> if match : <EOL> if not seen_find_package_qibuild : <EOL> new_lines . append ( '<STR_LIT>' ) <EOL> new_lines . append ( '<STR_LIT>' ) <EOL> seen_find_package_qibuild = True <EOL> else : <EOL> match = re . match ( "<STR_LIT>" , line ) <EOL> if match : <EOL> if not seen_find_package_qibuild : <EOL> new_lines . append ( '<STR_LIT>' ) <EOL> seen_find_package_qibuild = True <EOL> else : <EOL> new_lines . append ( line ) <EOL> if not seen_find_package_qibuild : <EOL> tmp_lines = new_lines [ : ] <EOL> new_lines = list ( ) <EOL> for line in tmp_lines : <EOL> new_lines . append ( line ) <EOL> regexp = re . compile ( r'<STR_LIT>' , re . IGNORECASE ) <EOL> if re . match ( regexp , line ) : <EOL> new_lines . append ( '<STR_LIT>' ) <EOL> if dry_run : <EOL> ui . info ( "<STR_LIT>" , cmakelists ) <EOL> for line in difflib . unified_diff ( old_lines , new_lines ) : <EOL> sys . stdout . write ( line ) <EOL> return <EOL> with open ( cmakelists , "<STR_LIT:w>" ) as fp : <EOL> ui . info ( "<STR_LIT>" , cmakelists ) <EOL> fp . writelines ( new_lines ) <EOL> def create_qiproj_xml ( args ) : <EOL> """<STR_LIT>""" <EOL> source_dir = args . source_dir <EOL> project_name = args . project_name <EOL> qiproj_xml = os . path . join ( source_dir , "<STR_LIT>" ) <EOL> if os . path . exists ( qiproj_xml ) : <EOL> return <EOL> proj_elem = etree . Element ( "<STR_LIT>" ) <EOL> proj_elem . set ( "<STR_LIT:version>" , "<STR_LIT:3>" ) <EOL> tree = etree . ElementTree ( element = proj_elem ) <EOL> qibuild_elem = etree . Element ( "<STR_LIT>" ) <EOL> qibuild_elem . set ( "<STR_LIT:name>" , project_name ) <EOL> proj_elem . append ( qibuild_elem ) <EOL> qisys . qixml . indent ( proj_elem ) <EOL> if args . dry_run : <EOL> ui . info ( "<STR_LIT>" , qiproj_xml , "<STR_LIT:\n>" <EOL> "<STR_LIT>" , "<STR_LIT:\n>" , etree . tostring ( proj_elem ) ) <EOL> return <EOL> ui . info ( "<STR_LIT>" , qiproj_xml ) <EOL> tree . write ( qiproj_xml ) <EOL> def configure_parser ( parser ) : <EOL> """<STR_LIT>""" <EOL> qisys . parsers . default_parser ( parser ) <EOL> parser . add_argument ( "<STR_LIT>" , nargs = "<STR_LIT:?>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , action = "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , action = "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . set_defaults ( dry_run = True , fix_cmake = True ) <EOL> def do ( args ) : <EOL> """<STR_LIT>""" <EOL> if not args . source_dir : <EOL> args . source_dir = os . getcwd ( ) <EOL> args . source_dir = qisys . sh . to_native_path ( args . source_dir ) <EOL> if not args . project_name : <EOL> args . project_name = guess_project_name ( args . source_dir ) <EOL> ui . info ( ui . green , "<STR_LIT>" , args . project_name ) <EOL> create_qiproj_xml ( args ) <EOL> if not args . fix_cmake : <EOL> return <EOL> source_dir = args . source_dir <EOL> project_name = args . project_name <EOL> cmakelists = os . path . join ( source_dir , "<STR_LIT>" ) <EOL> fix_root_cmake ( cmakelists , project_name , dry_run = args . dry_run ) <EOL> if args . dry_run : <EOL> ui . info ( ui . green , "<STR_LIT>" ) </s>
