Skip to content

Commit dba6b92

Browse files
committed
Created a prototype of a cloudflare file uploader.
1 parent f69c4dc commit dba6b92

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import argparse
2+
import toolviper
3+
import json
4+
import shutil
5+
import pathlib
6+
import subprocess
7+
8+
parser = argparse.ArgumentParser(
9+
description=f"Upload test files to R2 cloudfare (Tokens are fetched from user Env)",
10+
formatter_class=argparse.RawTextHelpFormatter,
11+
)
12+
13+
parser.add_argument("file", type=str, help="File to be uploaded")
14+
15+
parser.add_argument(
16+
"-p",
17+
"--project",
18+
type=str,
19+
default="astrohack",
20+
help="To which project folder to upload in cloudfare",
21+
)
22+
23+
parser.add_argument(
24+
"-t",
25+
"--telescope",
26+
type=str,
27+
default="VLA",
28+
help="Telescope with which data was taken",
29+
)
30+
31+
parser.add_argument(
32+
"-m",
33+
"--observation-mode",
34+
type=str,
35+
default="Holography",
36+
help="Observation mode",
37+
)
38+
39+
parser.add_argument(
40+
"-d",
41+
"--data-type",
42+
type=str,
43+
default="CASA MS V2",
44+
help="Data type",
45+
)
46+
47+
parser.add_argument(
48+
"-u",
49+
"--update-version",
50+
action="store_true",
51+
default=False,
52+
help="Update manifest version",
53+
)
54+
55+
parser.add_argument(
56+
"-a",
57+
"--manifest-path",
58+
type=str,
59+
default=None,
60+
help="Path to manifest file, if None fetches it from toolviper",
61+
)
62+
63+
64+
def execute_shell_command(command):
65+
results = subprocess.run(command, capture_output=True, text=True)
66+
return results.stdout
67+
68+
69+
def download_manifest(args, manifest_filename=".manifest.json"):
70+
if args.manifest_path is None:
71+
downloaded_manifest = (
72+
toolviper.utils.data.__file__.split("__")[0]
73+
+ ".cloudflare/file.download.json"
74+
)
75+
toolviper.utils.data.update()
76+
shutil.copyfile(downloaded_manifest, manifest_filename)
77+
else:
78+
manifest_filename = args.manifest_path
79+
80+
with open(manifest_filename, "r") as manifest:
81+
manifest = json.load(manifest)
82+
return manifest_filename, manifest
83+
84+
85+
def prepare_data_for_upload(args):
86+
is_dir = pathlib.Path(args.file).is_dir()
87+
if is_dir:
88+
meta_name = args.file + ".zip"
89+
execute_shell_command(["zip", "-r", meta_name, args.file])
90+
else:
91+
meta_name = args.file
92+
93+
sha256sum = execute_shell_command(["sha256sum", meta_name]).split()[0]
94+
file_path = pathlib.Path(meta_name)
95+
file_size_bytes = file_path.stat().st_size
96+
97+
file_properties = {
98+
"file": meta_name,
99+
"path": args.project,
100+
"dtype": args.data_type,
101+
"telescope": args.telescope,
102+
"size": f"{file_size_bytes:d}",
103+
"mode": args.observation_mode,
104+
"hash": sha256sum,
105+
}
106+
return file_properties
107+
108+
109+
def update_manifest_version(manifest):
110+
current = manifest["version"]
111+
rev, major, minor = current.split(".")
112+
minor = f"{int(minor)+1}"
113+
manifest["version"] = f"{rev}.{major}.{minor}"
114+
print(f"Updating manifest version from {current} to {manifest['version']}")
115+
116+
117+
def add_data_to_manifest(args, manifest, file_properties, manifest_filename):
118+
if args.update_version:
119+
update_manifest_version(manifest)
120+
121+
manifest["metadata"][args.file] = file_properties
122+
123+
with open(manifest_filename, "w") as manifest_file:
124+
json.dump(manifest, manifest_file, indent=4)
125+
126+
127+
def main():
128+
args = parser.parse_args()
129+
130+
manifest_filename, manifest = download_manifest(args)
131+
132+
file_properties = prepare_data_for_upload(args)
133+
134+
add_data_to_manifest(
135+
args,
136+
manifest,
137+
file_properties,
138+
manifest_filename,
139+
)
140+
141+
return
142+
143+
144+
if __name__ == "__main__":
145+
main()

0 commit comments

Comments
 (0)