def test_trash_one(self): ctx = self._make_context(timeout = 0.100) ctx.trash.start() tmp = file_util.save(path.join(ctx.stuff_dir, 'foo.txt'), content = 'foo\n') self.assertEqual( [ 'foo.txt' ], dir_util.list(ctx.stuff_dir, relative = True) ) ctx.trash.trash(tmp) time.sleep(0.250) self.assertEqual( [], dir_util.list(ctx.stuff_dir, relative = True) ) ctx.trash.stop()
def _scan_dir(clazz, d): 'Scan a directory for .pc files.' if not path.isdir(d): return [] return [ f for f in dir_util.list(d) if caca_pkg_config_file.is_pc_file(f) ]
def list_libraries(clazz, d, relative=True): 'List all libraries in a directory. Does not rescurse into subdirs. Resulting paths are absolute.' files = dir_util.list(d, relative=False) libs = [f for f in files if clazz.is_library(f)] if relative: libs = [file_util.remove_head(f, d) for f in libs] return libs
def _determine_files(clazz, where, files): if files: for f in files: p = path.join(where, f) if not path.isfile(p): raise IOError('File not found: %s' % (p)) return files else: return dir_util.list(where, relative = True, patterns = [ '*.sh' ])
def test_list_relative(self): tmp_dir = self.__make_tmp_files() expected_files = [ 'foo.txt', 'bar.txt', 'kiwi.jpg', 'kiwi.png', 'orange.png', ] self.assertEqual( sorted(expected_files), dir_util.list(tmp_dir, relative = True) )
def test(self): tmp_dir = self.__make_tmp_files() expected_files = [ 'foo.txt', 'bar.txt', 'kiwi.jpg', 'kiwi.png', 'orange.png', ] expected_files = [ path.join(tmp_dir, f) for f in expected_files ] self.assertEqual( sorted(expected_files), dir_util.list(tmp_dir) )
def test_extract_thin(self): expected_files = [ 'thin_cherry.o', 'thin_kiwi.o', ] tmp_dir = temp_file.make_temp_dir() ar_replacement.extract(self._test_file('thin_fruits_x86_64.a'), tmp_dir) actual_files = dir_util.list(tmp_dir, relative=True) actual_files = self._filter_contents(actual_files) self.assertEqual(expected_files, actual_files)
def _determine_manifest(clazz, stage_dir): 'Return the list of files to package. Maybe could do some filtering here. Using find because its faster that bes.fs.file_find.' stuff = dir_util.list(stage_dir, relative=True) rv = execute.execute(['find'] + stuff + ['-type', 'f'], cwd=stage_dir) files = text_line_parser.parse_lines(rv.stdout, strip_text=True, remove_empties=True) rv = execute.execute(['find'] + stuff + ['-type', 'l'], cwd=stage_dir) links = text_line_parser.parse_lines(rv.stdout, strip_text=True, remove_empties=True) return sorted(files + links)
def patch_make(clazz, working_dir): 'Create a patch out of 2 directories one ending in .orig.' dirs = dir_util.list(working_dir, relative = True) if len(dirs) != 2: raise RuntimeError('Found more than 2 directories in %s' % (working_dir)) if not dirs[1].endswith(clazz.ORIGINAL_DIR_TAIL): raise RuntimeError('Dir 2 should end in .orig instead it is %s' % (dir2)) base_dir = file_util.remove_tail(dirs[1], clazz.ORIGINAL_DIR_TAIL) if string_util.remove_tail(dirs[1], clazz.ORIGINAL_DIR_TAIL) != dirs[0]: raise RuntimeError('Dir 1 and 2 dont have the same name: %s %s' % (dirs[1], dirs[0])) cmd = 'diff -ur %s %s --exclude="*~" --exclude=".#*" --exclude="#*"' % (dirs[1], dirs[0]) patch = execute.execute(cmd, cwd = working_dir, raise_error = False).stdout return patch.strip()
def delete_file(self, filename): 'Delete a file or directory.' if path.isfile(filename): self.log_i('deleting single file %s' % (filename)) file_util.remove(filename) elif path.isdir(filename): files = dir_util.list(filename) self.log_i('deleting many files: %s' % (files)) for f in files: self.log_i('deleting next file %s' % (f)) file_util.remove(f) self.log_i('sleeping for %f seconds' % (self._sleep_time)) time.sleep(self._sleep_time) else: raise RuntimeError('invalid file type: %s' % (filename))
def _extract_darwin_fat(clazz, archive, dest_dir, tools): 'Extract fat archive.' tmp_dir = temp_file.make_temp_dir() thin_libs = clazz._fat_to_thin(archive, tmp_dir, tools) expected_contents = None file_util.mkdir(dest_dir) for arch, lib in thin_libs: objects_dir = lib + '.objdir' clazz._extract_with_ar(lib, objects_dir, tools) files = dir_util.list(objects_dir, relative = True) for f in files: src = path.join(objects_dir, f) dst_basename = arch + '_' + f dst = path.join(dest_dir, dst_basename) shutil.move(src, dst)
def test_extract_fat(self): expected_files = [ 'arm64_fat_cherry.o', 'arm64_fat_kiwi.o', 'armv7_fat_cherry.o', 'armv7_fat_kiwi.o', 'i386_fat_cherry.o', 'i386_fat_kiwi.o', 'x86_64_fat_cherry.o', 'x86_64_fat_kiwi.o', ] tmp_dir = temp_file.make_temp_dir() ar_replacement.extract(self._test_file('fat_fruits.a'), tmp_dir) actual_files = dir_util.list(tmp_dir, relative=True) actual_files = self._filter_contents(actual_files) self.assertEqual(expected_files, actual_files)
def execute(self, script, env, values, inputs): if not path.isdir(script.staged_files_bin_dir): return step_result(True, None) if values.get('skip_binary_third_party_prefix'): return step_result(True, None) binaries = dir_util.list(script.staged_files_bin_dir) for b in binaries: link_src = path.basename(b) if not link_src.startswith(env.config.third_party_prefix): link_filename = '%s%s' % (env.config.third_party_prefix, link_src) link_dst = path.join(script.staged_files_bin_dir, link_filename) file_util.symlink(link_src, link_dst) return step_result(True, None)
def list_eggs(clazz, d): return dir_util.list(d, patterns = [ '*.egg', '*.egg-info' ], relative = True)
def test_list_pattern(self): tmp_dir = self.__make_tmp_files() self.assertEqual( [ 'kiwi.jpg' ], dir_util.list(tmp_dir, relative = True, patterns = '*.jpg') ) self.assertEqual( [ 'kiwi.jpg', 'kiwi.png' ], dir_util.list(tmp_dir, relative = True, patterns = 'kiwi*') )
def _list_trash(self): if not path.isdir(self._location): print('WARNING: location disappeared: %s' % (self._location)) return [] return dir_util.list(self._location)