feat: pkg.link(quiet) + File.remove(quiet)

This commit is contained in:
relikd
2025-09-02 13:38:30 +02:00
parent e71ed66102
commit 4936302f83

35
brew.py
View File

@@ -1096,14 +1096,14 @@ class LocalPackage:
if unlinkOpt: if unlinkOpt:
rv += filter(None, [self.readOptLink(ensurePkg=False)]) rv += filter(None, [self.readOptLink(ensurePkg=False)])
if not dryRun:
self._resetCachedProperty(optLink=unlinkOpt, binLink=unlinkBin)
for lnk in rv: for lnk in rv:
if not quiet: if not quiet:
Log.info(f' unlink {Cellar.shortPath(lnk.path)} -> {lnk.raw}') Log.info(f' unlink {Cellar.shortPath(lnk.path)} -> {lnk.raw}')
if not dryRun: if not dryRun:
os.remove(lnk.path) os.remove(lnk.path)
if not dryRun:
self._resetCachedProperty(optLink=unlinkOpt, binLink=unlinkBin)
return rv return rv
def _resetCachedProperty(self, *, optLink: bool, binLink: bool) -> None: def _resetCachedProperty(self, *, optLink: bool, binLink: bool) -> None:
@@ -1170,17 +1170,15 @@ class LocalPackageVersion:
def link( def link(
self, *, linkOpt: bool = True, linkBin: bool = True, self, *, linkOpt: bool = True, linkBin: bool = True,
dryRun: bool = False, dryRun: bool = False, quiet: bool = False,
) -> None: ) -> None:
''' create symlinks `@/opt/<pkg>` and `@/bin/...` matching target ''' ''' create symlinks `@/opt/<pkg>` and `@/bin/...` matching target '''
if not self.installed: if not self.installed:
raise RuntimeError('Package not installed') raise RuntimeError('Package not installed')
if not dryRun:
self.pkg._resetCachedProperty(optLink=linkOpt, binLink=linkBin)
optLinkPath = os.path.join(Cellar.OPT, self.pkg.name)
queue = [] queue = []
optLinkPath = os.path.join(Cellar.OPT, self.pkg.name)
if linkOpt: if linkOpt:
queue.append(LinkTarget(optLinkPath, self.path + '/')) queue.append(LinkTarget(optLinkPath, self.path + '/'))
@@ -1196,10 +1194,14 @@ class LocalPackageVersion:
if os.path.islink(link.path) or os.path.exists(link.path): if os.path.islink(link.path) or os.path.exists(link.path):
Log.warn(f'skip already existing link: {short}', summary=True) Log.warn(f'skip already existing link: {short}', summary=True)
else: else:
Log.info(f' link {short} -> {relTgt}') if not quiet:
Log.info(f' link {short} -> {relTgt}')
if not dryRun: if not dryRun:
os.symlink(relTgt, link.path) os.symlink(relTgt, link.path)
if not dryRun:
self.pkg._resetCachedProperty(optLink=linkOpt, binLink=linkBin)
# Custom config files # Custom config files
def setDigest(self, digest: str) -> None: def setDigest(self, digest: str) -> None:
@@ -1444,6 +1446,8 @@ class TarPackage:
subset.append(x) subset.append(x)
if not pkg and x.isdir() and x.path.endswith('/.brew'): if not pkg and x.isdir() and x.path.endswith('/.brew'):
pkg, version, *_ = x.path.split('/') pkg, version, *_ = x.path.split('/')
if dryRun:
break
else: else:
Log.error(f'prohibited tar entry "{x.path}" in', shortPath, Log.error(f'prohibited tar entry "{x.path}" in', shortPath,
summary=True) summary=True)
@@ -2202,7 +2206,7 @@ class File:
return files, size return files, size
@staticmethod @staticmethod
def remove(path: str, *, dryRun: bool) -> int: def remove(path: str, *, dryRun: bool = False, quiet: bool = False) -> int:
'''Delete file or folder. Calculate and print size. Optional dry-run''' '''Delete file or folder. Calculate and print size. Optional dry-run'''
isdir = os.path.isdir(path) isdir = os.path.isdir(path)
if isdir: if isdir:
@@ -2210,11 +2214,12 @@ class File:
else: else:
size = 0 if os.path.islink(path) else os.path.getsize(path) size = 0 if os.path.islink(path) else os.path.getsize(path)
Log.main('{}: {} ({}{})'.format( if not quiet:
'Would remove' if dryRun else 'Removing', Log.main('{}: {} ({}{})'.format(
Cellar.shortPath(path), 'Would remove' if dryRun else 'Removing',
f'{files} files, ' if isdir else '', Cellar.shortPath(path),
Utils.humanSize(size))) f'{files} files, ' if isdir else '',
Utils.humanSize(size)))
if not dryRun: if not dryRun:
shutil.rmtree(path) if isdir else os.remove(path) shutil.rmtree(path) if isdir else os.remove(path)
return size return size