Skip to content

Instantly share code, notes, and snippets.

@ericfrederich
Last active August 29, 2015 14:03
Show Gist options
  • Save ericfrederich/0754a575e3d8688a2aa0 to your computer and use it in GitHub Desktop.
Save ericfrederich/0754a575e3d8688a2aa0 to your computer and use it in GitHub Desktop.
#!/bin/sh
if "true" : '''\'
then
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $DIR/venv/bin/activate
exec python "$0" "$@"
exit 127
fi
'''
import sys
import os
from subprocess import Popen, PIPE
def report(prefix, fname, msg=''):
'''
Single report function so that whether error or okay, they line up
'''
print '%s %s%-40s%s %s' % (prefix, Style.BRIGHT, msg, Style.RESET_ALL, fname)
def single_prefix(fname):
'''
Returns whether there is a single prefix or not for a tar file
'''
prefixes = set()
p = Popen(['tar', '-tf', fname], stdout=PIPE)
for line in p.stdout:
line = line.rstrip()
if '/' in line:
prefixes.add(line[:line.index('/')])
else:
prefixes.add(line)
if len(prefixes) != 1:
p.terminate()
return False, 'multiple prefixes'
ret = p.wait()
if ret != 0:
raise Exception('tar returned %d' % ret)
return True, prefixes.pop()
if __name__ == '__main__':
from colorama import init, Fore, Back, Style
init()
ERROR='[%sEE%s]' % (Fore.RED + Style.BRIGHT, Style.RESET_ALL)
OKAY ='[%sOK%s]' % (Fore.BLUE + Style.BRIGHT, Style.RESET_ALL)
for fname in sys.argv[1:]:
try:
single, msg = single_prefix(fname)
if single:
report(OKAY, fname, msg)
else:
report(ERROR, fname, msg)
except Exception as e:
report(ERROR, fname, str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment