1# internal use only for CI
2# some CI related util functions
3#
4# Copyright 2020 Espressif Systems (Shanghai) PTE LTD
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18import functools
19import logging
20import os
21import re
22import subprocess
23import sys
24
25IDF_PATH = os.path.abspath(os.getenv('IDF_PATH', os.path.join(os.path.dirname(__file__), '..', '..')))
26
27
28def get_submodule_dirs(full_path=False):  # type: (bool) -> list
29    """
30    To avoid issue could be introduced by multi-os or additional dependency,
31    we use python and git to get this output
32    :return: List of submodule dirs
33    """
34    dirs = []
35    try:
36        lines = subprocess.check_output(
37            ['git', 'config', '--file', os.path.realpath(os.path.join(IDF_PATH, '.gitmodules')),
38             '--get-regexp', 'path']).decode('utf8').strip().split('\n')
39        for line in lines:
40            _, path = line.split(' ')
41            if full_path:
42                dirs.append(os.path.join(IDF_PATH, path))
43            else:
44                dirs.append(path)
45    except Exception as e:  # pylint: disable=W0703
46        logging.warning(str(e))
47
48    return dirs
49
50
51def _check_git_filemode(full_path):  # type: (str) -> bool
52    try:
53        stdout = subprocess.check_output(['git', 'ls-files', '--stage', full_path]).strip().decode('utf-8')
54    except subprocess.CalledProcessError:
55        return True
56
57    mode = stdout.split(' ', 1)[0]  # e.g. 100644 for a rw-r--r--
58    if any([int(i, 8) & 1 for i in mode[-3:]]):
59        return True
60    return False
61
62
63def is_executable(full_path):  # type: (str) -> bool
64    """
65    os.X_OK will always return true on windows. Use git to check file mode.
66    :param full_path: file full path
67    :return: True is it's an executable file
68    """
69    if sys.platform == 'win32':
70        return _check_git_filemode(full_path)
71    return os.access(full_path, os.X_OK)
72
73
74def get_git_files(path=IDF_PATH, full_path=False):  # type: (str, bool) -> list[str]
75    """
76    Get the result of git ls-files
77    :param path: path to run git ls-files
78    :param full_path: return full path if set to True
79    :return: list of file paths
80    """
81    try:
82        files = subprocess.check_output(['git', 'ls-files'], cwd=path).decode('utf8').strip().split('\n')
83    except Exception as e:  # pylint: disable=W0703
84        logging.warning(str(e))
85        files = []
86    return [os.path.join(path, f) for f in files] if full_path else files
87
88
89# this function is a commit from
90# https://github.com/python/cpython/pull/6299/commits/bfd63120c18bd055defb338c075550f975e3bec1
91# In order to solve python https://bugs.python.org/issue9584
92# glob pattern does not support brace expansion issue
93def _translate(pat):  # type: (str) -> str
94    """Translate a shell PATTERN to a regular expression.
95    There is no way to quote meta-characters.
96    """
97    i, n = 0, len(pat)
98    res = ''
99    while i < n:
100        c = pat[i]
101        i = i + 1
102        if c == '*':
103            res = res + '.*'
104        elif c == '?':
105            res = res + '.'
106        elif c == '[':
107            j = i
108            if j < n and pat[j] == '!':
109                j = j + 1
110            if j < n and pat[j] == ']':
111                j = j + 1
112            while j < n and pat[j] != ']':
113                j = j + 1
114            if j >= n:
115                res = res + '\\['
116            else:
117                stuff = pat[i:j]
118                if '--' not in stuff:
119                    stuff = stuff.replace('\\', r'\\')
120                else:
121                    chunks = []
122                    k = i + 2 if pat[i] == '!' else i + 1
123                    while True:
124                        k = pat.find('-', k, j)
125                        if k < 0:
126                            break
127                        chunks.append(pat[i:k])
128                        i = k + 1
129                        k = k + 3
130                    chunks.append(pat[i:j])
131                    # Escape backslashes and hyphens for set difference (--).
132                    # Hyphens that create ranges shouldn't be escaped.
133                    stuff = '-'.join(s.replace('\\', r'\\').replace('-', r'\-')
134                                     for s in chunks)
135                # Escape set operations (&&, ~~ and ||).
136                stuff = re.sub(r'([&~|])', r'\\\1', stuff)
137                i = j + 1
138                if stuff[0] == '!':
139                    stuff = '^' + stuff[1:]
140                elif stuff[0] in ('^', '['):
141                    stuff = '\\' + stuff
142                res = '%s[%s]' % (res, stuff)
143        elif c == '{':
144            # Handling of brace expression: '{PATTERN,PATTERN,...}'
145            j = 1
146            while j < n and pat[j] != '}':
147                j = j + 1
148            if j >= n:
149                res = res + '\\{'
150            else:
151                stuff = pat[i:j]
152                i = j + 1
153
154                # Find indices of ',' in pattern excluding r'\,'.
155                # E.g. for r'a\,a,b\b,c' it will be [4, 8]
156                indices = [m.end() for m in re.finditer(r'[^\\],', stuff)]
157
158                # Splitting pattern string based on ',' character.
159                # Also '\,' is translated to ','. E.g. for r'a\,a,b\b,c':
160                # * first_part = 'a,a'
161                # * last_part = 'c'
162                # * middle_part = ['b,b']
163                first_part = stuff[:indices[0] - 1].replace(r'\,', ',')
164                last_part = stuff[indices[-1]:].replace(r'\,', ',')
165                middle_parts = [
166                    stuff[st:en - 1].replace(r'\,', ',')
167                    for st, en in zip(indices, indices[1:])
168                ]
169
170                # creating the regex from splitted pattern. Each part is
171                # recursivelly evaluated.
172                expanded = functools.reduce(
173                    lambda a, b: '|'.join((a, b)),
174                    (_translate(elem) for elem in [first_part] + middle_parts + [last_part])
175                )
176                res = '%s(%s)' % (res, expanded)
177        else:
178            res = res + re.escape(c)
179    return res
180
181
182def translate(pat):  # type: (str) -> str
183    res = _translate(pat)
184    return r'(?s:%s)\Z' % res
185
186
187magic_check = re.compile('([*?[{])')
188magic_check_bytes = re.compile(b'([*?[{])')
189# cpython github PR 6299 ends here
190
191# Here's the code block we're going to use to monkey patch ``glob`` module and ``fnmatch`` modules
192# DO NOT monkey patch here, only patch where you really needs
193#
194# import glob
195# import fnmatch
196# from idf_ci_utils import magic_check, magic_check_bytes, translate
197# glob.magic_check = magic_check
198# glob.magic_check_bytes = magic_check_bytes
199# fnmatch.translate = translate
200