Skip to content

Commit

Permalink
github-release: Download to a temp file and rename over the original
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Dec 3, 2023
1 parent c1319f1 commit 68af63e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
8 changes: 4 additions & 4 deletions github-release/sync.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
set -euo pipefail
[[ $DEBUG = true ]] && set -x
#!/bin/sh

python3 /usr/local/lib/tunasync/github-release.py --working-dir "$TO"
[ "$DEBUG" = "true" ] && set -x

exec python3 /usr/local/lib/tunasync/github-release.py --working-dir "$TO"
39 changes: 23 additions & 16 deletions github-release/tunasync/github-release.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3

import os
import sys
import threading
Expand All @@ -7,6 +8,7 @@
from pathlib import Path
from datetime import datetime


# Set BindIP
# https://stackoverflow.com/a/70772914
APTSYNC_BINDIP = os.getenv("BIND_ADDRESS", "")
Expand Down Expand Up @@ -62,28 +64,35 @@ def sizeof_fmt(num, suffix='iB'):
num /= 1024.0
return "%.2f%s%s" % (num, 'Y', suffix)

# wrap around requests.get to use token if available


# wrap around requests.get to use token if available
def github_get(*args, **kwargs):
headers = kwargs['headers'] if 'headers' in kwargs else {}
headers = kwargs.get('headers', {})
if 'GITHUB_TOKEN' in os.environ:
headers['Authorization'] = 'token {}'.format(
os.environ['GITHUB_TOKEN'])
headers['Authorization'] = 'token {}'.format(os.environ['GITHUB_TOKEN'])
kwargs['headers'] = headers
return requests.get(*args, **kwargs)


def do_download(remote_url: str, dst_file: Path, remote_ts: float):
# NOTE the stream=True parameter below
with github_get(remote_url, stream=True) as r:
r.raise_for_status()
with open(dst_file, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024**2):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
# f.flush()
os.utime(dst_file, (remote_ts, remote_ts))
tmpfile = dst_file.parent / (dst_file.name + '.downloading')
try:
# NOTE the stream=True parameter below
with github_get(remote_url, stream=True) as r:
r.raise_for_status()
with open(tmpfile, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024**2):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
# f.flush()
os.utime(tmpfile, (remote_ts, remote_ts))
except Exception as e:
raise
else:
tmpfile.rename(dst_file)
finally:
if tmpfile.is_file():
tmpfile.unlink()


def downloading_worker(q):
Expand All @@ -100,8 +109,6 @@ def downloading_worker(q):
do_download(url, dst_file, updated)
except Exception:
print("Failed to download", url, flush=True)
if dst_file.is_file():
dst_file.unlink()

q.task_done()

Expand Down

0 comments on commit 68af63e

Please sign in to comment.