-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar-by-size.py
41 lines (34 loc) · 1.19 KB
/
tar-by-size.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /usr/bin/env python3
from pathlib import Path
import os
import click
import tarfile
from itertools import count
def tarfiles_gen(prefix):
for i in count():
tar = tarfile.open(Path(prefix + "." + str(i) + ".tar"), mode="w")
yield tar
tar.close()
@click.command()
@click.option("--folder", "-f", type=click.Path(exists=True), required=True)
@click.option("--target-prefix", "-t", type=str, required=True)
@click.option("--max-size", "-s", type=int, required=True, help="Size in GB")
def main(folder, target_prefix, max_size):
folder = Path(folder)
max_size = max_size * 2**30
size = 0
tarfiles = tarfiles_gen(target_prefix)
tar = next(tarfiles)
for directory, _, files in os.walk(folder):
for file in files:
filepath = Path(directory, file)
size += filepath.stat().st_size
if size > max_size:
# rotate tarball
tar = next(tarfiles)
size = 0
click.echo(f"Rolled tarball. Now writing into {tar.name}")
tar.add(filepath, filepath.relative_to(folder))
tar.close()
if __name__ == "__main__":
main()