Skip to content

Commit

Permalink
Add rsync-size script
Browse files Browse the repository at this point in the history
  • Loading branch information
taoky committed Dec 23, 2024
1 parent ff2f241 commit 7fdeb5c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/ISSUE_TEMPLATE/01-mirror-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ body:
id: mirror_size
attributes:
label: 镜像大小 (Mirror size)
description: |
对于拥有 rsync 访问权限的上游,可以使用本仓库的 [rsync-size.py](https://github.com/ustclug/mirrorrequest/blob/master/rsync-size.py) 脚本获取仓库大小。
For upstreams with rsync access, you can use the [rsync-size.py](https://github.com/ustclug/mirrorrequest/blob/master/rsync-size.py) script in this repository to get the size.
validations:
required: false
- type: textarea
Expand Down
44 changes: 44 additions & 0 deletions rsync-size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0

import subprocess
import argparse


def human_readable_size(size: float) -> str:
suffix = ["B", "KB", "MB", "GB", "TB"]
level = 0
while size >= 10000:
level += 1
size /= 1024
return f"{size:.1f} {suffix[level]}"


def main(upstream: str) -> None:
p = subprocess.run(
["rsync", "-a", "--no-motd", "-r", "--list-only", upstream],
stdout=subprocess.PIPE,
text=True,
)
if p.returncode != 0:
print("Failed to run rsync")
exit(1)
total_size = 0
for line in p.stdout.splitlines():
line = line.strip()
splitted = line.split(maxsplit=4)
size = splitted[1].replace(",", "")
total_size += int(size)
print("Total size:", human_readable_size(total_size))


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Get total size from rsync upstream"
)
parser.add_argument(
"upstream", type=str, help="Upstream URL, like rsync://example.com/ubuntu/"
)
args = parser.parse_args()

main(args.upstream)

0 comments on commit 7fdeb5c

Please sign in to comment.