mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			Refactor mbedignore behaivor into an object
							parent
							
								
									4ef12ccdbc
								
							
						
					
					
						commit
						ff05e3ebd5
					
				| 
						 | 
					@ -41,6 +41,8 @@ from os import walk, sep
 | 
				
			||||||
from os.path import (join, splitext, dirname, relpath, basename, split, normcase,
 | 
					from os.path import (join, splitext, dirname, relpath, basename, split, normcase,
 | 
				
			||||||
                     abspath, exists)
 | 
					                     abspath, exists)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from .ignore import MbedIgnoreSet, IGNORE_FILENAME
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Support legacy build conventions: the original mbed build system did not have
 | 
					# Support legacy build conventions: the original mbed build system did not have
 | 
				
			||||||
# standard labels for the "TARGET_" and "TOOLCHAIN_" specific directories, but
 | 
					# standard labels for the "TARGET_" and "TOOLCHAIN_" specific directories, but
 | 
				
			||||||
# had the knowledge of a list of these directories to be ignored.
 | 
					# had the knowledge of a list of these directories to be ignored.
 | 
				
			||||||
| 
						 | 
					@ -143,10 +145,7 @@ class Resources(object):
 | 
				
			||||||
        # Path seperator style (defaults to OS-specific seperator)
 | 
					        # Path seperator style (defaults to OS-specific seperator)
 | 
				
			||||||
        self._sep = sep
 | 
					        self._sep = sep
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Ignore patterns from .mbedignore files and add_ignore_patters
 | 
					        self._ignoreset = MbedIgnoreSet()
 | 
				
			||||||
        self._ignore_patterns = []
 | 
					 | 
				
			||||||
        self._ignore_regex = re.compile("$^")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def ignore_dir(self, directory):
 | 
					    def ignore_dir(self, directory):
 | 
				
			||||||
        if self._collect_ignores:
 | 
					        if self._collect_ignores:
 | 
				
			||||||
| 
						 | 
					@ -241,27 +240,9 @@ class Resources(object):
 | 
				
			||||||
        self._legacy_ignore_dirs -= set(
 | 
					        self._legacy_ignore_dirs -= set(
 | 
				
			||||||
            [toolchain.target.name, LEGACY_TOOLCHAIN_NAMES[toolchain.name]])
 | 
					            [toolchain.target.name, LEGACY_TOOLCHAIN_NAMES[toolchain.name]])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def is_ignored(self, file_path):
 | 
					 | 
				
			||||||
        """Check if file path is ignored by any .mbedignore thus far"""
 | 
					 | 
				
			||||||
        return self._ignore_regex.match(normcase(file_path))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def add_ignore_patterns(self, root, base_path, patterns):
 | 
					    def add_ignore_patterns(self, root, base_path, patterns):
 | 
				
			||||||
        """Add a series of patterns to the ignored paths
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        Positional arguments:
 | 
					 | 
				
			||||||
        root - the directory containing the ignore file
 | 
					 | 
				
			||||||
        base_path - the location that the scan started from
 | 
					 | 
				
			||||||
        patterns - the list of patterns we will ignore in the future
 | 
					 | 
				
			||||||
        """
 | 
					 | 
				
			||||||
        real_base = relpath(root, base_path)
 | 
					        real_base = relpath(root, base_path)
 | 
				
			||||||
        if real_base == ".":
 | 
					        self._ignoreset.add_ignore_patterns(real_base, patterns)
 | 
				
			||||||
            self._ignore_patterns.extend(normcase(p) for p in patterns)
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            self._ignore_patterns.extend(
 | 
					 | 
				
			||||||
                normcase(join(real_base, pat)) for pat in patterns)
 | 
					 | 
				
			||||||
        if self._ignore_patterns:
 | 
					 | 
				
			||||||
            self._ignore_regex = re.compile("|".join(
 | 
					 | 
				
			||||||
                fnmatch.translate(p) for p in self._ignore_patterns))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _not_current_label(self, dirname, label_type):
 | 
					    def _not_current_label(self, dirname, label_type):
 | 
				
			||||||
        return (dirname.startswith(label_type + "_") and
 | 
					        return (dirname.startswith(label_type + "_") and
 | 
				
			||||||
| 
						 | 
					@ -405,15 +386,13 @@ class Resources(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for root, dirs, files in walk(path, followlinks=True):
 | 
					        for root, dirs, files in walk(path, followlinks=True):
 | 
				
			||||||
            # Check if folder contains .mbedignore
 | 
					            # Check if folder contains .mbedignore
 | 
				
			||||||
            if ".mbedignore" in files:
 | 
					            if IGNORE_FILENAME in files:
 | 
				
			||||||
                with open (join(root,".mbedignore"), "r") as f:
 | 
					                real_base = relpath(root, base_path)
 | 
				
			||||||
                    lines=f.readlines()
 | 
					                self._ignoreset.add_mbedignore(
 | 
				
			||||||
                    lines = [l.strip() for l in lines
 | 
					                    real_base, join(root, IGNORE_FILENAME))
 | 
				
			||||||
                             if l.strip() != "" and not l.startswith("#")]
 | 
					 | 
				
			||||||
                    self.add_ignore_patterns(root, base_path, lines)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            root_path =join(relpath(root, base_path))
 | 
					            root_path =join(relpath(root, base_path))
 | 
				
			||||||
            if self.is_ignored(join(root_path,"")):
 | 
					            if self._ignoreset.is_ignored(join(root_path,"")):
 | 
				
			||||||
                self.ignore_dir(root_path)
 | 
					                self.ignore_dir(root_path)
 | 
				
			||||||
                dirs[:] = []
 | 
					                dirs[:] = []
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
| 
						 | 
					@ -430,7 +409,7 @@ class Resources(object):
 | 
				
			||||||
                    self.ignore_dir(dir_path)
 | 
					                    self.ignore_dir(dir_path)
 | 
				
			||||||
                    dirs.remove(d)
 | 
					                    dirs.remove(d)
 | 
				
			||||||
                elif (d.startswith('.') or d in self._legacy_ignore_dirs or
 | 
					                elif (d.startswith('.') or d in self._legacy_ignore_dirs or
 | 
				
			||||||
                      self.is_ignored(join(root_path, d, ""))):
 | 
					                      self._ignoreset.is_ignored(join(root_path, d, ""))):
 | 
				
			||||||
                    self.ignore_dir(dir_path)
 | 
					                    self.ignore_dir(dir_path)
 | 
				
			||||||
                    dirs.remove(d)
 | 
					                    dirs.remove(d)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -472,7 +451,7 @@ class Resources(object):
 | 
				
			||||||
        scanning starting as base_path
 | 
					        scanning starting as base_path
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if  (self.is_ignored(relpath(file_path, base_path)) or
 | 
					        if  (self._ignoreset.is_ignored(relpath(file_path, base_path)) or
 | 
				
			||||||
             basename(file_path).startswith(".")):
 | 
					             basename(file_path).startswith(".")):
 | 
				
			||||||
            self.ignore_dir(relpath(file_path, base_path))
 | 
					            self.ignore_dir(relpath(file_path, base_path))
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
| 
						 | 
					@ -550,3 +529,4 @@ class Resources(object):
 | 
				
			||||||
                self.add_directory(path)
 | 
					                self.add_directory(path)
 | 
				
			||||||
        config.load_resources(self)
 | 
					        config.load_resources(self)
 | 
				
			||||||
        return self
 | 
					        return self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,69 @@
 | 
				
			||||||
 | 
					# mbed SDK
 | 
				
			||||||
 | 
					# Copyright (c) 2011-2013 ARM Limited
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					# you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					# You may obtain a copy of the License at
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					#     http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					# distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					# See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					# limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import fnmatch
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
 | 
					from os.path import normcase, join
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					IGNORE_FILENAME = ".mbedignore"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class MbedIgnoreSet(object):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    # The mbedignore rules as an object
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    A project in Mbed OS contains metadata files that exclude files from a build.
 | 
				
			||||||
 | 
					    These rules are stored as `fnmatch` patterns in text in a files named `.mbedignore`.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __init__(self):
 | 
				
			||||||
 | 
					        self._ignore_patterns = []
 | 
				
			||||||
 | 
					        self._ignore_regex = re.compile("$^")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def is_ignored(self, file_path):
 | 
				
			||||||
 | 
					        """Check if file path is ignored by any .mbedignore thus far"""
 | 
				
			||||||
 | 
					        return self._ignore_regex.match(normcase(file_path))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def add_ignore_patterns(self, in_name, patterns):
 | 
				
			||||||
 | 
					        """Ignore all files and directories matching the paterns in 
 | 
				
			||||||
 | 
					        directories named by in_name.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Positional arguments:
 | 
				
			||||||
 | 
					        in_name - the filename prefix that this ignore will apply to
 | 
				
			||||||
 | 
					        patterns - the list of patterns we will ignore in the future
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        if in_name == ".":
 | 
				
			||||||
 | 
					            self._ignore_patterns.extend(normcase(p) for p in patterns)
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            self._ignore_patterns.extend(
 | 
				
			||||||
 | 
					                normcase(join(in_name, pat)) for pat in patterns)
 | 
				
			||||||
 | 
					        if self._ignore_patterns:
 | 
				
			||||||
 | 
					            self._ignore_regex = re.compile("|".join(
 | 
				
			||||||
 | 
					                fnmatch.translate(p) for p in self._ignore_patterns))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def add_mbedignore(self, in_name, filepath):
 | 
				
			||||||
 | 
					        """Add a series of patterns to the ignored paths
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Positional arguments:
 | 
				
			||||||
 | 
					        in_name - the filename prefix that this ignore will apply to
 | 
				
			||||||
 | 
					        patterns - the list of patterns we will ignore in the future
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        with open (filepath) as f:
 | 
				
			||||||
 | 
					            patterns = [l.strip() for l in f
 | 
				
			||||||
 | 
					                        if l.strip() != "" and not l.startswith("#")]
 | 
				
			||||||
 | 
					            self.add_ignore_patterns(in_name, patterns)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue