1#
2# Copyright (C) 2016 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16"""Utility functions for build rule generator."""
17
18import os
19
20def HalNameDir(hal_name):
21    """Returns directory name corresponding to hal name."""
22    return hal_name.replace('.', '/')
23
24
25def HalVerDir(hal_version):
26    """Returns directory name corresponding to hal version."""
27    return "V" + hal_version.replace('.', '_')
28
29
30def WriteBuildRule(file_path, build_rule):
31    """Writes the build rule into specified file.
32
33    Opens file_path and writes build_rule into it. Creates intermediate
34    directories if necessary.
35
36    Args:
37      file_path: string, path to file to which to write.
38      build_rule: string, build rule to be written into file.
39
40    Returns:
41      True if updated, False otherwise
42    """
43    exist = True
44    dir_path = os.path.dirname(file_path)
45    if not os.path.exists(dir_path):
46        os.makedirs(dir_path)
47        exist = False
48    elif not os.path.isfile(file_path):
49        exist = False
50
51    if exist:
52        with open(file_path, 'r') as existing_file:
53            if build_rule == existing_file.read():
54                print 'Skipping %s' % file_path
55                return False
56
57    print 'Updating %s' % file_path
58    with open(file_path, 'w') as bp_file:
59        bp_file.write(build_rule)
60    return True
61
62
63def OnlySubdirsBpRule(warning_header, subdirs):
64    """Returns a .bp rule containing only subdirs field.
65
66    For example, 'subdirs = ["*"]' bp rule tells soong to look in all
67    sub-directories for Android.bp files.
68
69    Args:
70        subdirs: list of sub-directories.
71    """
72    result = warning_header
73
74    result += 'subdirs = [\n'
75    for subdir in subdirs:
76        result += '    "%s",\n' % subdir
77    result += ']\n'
78    return result
79
80
81def RemoveFilesInDirIf(dir_path, condition):
82    """Removes all files under directory under given condition.
83
84    Args:
85        dir_path: string, path to directory
86        condition: boolean function takes absolute file path,
87            returns True iff file needs to be removed.
88
89    Returns:
90        True if removed, False otherwise
91    """
92    removed = False
93    for base, _, files in os.walk(dir_path):
94        for f in files:
95            abs_path = os.path.join(base, f)
96            if condition(abs_path):
97                print "Removing", abs_path
98                os.remove(abs_path)
99                removed = True
100    return removed
101