Remove psutil dependency, and start using a lock file instead

This commit is contained in:
Micah Lee 2020-04-06 20:05:48 -07:00
parent 49fb1b334b
commit ea1b430aea
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
4 changed files with 15 additions and 52 deletions

View File

@ -24,7 +24,6 @@ import platform
import argparse import argparse
import signal import signal
import json import json
import psutil
from PyQt5 import QtCore, QtWidgets from PyQt5 import QtCore, QtWidgets
from onionshare.common import Common from onionshare.common import Common
@ -126,31 +125,11 @@ def main():
sys.exit() sys.exit()
# Is there another onionshare-gui running? # Is there another onionshare-gui running?
existing_pid = None if os.path.exists(common.gui.lock_filename):
for proc in psutil.process_iter(attrs=["pid", "name", "cmdline"]): with open(common.gui.lock_filename, "r") as f:
if proc.info["pid"] == os.getpid(): existing_pid = int(f.read())
continue
if proc.info["name"] == "onionshare-gui" and proc.status() != "zombie": print(f"Opening tab in existing OnionShare window (pid {existing_pid})")
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']})")
# Make an event for the existing OnionShare window # Make an event for the existing OnionShare window
if filenames: if filenames:
@ -163,6 +142,11 @@ def main():
f.write(json.dumps(obj) + "\n") f.write(json.dumps(obj) + "\n")
return return
else:
# Write the lock file
with open(common.gui.lock_filename, "w") as f:
f.write(f"{os.getpid()}\n")
# Launch the gui # Launch the gui
main_window = MainWindow(common, filenames) main_window = MainWindow(common, filenames)
@ -173,6 +157,7 @@ def main():
# Clean up when app quits # Clean up when app quits
def shutdown(): def shutdown():
main_window.cleanup() main_window.cleanup()
os.remove(common.gui.lock_filename)
qtapp.aboutToQuit.connect(shutdown) qtapp.aboutToQuit.connect(shutdown)

View File

@ -46,7 +46,10 @@ class GuiCommon:
# Start the Onion # Start the Onion
self.onion = Onion(common) 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") self.events_dir = os.path.join(self.common.build_data_dir(), "events")
if not os.path.exists(self.events_dir): if not os.path.exists(self.events_dir):
os.makedirs(self.events_dir, 0o700, True) os.makedirs(self.events_dir, 0o700, True)

26
poetry.lock generated
View File

@ -220,17 +220,6 @@ version = ">=0.12"
[package.extras] [package.extras]
dev = ["pre-commit", "tox"] 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]] [[package]]
category = "dev" category = "dev"
description = "library with cross-python path, ini-parsing, io, code, log facilities" 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"] testing = ["jaraco.itertools", "func-timeout"]
[metadata] [metadata]
content-hash = "7a4a60fcd1c6d6345fec30e7fbe87681288929468d069896acad0f23b9e90f5a" content-hash = "e341ce712d9198499f5e816edc1a62d75f5a913f22880ed631a8698c78d88fb7"
python-versions = "^3.7" python-versions = "^3.7"
[metadata.files] [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-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"},
{file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"}, {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 = [ py = [
{file = "py-1.8.1-py2.py3-none-any.whl", hash = "sha256:c20fdd83a5dbc0af9efd622bee9a5564e278f6380fffcacc43ba6f43db2813b0"}, {file = "py-1.8.1-py2.py3-none-any.whl", hash = "sha256:c20fdd83a5dbc0af9efd622bee9a5564e278f6380fffcacc43ba6f43db2813b0"},
{file = "py-1.8.1.tar.gz", hash = "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa"}, {file = "py-1.8.1.tar.gz", hash = "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa"},

View File

@ -28,7 +28,6 @@ requests = "*"
stem = "*" stem = "*"
urllib3 = "*" urllib3 = "*"
Werkzeug = "*" Werkzeug = "*"
psutil = "*"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
atomicwrites = "*" atomicwrites = "*"