class BuildIncludeFile: def __init__( self, path, # type: Path source_root=None, # type: Optional[Path] ): """ :param path: a full path to the file to be included :param source_root: the root path to resolve to """ self.path = Path(path) self.source_root = None if not source_root else Path(source_root).resolve() if not self.path.is_absolute() and self.source_root: self.path = (self.source_root / self.path).resolve() else: self.path = self.path.resolve() def __eq__(self, other): # type: (Union[BuildIncludeFile, Path]) -> bool if hasattr(other, "path"): return self.path == other.path return self.path == other def __ne__(self, other): # type: (Union[BuildIncludeFile, Path]) -> bool return not self.__eq__(other) def __hash__(self): return hash(self.path) def __repr__(self): # type: () -> str return str(self.path) def relative_to_source_root(self): # type(): -> Path if self.source_root is not None: return self.path.relative_to(self.source_root) return self.path
class BuildIncludeFile: def __init__( self, path, # type: Union[Path, str] project_root, # type: Union[Path, str] source_root=None, # type: Optional[Union[Path, str]] ): """ :param project_root: the full path of the project's root :param path: a full path to the file to be included :param source_root: the root path to resolve to """ self.path = Path(path) self.project_root = Path(project_root).resolve() self.source_root = None if not source_root else Path( source_root).resolve() if not self.path.is_absolute() and self.source_root: self.path = self.source_root / self.path else: self.path = self.path try: self.path = self.path.resolve() except FileNotFoundError: # this is an issue in in python 3.5, since resolve uses strict=True by # default, this workaround needs to be maintained till python 2.7 and # python 3.5 are dropped, until we can use resolve(strict=False). pass def __eq__(self, other): # type: (Union[BuildIncludeFile, Path]) -> bool if hasattr(other, "path"): return self.path == other.path return self.path == other def __ne__(self, other): # type: (Union[BuildIncludeFile, Path]) -> bool return not self.__eq__(other) def __hash__(self): # type: () -> int return hash(self.path) def __repr__(self): # type: () -> str return str(self.path) def relative_to_project_root(self): # type: () -> Path return self.path.relative_to(self.project_root) def relative_to_source_root(self): # type: () -> Path if self.source_root is not None: return self.path.relative_to(self.source_root) return self.path