0

cleaned up contrib/validateRegionFile script

This commit is contained in:
Alex Headley
2011-03-19 17:45:20 -04:00
parent fae67de9f0
commit 5ae361824b

View File

@@ -1,106 +1,105 @@
#!/usr/bin/python #!/usr/bin/env python
usage = "python contrib/%prog [OPTIONS] (<regionfilename>)*"
description = """
This script will valide a minecraft region file for errors
"""
from optparse import OptionParser
import sys
import re
import os.path import os.path
import logging import sys
def main():
# sys.path wrangling, so we can access Overviewer code
overviewer_dir = os.path.split(os.path.split(os.path.abspath(__file__))[0])[0] overviewer_dir = os.path.split(os.path.split(os.path.abspath(__file__))[0])[0]
sys.path.insert(0, overviewer_dir) sys.path.insert(0, overviewer_dir)
import nbt import nbt
#import chunk
#import quadtree
parser = OptionParser(usage=usage, description=description)
parser.add_option("-r", "--regions", dest="regiondir", help="Use path to the regions instead of a list of files")
parser.add_option("-v", dest="verbose", action="store_true", help="Lists why a chunk in a region failed")
opt, args = parser.parse_args() def check_region(region_filename):
chunk_errors = []
if not os.path.exists(region_filename):
raise Exception('Region file not found: %s' % region_filename)
try:
region = nbt.load_region(region_filename)
except IOError, e:
raise Exception('Error loading region (%s): %s' % (region_filename, e))
try:
chunks = region.get_chunk_info(False)
except IOError, e:
raise Exception('Error reading region header (%s): %s' % (region_filename, e))
except Exception, e:
raise Exception('Error reading region (%s): %s' % (region_filename, e))
for x,y in chunks:
try:
check_chunk(region, x, y)
except Exception, e:
chunk_errors.append(e)
return (chunk_errors, len(chunks))
if opt.regiondir: def check_chunk(region, x, y):
if os.path.exists(opt.regiondir): try:
for dirpath, dirnames, filenames in os.walk(opt.regiondir, 'region'): data = region.load_chunk(x ,y)
if not dirnames and filenames and "DIM-1" not in dirpath: except Exception, e:
for f in filenames: raise Exception('Error reading chunk (%i, %i): %s' % (x, y, e))
if f.startswith("r.") and f.endswith(".mcr"): if data is None:
p = f.split(".") raise Exception('Chunk (%i, %i) is unexpectedly empty' % (x, y))
args.append(os.path.join(dirpath, f)) else:
try:
processed_data = data.read_all()
except Exception, e:
raise Exception('Error reading chunk (%i, %i) data: %s' % (x, y, e))
if processed_data == []:
raise Exception('Chunk (%i, %i) is an unexpectedly empty set' % (x, y))
if len(args) < 1: if __name__ == '__main__':
print "You must list at least one region file" try:
from optparse import OptionParser
parser = OptionParser(usage='python contrib/%prog [OPTIONS] <path/to/regions|path/to/regions/*.mcr|regionfile1.mcr regionfile2.mcr ...>',
description='This script will valide a minecraft region file for errors.')
parser.add_option('-v', dest='verbose', action='store_true', help='Print additional information.')
opts, args = parser.parse_args()
region_files = []
for path in args:
if os.path.isdir(path):
for dirpath, dirnames, filenames in os.walk(path, True):
for filename in filenames:
if filename.startswith('r.') and filename.endswith('.mcr'):
if filename not in region_files:
region_files.append(os.path.join(dirpath, filename))
elif opts.verbose:
print('Ignoring non-region file: %s' % os.path.join(dirpath, filename))
elif os.path.isfile(path):
dirpath,filename = os.path.split(path)
if filename.startswith('r.') and filename.endswith('.mcr'):
if path not in region_files:
region_files.append(path)
else:
print('Ignoring non-region file: %s' % path)
else:
if opts.verbose:
print('Ignoring arg: %s' % path)
if len(region_files) < 1:
print 'You must list at least one region file.'
parser.print_help() parser.print_help()
sys.exit(1) sys.exit(1)
for regionfile in args:
_,shortname = os.path.split(regionfile)
chunk_pass = 0
chunk_total = 0
if not os.path.exists(regionfile):
print("File not found: %s"%( regionfile))
#print("Region:%s Passed %s/%s"%(shortname,chunk_pass,chunk_total))
continue
try:
mcr = nbt.load_region(regionfile)
except IOError, e:
if opt.verbose:
print("Error opening regionfile. It may be corrupt. %s"%( e))
continue
if mcr is not None:
try:
chunks = mcr.get_chunk_info(False)
except IOError, e:
if opt.verbose:
print("Error opening regionfile(bad header info). It may be corrupt. %s"%( e))
chunks = []
continue
except Exception, e:
if opt.verbose:
print("Error opening regionfile (%s): %s"%( regionfile,e))
continue
for x, y in chunks:
chunk_total += 1
try:
chunk_data = mcr.load_chunk(x, y)
except Exception, e:
if opt.verbose:
print("Error reading chunk (%i,%i): %s"%(x,y,e))
continue
if chunk_data is None:
if opt.verbose:
print("Chunk %s:%s is unexpectedly empty"%(x, y))
continue
else: else:
overall_chunk_total = 0
bad_chunk_total = 0
bad_region_total = 0
for region_file in region_files:
try: try:
processed = chunk_data.read_all() (chunk_errors, region_chunks) = check_region(region_file)
if processed == []: bad_chunk_total += len(chunk_errors)
if opt.verbose: overall_chunk_total += region_chunks
print("Chunk %s:%s is an unexpectedly empty set"%(x, y))
continue
else:
chunk_pass += 1
except Exception, e: except Exception, e:
if opt.verbose: bad_region_total += 1
print("Error opening chunk (%i, %i) It may be corrupt. %s"%( x, y, e)) print('FAILED(%s): %s' % (region_file, e))
continue
else: else:
if opt.verbose: if len(chunk_errors) is not 0:
print("Error opening regionfile.") print('WARNING(%s) Chunks: %i/%' % (region_file, region_chunks - len(chunk_errors), region_chunks))
continue if opts.verbose:
for error in chunk_errors:
print("Region:%s Passed %s/%s"%(shortname,chunk_pass,chunk_total)) print(error)
if __name__ == "__main__": elif opts.verbose:
try: print ('PASSED(%s) Chunks: %i/%i' % (region_file, region_chunks - len(chunk_errors), region_chunks))
main() if opts.verbose:
print 'REGIONS: %i/%i' % (len(region_files) - bad_region_total, len(region_files))
print 'CHUNKS: %i/%i' % (overall_chunk_total - bad_chunk_total, overall_chunk_total)
except KeyboardInterrupt: except KeyboardInterrupt:
print "Caught Ctrl-C" sys.exit(1)
except Exception, e:
print('ERROR: %s' % e)