def save(config, data): """Save data and create backup, creating a new data file if necessary.""" data_dir = os.path.dirname(config['data_path']) create_dir(data_dir) # atomically save by writing to temporary file and moving to destination try: temp = NamedTemporaryFile(delete=False, dir=data_dir) # Windows cannot reuse the same open file name temp.close() with open(temp.name, 'w', encoding='utf-8', errors='replace') as f: for path, weight in data.items(): f.write(unico('%s\t%s\n' % (weight, path))) f.flush() os.fsync(f) except IOError as ex: print('Error saving autojump data (disk full?)' % ex, file=sys.stderr) sys.exit(1) # move temp_file -> autojump.txt move_file(temp.name, config['data_path']) # create backup file if it doesn't exist or is older than BACKUP_THRESHOLD if not os.path.exists(config['backup_path']) or \ (time() - os.path.getmtime(config['backup_path']) > BACKUP_THRESHOLD): # noqa shutil.copy(config['data_path'], config['backup_path'])
def save(config, data): """Save data and create backup, creating a new data file if necessary.""" create_dir(os.path.dirname(config["data_path"])) # atomically save by writing to temporary file and moving to destination try: # write to temp file with open(config["tmp_path"], "w", encoding="utf-8", errors="replace") as f: for path, weight in data.items(): f.write(unico("%s\t%s\n" % (weight, path))) f.flush() os.fsync(f) except IOError as ex: print("Error saving autojump data (disk full?)" % ex, file=sys.stderr) sys.exit(1) # move temp_file -> autojump.txt move_file(config["tmp_path"], config["data_path"]) # create backup file if it doesn't exist or is older than BACKUP_THRESHOLD if not os.path.exists(config["backup_path"]) or ( time() - os.path.getmtime(config["backup_path"]) > BACKUP_THRESHOLD ): shutil.copy(config["data_path"], config["backup_path"])
def test_move_file(self): src = self.get_random_file() dst = self.get_random_path() assert_true(os.path.exists(src)) assert_false(os.path.exists(dst)) move_file(src, dst) assert_false(os.path.exists(src)) assert_true(os.path.exists(dst))
def migrate_osx_xdg_data(config): """ Older versions incorrectly used Linux XDG_DATA_HOME paths on OS X. This migrates autojump files from ~/.local/share/autojump to ~/Library/autojump """ assert is_osx(), 'This function should only be run on OS X.' xdg_data_home = os.path.join(os.path.expanduser('~'), '.local', 'share') xdg_aj_home = os.path.join(xdg_data_home, 'autojump') data_path = os.path.join(xdg_aj_home, 'autojump.txt') backup_path = os.path.join(xdg_aj_home, 'autojump.txt.bak') if os.path.exists(data_path): move_file(data_path, config['data_path']) if os.path.exists(backup_path): move_file(backup_path, config['backup_path']) # cleanup shutil.rmtree(xdg_aj_home) if len(os.listdir(xdg_data_home)) == 0: shutil.rmtree(xdg_data_home)
def migrate_osx_xdg_data(config): """ Older versions incorrectly used Linux XDG_DATA_HOME paths on OS X. This migrates autojump files from ~/.local/share/autojump to ~/Library/autojump """ assert is_osx(), "This function should only be run on OS X." xdg_data_home = os.path.join(os.path.expanduser("~"), ".local", "share") xdg_aj_home = os.path.join(xdg_data_home, "autojump") data_path = (os.path.join(xdg_aj_home, "autojump.txt"),) backup_path = (os.path.join(xdg_aj_home, "autojump.txt.bak"),) if os.path.exists(data_path): move_file(data_path, config["data_path"]) if os.path.exists(backup_path): move_file(backup_path, config["backup_path"]) # cleanup shutil.rmtree(xdg_aj_home) if len(os.listdir(xdg_data_home)) == 0: shutil.rmtree(xdg_data_home)
def load_backup(config): if os.path.exists(config['backup_path']): move_file(config['backup_path'], config['data_path']) return load(config) return {}