def runMe (): """ slowly removing red and green values for a small region """ signal.signal (signal.SIGHUP, signalhandler) signal.signal (signal.SIGINT, signalhandler) loadConfig () writelog ("[{0}]: Started. Continue set to {1}.".format (__name__, config['continue'])) width = 320 height = 240 framesize = width * height # check if we have to finish a previously worked region if config['continue']: region_width = config['region_width'] region_height = config['region_height'] start_pixel = config['start_pixel'] duration = config['duration'] else: region_height, region_width, duration, start_pixel = setValues () try: # this bot runs indefinitely (until signal is received) while True: # loop for duration frames for frame in range (0, duration): # loop over lines for line in range (start_pixel, start_pixel + width * region_height, width): compost.addEntropy (start_pixel) # loop over pixels in a line for index in range (0, region_width): pixel = duration + line + index color = compost.getPixelColor (pixel) # check if we have color. frames get removed so we could be # where there is nothing left ... if color: color[0] = color[0] / 10 color[1] = color[1] / 10 compost.setPixelColor (pixel, color) # set new values for next loop region_height, region_width, duration, start_pixel = setValues () except Exception as e: config['region_height'] = region_height config['region_width'] = region_width config['start_pixel'] = start_pixel config['duration'] = duration config['continue'] = True saveConfig () writelog ("[{0}]: Caught excption ({1}). Saving values for next run".format ( __name__, e)) return 0 return 0
def dropFrames (self): chunkCount = 0 dropCount = 0 for index in range (random.randint (0, 9), len (self._chunks), random.randint (12, 18)): self.mapChunk (index) dropCount += self._chunk.dropLastNFrames () chunkCount += 1 self.update () self.save () writelog ('[Compost]: dropped {0} frames from {1} chunks'.format (dropCount, chunkCount))
def dropFrames(self): chunkCount = 0 dropCount = 0 for index in range(random.randint(0, 9), len(self._chunks), random.randint(12, 18)): self.mapChunk(index) dropCount += self._chunk.dropLastNFrames() chunkCount += 1 self.update() self.save() writelog('[Compost]: dropped {0} frames from {1} chunks'.format( dropCount, chunkCount))
def runMe (): """ swapping random lines within a frame """ # each image is 320x240 pixels of 4 bytes width = 320 height = 240 csize = 4 wsize= width*csize size = width*height*csize random.seed() signal.signal (signal.SIGHUP, signalhandler) signal.signal (signal.SIGINT, signalhandler) writelog ("[{0}]: started".format (__name__)) for i in range(len(compost._chunks)/10): try: chunk1 = random.randint(0, len (compost._chunks) - 1) chunk2 = chunk1 while chunk2 == chunk1: chunk2 = random.randint(0, len (compost._chunks) - 1) #writelog ("[{0}]: swapping frames {1} <-> {2} (iteration {3})".format (__name__, chunk1, chunk2, i)) # save frame1 from chunk1 compost.mapChunk(chunk1) frameindex1 = random.randint(0, (len (compost._map) / size) - 1) * size frame1 = compost._map[frameindex1:frameindex1 + size] # save frame2 from chunk2 compost.mapChunk(chunk2) frameindex2 = random.randint(0, (len (compost._map) / size) - 1) * size frame2 = compost._map[frameindex2:frameindex2 + size] # write frame1 to chunk2 @ frameindex2 compost._map[frameindex2:frameindex2 + size] = frame1 # write frame2 to chunk1 @ frameindex1 compost.mapChunk (chunk1) compost._map[frameindex1:frameindex1 + size] = frame2 compost.addEntropy (frameindex1 + frameindex2) except Exception as e: writelog ("[{0}]: Caught exception ({1}). Exiting".format (__name__, e)) return 0 # print "{0} has done one cyle. resetting config".format (__name__) return 0
def runMe(): """ main method called by composter.py """ signal.signal(signal.SIGHUP, signalhandler) signal.signal(signal.SIGINT, signalhandler) frame_size = 76800 frame_width = 320 white = 255 min_length = 5 # shortest scratch max_length = 120 # longest scratch min_duration = 1 # lasts for 1 frame max_duration = 125 # lasts for 125 frames writelog("[{0}]: starting to make {1} scratches in video".format( __name__, len(compost._chunks))) # loop over the number of chunks available for chunk in range(0, len(compost._chunks)): try: random.seed() length = random.randint(min_length, max_length) duration = random.randint(min_duration, max_duration) startpixel = random.randint(0, (compost._pixels - length) - duration) for i in range(0, duration): startpixel += frame_size * i for pixel in range(startpixel, startpixel + length * frame_width, frame_width): compost.setPixelColor(pixel, [white, white, white]) compost.addEntropy(startpixel + (length * duration)) del length del duration del startpixel except Exception as e: writelog('[{0}]: caught exception ({1}). Exiting.'.format( __name__, e)) return 0 """ return a number != 0 to indicate an error to composter.py """ return 0
def runMe (): """ main method called by composter.py """ signal.signal (signal.SIGHUP, signalhandler) signal.signal (signal.SIGINT, signalhandler) try: loadConfig () writelog ("[{0}]: started".format (__name__)) saveConfig () """ Your code should go here. Some examples illustrate how things can work. Example loop over all pixels, saving the current pixels color: """ for pixel in range (0, compost._pixles): color = compost.getPixelColor (pixel) """ Example on how to use the memory mapping way of accessing data. We just save the three color channels of the first pixel from chunk number 4: """ compost.mapChunk (4) red = ord (compost._map[1]) green = ord (compost._map[2]) blue = ord (compost._map[3]) """ make use of the addEntropy method every once a while to help feed the machine's entropy pool. Like here we feed it the product of the three color channels from above """ compost.addEntropy (red * green * blue) except Exception as e: saveConfig () writelog ("[{0}]: Caught exception ({1}). Exiting.".format (__name__, e)) return 0 return 0
def runMe(): """ main method called by composter.py """ signal.signal(signal.SIGHUP, signalhandler) signal.signal(signal.SIGINT, signalhandler) try: loadConfig() writelog("[{0}]: started".format(__name__)) saveConfig() """ Your code should go here. Some examples illustrate how things can work. Example loop over all pixels, saving the current pixels color: """ for pixel in range(0, compost._pixles): color = compost.getPixelColor(pixel) """ Example on how to use the memory mapping way of accessing data. We just save the three color channels of the first pixel from chunk number 4: """ compost.mapChunk(4) red = ord(compost._map[1]) green = ord(compost._map[2]) blue = ord(compost._map[3]) """ make use of the addEntropy method every once a while to help feed the machine's entropy pool. Like here we feed it the product of the three color channels from above """ compost.addEntropy(red * green * blue) except Exception as e: saveConfig() writelog("[{0}]: Caught exception ({1}). Exiting.".format( __name__, e)) return 0 return 0
def runMe (): """ main method called by composter.py """ signal.signal (signal.SIGHUP, signalhandler) signal.signal (signal.SIGINT, signalhandler) frame_size = 76800 frame_width = 320 white = 255 min_length = 5 # shortest scratch max_length = 120 # longest scratch min_duration = 1 # lasts for 1 frame max_duration = 125 # lasts for 125 frames writelog ("[{0}]: starting to make {1} scratches in video".format ( __name__, len (compost._chunks))) # loop over the number of chunks available for chunk in range (0, len (compost._chunks)): try: random.seed () length = random.randint (min_length, max_length) duration = random.randint (min_duration, max_duration) startpixel = random.randint (0, (compost._pixels - length) - duration) for i in range (0, duration): startpixel += frame_size * i for pixel in range (startpixel, startpixel + length * frame_width, frame_width): compost.setPixelColor (pixel, [white, white, white]) compost.addEntropy (startpixel + (length * duration)) del length del duration del startpixel except Exception as e: writelog ('[{0}]: caught exception ({1}). Exiting.'.format (__name__, e)) return 0 """ return a number != 0 to indicate an error to composter.py """ return 0
def dropLastNFrames (self): """ Drops a random number of frames from Chunk if there are more than min_size frames left in Chunk. Returns the number of dropped frames. """ min_size = 5 dropNumFrames = 0 if self._frames > min_size: intervalMin = 1 intervalMax = self._frames / 10 + 1 random.seed () dropNumFrames = random.randint (intervalMin, intervalMax) self._map.resize ((self._frames - dropNumFrames) * 320 * 240 * 4) self.updateChunk () else: writelog ('[Chunk]: chunk {0} reached minimum size {1}.'.format (self._index, min_size)) self.closeChunk () return dropNumFrames
def runMe (): """ swapping random lines within a frame """ # each image is 320x240 pixels of 4 bytes width = 320 height = 240 csize = 4 wsize= width*csize size = width*height*csize random.seed() loadConfig () signal.signal (signal.SIGHUP, signalhandler) signal.signal (signal.SIGINT, signalhandler) writelog ("[{0}]: started".format (__name__)) try: for chunk in range (config["chunk"], len (compost._chunks)): # writelog ("{0} working on chunk {1}".format (__name__, chunk)) compost.mapChunk (chunk) for frame in range (0, len (compost._map) / size): randlength=random.randint(0, wsize/4) line1index = frame*size+(random.randint (0, size-randlength)/4)*4 line2index = frame*size+(random.randint (0, size-randlength)/4)*4 line = compost._map[line1index:(line1index+randlength)] compost._map[line1index:(line1index+randlength)] = compost._map[line2index:(line2index+randlength)] compost._map[line2index:(line2index+randlength)] = line compost.addEntropy (randlength) config["chunk"] = chunk saveConfig () except Exception as e: writelog ("[{0}]: Caught exception ({1}). Exiting".format (__name__, e)) config["chunk"] = chunk saveConfig () return 0 # print "{0} has done one cyle. resetting config".format (__name__) config["chunk"] = 0 saveConfig () return 0
def dropLastNFrames(self): """ Drops a random number of frames from Chunk if there are more than min_size frames left in Chunk. Returns the number of dropped frames. """ min_size = 5 dropNumFrames = 0 if self._frames > min_size: intervalMin = 1 intervalMax = self._frames / 10 + 1 random.seed() dropNumFrames = random.randint(intervalMin, intervalMax) self._map.resize((self._frames - dropNumFrames) * 320 * 240 * 4) self.updateChunk() else: writelog('[Chunk]: chunk {0} reached minimum size {1}.'.format( self._index, min_size)) self.closeChunk() return dropNumFrames
def runMe(): loadConfig() signal.signal(signal.SIGHUP, signalhandler) signal.signal(signal.SIGINT, signalhandler) writelog("[{0}]: starting to work on chunk {1} at byte {2}".format( __name__, config['chunk'], config['byte'])) forwardchunk = False if config['chunk'] > 0: forwardchunk = True forwardbyte = False if config['byte'] > 2: forwardbyte = True try: for chunk in range(0, len(compost._chunks)): # forward to config['chunk'] if neccessary if forwardchunk: if chunk < config['chunk']: continue else: writelog('[{0}]: forwarded to chunk {1}.'.format( __name__, chunk)) forwardchunk = False compost.mapChunk(chunk) # forward to config['byte'] if neccessary for byte in range(2, len(compost._map), 4): if forwardbyte: if byte < config['byte']: continue else: writelog('[{0}]: forwarded to byte {1}.'.format( __name__, byte)) forwardbyte = False green = ord(compost._map[byte]) blue = ord(compost._map[byte + 1]) if green > 0: compost._map[byte] = chr(green - 1) compost.addEntropy(green - 1) if blue > 0: compost._map[byte + 1] = chr(blue - 1) compost.addEntropy(blue - 1) # for the next run config['chunk'] = 0 config['byte'] = 2 saveConfig() # catch any exception and return except Exception as e: config['chunk'] = chunk config['byte'] = byte saveConfig() writelog( "[{0}]: caught exception ({1}). Saving chunk {2} at byte {3} for next run." .format(__name__, e, chunk, byte)) return 0 config["chunk"] = 0 config["byte"] = 2 saveConfig() return 0
def runMe(): """ main method called by composter.py """ signal.signal(signal.SIGHUP, signalhandler) signal.signal(signal.SIGINT, signalhandler) try: try: loadConfig() except: pass width = 320 height = 240 bytes_per_pixel = 4 frame_size = width * height min_square_width = 2 max_square_width = 16 writelog("[{0}]: started at chunk {1}".format(__name__, config["chunk"])) for chunk in range(0, len(compost._chunks)): if chunk < config["chunk"]: chunk = config["chunk"] compost.mapChunk(chunk) map_length = len(compost._map) num_frames = map_length / (frame_size * bytes_per_pixel) random.seed() # determine how many frames will be affected if num_frames <= 5: affected_frames = num_frames else: if num_frames <= 50: max_affected_frames = num_frames else: max_affected_frames = 50 affected_frames = random.randint(5, max_affected_frames) # width of a square square_width = random.randint(min_square_width, max_square_width) # pixel where square starts frame_pixel = random.randint(0, ((frame_size - width * square_width)) - square_width) # first affected frame start_frame = random.randint(0, num_frames - affected_frames) # first byte of square start_byte = (frame_pixel + frame_size * start_frame) * bytes_per_pixel # return some bits for RNG compost.addEntropy(start_byte) # repeat this line square_width * affected_frames times for f in range(0, affected_frames): frameoffset = f * frame_size * bytes_per_pixel # save this pixels color pixel_color = compost._map[start_byte + frameoffset : start_byte + frameoffset + bytes_per_pixel] # repeat pixel_color for the first square line for w in range(0, square_width): start = start_byte + frameoffset + w * bytes_per_pixel compost._map[start : start + bytes_per_pixel] = pixel_color # save this line square_line = compost._map[ start_byte + frameoffset : start_byte + frameoffset + square_width * bytes_per_pixel ] for h in range(0, square_width): start = start_byte + frameoffset + h * width * bytes_per_pixel try: compost._map[start : start + square_width * bytes_per_pixel] = square_line except IndexError: writelog( "Exception: square_width={0}, affected_frames={1}, start_byte={2}, start_frame={3}, map_length={4}, frameoffset={5}, lineoffset={6}".format( square_width, affected_frames, start_byte, start_frame, map_length, frameoffset, lineoffset, ) ) break config["chunk"] = 0 saveConfig() except Exception as e: config["chunk"] = chunk saveConfig() writelog("[{0}]: Caught exception ({1}). saving chunk={2} for next run".format(__name__, e, chunk)) return 0 """ return a number != 0 to indicate an error to composter.py """ return 0
def runMe (): """ main method called by composter.py """ signal.signal (signal.SIGHUP, signalhandler) signal.signal (signal.SIGINT, signalhandler) width = 320 height = 240 frame_size = width * height frame_number = 0 line_number = 0 try: loadConfig () forwardframe = False if config['forward2frame'] > 0: forwardframe = True forwardline = False if config['forward2line'] > 0: forwardline = True forwardpixel = False if config['forward2pixel'] > 0: forwardpixel = True writelog ("[{0}]: started".format (__name__)) for line in range (line_number, height): if forwardline: if line < config['forward2line']: # print 'forwarding line' continue else: # writelog ('[{0}]: forwarded to line {1}'.format (__name__, config['forward2line'])) forwardline = False for frame in range (frame_number, compost._frames): if forwardframe: if frame < config['forward2frame']: # print 'forwarding frame' continue else: writelog ('[{0}]: forwarded to frame {1}'.format (__name__, config['forward2frame'])) forwardframe = False start_pixel = frame_size * frame + line * width for pixel in range (start_pixel, start_pixel + width): if forwardpixel: if pixel < config['forward2pixel']: # print 'forwarding pixel' continue else: # writelog ('[{0}]: forwarded to pixel {1}'.format (__name__, config['forward2pixel'])) forwardpixel = False color = compost.getPixelColor (pixel) bw = int (color[0] * 0.3 + color[1] * 0.59 + color[2] * 0.11) compost.setPixelColor (pixel, [bw, bw, bw]) # print 'pixel {0} set to {1},{1},{1}'.format (pixel, bw) # give the last bw to the prng entropy pool compost.addEntropy (bw) writelog ('[{0}]: finished line {1}.'.format (__name__, line)) # remeber that we are done config['forward2frame'] = 0 config['forward2line'] = 0 config['forward2pixel'] = 0 saveConfig () except Exception as e: config['forward2frame'] = frame config['forward2line'] = line config['forward2pixel'] = pixel saveConfig () writelog ("[{0}]: Caught exception ({1}). Exiting.".format (__name__, e)) return 0 return 0
def mainLoop (): """ """ cycletime = 21600 # 6 hours, 4 cycles a day # cycletime = 3600 # cycletime = 30 signal.signal (signal.SIGTERM, interrupthandler) signal.signal (signal.SIGINT, interrupthandler) signal.signal (signal.SIGHUP, interrupthandler) savedbotfile = os.path.join (rundir, 'savedbot.txt') if os.path.isfile (pidfilename): writelog ("[composter]: stale (?) pidfile found. Exiting.") print "[composter]: stale (?) pidfile found. Exiting." return 1; pidfile = open (pidfilename, "w") pidfile.write ("{0}".format (os.getpid ())) pidfile.close () writelog ("[composter]: started with pid {0}".format (os.getpid ())) while True: botlist = BotList () numbots = len (botlist.bots) writelog ("[composter]: started cycle with {0} seconds per bot".format (cycletime / numbots)) for b in botlist.getList (): if os.path.isfile (savedbotfile): f = open (savedbotfile, 'r') s = f.readline () f.close () if b == s: writelog ("[composter]: continuing with saved bot {0}".format (b)) os.remove (savedbotfile) else: continue try: bot = __import__ (b) reload (bot) pid = os.fork () if pid == 0: return bot.runMe () else: signal.signal (signal.SIGALRM, alarmhandler) signal.alarm (cycletime / numbots) try: writelog ("[composter]: waiting for {0}[.py] with pid {1}".format (b, pid)) rv = os.waitpid (pid, 0) writelog ("[composter]: {0}[.py] with pid {1} returned {2}".format (b, pid, rv)) except VCTimeout: os.kill (pid, signal.SIGHUP) rv = os.waitpid (pid, 0) writelog ("[composter]: {0} with pid {1} returned {2} on SIGHUP after timeout".format (b, pid, rv)) except ImportError: writelog ("[composter]: Error importing {0}[.py]".format (b)) except SyntaxError: writelog ("[composter]: Bot {0} has Syntax Error".format (b)) except VCInterrupt: os.kill (pid, signal.SIGHUP) rv = os.waitpid (pid, 0) # save the running bot f = open (savedbotfile, 'w') f.write (b) f.close () writelog ("[composter]: Exiting. Saved bot '{0}' for next run.".format (b)) os.remove (pidfilename) return 0 compost = Compost () compost.dropFrames () writelog ('[composter]: {0}'.format (compost.stats ())) del compost writelog ("[composter]: Loop terminated abnormally.") os.remove (pidfilename) return 1
and cuts it into chunks of a defined size, adding the chunks to compost and storing them in /basedir/compost/. """ import hashlib import time import sys import os from Compost import Compost from vcconfig import * from VCLogger import writelog if os.path.isfile (infilename): infile = open (infilename, "r") else: writelog ("[cutvideo]: {0} not found".format (infilename)) print "[cutvideo]: {0} not found".format (infilename) sys.exit (1) # pixels_per_frame * bytes_per_pixel * frames # will be changed to aprox. 2GB chunksize = 320 * 240 * 4 * 68 chunk = True chunknum = 0 compost = Compost () while True: infile.seek (chunksize * chunknum) chunk = infile.read (chunksize) if not chunk:
def runMe(): """ main method called by composter.py """ signal.signal(signal.SIGHUP, signalhandler) signal.signal(signal.SIGINT, signalhandler) try: try: loadConfig() except: pass width = 320 height = 240 bytes_per_pixel = 4 frame_size = width * height min_square_width = 2 max_square_width = 16 writelog("[{0}]: started at chunk {1}".format(__name__, config["chunk"])) for chunk in range(0, len(compost._chunks)): if chunk < config["chunk"]: chunk = config["chunk"] compost.mapChunk(chunk) map_length = len(compost._map) num_frames = map_length / (frame_size * bytes_per_pixel) random.seed() # determine how many frames will be affected if num_frames <= 5: affected_frames = num_frames else: if num_frames <= 30: max_affected_frames = num_frames else: max_affected_frames = 30 affected_frames = random.randint(5, max_affected_frames) # width of a square square_width = random.randint(min_square_width, max_square_width) # pixel where square starts frame_pixel = random.randint( 0, ((frame_size - width * square_width)) - square_width) # first affected frame start_frame = random.randint(0, num_frames - affected_frames) # first byte of square start_byte = (frame_pixel + frame_size * start_frame) * bytes_per_pixel # save this pixels color pixel_color = compost._map[start_byte:start_byte + bytes_per_pixel] # return some bits for RNG compost.addEntropy(start_byte) # repeat pixel_color for the first square line for w in range(0, square_width): start = start_byte + w * bytes_per_pixel compost._map[start:start + bytes_per_pixel] = pixel_color # save this line square_line = compost._map[start_byte:start_byte + square_width * bytes_per_pixel] # repeat this line square_width * affected_frames times for f in range(0, affected_frames): frameoffset = f * frame_size * bytes_per_pixel for h in range(0, square_width): start = start_byte + frameoffset + h * width * bytes_per_pixel try: compost._map[start:start + square_width * bytes_per_pixel] = square_line except IndexError: writelog( 'Exception: square_width={0}, affected_frames={1}, start_byte={2}, start_frame={3}, map_length={4}, frameoffset={5}, lineoffset={6}' .format(square_width, affected_frames, start_byte, start_frame, map_length, frameoffset, lineoffset)) break config["chunk"] = 0 saveConfig() except Exception as e: config["chunk"] = chunk saveConfig() writelog( '[{0}]: Caught exception ({1}). saving chunk={2} for next run'. format(__name__, e, chunk)) return 0 """ return a number != 0 to indicate an error to composter.py """ return 0
def runMe (): loadConfig () signal.signal (signal.SIGHUP, signalhandler) signal.signal (signal.SIGINT, signalhandler) writelog ("[{0}]: starting to work on chunk {1} at byte {2}".format ( __name__, config['chunk'], config['byte'])) forwardchunk = False if config['chunk'] > 0: forwardchunk = True forwardbyte = False if config['byte'] > 2: forwardbyte = True try: for chunk in range (0, len (compost._chunks)): # forward to config['chunk'] if neccessary if forwardchunk: if chunk < config['chunk']: continue else: writelog ('[{0}]: forwarded to chunk {1}.'.format (__name__, chunk)) forwardchunk = False compost.mapChunk (chunk) # forward to config['byte'] if neccessary for byte in range (2, len (compost._map), 4): if forwardbyte: if byte < config['byte']: continue else: writelog ('[{0}]: forwarded to byte {1}.'.format (__name__, byte)) forwardbyte = False green = ord (compost._map[byte]) blue = ord (compost._map[byte+1]) if green > 0: compost._map[byte] = chr (green - 1) compost.addEntropy (green - 1) if blue > 0: compost._map[byte+1] = chr (blue - 1) compost.addEntropy (blue - 1) # for the next run config['chunk'] = 0 config['byte'] = 2 saveConfig () # catch any exception and return except Exception as e: config['chunk'] = chunk config['byte'] = byte saveConfig () writelog ("[{0}]: caught exception ({1}). Saving chunk {2} at byte {3} for next run.".format ( __name__, e, chunk, byte)) return 0 config["chunk"] = 0 config["byte"] = 2 saveConfig () return 0