Skip to content

Commit

Permalink
Merge pull request #40 from PolarGeospatialCenter/ortho-rpb-fix
Browse files Browse the repository at this point in the history
Fix bytes to string error when extracting RPB from tarfile
  • Loading branch information
ehusby authored Sep 23, 2021
2 parents 866eed5 + b5c2a4a commit d1ce957
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/ortho_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1747,23 +1747,32 @@ def ExtractRPB(item, rpb_p):
tar_p = os.path.splitext(item)[0] + ".tar"
logger.info(tar_p)
if os.path.isfile(tar_p):
fp_extracted = list()
try:
tar = tarfile.open(tar_p, 'r')
tarlist = tar.getnames()
for t in tarlist:
if '.rpb' in t.lower() or '_rpc' in t.lower(): #or '.til' in t.lower():
tf = tar.extractfile(t)
fp = os.path.splitext(rpb_p)[0] + os.path.splitext(t)[1]
fp_extracted.append(fp)
fpfh = open(fp, "w")
tfstr = tf.read()
if type(tfstr) is bytes and type(tfstr) is not str:
tfstr = tfstr.decode('utf-8')
#print(repr(tfstr))
fpfh.write(tfstr)
fpfh.close()
tf.close()
# status = 0
except Exception:
logger.error(utils.capture_error_trace())
logger.error("Cannot open Tar file: %s", tar_p)
logger.error("Caught Exception when working on Tar file: %s", tar_p)
fp_extracted = [fp for fp in fp_extracted if os.path.isfile(fp)]
if len(fp_extracted) > 0:
logger.error("Removing files extracted from Tar file:\n {}".format('\n '.join(fp_extracted)))
for fp in fp_extracted:
os.remove(fp)
rc = 1
else:
logger.info("Tar file does not exist: %s", tar_p)
Expand Down

0 comments on commit d1ce957

Please sign in to comment.