Commits

Aarni Koskela committed aeaadb4

Initial

Comments (0)

Files changed (1)

+# -- encoding: utf-8 --
+
+import BlockParser, sys, os, glob, time
+from itertools import *
+
+
+class Group(object):
+	def __init__(self, block):
+		self.block = block
+		self.name = block.name
+		self.fileData = {}
+		self.workingDir = block.get("wd")
+		if not self.workingDir:
+			self.workingDir = os.getcwdu()
+		self.workingDir = os.path.realpath(self.workingDir)
+		
+		self.run = block["run"].children
+		self.masks = block["files"].children
+	
+	def check(self, dryrun = False):
+		changedFiles = set()
+		newFiles = set()
+		delFiles = set()
+		
+		lastFileNames = set(self.fileData)
+		files = set(chain(*map(glob.glob, self.masks)))
+		if files != lastFileNames:
+			newFiles = files - lastFileNames
+			delFiles = lastFileNames - files
+
+		for file in files:
+			stat = os.stat(file)
+			oldStat = self.fileData.get(file)
+			if oldStat and (stat.st_mtime > oldStat.st_mtime or stat.st_size != oldStat.st_size):
+				changedFiles.add(file)
+			self.fileData[file] = stat
+		
+		self.newFiles = newFiles
+		self.delFiles = delFiles
+		self.changedFiles = changedFiles
+		
+		if newFiles or delFiles or changedFiles:
+			print self.name
+			if newFiles:
+				print "  +: %s" % " ".join(sorted(newFiles))
+			if delFiles:
+				print "  -: %s" % " ".join(sorted(delFiles))
+			if changedFiles:
+				print "  *: %s" % " ".join(sorted(changedFiles))
+			
+			if dryrun:
+				print "     (Dry run)"
+			else:
+				self.trigger()
+			
+			if delFiles:
+				for f in delFiles:
+					self.fileData.pop(f, None)
+			
+	def trigger(self):
+		os.chdir(self.workingDir)
+		for run in self.run:
+			if "%+" in run:
+				for file in self.newFiles:
+					self.runcmd(run.replace("%+", file))
+			elif "%-" in run:
+				for file in self.delFiles:
+					self.runcmd(run.replace("%-", file))
+			elif "%*" in run:
+				for file in self.changedFiles:
+					self.runcmd(run.replace("%*", file))
+			else:
+				self.runcmd(run)
+				
+	
+	def runcmd(self, cmd):
+		print ">> %s" % cmd
+		os.system(cmd)
+
+def run_cmd(args):
+	options = {}
+	files = []
+	
+	for arg in args:
+		if arg.startswith("--"):
+			options[arg[2:]] = True
+		elif arg.startswith("-"):
+			options[arg[1:]] = True
+		else:
+			files.append(arg)
+	
+	groups = []
+	for fn in files:
+		bp = BlockParser.BlockParser(False, True)
+		groupDatas = bp.parse(file(fn).read())
+		groups.extend(map(Group, groupDatas))
+		
+	dryFirst = bool(options.get("dry-first"))
+	dryRun = bool(options.get("dry-run"))
+	
+	if dryFirst:
+		for group in groups:
+			group.check(dryrun = True)
+	
+	while 1:
+		for group in groups:
+			group.check(dryrun = dryRun)
+		time.sleep(1)
+
+if __name__ == "__main__":
+	run_cmd(sys.argv[1:])