<s> """<STR_LIT>""" <EOL> import os <EOL> import shutil <EOL> import subprocess <EOL> import tarfile <EOL> import tempfile <EOL> from codalab . common import UsageError <EOL> from codalab . lib import path_util , file_util <EOL> from worker . file_util import un_tar_directory , un_gzip_stream <EOL> ARCHIVE_EXTS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def path_is_archive ( path ) : <EOL> if isinstance ( path , basestring ) : <EOL> for ext in ARCHIVE_EXTS : <EOL> if path . endswith ( ext ) : <EOL> return True <EOL> return False <EOL> def get_archive_ext ( fname ) : <EOL> """<STR_LIT>""" <EOL> for ext in ARCHIVE_EXTS : <EOL> if fname . endswith ( ext ) : <EOL> return ext <EOL> return '<STR_LIT>' <EOL> def strip_archive_ext ( path ) : <EOL> for ext in ARCHIVE_EXTS : <EOL> if path . endswith ( ext ) : <EOL> return path [ : - len ( ext ) ] <EOL> raise UsageError ( '<STR_LIT>' % path ) <EOL> def unpack ( ext , source , dest_path ) : <EOL> """<STR_LIT>""" <EOL> if ext != '<STR_LIT>' : <EOL> close_source = False <EOL> try : <EOL> if isinstance ( source , basestring ) : <EOL> source = open ( source , '<STR_LIT:rb>' ) <EOL> close_source = True <EOL> if ext == '<STR_LIT>' or ext == '<STR_LIT>' : <EOL> un_tar_directory ( source , dest_path , '<STR_LIT>' ) <EOL> elif ext == '<STR_LIT>' : <EOL> un_tar_directory ( source , dest_path , '<STR_LIT>' ) <EOL> elif ext == '<STR_LIT>' : <EOL> with open ( dest_path , '<STR_LIT:wb>' ) as f : <EOL> shutil . copyfileobj ( un_gzip_stream ( source ) , f ) <EOL> else : <EOL> raise UsageError ( '<STR_LIT>' ) <EOL> except ( tarfile . TarError , IOError ) : <EOL> raise UsageError ( '<STR_LIT>' ) <EOL> finally : <EOL> if close_source : <EOL> source . close ( ) <EOL> else : <EOL> delete_source = False <EOL> try : <EOL> if not isinstance ( source , basestring ) : <EOL> temp_path = dest_path + '<STR_LIT>' <EOL> with open ( temp_path , '<STR_LIT:wb>' ) as f : <EOL> shutil . copyfileobj ( source , f ) <EOL> source = temp_path <EOL> delete_source = True <EOL> exitcode = subprocess . call ( [ '<STR_LIT>' , '<STR_LIT>' , source , '<STR_LIT>' , dest_path ] ) <EOL> if exitcode != <NUM_LIT:0> : <EOL> raise UsageError ( '<STR_LIT>' ) <EOL> finally : <EOL> if delete_source : <EOL> path_util . remove ( source ) </s>
