def create_target_dir_structure(target_dir, verbose=False): song_dir = os.path.join(target_dir, u"In The Groove 2", u"Songs") if not os.path.exists(song_dir): os.makedirs(os.path.join(target_dir, u"In The Groove 2", u"Songs")) if verbose: logger.info(u"Created directory: {0}".format(song_dir)) elif not os.path.isdir(song_dir): raise Exception("Target path is not a directory", song_dir) else: if verbose: logger.info(u"Directory already exists: {0}".format(song_dir))
def patch_length(target_dir, verbose=False): song_dir = os.path.join(target_dir, u"In The Groove 2", u"Songs") all_files = [os.path.join(song_dir, f) for f in os.listdir(song_dir)] dirs = [d for d in all_files if os.path.isdir(d)] for song_dir in dirs: song_files = (os.path.join(song_dir, f) for f in os.listdir(song_dir)) ogg_files = (f for f in song_files if f.endswith(".ogg")) for ogg_file in ogg_files: if verbose: logger.info(u"Patching file: {0}".format(ogg_file)) oggpatch.patch_file(ogg_file, verbose=verbose)
def patch_length(self, new_length, verbose=True): current_length = self.get_length() if new_length < current_length: new_granule_pos = self.id_header.audio_sample_rate * new_length last_page = self.pages[-1] if verbose: logger.info(u"Current granule position: {0}".format(last_page.granule_pos)) logger.info(u"New granule position: {0}".format(new_granule_pos)) # Replace last page with patched version new_page_data = last_page.get_data_with_new_length(new_granule_pos) self.pages[-1] = OggPage(StringIO(new_page_data))
def patch_file(input_file, target_length=TARGET_LENGTH, output_file=None, verbose=True): patched = False if target_length < 0: logger.error(u"Bad length ({0}), not patching file".format(target_length)) return with open(input_file, "rb") as infile: bitstreams = list(get_bitstreams(infile)) for bitstream in bitstreams: length = bitstream.get_length() if verbose: logger.info(u"Current file length: {0}".format(pprint_time(length))) logger.info(u"Target file length: {0}".format(pprint_time(target_length))) if length > target_length: patched = True bitstream.patch_length(target_length, verbose=verbose) if patched: if output_file is None: output_file = input_file if verbose: logger.info(u"Writing patched file to {0}".format(output_file)) with open(output_file, "wb") as outfile: for bitstream in bitstreams: bitstream.write_to_file(outfile) elif verbose: logger.info(u"Not patching file; file already appears to be {0} or shorter.".format( pprint_time(target_length)))
def copy_songs(input_path, target_dir, verbose=False): logger.info(u"INPUT DIR: {0}".format(repr(input_path))) all_files = [os.path.join(input_path, f) for f in os.listdir(input_path)] dirs = [f for f in all_files if os.path.isdir(f)] oddballs = [f for f in all_files if (not os.path.isdir(f)) and (not os.path.isfile(f))] if len(oddballs) > 0: logger.info(u"ODDBALLS: {0}".format(repr(oddballs))) # If directories present: recurse into them. if len(dirs) > 0: for d in dirs: copy_songs(d, target_dir, verbose=verbose) # Check whether this is a song directory. files = [f for f in all_files if os.path.isfile(f)] stepfile_exists = any( (f.endswith(".sm") or f.endswith(".dwi")) for f in all_files) if not stepfile_exists: return # This is a song directory. Are we compatible? # Currently we must have a .sm and .ogg file. .dwi and .mp3 are # not supported. sm_exists = any(f.endswith(".sm") for f in all_files) ogg_exists = any(f.endswith(".ogg") for f in all_files) mp3_exists = any(f.endswith(".mp3") for f in all_files) if not sm_exists: logger.error(u"Directory {0}: Could not find .sm; only .dwi was found. Skipping.".format(input_path)) return if not ogg_exists: if any(f.endswith(".mp3") for f in all_files): logger.error(u"Directory {0}: Could not find .ogg; only .mp3 was found. Skipping.".format(input_path)) else: logger.error(u"Directory {0}: Could not find .ogg. Skipping.".format(input_path)) return # We are compatible. Check for destination directory; complain # LOUDLY if not able to create it. song_dir_name = os.path.split(input_path)[-1] target_song_dir = os.path.join( target_dir, u"In The Groove 2", u"Songs", song_dir_name) if os.path.exists(target_song_dir): logger.error(u"ERROR: {0} already exists; not copying files from {1}.".format(target_song_dir, input_path)) return os.makedirs(target_song_dir) for ext in ".sm", ".ogg": for src_file in (f for f in all_files if f.endswith(ext)): dest_file = os.path.join( target_song_dir, os.path.basename(src_file)) if verbose: logger.info(u"Copying: {0}\n to: {1}".format(src_file, dest_file)) shutil.copyfile(src_file, dest_file)
def check_file(input_file, target_length, verbose=True): if target_length < 0: logger.error(u"Bad length ({0}), not patching file".format(target_length)) return with open(input_file, "rb") as infile: bitstreams = list(get_bitstreams(infile)) for bitstream in bitstreams: length = bitstream.get_length() if verbose: logger.info(u"Current file length: {0}".format(pprint_time(length))) logger.info(u"Target file length: {0}".format(pprint_time(target_length))) if length > target_length: logger.error(u"File exceeds {0}. Length: {1}".format( pprint_time(target_length), pprint_time(length))) return False else: if verbose: logger.info(u"File passes length check.") continue return True