Results 1 to 1 of 1
netstrider and I decided to practice our Python skills by translating a BASH script to Python. The below is a working BASH script, although it's a bit crude and not ...
- 05-24-2008 #1
Python practise (not a question)
netstrider and I decided to practice our Python skills by translating a BASH script to Python. The below is a working BASH script, although it's a bit crude and not fully developed in terms of features.
A couple of things. I spend more time adding features than on cleaning code. So it's a bit crude, although it works. Also, my lay out got destroyed (it was so beautifully aligned :-s). I hope it comes back when pasted into Vim (untested :-p)
The rest should be self evident from the comments.
I guess the first thing to do is just read this and let it sink for a while. Then build the core functionality in Python. And add module after module until it's the new wave in administrative toolsCode:#!/bin/bash # Name: CusToM - ctm # Author: aka Freston # Purpose: Customize configs safely and easily # Keeps customized files in central location # so to keep better track of them # Version: Alpha 0.03 # Copyright: It's a bleedin' bash script # # TODO # PERMISSION RELATED: # Allow for user-owned custom files (for example user scripts) # Append username to user config files like .bashrc -> user.bashrc # Sanetize permissions for above functionality # Check user UID for permissions to avoid obnoxious errors # # INTERNAL WORKINGS: # Relative path breaks meta data on first run of 'ctm filename' # Cleanup module (no need to keep n+763 backups of any given file) # Remark function (to keep track of different versions) # Restore original # Clean up on error messages (mainly grep and test spit out ugly errors) # # INTERACTIVITY: # Create interactive archive of specified, range or all custom files # Interactive restore from archive, after clean install for example # View list of different versions of custom file, including remarks # Toggle between different versions of custom file (?) # # DESCRIPTION OF FUNCTIONALITY # If /path/filename is given, this script: # 1) Checks whether target is a regular file & if so # - Treats regular file as original file # - Moves the original file to secure location # - Makes backup and workfile # - Makes symlink from /path/filename to workfile # - Allows for editing # 2) Checks whether target is a symlink & if yes # - Backs up the target (Yay for Dunglish!) # - Allows editing of said file # 3) Allows thus for editing of files while # - Automagicaly keeping both the original and # backups for easy restore (with the -r option) # - Needing only 'filename' if previous custom file # was created (ctm fstab) # 4) See in one glance which files have been customized # - Home brew scripts # - Flat text files and documentation # - Config files # - System settings # CONVENTIONS: # - Target: The intended file (example: /etc/fstab) -or- the softlink # that replaces target (/etc/fstab@ -> /etc/custom/fstab) # Note: after first 'ctm target', no path needs to be specified # - Original: ctm stores the original file away from regular backups # - Workfile: Active version where target points to (/etc/custom/fstab) # - Metadata: Extra file that stores extra data needed by ctm to function # - Keeps track of full path # - Remark function will be added # - Backup: Previous versions of workfile, for easy restore # - Custom file: A file that has been through the ctm NewLink function # storing original, making backup, link, workfile and metadata ############# # VARIABLES:# ############# if [ $2 ] ; then TARGET=$2 else TARGET=$1 fi FILENAME=${TARGET##*/} CUSTOM=/etc/custom/ # NEVER point this path outside / partition, ever! BACKUP=$CUSTOM/backup/$FILENAME ORIGINAL=$CUSTOM/original/$FILENAME WORKFILE=$CUSTOM/$FILENAME COUNT=100 # Before we start: if [ ! -d $CUSTOM ] ; then echo -n "Yes means no and no means yes. Delete all files? (Y/n): " read INPUT # This of course doesn't do if [ $INPUT = 'justwork' ] ; then # anything except for testing mkdir /$CUSTOM # whether $user has understood the mkdir /$CUSTOM/backup # script, so to know the magic mkdir /$CUSTOM/original # word that get's the script to else # 'just work'. echo wrong # People should know better then to exit 2 # run alpha phase scripts without fi # understanding how they work. fi ############# # FUNCTIONS:# ############# NewLink () { # Create new custom file cp $TARGET $ORIGINAL # Backup original target cp $TARGET $BACKUP.$COUNT # Create first backup for workfile mv $TARGET $WORKFILE # Create workfile echo $TARGET > $CUSTOM.$FILENAME.meta # Add metadata to workfile ln -s $WORKFILE $TARGET # Link workfile to target } MetaData () { if [ -f $CUSTOM.$FILENAME.meta ] ; then # If metadata exists, continue METADATA=`head -1 $CUSTOM.$FILENAME.meta` else # Else, abort transaction echo $METADATA # debug info echo "No link found, type vim $0 to fix this error" exit 4 fi } CountBackup () { # UGLY Rewrite these lines cBACKUP=`ls $BACKUP* | tail -1` # Doesn't actually count but CBACKUP=${cBACKUP##*.} # looks up last 'count' to } # continue from there LinkExists () { # The default action: COUNT=`expr $CBACKUP + 1` # Add a backup of workfile cp $WORKFILE $BACKUP.$COUNT } RestorePrevious () { # Restore previous workfile from mv `ls $BACKUP* | tail -1` $WORKFILE # backup } HelpText () { cat << EOF Give full path for new custom files $0 /path/file Edit file while creating backup -r /path/file Restore previous config file -h Help -l List custom config files EOF } DefaultWork () { if [ -f $CUSTOM$FILENAME ] ; then # Prior file created? MetaData # Look up full path if [ -h $METADATA ] ; then # If link exists in /path CountBackup # Count number of backups LinkExists # Create backup of workfile vim $WORKFILE # Edit workfile fi elif [ -f $TARGET ] ; then # Target is regular file? NewLink # Create new custom file vim $WORKFILE # Edit workfile else echo "No function for $1, try full path or -h for help" exit 3 fi } # This may benefit from a rewrite # dunno how yet :-s # With a case loop perhaps? if [ ! $1 ] ; then head -n 55 `which ctm` # Remove this in beta stage HelpText exit 0 elif [ $1 = '-h' ] ; then # Help function HelpText exit 0 elif [ $1 = '-l' ] ; then # List custom files ls $CUSTOM/original exit 0 elif [ $1 = '-r' ] ; then # Restore previous workfile RestorePrevious # More functions will be added here else DefaultWork fi
Can't tell an OS by it's GUI


Reply With Quote