Clone of official tools
resources/ignore.py@47:21ae3e5a7128, 2021-02-04 (annotated)
- Committer:
- Anders Blomdell
- Date:
- Thu Feb 04 17:17:13 2021 +0100
- Revision:
- 47:21ae3e5a7128
- Parent:
- 43:2a7da56ebd24
Add a few normpath calls
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
theotherjimmy |
43:2a7da56ebd24 | 1 | # mbed SDK |
theotherjimmy |
43:2a7da56ebd24 | 2 | # Copyright (c) 2011-2013 ARM Limited |
theotherjimmy |
43:2a7da56ebd24 | 3 | # |
theotherjimmy |
43:2a7da56ebd24 | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
theotherjimmy |
43:2a7da56ebd24 | 5 | # you may not use this file except in compliance with the License. |
theotherjimmy |
43:2a7da56ebd24 | 6 | # You may obtain a copy of the License at |
theotherjimmy |
43:2a7da56ebd24 | 7 | # |
theotherjimmy |
43:2a7da56ebd24 | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
theotherjimmy |
43:2a7da56ebd24 | 9 | # |
theotherjimmy |
43:2a7da56ebd24 | 10 | # Unless required by applicable law or agreed to in writing, software |
theotherjimmy |
43:2a7da56ebd24 | 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
theotherjimmy |
43:2a7da56ebd24 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
theotherjimmy |
43:2a7da56ebd24 | 13 | # See the License for the specific language governing permissions and |
theotherjimmy |
43:2a7da56ebd24 | 14 | # limitations under the License. |
theotherjimmy |
43:2a7da56ebd24 | 15 | |
theotherjimmy |
43:2a7da56ebd24 | 16 | import fnmatch |
theotherjimmy |
43:2a7da56ebd24 | 17 | import re |
theotherjimmy |
43:2a7da56ebd24 | 18 | from os.path import normcase, join |
theotherjimmy |
43:2a7da56ebd24 | 19 | |
theotherjimmy |
43:2a7da56ebd24 | 20 | |
theotherjimmy |
43:2a7da56ebd24 | 21 | IGNORE_FILENAME = ".mbedignore" |
theotherjimmy |
43:2a7da56ebd24 | 22 | |
theotherjimmy |
43:2a7da56ebd24 | 23 | |
theotherjimmy |
43:2a7da56ebd24 | 24 | class MbedIgnoreSet(object): |
theotherjimmy |
43:2a7da56ebd24 | 25 | """ |
theotherjimmy |
43:2a7da56ebd24 | 26 | # The mbedignore rules as an object |
theotherjimmy |
43:2a7da56ebd24 | 27 | |
theotherjimmy |
43:2a7da56ebd24 | 28 | A project in Mbed OS contains metadata files that exclude files from a build. |
theotherjimmy |
43:2a7da56ebd24 | 29 | These rules are stored as `fnmatch` patterns in text in a files named `.mbedignore`. |
theotherjimmy |
43:2a7da56ebd24 | 30 | """ |
theotherjimmy |
43:2a7da56ebd24 | 31 | |
theotherjimmy |
43:2a7da56ebd24 | 32 | def __init__(self): |
theotherjimmy |
43:2a7da56ebd24 | 33 | self._ignore_patterns = [] |
theotherjimmy |
43:2a7da56ebd24 | 34 | self._ignore_regex = re.compile("$^") |
theotherjimmy |
43:2a7da56ebd24 | 35 | |
theotherjimmy |
43:2a7da56ebd24 | 36 | def is_ignored(self, file_path): |
theotherjimmy |
43:2a7da56ebd24 | 37 | """Check if file path is ignored by any .mbedignore thus far""" |
theotherjimmy |
43:2a7da56ebd24 | 38 | return self._ignore_regex.match(normcase(file_path)) |
theotherjimmy |
43:2a7da56ebd24 | 39 | |
theotherjimmy |
43:2a7da56ebd24 | 40 | def add_ignore_patterns(self, in_name, patterns): |
theotherjimmy |
43:2a7da56ebd24 | 41 | """Ignore all files and directories matching the paterns in |
theotherjimmy |
43:2a7da56ebd24 | 42 | directories named by in_name. |
theotherjimmy |
43:2a7da56ebd24 | 43 | |
theotherjimmy |
43:2a7da56ebd24 | 44 | Positional arguments: |
theotherjimmy |
43:2a7da56ebd24 | 45 | in_name - the filename prefix that this ignore will apply to |
theotherjimmy |
43:2a7da56ebd24 | 46 | patterns - the list of patterns we will ignore in the future |
theotherjimmy |
43:2a7da56ebd24 | 47 | """ |
theotherjimmy |
43:2a7da56ebd24 | 48 | if in_name == ".": |
theotherjimmy |
43:2a7da56ebd24 | 49 | self._ignore_patterns.extend(normcase(p) for p in patterns) |
theotherjimmy |
43:2a7da56ebd24 | 50 | else: |
theotherjimmy |
43:2a7da56ebd24 | 51 | self._ignore_patterns.extend( |
theotherjimmy |
43:2a7da56ebd24 | 52 | normcase(join(in_name, pat)) for pat in patterns) |
theotherjimmy |
43:2a7da56ebd24 | 53 | if self._ignore_patterns: |
theotherjimmy |
43:2a7da56ebd24 | 54 | self._ignore_regex = re.compile("|".join( |
theotherjimmy |
43:2a7da56ebd24 | 55 | fnmatch.translate(p) for p in self._ignore_patterns)) |
theotherjimmy |
43:2a7da56ebd24 | 56 | |
theotherjimmy |
43:2a7da56ebd24 | 57 | def add_mbedignore(self, in_name, filepath): |
theotherjimmy |
43:2a7da56ebd24 | 58 | """Add a series of patterns to the ignored paths |
theotherjimmy |
43:2a7da56ebd24 | 59 | |
theotherjimmy |
43:2a7da56ebd24 | 60 | Positional arguments: |
theotherjimmy |
43:2a7da56ebd24 | 61 | in_name - the filename prefix that this ignore will apply to |
theotherjimmy |
43:2a7da56ebd24 | 62 | patterns - the list of patterns we will ignore in the future |
theotherjimmy |
43:2a7da56ebd24 | 63 | """ |
theotherjimmy |
43:2a7da56ebd24 | 64 | with open (filepath) as f: |
theotherjimmy |
43:2a7da56ebd24 | 65 | patterns = [l.strip() for l in f |
theotherjimmy |
43:2a7da56ebd24 | 66 | if l.strip() != "" and not l.startswith("#")] |
theotherjimmy |
43:2a7da56ebd24 | 67 | self.add_ignore_patterns(in_name, patterns) |
theotherjimmy |
43:2a7da56ebd24 | 68 | |
theotherjimmy |
43:2a7da56ebd24 | 69 |