Skip to content

Commit e82df1e

Browse files
committed
scripts: Add upload_bugtool.py
Signed-off-by: Tu Dinh <[email protected]>
1 parent ae6b8f9 commit e82df1e

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Upload a bugtool archive to Nextcloud.
5+
# This script should run on both Python 2 and 3.
6+
7+
from __future__ import print_function
8+
9+
import argparse
10+
import getpass
11+
import os
12+
import subprocess
13+
import sys
14+
15+
try:
16+
from itertools import zip_longest
17+
except ImportError:
18+
from itertools import izip_longest as zip_longest
19+
20+
try:
21+
import urllib.parse as urlparse
22+
from urllib.parse import quote as urlquote
23+
except ImportError:
24+
import urlparse
25+
from urllib import quote as urlquote
26+
27+
try:
28+
from xcp.branding import PLATFORM_VERSION
29+
except ImportError:
30+
PLATFORM_VERSION = "9999" # failsafe value, assume dev build
31+
32+
33+
def version_lt(a, b):
34+
for ai, bi in zip_longest(a, b, fillvalue=0):
35+
if ai < bi:
36+
return True
37+
elif ai > bi:
38+
return False
39+
return False
40+
41+
42+
def parse_share_link(share_link):
43+
"""
44+
Parses the Nextcloud/Owncloud share link to extract the base URL and token.
45+
Args:
46+
share_link (str): The share link URL.
47+
Returns:
48+
tuple: A tuple containing (base_url, token).
49+
"""
50+
parsed_url = urlparse.urlparse(share_link)._replace(query="", fragment="")
51+
path_segments = parsed_url.path.split("/")
52+
53+
if path_segments[-3:-1] == ["index.php", "s"]:
54+
base_url = urlparse.urljoin(share_link, "../..")
55+
token = path_segments[-1]
56+
elif path_segments[-2] == "s":
57+
base_url = urlparse.urljoin(share_link, "..")
58+
token = path_segments[-1]
59+
else:
60+
raise ValueError(
61+
"Invalid share link format. Could not determine base URL from: %s"
62+
% share_link
63+
)
64+
65+
return base_url, token
66+
67+
68+
def upload(base_url, folder_token, password, file):
69+
print("Uploading %s" % file, file=sys.stderr)
70+
71+
upload_filename = urlquote(os.path.basename(file))
72+
target_url = urlparse.urljoin(base_url, "public.php/webdav/%s" % upload_filename)
73+
cred = "%s:%s" % (folder_token, password)
74+
75+
# fmt: off
76+
curl_args = [
77+
"curl",
78+
"--show-error",
79+
"--fail",
80+
"--upload-file", file,
81+
"--user", cred,
82+
"--header", "X-Requested-With: XMLHttpRequest",
83+
target_url,
84+
]
85+
# fmt: on
86+
platform_version = tuple(int(x) for x in PLATFORM_VERSION.split("."))
87+
if version_lt(platform_version, (3, 3, 0)):
88+
curl_args += [
89+
"--ciphers",
90+
"ECDHE-RSA-AES256-SHA384,ECDHE-RSA-AES256-GCM-SHA384,AES256-SHA256,AES128-SHA256,ECDHE-ECDSA-AES128-GCM-SHA256",
91+
]
92+
93+
subprocess.check_call(curl_args, stdout=sys.stdout, stderr=sys.stderr)
94+
95+
96+
def main():
97+
parser = argparse.ArgumentParser(
98+
description="Uploads a file to a Nextcloud/Owncloud shared drop folder."
99+
)
100+
parser.add_argument(
101+
"-p", action="store_true", help="Prompt for password for the share link."
102+
)
103+
parser.add_argument("share_link", help="The Nextcloud/Owncloud share link URL.")
104+
parser.add_argument("files", nargs="+", help="Files to upload.")
105+
106+
args = parser.parse_args()
107+
108+
if any(not os.path.exists(file) in file for file in args.files):
109+
print("Error: Given file path does not exist", file=sys.stderr)
110+
sys.exit(1)
111+
112+
password = ""
113+
if args.p:
114+
if sys.stdin.isatty():
115+
password = getpass.getpass("Enter password for share link: ")
116+
else:
117+
print(
118+
"Error: Cannot prompt for password when not on a TTY.", file=sys.stderr
119+
)
120+
sys.exit(1)
121+
122+
base_url, folder_token = parse_share_link(args.share_link)
123+
124+
for file in args.files:
125+
upload(base_url, folder_token, password, file)
126+
127+
128+
if __name__ == "__main__":
129+
main()

0 commit comments

Comments
 (0)