def extractall_with_single_toplevel(self, f, names): """Same as ``extractall`` but ensures a single toplevel directory Some compressed archives do not stick to the convension of having a single top-level directory. For eg., http://code.google.com/p/grapefruit/issues/detail?id=3 In such cases, a new toplevel directory corresponding to the name of the compressed file (eg: 'grapefruit-0.1a3' if compressed file is named 'grapefruit-0.1a3.tar.gz') is created and then extraction happens *inside* that directory. - f: tarfile/zipefile file object - names: List of filenames in the archive Return the absolute path to the toplevel directory. """ toplevels = _find_top_level_directories(names, sep='/') if len(toplevels) == 0: raise sh.PackError('archive is empty') elif len(toplevels) > 1: toplevel = _archive_basename(self.filename) os.mkdir(toplevel) with sh.cd(toplevel): f.extractall() return path.abspath(toplevel) else: f.extractall() toplevel = path.abspath(toplevels[0]) assert path.exists(toplevel) if not path.isdir(toplevel): # eg: http://pypi.python.org/pypi/DeferArgs/0.4 raise SingleFile('archive has a single file: %s', toplevel) return toplevel
def test_sh_rm_dir(): with sh.tmpdir(): sh.mkdirs('adir') with sh.cd('adir'): with open('afile', 'w') as f: f.close() assert path.exists('afile') assert path.exists('adir') sh.rm('adir') assert not path.exists('adir')
def extract_over2(self, target_dir): with sh.cd(target_dir): with self._open_data() as tf: yield tf tf.extractall()
def extract_over(self, target_dir): """Extract the package data over ``target_dir``""" with sh.cd(target_dir): with self._open_data() as tf: tf.extractall()