def unzip(zip_path, temp_path, file_name): ''' Take an absolute path to a zip file |zip_path| and return the path to a file or directory called |file_name| in |temp_path| containing its unzipped contents. Assume the zip file contains one file/directory called |file_name|. If |file_name| is not specified, then return the temp_path itself. ''' path_util.check_isfile(zip_path, 'unzip_directory') if file_name: temp_subpath = os.path.join(temp_path, file_name) else: temp_subpath = temp_path print_util.open_line('Unzipping %s to %s' % (zip_path, temp_subpath)) if os.system("cd %s && unzip -q %s" % (temp_path, zip_path)) != 0: raise UsageError('unzip failed') print_util.clear_line() # Corner case: note that the temp_subpath might not 'exist' because it is a # symlink (which is broken until it's put in the right place). if not os.path.exists(temp_subpath) and not os.path.islink(temp_subpath): raise UsageError('Zip file %s missing %s (%s doesn\'t exist)' % (zip_path, file_name, temp_subpath)) return temp_subpath
def open_file(self, path, mode): ''' Open a file handle to the given path and return a uuid identifying it. ''' path_util.check_isfile(path, 'open_file') file_uuid = uuid.uuid4().hex self.file_handles[file_uuid] = open(path, mode) return file_uuid
def unzip(zip_path): ''' Take an absolute path to a zip file and return the path to a file or directory containing its unzipped contents. ''' path_util.check_isfile(zip_path, 'unzip_directory') temp_path = tempfile.mkdtemp() temp_subpath = os.path.join(temp_path, ZIP_SUBPATH) zip_file = ZipFile(zip_path, 'r') names = zip_file.namelist() if any(not name.startswith(ZIP_SUBPATH) for name in names): raise UsageError('Got unexpected member in zip: %s' % (name,)) zip_file.extractall(temp_path) return temp_subpath
def unzip(zip_path, temp_path, file_name): ''' Take an absolute path to a zip file |zip_path| and return the path to a file or directory called |file_name| in |temp_path| containing its unzipped contents. Assume the zip file really contains one thing called |file_name|. ''' path_util.check_isfile(zip_path, 'unzip_directory') temp_subpath = os.path.join(temp_path, file_name) print_util.open_line('Unzipping %s to %s' % (zip_path, temp_subpath)) if os.system("cd %s && unzip -q %s" % (temp_path, zip_path)) != 0: raise UsageError('unzip failed') print_util.clear_line() # Corner case: note that the temp_subpath might not 'exist' because it is a # symlink (which is broken until it's put in the right place). if not os.path.exists(temp_subpath) and not os.path.islink(temp_subpath): raise UsageError('Zip file %s missing %s (%s doesn\'t exist)' % (zip_path, file_name, temp_subpath)) return temp_subpath
def unzip(zip_path, temp_path, sub_path=ZIP_SUBPATH): ''' Take an absolute path to a zip file and return the path to a file or directory containing its unzipped contents. The returned contents should live in temp_path. ''' path_util.check_isfile(zip_path, 'unzip_directory') temp_subpath = os.path.join(temp_path, sub_path) # TODO(pliang): ZipFile doesn't preserve permissions, so must resort to # system calls (only works in Linux). if os.system("cd %s && unzip -q %s" % (temp_path, zip_path)) != 0: raise UsageError('unzip failed') # Corner case: note that the temp_subpath might not 'exist' because it is a # symlink (which is broken until it's put in the right place). if not os.path.exists(temp_subpath) and not os.path.islink(temp_subpath): raise UsageError('Zip file %s missing %s (%s doesn\'t exist)' % (zip_path, ZIP_SUBPATH, temp_subpath)) #zip_file = ZipFile(zip_path, 'r') #names = zip_file.namelist() #if any(not name.startswith(ZIP_SUBPATH) for name in names): # raise UsageError('Got unexpected member in zip: %s' % (name,)) #zip_file.extractall(temp_path) return temp_subpath
def open_target(self, target): check_has_read_permission_on_bundles(self.model, self._current_user(), [target[0]]) path = self.get_target_path(target) path_util.check_isfile(path, 'open_target') return open(path)
def open_target(self, target): (bundle_spec, subpath) = target path = self.get_target_path(target) path_util.check_isfile(path, 'open_target') return open(path)