def __init__(self, name, directory=".", packages=None, includes=None): self._name = module_name(name) self._in_src = False self._is_package = False self._path = Path(directory) self._includes = [] packages = packages or [] includes = includes or [] if not packages: # It must exist either as a .py file or a directory, but not both pkg_dir = Path(directory, self._name) py_file = Path(directory, self._name + ".py") if pkg_dir.is_dir() and py_file.is_file(): raise ValueError("Both {} and {} exist".format( pkg_dir, py_file)) elif pkg_dir.is_dir(): packages = [{"include": str(pkg_dir.relative_to(self._path))}] elif py_file.is_file(): packages = [{"include": str(py_file.relative_to(self._path))}] else: # Searching for a src module src = Path(directory, "src") src_pkg_dir = src / self._name src_py_file = src / (self._name + ".py") if src_pkg_dir.is_dir() and src_py_file.is_file(): raise ValueError("Both {} and {} exist".format( pkg_dir, py_file)) elif src_pkg_dir.is_dir(): packages = [{ "include": str(src_pkg_dir.relative_to(src)), "from": str(src.relative_to(self._path)), }] elif src_py_file.is_file(): packages = [{ "include": str(src_py_file.relative_to(src)), "from": str(src.relative_to(self._path)), }] else: raise ModuleOrPackageNotFound( "No file/folder found for package {}".format(name)) for package in packages: formats = package.get("format") if formats and not isinstance(formats, list): formats = [formats] self._includes.append( PackageInclude( self._path, package["include"], formats=formats, source=package.get("from"), )) for include in includes: self._includes.append( Include(self._path, include["path"], formats=include["format"]))
def executable(): global _executable if _executable is not None: return _executable if WINDOWS and PY36: # Finding git via where.exe where = "%WINDIR%\\System32\\where.exe" paths = decode( subprocess.check_output([where, "git"], shell=True, encoding="oem")).split("\n") for path in paths: if not path: continue path = Path(path.strip()) try: path.relative_to(Path.cwd()) except ValueError: _executable = str(path) break else: _executable = "git" if _executable is None: raise RuntimeError("Unable to find a valid git executable") return _executable
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
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