#!/usr/bin/python import re, os , sys, shutil, time from daemon import Daemon from subprocess import Popen debug = True """ __ __ __ ______ | |--.---.-.--.--.-----.--------.-----.|__|.-----.| |_| |.----. | | _ | | |__ --| | -__|| ||__ --|| _| -- || _| |__|__|___._|_____|_____|__|__|__|_____||__||_____||____|______||__| Keeps your $home clean! Greetz to: jx - For helping me with some silly stuff <3 gunn0r - For the l33t name and because he is a l33ty <3 err000r - For showing me the IDE I am coding this on <3 Visit back2hack.cc && x5dij.back2hack.cc ! """ class KicsDaemon(Daemon): rules = {} vars = {} scripts = {} cmmt_regex = re.compile(r'^\s*#') empty_regex = re.compile(r'^\s*$') dir_regex = re.compile(r'^\s*\$\{\s*([A-Z]{3,})\s*,\s*(.?[a-z]{3,})\s*,\s*([a-zA-Z]{3,})\s*}\s*$') sub_dir_regex = re.compile(r'^\s*\$\{\s*([A-Z]{3,})\s*,\s*([A-Z]{3,})\s*,\s*([a-zA-Z]{3,})\s*}\s*$') rule_regex = re.compile(r'^\s*([A-Za-z0-9]{1,5})\s*=>\s*([A-Z]{3,})\s*$') script_regex = re.compile(r'^\s*S\{\s*([A-Z]{3,}),\s*(.*)\s*}\s*$') home = os.path.expanduser('~')+'/' desk = home+'Desktop/' def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): if debug: Daemon.__init__(self, pidfile, stdin, '/dev/stdout', '/dev/stderr') else: Daemon.__init__(self, pidfile, stdin, stdout, stderr) def run(self): self.parseFile() while(True): self.progressDir(self.home[:len(self.home)-1]) time.sleep(10) def progressDir(self, directory): for file in os.listdir(directory): path = directory+'/'+file if os.path.isdir(path) and file == "Desktop": self.progressDir(path) else: try: shutil.move(path,\ self.home+self.vars[self.rules[file.split('.')[-1]]]) except KeyError: for scriptname in self.scripts.keys(): print '/bin/sh '+self.scripts[scriptname].replace('[n]', '\n')+' '+path process = Popen('/bin/sh '+self.scripts[scriptname].replace('[n]', '\n')+' '+path) ret = process.wait() if(ret == 0): success = True name = scriptname break else: success = False if(success): try: shutil.move(path,\ self.home+self.vars[self.rules[scriptname]]) except KeyError: print('LOL, you defined script '+scriptname+', but you dont use it?') pass except shutil.Error: shutil.move(path,\ self.home+self.vars[self.rules[file.split('.')[-1]]]+'/'+\ self.getFreeFilename(self.home+self.vars[self.rules[file.split('.')[-1]]], file)) except shutil.Error: shutil.move(path,\ self.home+self.vars[self.rules[file.split('.')[-1]]]+'/'+\ self.getFreeFilename(self.home+self.vars[self.rules[file.split('.')[-1]]], file)) def parseFile(self): try: conf = open(self.home+'.kics', 'r') except IOError: try: shutil.copy('./example.kics', '~/.kics') except IOError: try: f = open(self.home+'.kics', 'w') f.close() except IOError: sys.stderr.write('WTF?! Cannot create configfile! Check the shit!\n') exit(3) for line in conf.readlines(): line = line[:len(line)-1] if self.cmmt_regex.search(line) or self.empty_regex.search(line): continue match = self.dir_regex.search(line) if match: path = self.home+match.group(2) lnpath = self.desk+match.group(3) if not os.path.isdir(path): if os.path.exists(path): os.remove(path) os.mkdir(path) if os.path.exists(lnpath): if os.path.isdir(lnpath): os.rmdir(lnpath) else: os.remove(lnpath) os.symlink(path, lnpath) self.vars[match.group(1)] = match.group(2) continue match = self.sub_dir_regex.search(line) if match: if not self.vars.has_key(match.group(1)): sys.stderr.write("The Variable "+match.group(1)+ "is not defined! Can't create subdir!") sys.exit(4) path = self.home+self.vars[match.group(1)]+match.group(3) if not os.path.isdir(path): if os.path.exists(path): os.remove(path) os.mkdir(path) match = self.rule_regex.search(line) if match: self.rules[match.group(1)] = match.group(2) continue match = self.script_regex.search(line) if match: self.scripts[match.group(1)] = match.group(2) continue self.parseError() def parseError(self): sys.stderr.write("""Error while parsing ~/.kics! Dir Syntax is: ${ VARNAME, self.homefolder, self.desktopfolder } Rule Syntax is: filesuffix => VARNAME Comment lines must begin with # Whitespaces can be placed anywhere and as often as you like! For valid example see example.kics! """) sys.exit(1) def getFreeFilename(self, path, filename): count = 1 while os.path.exists(path+filename+'_'+str(count)): count+=1 return filename+'_'+str(count) if __name__ == "__main__": daemon = KicsDaemon('/tmp/kics.pid', '/dev/null', '/tmp/kics_stdout', '/kics_stderr') if len(sys.argv) == 2: if 'start' == sys.argv[1]: daemon.start() elif 'stop' == sys.argv[1]: daemon.stop() elif 'restart' == sys.argv[1]: daemon.restart() else: print "Unknown command" sys.exit(2) sys.exit(0) else: print "usage: %s start|stop|restart" % sys.argv[0] sys.exit(2)