(ar_path: str)
return unique_strings
For Linux libraries, this method requires ar (from binutils), nm (from binutils), and strings.
For Linux libraries, this method requires ar (from binutils), nm (from binutils), and strings.
For Linux libraries this method requires ar ( from binutils ) nm ( from binutils ) and strings .
def get_unique_strings(ar_path: str) -> List[str]:
    """
    For Linux libraries, this method requires ar (from binutils), nm (from binutils), and strings.
    """
    # get symbols
    nm_output = subprocess.check_output(["nm", ar_path])
    nm_lines = nm_output.decode("utf-8").split("\n")
    symbols = set()
    for nm_line in nm_lines:
        symbol_types = "UuVvTtRrDdWwBbNn"
        for symbol_type in symbol_types:
            if f" {symbol_type} " in nm_line:
                # parse it
                symbol = nm_line[nm_line.find(f" {symbol_type}") + 3: ].strip(" ")
                if "." in symbol:
                    symbols |= set(symbol.split("."))
                else:
                    symbols.add(symbol)
                break

    # extract the archive file into a temporary directory
    all_strings = set()
    with tempfile.TemporaryDirectory() as tempdirname:
        cwd = os.getcwd()
        os.chdir(tempdirname)
        subprocess.call(["ar", "x", ar_path])

        for filename in os.listdir("."):
            if filename.endswith(".o"):
                strings = subprocess.check_output(["strings", "-n", "8", filename])
                strings = strings.decode("utf-8").split("\n")
                non_symbol_strings = set()
                for s in strings:
                    if s in symbols:
                        continue
                    if "." in s and any(subs in symbols for subs in s.split(".")):
                        continue
                    # C++ specific
                    if "::" in s:
                        continue
                    if "_" in s:
                        # make sure it's not a substring of any symbol
                        is_substring = False
                        for symbol in symbols:
                            if s in symbol:
                                is_substring = True
                                break
                        if is_substring:
                            continue
                    non_symbol_strings.add(s)
                all_strings |= non_symbol_strings

        os.chdir(cwd)

    grouped_strings = defaultdict(set)
    for s in all_strings:
        grouped_strings[s[:5]].add(s)
    sorted_strings = list(sorted(all_strings, key=len, reverse=True))

    ctr = 0
    picked = set()
    unique_strings = [ ]
    for s in sorted_strings:
        if s[:5] in picked:
            continue
        unique_strings.append(s[:MAX_UNIQUE_STRING_LEN])
        picked.add(s[:5])
        ctr += 1
        if ctr >= UNIQUE_STRING_COUNT:
            break
    return unique_strings
def get_unique_strings ( ar_path : str ) -> List [ str ] : # get symbols nm_output = subprocess . check_output ( [ "nm" , ar_path ] ) nm_lines = nm_output . decode ( "utf-8" ) . split ( "\n" ) symbols = set ( ) for nm_line in nm_lines : symbol_types = "UuVvTtRrDdWwBbNn" for symbol_type in symbol_types : if f" {symbol_type} " in nm_line : # parse it symbol = nm_line [ nm_line . find ( f" {symbol_type}" ) + 3 : ] . strip ( " " ) if "." in symbol : symbols |= set ( symbol . split ( "." ) ) else : symbols . add ( symbol ) break # extract the archive file into a temporary directory all_strings = set ( ) with tempfile . TemporaryDirectory ( ) as tempdirname : cwd = os . getcwd ( ) os . chdir ( tempdirname ) subprocess . call ( [ "ar" , "x" , ar_path ] ) for filename in os . listdir ( "." ) : if filename . endswith ( ".o" ) : strings = subprocess . check_output ( [ "strings" , "-n" , "8" , filename ] ) strings = strings . decode ( "utf-8" ) . split ( "\n" ) non_symbol_strings = set ( ) for s in strings : if s in symbols : continue if "." in s and any ( subs in symbols for subs in s . split ( "." ) ) : continue # C++ specific if "::" in s : continue if "_" in s : # make sure it's not a substring of any symbol is_substring = False for symbol in symbols : if s in symbol : is_substring = True break if is_substring : continue non_symbol_strings . add ( s ) all_strings |= non_symbol_strings os . chdir ( cwd ) grouped_strings = defaultdict ( set ) for s in all_strings : grouped_strings [ s [ : 5 ] ] . add ( s ) sorted_strings = list ( sorted ( all_strings , key = len , reverse = True ) ) ctr = 0 picked = set ( ) unique_strings = [ ] for s in sorted_strings : if s [ : 5 ] in picked : continue unique_strings . append ( s [ : MAX_UNIQUE_STRING_LEN ] ) picked . add ( s [ : 5 ] ) ctr += 1 if ctr >= UNIQUE_STRING_COUNT : break return unique_strings