Skip to content

Instantly share code, notes, and snippets.

@arrio464
Created November 4, 2024 08:15
Show Gist options
  • Save arrio464/a3d39e93bf821212deb2fd7dfab1e6e6 to your computer and use it in GitHub Desktop.
Save arrio464/a3d39e93bf821212deb2fd7dfab1e6e6 to your computer and use it in GitHub Desktop.
scoop shim priority manage helper

By default, in scoop, later installed software will overwrite the shims of previously installed software.

There is no way to change the priority except scoop shim alter or scoop reset.

This is troublesome when you have many packages. So I wrote this script to make it more user friendly.

Just copy and run it in Python. Enjoy!

Known issue:

  • If there are spaces in the 'Name', may causing unexpected error
import os
import subprocess
def get_shim_list():
result = subprocess.run(
["pwsh", "-Command", "scoop shim list"],
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
)
return result.stdout.strip()
def parse_shim_list(output):
lines = output.strip().split("\n")[3:] # Skip header lines
shims = {}
for line in lines:
parts = line.split()[:-2]
if len(parts) < 3:
continue
name = parts[0]
source = parts[1]
alternatives = parts[2:]
print(f"{name}\t from {source}\t have alternatives: {", ".join(alternatives)}")
shims[name] = {"source": source, "alternatives": alternatives}
return shims
def modify_shim_priority(name, source, alternatives):
if len(alternatives) > 1 and alternatives[0] == source:
for alternative in alternatives:
choice = (
input(f"Do you want to use '{name}' from '{alternative}'? (Y/n): ")
.strip()
.lower()
or "y"
)
if choice in ["y", "yes"]:
cur_shim_path = os.path.join(
os.environ["SCOOP"], "shims", f"{name}.shim"
)
bak_shim_path = os.path.join(
os.environ["SCOOP"], "shims", f"{name}.shim.{source}"
)
new_shim_path = os.path.join(
os.environ["SCOOP"], "shims", f"{name}.shim.{alternative}"
)
os.rename(cur_shim_path, bak_shim_path)
os.rename(new_shim_path, cur_shim_path)
print(f"Modified shim priority for '{name}' to use '{alternative}'.")
break
else:
print("No changes made.")
else:
print(f"No modification needed for '{name}'.")
def main():
print("Scanning for shims, it may take a while...")
print("-" * 80)
output = get_shim_list()
shims = parse_shim_list(output)
print("-" * 80)
for name, details in shims.items():
choice = (
input(
f"Do you want to modify shim priority for '{name}'? (Currently using '{details['source']}') (y/N): "
)
.strip()
.lower()
or "n"
)
if choice in ["y", "yes"]:
modify_shim_priority(name, details["source"], details["alternatives"])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment