Skip to content

Commit

Permalink
fix(core): Fix self-update command error on file replace (#138)
Browse files Browse the repository at this point in the history
Close #138
  • Loading branch information
Toilal committed Dec 22, 2020
1 parent 140729f commit 97e4f81
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions ddb/feature/core/actions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]


Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 97e4f81

Please sign in to comment.