From fc658e5b9e182cbca4d36e08f6aa27436e9dde80 Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 20 Oct 2023 23:41:58 +0000 Subject: [PATCH] utcfromtimestamp was deprecated and nobody told me, not even the deprecationwarning that got silently generated burning 20~30% of all CPU-time without actually displaying it anywhere, nice python 3.12.0 is now only 5% slower than 3.11.6 also fixes some other, less-performance-fatal deprecations --- .vscode/launch.json | 4 ++++ .vscode/launch.py | 2 +- .vscode/tasks.json | 1 + bin/hooks/msg-log.py | 12 ++++++++++-- bin/hooks/notify2.py | 5 +++-- bin/hooks/xiu-sha.py | 9 ++++++--- bin/partyfuse.py | 5 +++-- bin/partyjournal.py | 8 ++++---- copyparty/__main__.py | 5 ++++- copyparty/authsrv.py | 3 ++- copyparty/httpcli.py | 3 ++- copyparty/sutil.py | 4 ++-- copyparty/svchub.py | 19 +++++++++++++------ copyparty/util.py | 24 ++++++++++++++++++++++-- scripts/sfx.py | 5 ++++- tests/util.py | 7 +++++-- 16 files changed, 86 insertions(+), 30 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 45f03dda..c611c5a1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,6 +9,10 @@ "console": "integratedTerminal", "cwd": "${workspaceFolder}", "justMyCode": false, + "env": { + "PYDEVD_DISABLE_FILE_VALIDATION": "1", + "PYTHONWARNINGS": "always", //error + }, "args": [ //"-nw", "-ed", diff --git a/.vscode/launch.py b/.vscode/launch.py index 2e9e20e7..0d4cbf6c 100755 --- a/.vscode/launch.py +++ b/.vscode/launch.py @@ -41,7 +41,7 @@ if sfx: argv = [sys.executable, sfx] + argv sp.check_call(argv) elif re.search(" -j ?[0-9]", " ".join(argv)): - argv = [sys.executable, "-m", "copyparty"] + argv + argv = [sys.executable, "-Wa", "-m", "copyparty"] + argv sp.check_call(argv) else: sys.path.insert(0, os.getcwd()) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index b632e2b8..35ce1994 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -11,6 +11,7 @@ "type": "shell", "command": "${config:python.pythonPath}", "args": [ + "-Wa", //-We ".vscode/launch.py" ] } diff --git a/bin/hooks/msg-log.py b/bin/hooks/msg-log.py index bd419466..c6330533 100755 --- a/bin/hooks/msg-log.py +++ b/bin/hooks/msg-log.py @@ -7,7 +7,11 @@ import json import os import sys import time -from datetime import datetime + +try: + from datetime import datetime, timezone +except: + from datetime import datetime """ @@ -96,7 +100,11 @@ def main(argv=None): msg_info = json.loads(sys.argv[1]) # print(msg_info) - dt = datetime.utcfromtimestamp(msg_info["at"]) + try: + dt = datetime.fromtimestamp(msg_info["at"], timezone.utc) + except: + dt = datetime.utcfromtimestamp(msg_info["at"]) + msg_info["datetime"] = dt.strftime("%Y-%m-%d, %H:%M:%S") msg_text = TEMPLATE % msg_info diff --git a/bin/hooks/notify2.py b/bin/hooks/notify2.py index 87b80c1f..a68e6924 100755 --- a/bin/hooks/notify2.py +++ b/bin/hooks/notify2.py @@ -4,7 +4,7 @@ import json import os import sys import subprocess as sp -from datetime import datetime +from datetime import datetime, timezone from plyer import notification @@ -43,7 +43,8 @@ def main(): fp = inf["ap"] sz = humansize(inf["sz"]) dp, fn = os.path.split(fp) - mt = datetime.utcfromtimestamp(inf["mt"]).strftime("%Y-%m-%d %H:%M:%S") + dt = datetime.fromtimestamp(inf["mt"], timezone.utc) + mt = dt.strftime("%Y-%m-%d %H:%M:%S") msg = f"{fn} ({sz})\nšŸ“ {dp}" title = "File received" diff --git a/bin/hooks/xiu-sha.py b/bin/hooks/xiu-sha.py index d8a96473..f2441965 100755 --- a/bin/hooks/xiu-sha.py +++ b/bin/hooks/xiu-sha.py @@ -3,7 +3,7 @@ import hashlib import json import sys -from datetime import datetime +from datetime import datetime, timezone _ = r""" @@ -43,8 +43,11 @@ except: return p +UTC = timezone.utc + + def humantime(ts): - return datetime.utcfromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S") + return datetime.fromtimestamp(ts, UTC).strftime("%Y-%m-%d %H:%M:%S") def find_files_root(inf): @@ -96,7 +99,7 @@ def main(): ret.append("# {} files, {} bytes total".format(len(inf), total_sz)) ret.append("") - ftime = datetime.utcnow().strftime("%Y-%m%d-%H%M%S.%f") + ftime = datetime.now(UTC).strftime("%Y-%m%d-%H%M%S.%f") fp = "{}xfer-{}.sha512".format(inf[0]["ap"][:di], ftime) with open(fsenc(fp), "wb") as f: f.write("\n".join(ret).encode("utf-8", "replace")) diff --git a/bin/partyfuse.py b/bin/partyfuse.py index 53261d96..365067e1 100755 --- a/bin/partyfuse.py +++ b/bin/partyfuse.py @@ -46,12 +46,13 @@ import traceback import http.client # py2: httplib import urllib.parse import calendar -from datetime import datetime +from datetime import datetime, timezone from urllib.parse import quote_from_bytes as quote from urllib.parse import unquote_to_bytes as unquote WINDOWS = sys.platform == "win32" MACOS = platform.system() == "Darwin" +UTC = timezone.utc info = log = dbg = None @@ -176,7 +177,7 @@ class RecentLog(object): def put(self, msg): msg = "{:10.6f} {} {}\n".format(time.time() % 900, rice_tid(), msg) if self.f: - fmsg = " ".join([datetime.utcnow().strftime("%H%M%S.%f"), str(msg)]) + fmsg = " ".join([datetime.now(UTC).strftime("%H%M%S.%f"), str(msg)]) self.f.write(fmsg.encode("utf-8")) with self.mtx: diff --git a/bin/partyjournal.py b/bin/partyjournal.py index 65d0cc3b..c580d59a 100755 --- a/bin/partyjournal.py +++ b/bin/partyjournal.py @@ -20,12 +20,13 @@ import sys import base64 import sqlite3 import argparse -from datetime import datetime +from datetime import datetime, timezone from urllib.parse import quote_from_bytes as quote from urllib.parse import unquote_to_bytes as unquote FS_ENCODING = sys.getfilesystemencoding() +UTC = timezone.utc class APF(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): @@ -155,11 +156,10 @@ th { link = txt.decode("utf-8")[4:] sz = "{:,}".format(sz) + dt = datetime.fromtimestamp(at if at > 0 else mt, UTC) v = [ w[:16], - datetime.utcfromtimestamp(at if at > 0 else mt).strftime( - "%Y-%m-%d %H:%M:%S" - ), + dt.strftime("%Y-%m-%d %H:%M:%S"), sz, imap.get(ip, ip), ] diff --git a/copyparty/__main__.py b/copyparty/__main__.py index 8e8cb6c5..1e259432 100755 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -186,7 +186,10 @@ def init_E(E: EnvParams) -> None: with open_binary("copyparty", "z.tar") as tgz: with tarfile.open(fileobj=tgz) as tf: - tf.extractall(tdn) # nosec (archive is safe) + try: + tf.extractall(tdn, filter="tar") + except TypeError: + tf.extractall(tdn) # nosec (archive is safe) return tdn diff --git a/copyparty/authsrv.py b/copyparty/authsrv.py index 8a98dbd6..f3c1ca2f 100644 --- a/copyparty/authsrv.py +++ b/copyparty/authsrv.py @@ -23,6 +23,7 @@ from .util import ( UNPLICATIONS, ODict, Pebkac, + UTC, absreal, afsenc, get_df, @@ -215,7 +216,7 @@ class Lim(object): if self.rot_re.search(path.replace("\\", "/")): return path, "" - suf = datetime.utcnow().strftime(self.rotf) + suf = datetime.now(UTC).strftime(self.rotf) if path: path += "/" diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 455cb476..26652f1a 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -42,6 +42,7 @@ from .util import ( MultipartParser, ODict, Pebkac, + UTC, UnrecvEOF, absreal, alltrace, @@ -3992,7 +3993,7 @@ class HttpCli(object): margin = "-" sz = inf.st_size - zd = datetime.utcfromtimestamp(linf.st_mtime) + zd = datetime.fromtimestamp(linf.st_mtime, UTC) dt = "%04d-%02d-%02d %02d:%02d:%02d" % ( zd.year, zd.month, diff --git a/copyparty/sutil.py b/copyparty/sutil.py index 61c1396b..784e122c 100644 --- a/copyparty/sutil.py +++ b/copyparty/sutil.py @@ -8,7 +8,7 @@ from datetime import datetime from .__init__ import CORES from .bos import bos from .th_cli import ThumbCli -from .util import vjoin +from .util import UTC, vjoin if True: # pylint: disable=using-constant-test from typing import Any, Generator, Optional @@ -108,7 +108,7 @@ def errdesc(errors: list[tuple[str, str]]) -> tuple[dict[str, Any], list[str]]: tf_path = tf.name tf.write("\r\n".join(report).encode("utf-8", "replace")) - dt = datetime.utcnow().strftime("%Y-%m%d-%H%M%S") + dt = datetime.now(UTC).strftime("%Y-%m%d-%H%M%S") bos.chmod(tf_path, 0o444) return { diff --git a/copyparty/svchub.py b/copyparty/svchub.py index 11c4100c..01d9df11 100644 --- a/copyparty/svchub.py +++ b/copyparty/svchub.py @@ -45,6 +45,7 @@ from .util import ( HLog, HMaccas, ODict, + UTC, alltrace, ansi_re, min_ex, @@ -484,7 +485,7 @@ class SvcHub(object): self.args.nc = min(self.args.nc, soft // 2) def _logname(self) -> str: - dt = datetime.utcnow() + dt = datetime.now(UTC) fn = str(self.args.lo) for fs in "YmdHMS": fs = "%" + fs @@ -733,7 +734,7 @@ class SvcHub(object): return with self.log_mutex: - zd = datetime.utcnow() + zd = datetime.now(UTC) ts = self.log_dfmt % ( zd.year, zd.month * 100 + zd.day, @@ -751,7 +752,7 @@ class SvcHub(object): self.logf.close() self._setup_logfile("") - dt = datetime.utcnow() + dt = datetime.now(UTC) # unix timestamp of next 00:00:00 (leap-seconds safe) day_now = dt.day @@ -759,14 +760,20 @@ class SvcHub(object): dt += timedelta(hours=12) dt = dt.replace(hour=0, minute=0, second=0) - self.next_day = calendar.timegm(dt.utctimetuple()) + try: + tt = dt.utctimetuple() + except: + # still makes me hella uncomfortable + tt = dt.timetuple() + + self.next_day = calendar.timegm(tt) def _log_enabled(self, src: str, msg: str, c: Union[int, str] = 0) -> None: """handles logging from all components""" with self.log_mutex: now = time.time() if now >= self.next_day: - dt = datetime.utcfromtimestamp(now) + dt = datetime.fromtimestamp(now, UTC) zs = "{}\n" if self.no_ansi else "\033[36m{}\033[0m\n" zs = zs.format(dt.strftime("%Y-%m-%d")) print(zs, end="") @@ -789,7 +796,7 @@ class SvcHub(object): else: msg = "%s%s\033[0m" % (c, msg) - zd = datetime.utcfromtimestamp(now) + zd = datetime.fromtimestamp(now, UTC) ts = self.log_efmt % ( zd.hour, zd.minute, diff --git a/copyparty/util.py b/copyparty/util.py index 5172dd04..6f4ad76d 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -25,7 +25,6 @@ import threading import time import traceback from collections import Counter -from datetime import datetime from email.utils import formatdate from ipaddress import IPv4Address, IPv4Network, IPv6Address, IPv6Network @@ -35,6 +34,27 @@ from .__init__ import ANYWIN, EXE, MACOS, PY2, TYPE_CHECKING, VT100, WINDOWS from .__version__ import S_BUILD_DT, S_VERSION from .stolen import surrogateescape +try: + from datetime import datetime, timezone + + UTC = timezone.utc +except: + from datetime import datetime, timedelta, tzinfo + + TD_ZERO = timedelta(0) + + class _UTC(tzinfo): + def utcoffset(self, dt): + return TD_ZERO + + def tzname(self, dt): + return "UTC" + + def dst(self, dt): + return TD_ZERO + + UTC = _UTC() + if sys.version_info >= (3, 7) or ( sys.version_info >= (3, 6) and platform.python_implementation() == "CPython" @@ -1131,7 +1151,7 @@ def stackmon(fp: str, ival: float, suffix: str) -> None: buf = lzma.compress(buf, preset=0) if "%" in fp: - dt = datetime.utcnow() + dt = datetime.now(UTC) for fs in "YmdHMS": fs = "%" + fs if fs in fp: diff --git a/scripts/sfx.py b/scripts/sfx.py index 88fac94e..0ece86e4 100644 --- a/scripts/sfx.py +++ b/scripts/sfx.py @@ -295,7 +295,10 @@ def unpack(): # the only possible input is a single tar.bz2 # which gets hardcoded into this script at build stage # skip 0 - tf.extractall(mine) + try: + tf.extractall(mine, filter="tar") + except TypeError: + tf.extractall(mine) os.remove(tar) diff --git a/tests/util.py b/tests/util.py index 77e4c4e3..82f7d26b 100644 --- a/tests/util.py +++ b/tests/util.py @@ -124,7 +124,7 @@ class Cfg(Namespace): ex = "df loris re_maxage rproxy rsp_jtr rsp_slp s_wr_slp theme themes turbo" ka.update(**{k: 0 for k in ex.split()}) - ex = "ah_alg bname doctitle favico html_head lg_sbf log_fk md_sbf mth name textfiles unlist vname R RS SR" + ex = "ah_alg bname doctitle favico html_head lg_sbf log_fk md_sbf name textfiles unlist vname R RS SR" ka.update(**{k: "" for k in ex.split()}) ex = "on403 on404 xad xar xau xban xbd xbr xbu xiu xm" @@ -141,8 +141,11 @@ class Cfg(Namespace): fk_salt="a" * 16, unpost=600, u2sort="s", + u2ts="c", + sort="href", mtp=[], - mte="a", + mte={"a": True}, + mth={}, lang="eng", logout=573, **ka