From 97e4f81d799fb5617a82dce8b9d533d5b489cae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Alvergnat?= Date: Tue, 22 Dec 2020 23:24:16 +0100 Subject: [PATCH] fix(core): Fix self-update command error on file replace (#138) Close #138 --- ddb/feature/core/actions.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ddb/feature/core/actions.py b/ddb/feature/core/actions.py index 0fb732f1..03731e2f 100644 --- a/ddb/feature/core/actions.py +++ b/ddb/feature/core/actions.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import os -import shutil import sys +import shutil from datetime import date from pathlib import Path from tempfile import NamedTemporaryFile @@ -135,6 +135,8 @@ def get_binary_path(): Get the binary path :return: """ + if config.cwd: + return os.path.join(config.cwd, sys.argv[0]) return sys.argv[0] @@ -448,18 +450,22 @@ def self_update_binary(github_repository, version): with requests.get(url, stream=True) as response: response.raise_for_status() - with NamedTemporaryFile() as tmp: + tmp = NamedTemporaryFile(delete=False) + try: if not progress_bar: content_length = int(response.headers['content-length']) - progress_bar = IncrementalBar('Downloading', max=content_length) + progress_bar = IncrementalBar('Downloading', max=content_length, suffix='%(percent)d%%') for chunk in response.iter_content(32 * 1024): progress_bar.next(len(chunk)) # pylint:disable=not-callable tmp.write(chunk) tmp.flush() + finally: + tmp.close() - binary_path = get_binary_destination_path(binary_path) - shutil.copyfile(tmp.name, binary_path) + binary_path = get_binary_destination_path(binary_path) + shutil.copymode(binary_path, tmp.name) + os.rename(tmp.name, binary_path) progress_bar.finish()