def test_rmtree_file(self): with infra_libs.temporary_directory() as tempdir: tmpfile = os.path.join(tempdir, 'foo') with open(tmpfile, 'w') as f: f.write('asdfasdf') infra_libs.rmtree(tmpfile) self.assertFalse(os.path.exists(tmpfile))
def ensure_depot_tools(): """Fetches depot_tools to temp dir to use it to fetch the gclient solution.""" # We don't really want to trust that the existing version of depot_tools # is pristine and uncorrupted. So delete it and re-clone. print 'Setting up depot_tools in %s' % TEMP_DEPOT_TOOLS infra_libs.rmtree(TEMP_DEPOT_TOOLS) parent = os.path.dirname(TEMP_DEPOT_TOOLS) if not os.path.exists(parent): os.makedirs(parent) call([GIT, 'clone', DEPOT_TOOLS_URL], cwd=parent) return TEMP_DEPOT_TOOLS
def maybe_clean_rootdir(root_dir): """Clean out root_dir if the disk is over 90% full.""" # TODO(hinoka): Windows support. if sys.platform.startswith('win'): return st = os.statvfs(root_dir) percent = st.f_bavail * 100 / st.f_blocks if percent > 10: return # else: Less than 10% disk free. Delete everything in the root dir. print 'Free space less than 10%%, clearing out %s' % root_dir for dirname in os.listdir(root_dir): fullname = os.path.join(root_dir, dirname) print 'Deleting %s' % fullname infra_libs.rmtree(fullname)
def ensure_checkout(root_dir, depot_tools, internal): """Ensure that /b/.gclient is correct and the build checkout is there.""" gclient_bin = os.path.join(depot_tools, GCLIENT_BIN) env = inject_path_in_environ(os.environ.copy(), depot_tools) write_gclient_file(root_dir, internal) rc = call([gclient_bin, 'sync'], cwd=root_dir, env=env) if rc: print 'Gclient sync failed, cleaning and trying again' for filename in os.listdir(root_dir): full_path = os.path.join(root_dir, filename) print 'Deleting %s...' % full_path if os.path.isdir(full_path): # shutil.rmtree doesn't work on Windows, but ours works. infra_libs.rmtree(full_path) else: os.remove(full_path) write_gclient_file(root_dir, internal) rc = call([gclient_bin, 'sync'], cwd=root_dir, env=env) if rc: raise Exception('Could not ensure gclient file is correct.')
def main(self, opts): for path, pattern, max_age_secs in DIRECTORIES[sys.platform]: max_age_secs = max_age_secs or DEFAULT_MAX_AGE_SECS delete_before = time.time() - max_age_secs if not os.path.isdir(path): continue # TODO(pgervais,http://crbug.com/564743): make this recursive names = os.listdir(path) if pattern is not None: names = fnmatch.filter(names, pattern) print 'Found %d files in %s' % (len(names), path) for name in names: filepath = os.path.join(path, name) if os.path.getctime(filepath) < delete_before: print 'Deleting', filepath if not opts.dry_run: if os.path.isdir(filepath): infra_libs.rmtree(filepath) else: os.unlink(filepath)
def main(argv): parser = argparse.ArgumentParser('rmtree') parser.add_argument( '--try-as-root', action='store_true', help='If true, try and use elevate permissions prior to deleting the ' 'target. Is elevation fails, proceed as current user.') parser.add_argument('targets', nargs='*', help='The target file or directory to remove.') infra_libs.logs.add_argparse_options(parser) args = parser.parse_args(argv) infra_libs.logs.process_argparse_options(args) if args.try_as_root and _can_try_as_root(): # Set the sentinel so we don't infinitely recurse. env = os.environ.copy() env[_ENV_TRY_AS_ROOT] = '1' # Execute the same command with "sudo". Will return non-zero if there was # an error, or if "sudo" could not be executed without a password. cmd = ['sudo', '-n', sys.executable, SELF] + argv LOGGER.debug('(--try-as-root) Escalating: %r', cmd) rc = subprocess.call(cmd, env=env) if rc == 0: return 0 LOGGER.info( '(--try-as-root) Failed to execute as root (%d); trying as ' 'current user.', rc) for target in args.targets: LOGGER.info('Removing target: %r', target) infra_libs.rmtree(target) return 0
def tearDown(self): super(OwnServiceTest, self).tearDown() infra_libs.rmtree(self.root_directory)
def tearDown(self): mock.patch.stopall() infra_libs.rmtree(self.state_directory)
def tearDown(self): mock.patch.stopall() infra_libs.rmtree(self.temp_dir)
def test_rmtree_directory(self): with infra_libs.temporary_directory() as tempdir: infra_libs.rmtree(tempdir) self.assertFalse(os.path.exists(tempdir))
def tearDown(self): setattr(git_cookie_daemon, 'LOGGER', self.old_log) setattr(requests, 'get', self.old_get) infra_libs.rmtree(self.temp_dir)