You are not logged in.

#1 2014-09-11 06:04:58

citizen-rosebud
Member
Registered: 2014-09-11
Posts: 1

Wrapping tasks as sub-commands in a python script (pacman edition)

I wanted to share a script with you all, along with its motivations.

I originally wrote this script to dumb down routine pacman tasks. If you're like me, you probably remember the temporary confusion of going through the pacman manual for the first time. If you're even more like me, you probably gave a hefty sigh of relief when you found the pacman aliases up on the wiki. In my case, those spoilers made Arch more comfortable... So naturally, I never remembered the underlying pacman flags. Even though I've been relying on those things for years now, only a few are committed to memory. Recently I got fed up with checking my .*shrc every time I forgot an alias. I wanted that documentation anywhere except buried in a comment in a dotfile.

At any rate, I started to realize aliases weren't going to cut it. One major pitfall of aliases, as useful as they can be, is that they fail to organize in groups. No such thing as a namespace in sh as far as I'm aware, and naming conventions always end up feeling awkward. IMHO a more aesthetic solution would be to group these aliases under a super-command. And why not throw in a minimal amount of documentation for kicks? I would have attempted this in sh for portability but that language scares me sometimes. Python felt better suited to at least sketch it out:

  • Usage: script-name command [arguments]

  • Commands are hard coded at the top of the script.

  • Commands are stored along with a single line description.

  • When run with no arguments or a bad command, a list of valid commands and their descriptions is printed.

  • On a match, command is sent to sh for execution.

  • Appends extra arguments as you would expect.

  • Prints the full command run before execution. This helps you actually learn the underlying flags if you are so inclined.

  • Has a builtin "teach" command which shows the underlying command, alias, and description. Good for studying tongue

And of course it's not like you can't change any of that... One thing that will need attention is the print formatting, which was tuned to fit that content. I'm sure that could be figured out more intelligently wink

#!/bin/env python2
import sys,subprocess

commands = [
	["upgrade", "sudo pacman -Syu", "Synchronize with repositories and update all packages"],
	["install", "sudo pacman -S", "Install package(s) from the repositories"],
	["file", "sudo pacman -U", "Install package directly from file"],
	["remove", "sudo pacman -R", "Uninstall package(s), retaining configuration and dependencies"],
	["purge", "sudo pacman -Rns", "Uninstall package(s) completely, including dependencies"],
	["info", "pacman -Si", "Display package information from repositories"],
	["search", "pacman -Ss", "Search for package(s) in the repositories"],
	["local-info", "pacman -Qi", "Display package information from the local database"],
	["local-search", "pacman -Qs", "Search for package(s) in the local database"],
	["refresh", "sudo pacman -Syy", "Force refresh of package lists (after updating mirrorlist)"]
]

def Help():
	fmt = "%-14s\t%s"
	print("#== Valid Subcommands ==#")
	for c in commands:
		print(fmt) % (c[0],c[2])
	print(fmt) % ("teach","[Bonus!] Show how to run the above commands without this script")

def Teach():
	print("#== Shell Equivalents ==#")
	for c in commands:
		print("%-18s\t%-14s\t%s") % (c[1],'('+c[0]+')',c[2])

n = len(sys.argv)
if n > 1:
	cmd = sys.argv[1]
	run = False
	for c in commands:
		if cmd == c[0]:
			run = c[1]
	if run:
		for i in range(2,n):
			run += " '"+sys.argv[i]+"'"
		print("#> %s") % run
		subprocess.call(['sh','-c',run])
	else:
		if cmd == "teach":
			Teach()
		else:
			Help()
else:
	Help()

I will probably round off this concept in the future and post it on the wiki. I'm sure the pacman example has been done to death before, but I am thinking of this more as a general template for a shortcut-tool. Let me know what you think!

Offline

Board footer

Powered by FluxBB