Skip to content

Instantly share code, notes, and snippets.

@MercuryRising
Last active December 15, 2015 11:09
Show Gist options
  • Save MercuryRising/5251020 to your computer and use it in GitHub Desktop.
Save MercuryRising/5251020 to your computer and use it in GitHub Desktop.
Easily alias your bashrc file (a 'step through' of making the program)
'''
We are going to make a program that lets us run something like:
python easyalias.py sl ls
or
python easyalias.py I_want_my_python_now python
and adds the alias to our .bashrc file, letting us easily add aliases that persist without directly editing the file.
After we run our command, we'll have to source the .bashrc file with
source ~/.bashrc
We could automate this action, but just in case we mess up (even though we'll make sure the user entered what they wanted), we'll be able to change it out manually if we need to.
Although we'll keep it short, there's many ways we could continue to iterate this program to make it better:
We could use regexes to grab sensible lines (run the script a few times (the one at the end), and you'll see why it could be better)
We could automatically source our bash file so our aliases would occur right away (if you're wondering, this line will do it)
import subprocess; subprocess.call(["source", bashPath])
But let's just get on with the show.
'''
''' Version 1 - define inputs and outputs (usually done in my head, although it's always good to get your fingers moving when you're starting from scratch)'''
# Bash RC aliaser
# inputs: aliasName aliasDestination
# outputs: run source ~/.bashrc for commands to take effect
''' Version 2 - check & grab input from user '''
# Bash RC aliaser
# inputs: aliasName aliasDestination
# outputs: run source ~/.bashrc for commands to take effect
# sys is needed to grab input variables from command line
import sys
# just print the command line arguments and see what they are
print sys.argv
''' run program, see what it does
MerucryRising@MerucryRising-PC:~/programming$ python easyalias.py
['easyalias.py']
MerucryRising@MerucryRising-PC:~/programming$ python easyalias.py hey now
['easyalias.py', 'hey', 'now']
'''
''' Version 3 - check length of sys.argv to make sure it's right (3) '''
# Bash RC aliaser
# inputs: aliasName aliasDestination
# outputs: run source ~/.bashrc for commands to take effect
# sys is needed to grab input variables from command line
import sys
if len(sys.argv) == 3:
# We're good - we have script name, aliasName, aliasDestination
print len(sys.argv)
else:
print "Usage: scriptName aliasName aliasDestination"
''' Run program:
MerucryRising@MerucryRising-PC:~/programming$ python easyalias.py hey now
3
MerucryRising@MerucryRising-PC:~/programming$ python easyalias.py hey now hey
Usage: scriptName aliasName aliasDestination
MerucryRising@MerucryRising-PC:~/programming$ python easyalias.py hey
Usage: scriptName aliasName aliasDestination
'''
''' Version 3 - make sure we have the right user supplied information before we start changing things around '''
# Bash RC aliaser
# inputs: aliasName aliasDestination
# outputs: run source ~/.bashrc for commands to take effect
# sys is needed to grab input variables from command line
import sys
if len(sys.argv) == 3:
# We're good - we have script name, aliasName, aliasDestination
# Verify we have what the user wanted
aliasName = sys.argv[1]
aliasDestination = sys.argv[2]
verify = raw_input("Mapping {aliasName} to {aliasDestination} - is this what you want? > ".format(aliasName=aliasName, aliasDestination=aliasDestination))
if "y" in verify.lower():
print "Mapping command now"
else:
print "Not what you wanted. Run again with desired aliases."
else:
print "Usage: scriptName aliasName aliasDestination"
''' Running program
MerucryRising@MerucryRising-PC:~/programming$ python easyalias.py hey now
Mapping hey to now - is this what you want? > y
Mapping command now
'''
''' Version 4 - add checks to look in file and see if our aliasName or aliasDestination are in there already'''
# Bash RC aliaser
# inputs: aliasName aliasDestination
# outputs: run source ~/.bashrc for commands to take effect
# sys is needed to grab input variables from command line
import sys
bashPath = "/home/MerucryRising/.bashrc"
def alias_command(name, destination):
print "Mapping %s to %s" %(name, destination)
def check_conflicts(words):
# open up the file in read mode, grab the data
with open(bashPath, "rb") as f:
lines = f.readlines()
conflicts = []
for line in lines:
for word in words:
if word in line:
conflicts.append((word, line))
return conflicts
if len(sys.argv) == 3:
# We're good - we have script name, aliasName, aliasDestination
# Verify we have what the user wanted
aliasName = sys.argv[1]
aliasDestination = sys.argv[2]
# Check for conflicts in the file we're going to append to, just to make sure we don't map over something that's already there
conflicts = check_conflicts([aliasName, aliasDestination])
conflictsFormatted = "\n".join( (" ".join(pair) for pair in conflicts) )
verify = raw_input("Mapping {aliasName} to {aliasDestination}\nPossible Conflicts:\n{conflicts}\nIs this what you want? > ".format(aliasName=aliasName, aliasDestination=aliasDestination, conflicts=conflictsFormatted))
if "y" in verify.lower():
alias_command(aliasName, aliasDestination)
else:
print "Not what you wanted. Run again with desired aliases."
else:
print "Usage: scriptName aliasName aliasDestination"
''' Output:
MerucryRising@MerucryRising-PC:~/programming$ python easyalias.py hey now
Mapping hey to now
Possible Conflicts:
now # set a fancy prompt (non-color, unless we know we "want" color)
Is this what you want? > y
Mapping hey to now
'''
''' Version 5: get the line that we're going to append to the bashrc file ready (added this to alias_command(name, destination)'''
def alias_command(name, destination):
# Create the new line that we're going to add to our bash file
line = '\nalias %s="%s"'%(name, destination)
print "Adding line: %s to %s"%(line, bashPath)
''' Output:
MerucryRising@MerucryRising-PC:~/programming$ python easyalias.py hey now
Mapping hey to now
Possible Conflicts:
now # set a fancy prompt (non-color, unless we know we "want" color)
Is this what you want? > y
Adding line:
alias hey="now" to /home/MerucryRising/.bashrc
'''
''' Version 6: Adding it all together, testing it out '''
# Bash RC aliaser
# inputs: aliasName aliasDestination
# outputs: run source ~/.bashrc for commands to take effect
# sys is needed to grab input variables from command line
import sys
bashPath = "/home/MerucryRising/.bashrc"
def alias_command(name, destination):
# Create the new line that we're going to add to our bash file
line = '\nalias %s="%s"\n'%(name, destination)
print "Adding line: %s to %s"%(line, bashPath)
with open(bashPath, "ab") as f:
f.write(line)
def check_conflicts(words):
# open up the file in read mode, grab the data
with open(bashPath, "rb") as f:
lines = f.readlines()
conflicts = []
for line in lines:
for word in words:
if word in line:
conflicts.append((word, line))
return conflicts
if len(sys.argv) == 3:
# We're good - we have script name, aliasName, aliasDestination
# Verify we have what the user wanted
aliasName = sys.argv[1]
aliasDestination = sys.argv[2]
# Check for conflicts in the file we're going to append to, just to make sure we don't map over something that's already there
conflicts = check_conflicts([aliasName, aliasDestination])
conflictsFormatted = "\n".join( (" ".join(pair) for pair in conflicts) )
verify = raw_input("Mapping {aliasName} to {aliasDestination}\nPossible Conflicts:\n{conflicts}\nIs this what you want? > ".format(aliasName=aliasName, aliasDestination=aliasDestination, conflicts=conflictsFormatted))
if "y" in verify.lower():
alias_command(aliasName, aliasDestination)
else:
print "Not what you wanted. Run again with desired aliases."
else:
print "Usage: scriptName aliasName aliasDestination"
''' Output:
MerucryRising@MerucryRising-PC:~/programming$ python easyalias.py gimme_python_now /usr/bin/python
Mapping gimme_python_now to /usr/bin/python
Possible Conflicts:
Is this what you want? > y
Adding line:
alias gimme_python_now="/usr/bin/python"
to /home/MerucryRising/.bashrc
MerucryRising@MerucryRising-PC:~/programming$ source ~/.bashrc
giMerucryRising@MerucryRising-PC:~/programming$ gimme_python_now
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment