1# 2# Copyright OpenEmbedded Contributors 3# 4# SPDX-License-Identifier: MIT 5# 6 7import os 8import re 9import errno 10 11def write_file(path, data): 12 # In case data is None, return immediately 13 if data is None: 14 return 15 wdata = data.rstrip() + "\n" 16 with open(path, "w") as f: 17 f.write(wdata) 18 19def append_file(path, data): 20 # In case data is None, return immediately 21 if data is None: 22 return 23 wdata = data.rstrip() + "\n" 24 with open(path, "a") as f: 25 f.write(wdata) 26 27def read_file(path): 28 data = None 29 with open(path) as f: 30 data = f.read() 31 return data 32 33def remove_from_file(path, data): 34 # In case data is None, return immediately 35 if data is None: 36 return 37 try: 38 rdata = read_file(path) 39 except IOError as e: 40 # if file does not exit, just quit, otherwise raise an exception 41 if e.errno == errno.ENOENT: 42 return 43 else: 44 raise 45 46 contents = rdata.strip().splitlines() 47 for r in data.strip().splitlines(): 48 try: 49 contents.remove(r) 50 except ValueError: 51 pass 52 write_file(path, "\n".join(contents)) 53