#!/usr/bin/python import base64 import json import os import random import sys import threading import time import uuid import zlib HZ_RANGE = range(0, 100000) FRAME_RANGE = range(0, 10000) CODE_COUNT_RANGE = range(1000, 1500) USE_THREADS = False def generate_pairs(): codes = [] times = [] code_count = random.choice(CODE_COUNT_RANGE) beg = 1 for code_index in range(0, code_count): frame = random.choice(range(beg, beg + 50)) times.extend([frame] * 6) codes.extend(random.sample(HZ_RANGE, 6)) beg = frame assert(len(codes) == len(times)) # these should match up! return codes, times def encode(codes, times): s = ' '.join('%d %d' % (c, t - 1) for c, t in zip(codes, times)) compressed = zlib.compress(s) encoded = base64.urlsafe_b64encode(compressed) return encoded.decode() def shuffle(codes, times): indices = [i for i in range(0, len(codes))] random.shuffle(indices) new_codes = [] new_times = [] for i in indices: new_codes.append(codes[i]) new_times.append(times[i]) return new_codes, new_times def chop(codes, times): cutoff = int(len(codes) / 2) return codes[:cutoff], times[:cutoff] def encode_and_wrap(codes, times, i, suffix): return { 'metadata': { 'track_id': str(uuid.uuid4()), 'artist': 'Chris P. Bacon And The Hot Pockets', 'title': 'Did It Work {}?'.format(i), 'duration': 600, 'version': float(i), 'release': 'Whatcha Gonna Pick? Volume {}'.format(i) }, 'codever': '4.12', 'code': encode(codes, times)} def generate_code_set(i): code_data = [] codes, times = generate_pairs() shuffled_codes, shuffled_times = shuffle(codes, times) chopped_codes, chopped_times = chop(codes, times) code_data.append(encode_and_wrap(codes, times, i, 'orig')) code_data.append(encode_and_wrap(shuffled_codes, shuffled_times, i, 'shuffled')) code_data.append(encode_and_wrap(chopped_codes, chopped_times, i, 'chopped')) return code_data def dump_codes(file_index, num_codes, output_dir): output_file_name = output_dir + '/codes_{0:03d}.json'.format(file_index) code_data = [] for i in range(num_codes): codes, times = generate_pairs() offset = file_index * num_codes code_data.append(encode_and_wrap(codes, times, offset + i, 'orig')) with open(output_file_name, 'w') as codes_out: json.dump(code_data, codes_out) class CodeThread(threading.Thread): def __init__(self, thread_id, num_codes, output_dir): threading.Thread.__init__(self) self.thread_id = thread_id self.num_codes = num_codes self.output_dir = output_dir def run(self): started_at = time.time() dump_codes(self.thread_id, self.num_codes, self.output_dir) finished_at = time.time() run_time = finished_at - started_at print('Thread {} finished in {} seconds'.format(self.thread_id, run_time)) if len(sys.argv) < 3: print('Usage: {} num_files num_codes_per_file'.format(sys.argv[0])) exit(-1) num_files = int(sys.argv[1]) num_codes_per_file = int(sys.argv[2]) output_dir = '/var/tmp/codegen/{}'.format(time.time()) os.mkdir(output_dir) print('Creating files in directory {}'.format(output_dir)) if USE_THREADS: threads = [] for thread_index in range(0, num_files): t = CodeThread(thread_index, num_codes_per_file, output_dir) t.setDaemon(True) threads.append(t) for t in threads: t.start() wait_count = 0 while threading.active_count() > 1: time.sleep(1) wait_count += 1 if wait_count % 20 == 0: print('Got {} active threads...'.format(threading.active_count())) else: for i in range(0, num_files): dump_codes(i, num_codes_per_file, output_dir)