1*e4a36f41SAndroid Build Coastguard Worker# Copyright 2018 - The Android Open Source Project 2*e4a36f41SAndroid Build Coastguard Worker# 3*e4a36f41SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*e4a36f41SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*e4a36f41SAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*e4a36f41SAndroid Build Coastguard Worker# 7*e4a36f41SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*e4a36f41SAndroid Build Coastguard Worker# 9*e4a36f41SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*e4a36f41SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*e4a36f41SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*e4a36f41SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*e4a36f41SAndroid Build Coastguard Worker# limitations under the License. 14*e4a36f41SAndroid Build Coastguard Worker 15*e4a36f41SAndroid Build Coastguard Worker"""File-related utilities.""" 16*e4a36f41SAndroid Build Coastguard Worker 17*e4a36f41SAndroid Build Coastguard Worker 18*e4a36f41SAndroid Build Coastguard Workerimport os 19*e4a36f41SAndroid Build Coastguard Workerimport shutil 20*e4a36f41SAndroid Build Coastguard Workerimport tempfile 21*e4a36f41SAndroid Build Coastguard Worker 22*e4a36f41SAndroid Build Coastguard Worker 23*e4a36f41SAndroid Build Coastguard Workerdef make_parent_dirs(file_path): 24*e4a36f41SAndroid Build Coastguard Worker """Creates parent directories for the file_path.""" 25*e4a36f41SAndroid Build Coastguard Worker if os.path.exists(file_path): 26*e4a36f41SAndroid Build Coastguard Worker return 27*e4a36f41SAndroid Build Coastguard Worker 28*e4a36f41SAndroid Build Coastguard Worker parent_dir = os.path.dirname(file_path) 29*e4a36f41SAndroid Build Coastguard Worker if parent_dir and not os.path.exists(parent_dir): 30*e4a36f41SAndroid Build Coastguard Worker os.makedirs(parent_dir) 31*e4a36f41SAndroid Build Coastguard Worker 32*e4a36f41SAndroid Build Coastguard Worker 33*e4a36f41SAndroid Build Coastguard Workerdef filter_out(pattern_files, input_file): 34*e4a36f41SAndroid Build Coastguard Worker """"Removes lines in input_file that match any line in pattern_files.""" 35*e4a36f41SAndroid Build Coastguard Worker 36*e4a36f41SAndroid Build Coastguard Worker # Prepares patterns. 37*e4a36f41SAndroid Build Coastguard Worker patterns = [] 38*e4a36f41SAndroid Build Coastguard Worker for f in pattern_files: 39*e4a36f41SAndroid Build Coastguard Worker patterns.extend(open(f).readlines()) 40*e4a36f41SAndroid Build Coastguard Worker 41*e4a36f41SAndroid Build Coastguard Worker # Copy lines that are not in the pattern. 42*e4a36f41SAndroid Build Coastguard Worker tmp_output = tempfile.NamedTemporaryFile(mode='w+') 43*e4a36f41SAndroid Build Coastguard Worker with open(input_file, 'r') as in_file: 44*e4a36f41SAndroid Build Coastguard Worker tmp_output.writelines(line for line in in_file.readlines() 45*e4a36f41SAndroid Build Coastguard Worker if line not in patterns) 46*e4a36f41SAndroid Build Coastguard Worker # Append empty line because a completely empty file 47*e4a36f41SAndroid Build Coastguard Worker # will trip up secilc later on: 48*e4a36f41SAndroid Build Coastguard Worker tmp_output.write("\n") 49*e4a36f41SAndroid Build Coastguard Worker tmp_output.flush() 50*e4a36f41SAndroid Build Coastguard Worker 51*e4a36f41SAndroid Build Coastguard Worker # Replaces the input_file. 52*e4a36f41SAndroid Build Coastguard Worker shutil.copyfile(tmp_output.name, input_file) 53