Browse Source

printem() function to always flushem stdout lol

Wazakindjes 1 year ago
parent
commit
eb81cdd72b
1 changed files with 37 additions and 34 deletions
  1. 37 34
      hurr.py

+ 37 - 34
hurr.py

@@ -46,6 +46,10 @@ MUHCONF = {
 	'fuckexif': None,
 }
 
+def printem(msg):
+	print msg
+	sys.stdout.flush()
+
 def getscrots():
 	global MUHCONF, OLDFILES, SKIPPEM
 	p = re.compile(MUHCONF['scrotpattern'], re.I)
@@ -155,7 +159,7 @@ def upload(local, remote):
 					try:
 						if exif_raw['GPS'] != {}:
 							exif_raw.pop('GPS')
-							print "Stripped GPS EXIF"
+							printem("Stripped GPS EXIF")
 					except:
 						pass
 
@@ -163,7 +167,7 @@ def upload(local, remote):
 					try:
 						orientation = exif_raw['0th'][EXIF_ORIENT_TAG]
 						if orientation in [6, 8, 3, 2, 5, 7, 4]:
-							print "Found orientation %d instead of 1, rotating/flipping that shit" % (orientation)
+							printem("Found orientation %d instead of 1, rotating/flipping that shit" % (orientation))
 							growlnotif(3, desc="Rotating image from EXIF orientation tag lol")
 							exif_raw['0th'][EXIF_ORIENT_TAG] = 1
 
@@ -184,12 +188,12 @@ def upload(local, remote):
 
 							if exif_raw['thumbnail'] != {} and not strippem_all:
 								exif_raw.pop('thumbnail')
-								print "Stripped thumbnail IFD to prevent previews etc from fucking up"
+								printem("Stripped thumbnail IFD to prevent previews etc from fucking up")
 					except:
 						pass
 
 					if strippem_all:
-						print "Stripping all EXIF data lol"
+						printem("Stripping all EXIF data lol")
 						exif_raw = {}
 					image_exif.save(local, exif=piexif.dump(exif_raw))
 					image_exif.close()
@@ -222,7 +226,7 @@ def archiver():
 			date = datetime.datetime.now()
 			folder = "%d-%02d-%02d--%02d:%02d:%02d" % (date.year, date.month, date.day, date.hour, date.minute, date.second)
 
-			print "Ayyy we archiving fam: %s/%s" % (MUHCONF['archivebase'], folder)
+			printem("Ayyy we archiving fam: %s/%s" % (MUHCONF['archivebase'], folder))
 
 			SSH.exec_command('mkdir -p %s/%s' % (MUHCONF['archivebase'], folder))
 			SSH.exec_command('find %s -type f -mmin +%d -exec mv "{}" "%s/%s" \;' % (MUHCONF['remotebase'], MUHCONF['archivetime'], MUHCONF['archivebase'], folder));
@@ -243,7 +247,7 @@ def main(path):
 		if upload(path, remotefile) == 0:
 			growlnotif(1)
 		if path.startswith("/tmp/"):
-			print "Cleaning up tmpfile: %s" % path
+			printem("Cleaning up tmpfile: %s" % path)
 			os.remove(path);
 		sys.exit(0)
 
@@ -278,7 +282,7 @@ def runUpload(newfiles, basepath):
 		for file in newfiles:
 			path = '%s/%s' % (basepath, file)
 			remotefile = ''.join([random.choice(string.ascii_letters + string.digits + '-_') for c in range(8)]) + (os.path.splitext(file)[1])
-			print "Uploading %s to %s" % (path, remotefile)
+			printem("Uploading %s to %s" % (path, remotefile))
 			if upload(path, remotefile) == 0:
 				growlnotif(1)
 
@@ -293,11 +297,11 @@ if __name__ == "__main__":
 	MUHCONF['icondir'] = "%s/icinz" % muhdir
 	Config = ConfigParser.ConfigParser()
 	gottem = Config.read(muhdir + "/hurr.conf")
-	print "Starting up HurrDroppert v%s lol" % VERSION
-	print "Parsing config: %s/hurr.conf\r\n" % muhdir
+	printem("Starting up HurrDroppert v%s lol" % VERSION)
+	printem("Parsing config: %s/hurr.conf\r\n" % muhdir)
 
 	if gottem == []:
-		print "Unable to read %s/hurr.conf" % muhdir
+		printem("Unable to read %s/hurr.conf" % muhdir)
 		sys.exit(1)
 
 	for csect in Config.sections():
@@ -316,7 +320,7 @@ if __name__ == "__main__":
 							cval = MUHCONF[cval]
 
 						if cval[0] == '~':
-							print "[%s] Expanding special path characters in %s" % (copt, cval)
+							printem("[%s] Expanding special path characters in %s" % (copt, cval))
 							cval = os.path.expanduser(cval)
 
 						if copt.find("scrotpattern") != -1:
@@ -324,45 +328,45 @@ if __name__ == "__main__":
 								p = re.compile(cval, re.I)
 							except re.error as e:
 								errd = True
-								print "Invalid regular expression for %s: %s" % (copt, cval)
-								print "Regex error: [%s]" % e.message
+								printem("Invalid regular expression for %s: %s" % (copt, cval))
+								printem("Regex error: [%s]" % e.message)
 
 					if copt in MUHCONF.keys() and not cval in [None, -1, '']:
 						MUHCONF[copt] = cval
 			except:
 				if errd == False:
-					print "Invalid value '%s' for %s" % (cval, copt)
+					printem("Invalid value '%s' for %s" % (cval, copt))
 
 			if errd == True:
 				sys.exit(1)
 
 	if None in MUHCONF.values():
-		print "Incomplete config file lol"
+		printem("Incomplete config file lol")
 		sys.exit(1)
 
 	for copt in MUHCONF.keys():
 		cval = MUHCONF[copt]
-		print "** %s = %s" % (copt, cval)
+		printem("** %s = %s" % (copt, cval))
 
 	for oi in ExifTags.TAGS.keys():
 		if ExifTags.TAGS[oi] == 'Orientation':
 			EXIF_ORIENT_TAG = oi
 			break
 
-	print "\r\nGetting current directory contents"
+	printem("\r\nGetting current directory contents")
 	OLDFILES = getscrots()
 	OLDFILES_ALT = getscrots_alt()
 	OLDFILES_DB = getscrots_db()
 
-	print "Setting up SSH using key pair: %s/%s{,.pub}" % (MUHCONF['sshdir'], MUHCONF['sshkey'])
+	printem("Setting up SSH using key pair: %s/%s{,.pub}" % (MUHCONF['sshdir'], MUHCONF['sshkey']))
 	SSH = paramiko.SSHClient()
 	SSH.known_hosts = None
 
-	print "Loading known_hosts from %s/known_hosts" % MUHCONF['sshdir']
+	printem("Loading known_hosts from %s/known_hosts" % MUHCONF['sshdir'])
 	SSH.load_host_keys(MUHCONF['sshdir'] + "/known_hosts")
 	SSH.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
-	print "Setting up Growl"
+	printem("Setting up Growl")
 	GROWL = gntp.notifier.GrowlNotifier(
 		applicationName = MUHCONF['growlname'],
 		notifications = ['Uploading', 'Complete', 'Error', 'Working'],
@@ -370,12 +374,12 @@ if __name__ == "__main__":
 	)
 	GROWL.register()
 
-	print "Checking for daemon mode"
+	printem("Checking for daemon mode")
 	if len(sys.argv) > 1:
-		print "Seems to be a quick file upload, checking existence of %s" % sys.argv[1]
+		printem("Seems to be a quick file upload, checking existence of %s" % sys.argv[1])
 		temppath = ' '.join(sys.argv[1:])
 		if temppath.startswith("https://") or temppath.startswith("http://"):
-			print "Seems to be a URL too, checkin em sanity lol"
+			printem("Seems to be a URL too, checkin em sanity lol")
 			try:
 				checkem = urllib2.urlopen(temppath)
 				muhext = re.sub(r'([?&].*)+', '', temppath)
@@ -387,23 +391,23 @@ if __name__ == "__main__":
 				with open(temppath, "wb") as tmpf:
 					tmpf.write(checkem.read())
 			except Exception as muherr:
-				print "Unable to download from URL fam: %s" % (str(muherr))
+				printem("Unable to download from URL fam: %s" % (str(muherr)))
 				sys.exit(1)
 
 		elif os.path.isfile(temppath) == False:
-			print "File doesn't exist lol"
+			printem("File doesn't exist lol")
 			sys.exit(1)
 
-		print "Calling main function"
+		printem("Calling main function")
 		main(temppath)
 	else:
-		print "Setting up threads"
+		printem("Setting up threads")
 		run_event = threading.Event()
 		run_event.set()
 		threads = []
 		args = []
 
-		print "Getting current image count"
+		printem("Getting current image count")
 		SSH.connect(MUHCONF['server'], username=MUHCONF['joeser'], key_filename="%s/%s" % (MUHCONF['sshdir'], MUHCONF['sshkey']));
 		checkamount = 'ls -1 %s | wc -l' % MUHCONF['remotebase']
 		stdin, stdout, stderr = SSH.exec_command(checkamount)
@@ -414,9 +418,9 @@ if __name__ == "__main__":
 			tarchiver = threading.Thread(target=archiver)
 			tarchiver.daemon = True
 			tarchiver.start()
-			print "Archiver thread created (folder: %s)" % MUHCONF['archivebase']
+			printem("Archiver thread created (folder: %s)" % MUHCONF['archivebase'])
 		else:
-			print "Archiving disabled"
+			printem("Archiving disabled")
 
 		args.append(None)
 		tmain = threading.Thread(target=main, args=args)
@@ -424,15 +428,14 @@ if __name__ == "__main__":
 		tmain.start()
 		threads.append(tmain)
 
-		print "Started up, calling main function"
+		printem("Started up, calling main function")
 
 		try:
 			while 1:
 				time.sleep(.1)
 		except KeyboardInterrupt:
-			print "Attempting to close threads"
+			printem("Attempting to close threads")
 			run_event.clear()
 			for thread in threads:
 				thread.join(5)
-			print "Threads successfully closed"
-
+			printem("Threads successfully closed")