Skip to content

Instantly share code, notes, and snippets.

@aquinzi
Last active December 20, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aquinzi/6090980 to your computer and use it in GitHub Desktop.
Save aquinzi/6090980 to your computer and use it in GitHub Desktop.
Displays lines (with number) in files that have TODO or FIXME
# Displays lines in files (grepy) that have TODO or FIXME
# script -> where script runs. script del file num -> del line
import re, os, sys, argparse
import shutil
filesIgnore = ('.bak')
try:
from colorama import Fore, init, Style
init()
color = True
except Exception:
color = False
def exit():
sys.exit(0)
def getFilename(file):
path, ext = os.path.splitext(file)
return path, ext
# Quick'n'Dirty way of having something like: (program) del 5 sdfsdf.txt
# the source optional, and defaults to ./
def args():
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=''' Default behaviour is to list files where script runs
Optional options can be:
To specify the path to list files:
%(prog)s -source/-s [path]
Ex. %(prog)s -source/-s C:\projects\hello_world
To delete a line:
%(prog)s del/d [file] [line number]
Ex. %(prog)s del hello.py 5
''', usage="%(prog)s")
parser.add_argument("-source", default="./", help=argparse.SUPPRESS)
parser.add_argument("del", default="", nargs="*", help=argparse.SUPPRESS)
return vars(parser.parse_args()) # to dict
def printEndMsg(message):
print message
print ("\n------------------------------")
exit()
def listFiles(source):
for root,subdirs,files in os.walk(source):
for filename in files:
if not filename.endswith(filesIgnore):
file = os.path.join(root, filename)
newFile = True
with open(file,'r') as f:
for number, line in enumerate(f):
#re.I o re.IGNORECASE
match = re.search(r"\bTODO\b|\bFIXME\b", line, re.I)
if match is not None:
tmpNum = number+1
newLine = line.replace('#', '')
relPath = os.path.relpath(file)
if newFile:
newFile = False
if color:
print Style.NORMAL+Fore.CYAN + relPath+'\n\t '+Style.BRIGHT +Fore.RED+':',tmpNum,' '+ Fore.GREEN+newLine,
else:
print '%s\n\t : %d %s' % (relPath, tmpNum, newLine), # ending , removes blank line
else:
if color:
print '\t '+Style.BRIGHT +Fore.RED+':',tmpNum,' '+ Fore.GREEN+newLine,
else:
print '\t : ',tmpNum,' '+ newLine,
theArgs = args()
source = theArgs['source']
toDel = False
print ("------------------------------\n")
if theArgs['del'] == "":
toDel = False
else:
if len(theArgs['del']) == 3:
if theArgs['del'][0] in ("del", "d"):
if theArgs['del'][2].isdigit():
if os.path.isfile(theArgs['del'][1]):
toDel = [theArgs['del'][1], theArgs['del'][2]]
else:
printEndMsg("File does not exist or is a directory")
else:
printEndMsg("second param. must be number")
else:
printEndMsg("unknown command")
else:
printEndMsg("incomplete del command")
if toDel:
# preparing to delete
lineDelete = int(toDel[1])-1
with open(toDel[0]) as input:
# Make backup
path, ext = getFilename(toDel[0])
newFile = path+ext+".bak"
shutil.copy2(toDel[0],newFile)
text = input.readlines()
del text[lineDelete]
with open(toDel[0], "w") as output:
output.writelines(text)
listFiles(source)
else:
# just list
listFiles(source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment