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 test_list_pattern_basename_is_true(self): tmp_dir = self._make_temp_content([ 'file foo.txt "foo.txt"', 'file bar.txt "bar.txt"', 'file kiwi.jpg "kiwi.jpg"', 'file kiwi.png "kiwi.png"', 'file orange.png "orange.png"', #'file emptyfile.txt', #'dir emptydir', ]) self.assertEqual( [], dir_util.list(tmp_dir, relative = True, patterns = 'kiwi*', basename = False) ) self.assertEqual( [ 'kiwi.jpg', 'kiwi.png' ], dir_util.list(tmp_dir, relative = True, patterns = 'kiwi*', basename = True) )
def __exit__(self, type, value, traceback): if not path.isdir(self._where): raise IOError('Directory not found: "{}"'.format(self._where)) after = set(dir_util.list(self._where)) diff = after - self._before if diff: file_util.remove(list(diff))
def _cleanup_usr_local_links(self): 'Cleanup symlinks in /usr/local/bin that break after uninstalling python' links = dir_util.list('/usr/local/bin') broken_links = [l for l in links if file_symlink.is_broken(l)] for broken_link in broken_links: target = os.readlink(broken_link) if 'Library/Frameworks/Python.framework/Versions' in target: self.blurb_verbose( 'Removing broken /usr/local link: {}'.format(broken_link)) file_util.remove(broken_link)
def test_list_function(self): tmp_dir = self._make_temp_content([ 'file foo.txt "foo.txt"', 'file bar.txt "bar.txt"', 'file kiwi.jpg "kiwi.jpg"', 'file kiwi.png "kiwi.png"', 'file orange.png "orange.png"', #'file emptyfile.txt', #'dir emptydir', ]) self.assertEqual( [ 'kiwi.jpg' ], dir_util.list(tmp_dir, relative = True, function = lambda f: f.endswith('.jpg')) )
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'], basename=True)
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 clone(clazz, address, root_dir, options = None): check.check_git_clone_options(options, allow_none = True) address = git_address_util.resolve(address) options = options or git_clone_options() clazz.log.log_d('clone: address={} root_dir={} options={}'.format(address, root_dir, options)) if path.exists(root_dir): if not path.isdir(root_dir): raise git_error('root_dir "{}" is not a directory.'.format(root_dir)) if options.enforce_empty_dir: if not dir_util.is_empty(root_dir): files = dir_util.list(root_dir, relative = True) sorted_files = sorted(files, key = lambda f: f.lower()) printed_files = '\n '.join(sorted_files).strip() raise git_error('root_dir "{}" is not empty:\n {}\n'.format(root_dir, printed_files)) else: file_util.mkdir(root_dir) args = [ 'clone' ] if options.depth: args.extend([ '--depth', str(options.depth) ]) if options.jobs: args.extend([ '--jobs', str(options.jobs) ]) if options.branch: args.extend([ '--branch', options.branch ]) if options.submodules_recursive: args.extend([ '--recursive' ]) if options.shallow_submodules: args.extend([ '--shallow-submodules' ]) args.append(address) args.append(root_dir) extra_env = git_lfs.lfs_make_env(options.lfs) clazz.log.log_d('clone: args="{}" extra_env={}'.format(' '.join(args), extra_env)) clone_rv = git_exe.call_git(os.getcwd(), args, extra_env = extra_env, num_tries = options.num_tries, retry_wait_seconds = options.retry_wait_seconds) clazz.log.log_d('clone: clone_rv="{}"'.format(str(clone_rv))) sub_rv = None if options.branch: git.checkout(root_dir, options.branch) if options.submodules or options.submodule_list: sub_rv = clazz._submodule_init(root_dir, options) return clone_rv, sub_rv
def test_list_relative(self): tmp_dir = self._make_temp_content([ 'file foo.txt "foo.txt"', 'file bar.txt "bar.txt"', 'file kiwi.jpg "kiwi.jpg"', 'file kiwi.png "kiwi.png"', 'file orange.png "orange.png"', #'file emptyfile.txt', #'dir emptydir', ]) 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_list(self): tmp_dir = self._make_temp_content([ 'file foo.txt "foo.txt"', 'file bar.txt "bar.txt"', 'file kiwi.jpg "kiwi.jpg"', 'file kiwi.png "kiwi.png"', 'file orange.png "orange.png"', #'file emptyfile.txt', #'dir emptydir', ]) 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 restore(clazz, blurber, where): check.check_string(where) if not path.exists(where): blurber.blurb('Stash directory not found: "{}"'.format(where)) return 1 if not path.isdir(where): blurber.blurb('Not a directory: "{}"'.format(where)) return 1 files = dir_util.list(where) if not files: blurber.blurb('No stash files found in: "{}"'.format(where)) return 1 for filename in files: args = ['load', '--input', filename] docker_exe.call_docker(args, non_blocking=False) return 0
def _list_trash(self): if not path.isdir(self._location): print('WARNING: location disappeared: %s' % (self._location)) return [] return dir_util.list(self._location)
def __enter__(self): if not path.isdir(self._where): raise IOError('Directory not found: "{}"'.format(self._where)) self._before = set(dir_util.list(self._where)) return self
def versions(self): 'Return the python versions on disk' if not path.exists(self._options.root_dir): return [] dirs = dir_util.list(self._options.root_dir, relative=True) return semantic_version.sort_string_list(dirs)
def list_eggs(clazz, d): return dir_util.list(d, patterns=['*.egg', '*.egg-info'], relative=True, basename=True)
def possible_python_bin_dirs(clazz): 'Return a list of possible dirs where the python executable might be.' return dir_util.list(r'C:\Program Files', patterns='Python*', basename=True)