I tried to write a small wrapper script for the unix find-command. Something I messed up with the parameter passing. Could you give me a hint regarding my mistake?
The error message is
Traceback (most recent call last):
File "frep.py", line 43, in <module>
inst.prep_shell_commands(self.extension, self.search_string, self.rel_numbers)
NameError: name 'self' is not defined
This is the code:
import os
import subprocess
import string
import sys
class Frep(object):
extension = ""
search_string =""
commands = []
rel_numbers = ""
def get_params(self):
Frep.no_of_params = len(sys.argv)
if Frep.no_of_params == 4:
Frep.extension = str(sys.argv[1])
Frep.search_string = str(sys.argv[2])
Frep.rel_numbers = str(sys.argv[3])
else:
print "Usage: frep [FILE_EXTENSION] [SEARCH_STRING] [RELEASE_NUMBERS]"
print "Example: frep sql my_search_string [5-6]"
print " "
sys.exit()
def prep_shell_commands(self, ext, ss, reln):
print ext
tmp_folderlist = string.split(subprocess.check_output("find /data/grep_dir -maxdepth 1 -type d -name '["+reln+"]*'", shell=True), '\n')
#tmp_folderlist = string.split(subprocess.check_output("find /data/grep_dir -maxdepth 1 -type d -name '[6-7]*'", shell=True), '\n')
for d in tmp_folderlist:
commands.append("find " + d + " -type f -name '*" + ext +"' -exec grep -il '" + ss +"' {} \;")
print commands
def exec_commands(self, c_list):
for c in c_list:
os.system(c)
inst = Frep()
inst.prep_shell_commands(self.extension, self.search_string, self.rel_numbers)
exec_commands(self.commands)
Here's a re-write of your code. This version's not perfect, but it shows how to define a class & how to use a class instance in the way that it's usually done in Python.
FWIW, it's probably better to validate input data before passing it into your class, but I guess it's ok here.
I haven't tested this code, since I don't have the required directories and files, but hopefully it doesn't contain any horrible errors. :)
#!/usr/bin/env python
import os
import subprocess
import sys
class Frep(object):
def __init__(self, args):
if len(args) != 3:
self.usage()
self.extension = args[0]
self.search_string = args[1]
self.rel_numbers = args[2]
self.commands = []
def usage(self):
print "Usage: frep FILE_EXTENSION SEARCH_STRING RELEASE_NUMBERS"
print "Example: frep sql my_search_string [5-6]"
print " "
sys.exit()
def prep_shell_commands(self):
print self.extension
cmd = "find /data/grep_dir -maxdepth 1 -type d -name '[" + self.rel_numbers + "]*'"
tmp_folderlist = subprocess.check_output(cmd, shell=True).split('\n')
for d in tmp_folderlist:
cmd = "find " + d + " -type f -name '*" + self.extension + "' -exec grep -il '" + self.search_string + "' {} \;"
self.commands.append(cmd)
print self.commands
def exec_commands(self):
for cmd in self.commands:
os.system(cmd)
def main():
inst = Frep(sys.argv[1:])
inst.prep_shell_commands()
inst.exec_commands()
if __name__ == '__main__':
main()