debug the posting, clean up a bit

This commit is contained in:
Mirek Kratochvil 2025-10-12 14:02:41 +02:00
parent c0b4d8692b
commit 18dea37595

View file

@ -13,9 +13,9 @@ import time
VERSION = "0.1.0" VERSION = "0.1.0"
DPASTE_URL = "https://dpaste.com" DPASTE_URL = "https://dpaste.com" # TODO any good way to parametrize this?
STATUS_LENGTH_LIMIT = 400 STATUS_LENGTH_LIMIT = 400 # TODO obtain from instance
def trace(x): def trace(x):
@ -45,17 +45,19 @@ def do_post_status(args, body, parent=None, optional=None):
if optional if optional
else "" else ""
) )
data = {status: st, visibility: "unlisted"} data = {"status": st, "visibility": "direct"} # TODO parametrize direct
if parent: if parent:
data["in_reply_to_id"] = parent data["in_reply_to_id"] = parent
r = requests.post( r = requests.post(
args.instance_url + "/api/v1/statuses", data=data, headers=headers args.instance_url + "/api/v1/statuses", data=data, headers=headers
) )
if r.status_code != 200: if r.status_code != 200:
raise ("mastodon status posting failed ({r.status_code})") raise ("mastodon status posting failed ({r.status_code})")
return r.json.id rj = r.json()
return (rj["id"], rj["url"])
def do_pastebin_file(file): def do_pastebin_file(file):
@ -66,16 +68,16 @@ def do_pastebin_file(file):
r = requests.post( r = requests.post(
DPASTE_URL + "/api/v2/", DPASTE_URL + "/api/v2/",
data={ data={
"contents": open(file, "r").read(), "content": open(file, "r").read(),
"syntax": "diff", "syntax": "diff",
"title": os.path.basename(file), "title": os.path.basename(file),
"expiry_days": 30, "expiry_days": 1, # TODO remove after testing
}, },
headers={"User-agent": f"patchodon v{VERSION}"}, headers={"User-agent": f"patchodon v{VERSION}"},
) )
if r.status_code != 201: if r.status_code != 201:
raise (f"dpaste POST failed for `{file}'") raise (f"dpaste POST failed for `{file}'")
sleep(1.1) time.sleep(1.1)
return r.headers["location"] + ".txt" return r.headers["location"] + ".txt"
@ -83,21 +85,34 @@ def split_off_diff(s):
return s.split("\ndiff --git ")[0] return s.split("\ndiff --git ")[0]
def mapl(f, xs):
return list(map(f, xs))
def mayline(s):
if s:
return s + "\n"
else:
return ""
def do_post(args): def do_post(args):
files = args.patchfile files = args.patchfile
if not files: if not files:
trace("reading patchfile series from stdin") trace("reading patchfile series from stdin")
files = sys.stdin.readlines().map(lambda x: x.rstrip(chars="\n")) files = mapl(lambda x: x.rstrip(chars="\n"), sys.stdin.readlines())
n_patches = len(files) n_patches = len(files)
hashes = files.map(lambda x: hashlib.sha1(open(x, "r").read()).hexdigest()) hashes = mapl(
short_hashes = hashes.map(lambda x: x[0:8]) lambda x: hashlib.sha1(open(x, "r").read().encode()).hexdigest(), files
full_hash = hashlib.sha1(hashes.join(" ")).hexdigest() )
paste_raw_urls = map(do_pastebin_file, files) short_hashes = mapl(lambda x: x[0:8], hashes)
full_hash = hashlib.sha1(" ".join(hashes).encode()).hexdigest()
paste_raw_urls = mapl(do_pastebin_file, files)
trace("posting the header...") trace("posting the header...")
parent_post_id, url = do_post_status( parent_post_id, url = do_post_status(
args, args,
args.subject f"{mayline(args.recipient)} {mayline(args.subject)}\n"
+ f"\n[patchodon hash {full_hash} shorts {short_hashes.join(' ')}]", f"[patchodon hash {full_hash} / {' '.join(short_hashes)}]",
) )
for fn, pst, hsh, series in zip( for fn, pst, hsh, series in zip(
files, paste_raw_urls, hashes, range(n_patches) files, paste_raw_urls, hashes, range(n_patches)
@ -105,7 +120,9 @@ def do_post(args):
trace(f"posting patch {series+1}/{n_patches}...") trace(f"posting patch {series+1}/{n_patches}...")
parent_post_id, _ = do_post_status( parent_post_id, _ = do_post_status(
args, args,
f"[patchodon {series+1}/{n_patches} {hsh}\n{pst}\n", f"{mayline(args.recipient)}"
f"[patchodon {series+1}/{n_patches} {hsh}]\n"
f"{pst}\n",
parent=parent_post_id, parent=parent_post_id,
optional=split_off_diff(open(fn, "r").read()), optional=split_off_diff(open(fn, "r").read()),
) )
@ -132,8 +149,8 @@ def main():
group.add_argument( group.add_argument(
"--debug-api-token", "--debug-api-token",
help=( help=(
"specify the API token (not very secure, good for debugging" "specify the API token on command line (not very secure,"
" only)" " good for debugging only)"
), ),
) )
group.add_argument( group.add_argument(
@ -144,15 +161,29 @@ def main():
) )
post.add_argument( post.add_argument(
"-i", "--instance-url", help="mastodon instance URL to post to" "-i",
"--instance-url",
help=(
"mastodon instance URL to post to, such as"
" `https://mastodon.example/'"
),
)
post.add_argument(
"-r",
"--recipient",
default=None,
help=(
"user tag to prepend to all posted statuses (required esp. for"
" direct sending of statuses))"
),
) )
post.add_argument( post.add_argument(
"-s", "-s",
"--subject", "--subject",
default="", default=None,
help=( help=(
"start of the initial post (e.g. to @tag someone and name the" "start text of the initial post (e.g. to @tag someone and name"
" project and topic)" " the project and topic)"
), ),
) )
post.add_argument( post.add_argument(