def run(self): logger.debug('Looking for files in %r' % self.root) for root, dirs, files in os.walk(self.root): for file in files: name, ext = os.path.splitext(file) if ext.lower() != '.jpg' or name.endswith('_wm'): continue path = os.path.join(root, file) self.queue.put(path) sys.stdout.write('+') sys.stdout.flush()
def main(): signature = os.path.join(os.path.expanduser('~'), '.signature.jpg') if not os.path.exists(signature): signature = os.path.join(os.path.dirname(__file__), 'signature.jpg') parser = argparse.ArgumentParser(description='Sign some pictures.') parser.add_argument('pic', help="Directory or single picture.", action='store') parser.add_argument('--signature', help=("Signature file. If not given, will look at " "~/.signature.jpg then fallback to the " "included signature."), default=signature) parser.add_argument('--debug', action='store_true', default=False, help="Debug mode") parser.add_argument('--phose', action='store_true', default=False, help="Use Powerhose") parsed = parser.parse_args() import logging if parsed.debug: level = logging.DEBUG else: level = logging.INFO logger.setLevel(level) ch = logging.StreamHandler() if parsed.debug: ch.setLevel(level) else: ch.setLevel(level) formatter = logging.Formatter('[%(asctime)s][%(name)s] %(message)s') ch.setFormatter(formatter) logger.addHandler(ch) phose_logger = logging.getLogger('powerhose') phose_logger.addHandler(ch) queue = Queue.Queue() if not os.path.exists(parsed.pic): print("%r does not seem to exist" % parsed.pic) sys.exit(1) logger.info('Using signature file %r' % parsed.signature) # looking for files if os.path.isdir(parsed.pic): logger.info('Looking for files in %r' % parsed.pic) finder = FileFinder(parsed.pic, queue) finder.start() time.sleep(.1) # give it a chance to start else: finder = None queue.put(parsed.pic) # run the cluster if parsed.phose and finder is not None: from powerhose import get_cluster from powerhose.client import Pool pool = Pool(timeout=30.) logger.debug('Starting the PowerHose cluster') cluster = get_cluster('signpic.sign.apply_signature', background=True) cluster.start() time.sleep(1.) else: if parsed.phose: logger.warning('Not using --phose for a single picture!') pool = None try: workers = [Worker(queue, pool, parsed.signature, parsed.phose) for i in range(10)] for worker in workers: worker.start() if finder is not None: finder.join() for worker in workers: worker.join() finally: if parsed.phose: cluster.stop() sys.stdout.write('Done.\n')