def _upgrade_magic_folder_config(basedir): """ Helper that upgrades from single-magic-folder-only configs to multiple magic-folder configuration style (in YAML) """ config_fname = os.path.join(basedir, "tahoe.cfg") config = configutil.get_config(config_fname) collective_fname = os.path.join(basedir, "private", "collective_dircap") upload_fname = os.path.join(basedir, "private", "magic_folder_dircap") magic_folders = { u"default": { u"directory": config.get("magic_folder", "local.directory").decode("utf-8"), u"collective_dircap": fileutil.read(collective_fname), u"upload_dircap": fileutil.read(upload_fname), u"poll_interval": int(config.get("magic_folder", "poll_interval")), }, } fileutil.move_into_place( source=os.path.join(basedir, "private", "magicfolderdb.sqlite"), dest=os.path.join(basedir, "private", "magicfolder_default.sqlite"), ) save_magic_folders(basedir, magic_folders) config.remove_option("magic_folder", "local.directory") config.remove_option("magic_folder", "poll_interval") configutil.write_config(os.path.join(basedir, 'tahoe.cfg'), config) fileutil.remove_if_possible(collective_fname) fileutil.remove_if_possible(upload_fname)
def test_remove_if_possible(self): basedir = "util/FileUtil/test_remove_if_possible" fileutil.make_dirs(basedir) self.touch(basedir, "here") fn = os.path.join(basedir, "here") fileutil.remove_if_possible(fn) self.failIf(os.path.exists(fn)) fileutil.remove_if_possible(fn) # should be idempotent fileutil.rm_dir(basedir) fileutil.remove_if_possible(fn) # should survive errors
#!/usr/bin/env python from allmydata.util import fileutil import re, shutil, sys R=re.compile("^#! */usr/bin/python *$") for fname in sys.argv[1:]: inf = open(fname, "rU") rntf = fileutil.ReopenableNamedTemporaryFile() outf = open(rntf.name, "w") first = True for l in inf: if first and R.search(l): outf.write("#!/usr/bin/env python\n") else: outf.write(l) first = False outf.close() try: shutil.move(rntf.name, fname) except EnvironmentError: # Couldn't atomically overwrite, so just hope that this process doesn't die # and the target file doesn't get recreated in between the following two # operations: shutil.move(fname, fname + ".bak") shutil.move(rntf.name, fname) fileutil.remove_if_possible(fname + ".bak")