From d2517681af2f5067123906561e616e7f8b2b1d90 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Mon, 6 Apr 2020 20:05:48 -0700 Subject: [PATCH] Remove psutil dependency, and start using a lock file instead --- onionshare_gui/__init__.py | 35 ++++++++++------------------------- onionshare_gui/gui_common.py | 5 ++++- poetry.lock | 26 +------------------------- pyproject.toml | 1 - 4 files changed, 15 insertions(+), 52 deletions(-) diff --git a/onionshare_gui/__init__.py b/onionshare_gui/__init__.py index 90b6d7ac..9f8ca832 100644 --- a/onionshare_gui/__init__.py +++ b/onionshare_gui/__init__.py @@ -24,7 +24,6 @@ import platform import argparse import signal import json -import psutil from PyQt5 import QtCore, QtWidgets from onionshare.common import Common @@ -126,31 +125,11 @@ def main(): sys.exit() # Is there another onionshare-gui running? - existing_pid = None - for proc in psutil.process_iter(attrs=["pid", "name", "cmdline"]): - if proc.info["pid"] == os.getpid(): - continue + if os.path.exists(common.gui.lock_filename): + with open(common.gui.lock_filename, "r") as f: + existing_pid = int(f.read()) - if proc.info["name"] == "onionshare-gui" and proc.status() != "zombie": - existing_pid = proc.info["pid"] - break - else: - # Dev mode onionshare? - if proc.info["cmdline"] and len(proc.info["cmdline"]) >= 2: - if ( - ( - os.path.basename(proc.info["cmdline"][0]).lower() == "python" - or os.path.basename(proc.info["cmdline"][0]).lower() - == "python3" - ) - and os.path.basename(proc.info["cmdline"][1]) == "onionshare-gui" - and proc.status() != "zombie" - ): - existing_pid = proc.info["pid"] - break - - if existing_pid: - print(f"Opening tab in existing OnionShare window (pid {proc.info['pid']})") + print(f"Opening tab in existing OnionShare window (pid {existing_pid})") # Make an event for the existing OnionShare window if filenames: @@ -163,6 +142,11 @@ def main(): f.write(json.dumps(obj) + "\n") return + else: + # Write the lock file + with open(common.gui.lock_filename, "w") as f: + f.write(f"{os.getpid()}\n") + # Launch the gui main_window = MainWindow(common, filenames) @@ -173,6 +157,7 @@ def main(): # Clean up when app quits def shutdown(): main_window.cleanup() + os.remove(common.gui.lock_filename) qtapp.aboutToQuit.connect(shutdown) diff --git a/onionshare_gui/gui_common.py b/onionshare_gui/gui_common.py index 4381545e..18edded1 100644 --- a/onionshare_gui/gui_common.py +++ b/onionshare_gui/gui_common.py @@ -46,7 +46,10 @@ class GuiCommon: # Start the Onion self.onion = Onion(common) - # Directory to watch for events + # Lock filename + self.lock_filename = os.path.join(self.common.build_data_dir(), "lock") + + # Events filenames self.events_dir = os.path.join(self.common.build_data_dir(), "events") if not os.path.exists(self.events_dir): os.makedirs(self.events_dir, 0o700, True) diff --git a/poetry.lock b/poetry.lock index cc053fa4..294522d6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -220,17 +220,6 @@ version = ">=0.12" [package.extras] dev = ["pre-commit", "tox"] -[[package]] -category = "main" -description = "Cross-platform lib for process and system monitoring in Python." -name = "psutil" -optional = false -python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "5.7.0" - -[package.extras] -enum = ["enum34"] - [[package]] category = "dev" description = "library with cross-python path, ini-parsing, io, code, log facilities" @@ -429,7 +418,7 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] testing = ["jaraco.itertools", "func-timeout"] [metadata] -content-hash = "7a4a60fcd1c6d6345fec30e7fbe87681288929468d069896acad0f23b9e90f5a" +content-hash = "e341ce712d9198499f5e816edc1a62d75f5a913f22880ed631a8698c78d88fb7" python-versions = "^3.7" [metadata.files] @@ -547,19 +536,6 @@ pluggy = [ {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"}, {file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"}, ] -psutil = [ - {file = "psutil-5.7.0-cp27-none-win32.whl", hash = "sha256:298af2f14b635c3c7118fd9183843f4e73e681bb6f01e12284d4d70d48a60953"}, - {file = "psutil-5.7.0-cp27-none-win_amd64.whl", hash = "sha256:75e22717d4dbc7ca529ec5063000b2b294fc9a367f9c9ede1f65846c7955fd38"}, - {file = "psutil-5.7.0-cp35-cp35m-win32.whl", hash = "sha256:f344ca230dd8e8d5eee16827596f1c22ec0876127c28e800d7ae20ed44c4b310"}, - {file = "psutil-5.7.0-cp35-cp35m-win_amd64.whl", hash = "sha256:e2d0c5b07c6fe5a87fa27b7855017edb0d52ee73b71e6ee368fae268605cc3f5"}, - {file = "psutil-5.7.0-cp36-cp36m-win32.whl", hash = "sha256:a02f4ac50d4a23253b68233b07e7cdb567bd025b982d5cf0ee78296990c22d9e"}, - {file = "psutil-5.7.0-cp36-cp36m-win_amd64.whl", hash = "sha256:1413f4158eb50e110777c4f15d7c759521703bd6beb58926f1d562da40180058"}, - {file = "psutil-5.7.0-cp37-cp37m-win32.whl", hash = "sha256:d008ddc00c6906ec80040d26dc2d3e3962109e40ad07fd8a12d0284ce5e0e4f8"}, - {file = "psutil-5.7.0-cp37-cp37m-win_amd64.whl", hash = "sha256:73f35ab66c6c7a9ce82ba44b1e9b1050be2a80cd4dcc3352cc108656b115c74f"}, - {file = "psutil-5.7.0-cp38-cp38-win32.whl", hash = "sha256:60b86f327c198561f101a92be1995f9ae0399736b6eced8f24af41ec64fb88d4"}, - {file = "psutil-5.7.0-cp38-cp38-win_amd64.whl", hash = "sha256:d84029b190c8a66a946e28b4d3934d2ca1528ec94764b180f7d6ea57b0e75e26"}, - {file = "psutil-5.7.0.tar.gz", hash = "sha256:685ec16ca14d079455892f25bd124df26ff9137664af445563c1bd36629b5e0e"}, -] py = [ {file = "py-1.8.1-py2.py3-none-any.whl", hash = "sha256:c20fdd83a5dbc0af9efd622bee9a5564e278f6380fffcacc43ba6f43db2813b0"}, {file = "py-1.8.1.tar.gz", hash = "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa"}, diff --git a/pyproject.toml b/pyproject.toml index a5bcf469..fa83d133 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,6 @@ requests = "*" stem = "*" urllib3 = "*" Werkzeug = "*" -psutil = "*" [tool.poetry.dev-dependencies] atomicwrites = "*"