2016-04-11 22:03:05 +00:00
__version__ = " 2.5.1 "
2016-02-13 04:36:33 +00:00
try :
import numpy as np
except ImportError :
raise RuntimeError ( " atrcopy %s requires numpy " % __version__ )
from errors import *
from ataridos import AtariDosDiskImage , AtariDosFile
2016-02-13 05:36:08 +00:00
from diskimages import AtrHeader , BootDiskImage
2016-02-13 04:36:33 +00:00
from kboot import KBootImage
2016-04-13 00:04:21 +00:00
from segments import SegmentData , SegmentSaver , DefaultSegment , EmptySegment , ObjSegment , RawSectorsSegment
2016-02-13 04:36:33 +00:00
from spartados import SpartaDosDiskImage
from utils import to_numpy
2016-02-13 05:36:08 +00:00
2016-02-13 04:36:33 +00:00
def process ( image , dirent , options ) :
skip = False
action = " copying to "
filename = dirent . get_filename ( )
outfilename = filename
if options . no_sys :
if dirent . ext == " SYS " :
skip = True
action = " skipping system file "
if not skip :
if options . xex :
outfilename = " %s %s .XEX " % ( dirent . filename , dirent . ext )
if options . lower :
outfilename = outfilename . lower ( )
if options . dry_run :
action = " DRY_RUN: %s " % action
skip = True
if options . extract :
print " %s : %s %s " % ( dirent , action , outfilename )
if not skip :
bytes = image . get_file ( dirent )
with open ( outfilename , " wb " ) as fh :
fh . write ( bytes )
else :
print dirent
def run ( ) :
import sys
import argparse
parser = argparse . ArgumentParser ( description = " Extract images off ATR format disks " )
parser . add_argument ( " -v " , " --verbose " , default = 0 , action = " count " )
parser . add_argument ( " -d " , " --debug " , action = " store_true " , default = False , help = " debug the currently under-development parser " )
parser . add_argument ( " -l " , " --lower " , action = " store_true " , default = False , help = " convert filenames to lower case " )
parser . add_argument ( " --dry-run " , action = " store_true " , default = False , help = " don ' t extract, just show what would have been extracted " )
parser . add_argument ( " -n " , " --no-sys " , action = " store_true " , default = False , help = " only extract things that look like games (no DOS or .SYS files) " )
parser . add_argument ( " -x " , " --extract " , action = " store_true " , default = False , help = " extract files " )
parser . add_argument ( " --xex " , action = " store_true " , default = False , help = " add .xex extension " )
parser . add_argument ( " -f " , " --force " , action = " store_true " , default = False , help = " force operation on disk images that have bad directory entries or look like boot disks " )
parser . add_argument ( " files " , metavar = " ATR " , nargs = " + " , help = " an ATR image file [or a list of them] " )
parser . add_argument ( " -s " , " --segments " , action = " store_true " , default = False , help = " display segments " )
options , extra_args = parser . parse_known_args ( )
for filename in options . files :
with open ( filename , " rb " ) as fh :
2016-03-25 23:07:16 +00:00
rawdata = SegmentData ( fh . read ( ) )
data = rawdata . get_data ( )
2016-02-13 04:36:33 +00:00
image = None
if options . debug :
header = AtrHeader ( data [ 0 : 16 ] )
2016-03-25 23:07:16 +00:00
image = SpartaDosDiskImage ( rawdata , filename )
2016-02-13 04:36:33 +00:00
else :
try :
data = to_numpy ( data )
try :
header = AtrHeader ( data [ 0 : 16 ] )
for format in [ KBootImage , SpartaDosDiskImage , AtariDosDiskImage ] :
if options . verbose : print " trying " , format . __name__
try :
2016-03-25 23:07:16 +00:00
image = format ( rawdata , filename )
2016-02-13 04:36:33 +00:00
print " %s : %s " % ( filename , image )
break
except InvalidDiskImage :
pass
except AtrError :
for format in [ AtariDosDiskImage ] :
try :
2016-03-25 23:07:16 +00:00
image = format ( rawdata , filename )
2016-02-13 04:36:33 +00:00
print " %s : %s " % ( filename , image )
break
except :
raise
#pass
except AtrError :
if options . verbose : print " %s : Doesn ' t look like a supported disk image " % filename
try :
2016-03-25 23:07:16 +00:00
image = AtariDosFile ( rawdata )
2016-02-13 04:36:33 +00:00
print " %s : \n %s " % ( filename , image )
except InvalidBinaryFile :
if options . verbose : print " %s : Doesn ' t look like an XEX either " % filename
continue
if image is None :
2016-03-25 23:07:16 +00:00
image = BootDiskImage ( rawdata , filename )
2016-02-13 04:36:33 +00:00
if options . segments :
image . parse_segments ( )
print " \n " . join ( [ str ( a ) for a in image . segments ] )
elif image . files or options . force :
for dirent in image . files :
try :
process ( image , dirent , options )
except FileNumberMismatchError164 :
print " Error 164: %s " % str ( dirent )
except ByteNotInFile166 :
print " Invalid sector for: %s " % str ( dirent )