1#!/usr/bin/python3 2# Processing of symbols and abilist files. 3# Copyright (C) 2020-2022 Free Software Foundation, Inc. 4# This file is part of the GNU C Library. 5# 6# The GNU C Library is free software; you can redistribute it and/or 7# modify it under the terms of the GNU Lesser General Public 8# License as published by the Free Software Foundation; either 9# version 2.1 of the License, or (at your option) any later version. 10# 11# The GNU C Library is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14# Lesser General Public License for more details. 15# 16# You should have received a copy of the GNU Lesser General Public 17# License along with the GNU C Library; if not, see 18# <https://www.gnu.org/licenses/>. 19 20"""Symbol processing for glibc.""" 21 22import os 23 24def replace_file(path, new_contents): 25 """Atomically replace PATH with lines from NEW_CONTENTS. 26 27 NEW_CONTENTS must be a sequence of strings. 28 29 """ 30 temppath = path + 'T' 31 with open(temppath, 'w') as out: 32 for line in new_contents: 33 out.write(line) 34 os.rename(temppath, path) 35 36class VersionedSymbol: 37 """A combination of a symbol and its version.""" 38 39 def __init__(self, symbol, version): 40 """Construct a new versioned symbol.""" 41 assert symbol 42 assert version 43 self.symbol = symbol 44 self.version = version 45 46 def __str__(self): 47 return self.symbol + '@' + self.version 48 49 def __eq__(self, other): 50 return self.symbol == other.symbol and self.version == other.version 51 52 def __hash__(self): 53 return hash(self.symbol) ^ hash(self.version) 54 55def read_abilist(path): 56 """Read the abilist file at PATH. 57 58 Return a dictionary from VersionedSymbols to their flags (as 59 strings). 60 61 """ 62 result = {} 63 with open(path) as inp: 64 for line in inp: 65 version, symbol, flags = line.strip().split(' ', 2) 66 versym = VersionedSymbol(symbol, version) 67 if versym in result: 68 raise IOError("{}: duplicate symbol {}".format(path, versym)) 69 result[versym] = flags 70 return result 71 72def abilist_lines(symbols): 73 """Build the abilist file contents (as a list of lines). 74 75 SYMBOLS is a dictionary from VersionedSymbols to their flags. 76 77 """ 78 result = [] 79 for versym, flags in symbols.items(): 80 result.append('{} {} {}\n'.format( 81 versym.version, versym.symbol, flags)) 82 result.sort() 83 return result 84