def _createShortPath(longPath) -> str: import utils longPath = OsUtils.toNativePath(longPath) if not CraftShortPath._useShortpaths: return longPath if not os.path.isdir(CraftCore.standardDirs.junctionsDir()): os.makedirs(CraftCore.standardDirs.junctionsDir()) path = OsUtils.toNativePath( os.path.join(CraftCore.standardDirs.junctionsDir(), str(zlib.crc32(bytes(longPath, "UTF-8"))))) if len(longPath) < len(path): CraftCore.debug.log.debug( f"Using junctions for {longPath} wouldn't save characters returning original path" ) CraftCore.debug.log.debug( f"{longPath}\n" f"{path}, gain:{len(longPath) - len(path)}") return longPath os.makedirs(longPath, exist_ok=True) if not os.path.isdir(path): # note: mklink is a CMD command => needs shell if not utils.system(["mklink", "/J", path, longPath], shell=True, stdout=subprocess.DEVNULL, logCommand=False): CraftCore.debug.log.critical( f"Could not create shortpath {path}, for {longPath}") return longPath else: if not os.path.samefile(path, longPath): CraftCore.debug.log.critical( f"Existing short path {path}, did not match {longPath}") return longPath return path
def _createShortPath(longPath) -> str: import utils longPath = OsUtils.toNativePath(longPath) utils.createDir(CraftCore.standardDirs.junctionsDir()) path = OsUtils.toNativePath( os.path.join(CraftCore.standardDirs.junctionsDir(), hex(zlib.crc32(bytes(longPath, "UTF-8")))[2:])) if len(longPath) < len(path): CraftCore.debug.log.debug( f"Using junctions for {longPath} wouldn't save characters returning original path" ) CraftCore.debug.log.debug( f"{longPath}\n" f"{path}, gain:{len(longPath) - len(path)}") return longPath utils.createDir(longPath) if not os.path.isdir(path): if OsUtils.isUnix(): ok = utils.createSymlink(longPath, path, useAbsolutePath=True, targetIsDirectory=True) else: # note: mklink is a CMD command => needs shell ok = utils.system(["mklink", "/J", path, longPath], shell=True, stdout=subprocess.DEVNULL, logCommand=False) if not ok: CraftCore.debug.log.critical( f"Could not create shortpath {path}, for {longPath}") return longPath else: if not os.path.samefile(path, longPath): CraftCore.debug.log.critical( f"Existing short path {path}, did not match {longPath}") return longPath return path
def installShortcut(name : str, path : str, workingDir : str, icon : str, desciption : str): if not CraftCore.compiler.isWindows: return True from shells import Powershell pwsh = Powershell() shortcutPath = Path(os.environ["APPDATA"]) / f"Microsoft/Windows/Start Menu/Programs/Craft/{name}.lnk" shortcutPath.parent.mkdir(parents=True, exist_ok=True) return pwsh.execute([os.path.join(CraftCore.standardDirs.craftBin(), "install-lnk.ps1"), "-Path", pwsh.quote(path), "-WorkingDirectory", pwsh.quote(OsUtils.toNativePath(workingDir)), "-Name", pwsh.quote(shortcutPath), "-Icon", pwsh.quote(icon), "-Description", pwsh.quote(desciption)])
def patchInstallPrefix( self, files: [str], oldPaths: [str] = None, newPath: str = CraftCore.standardDirs.craftRoot() ) -> bool: if isinstance(oldPaths, str): oldPaths = [oldPaths] elif not oldPaths: oldPaths = [self.subinfo.buildPrefix] newPathWin = OsUtils.toNativePath(newPath) newPathUnix = OsUtils.toUnixPath(newPath) newPath = newPathUnix for fileName in files: if not os.path.exists(fileName): CraftCore.log.warning(f"File {fileName} not found.") return False with open(fileName, "rb") as f: content = f.read() dirty = False for oldPath in oldPaths: assert os.path.isabs(oldPath) if CraftCore.compiler.isWindows: _, ext = os.path.splitext(fileName) # keep unix path sep or use unix path sep for specific type # prl and pri files might use \\\\ as sep which can be replaced by a / but not by a single \\ if oldPath[2] == "/" or ext in {".prl", ".pri"}: newPath = newPathUnix else: # keep windows sep newPath = newPathWin oldPathBinary = oldPath.encode() if oldPath != newPath and oldPathBinary in content: dirty = True CraftCore.log.info( f"Patching {fileName}: replacing {oldPath} with {newPath}" ) content = content.replace(oldPathBinary, newPath.encode()) if dirty: with utils.makeWritable(fileName): with open(fileName, "wb") as f: f.write(content) return True