Ejemplo n.º 1
0
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:
        temp = NamedTemporaryFile(delete=False)
        # 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'])
Ejemplo n.º 2
0
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'])
Ejemplo n.º 3
0
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'])
Ejemplo n.º 4
0
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():
                weight_with_age = weight * 0.999
                slash_only_path = re.sub(re.escape(os.sep), "/", path)
                f.write(unico("%s\t%s\n" % (weight_with_age, slash_only_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"])
Ejemplo n.º 5
0
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"])
Ejemplo n.º 6
0
    def test_create_dir(self):
        path = self.get_random_path()
        create_dir(path)
        assert_true(os.path.exists(path))

        # should not raise OSError if directory already exists
        create_dir(path)
        assert_true(os.path.exists(path))