Skip to content

Commit

Permalink
removed psutil dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
davidaustinm committed Dec 5, 2024
1 parent 8509d1d commit 51f67ea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 58 deletions.
32 changes: 1 addition & 31 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 16 additions & 26 deletions prefig/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import click_log
import os
import sys
import psutil
import socket
import shutil
import subprocess
import time
Expand Down Expand Up @@ -391,26 +391,26 @@ def view(filename, ignore_annotations, restart_server, port):
else:
home_dir = os.path.expanduser('~')

active_port = find_active_server(port, restart_server)
if active_port is None:
subprocess.Popen(
# we formerly used psutil to do this, now we use socket
# to check to see if the port is in use
if not port_in_use(port):
process = subprocess.Popen(
['python3', '-m', 'http.server', port, '-d', home_dir]
)
active_port = port
active_port = port

if os.environ.get('CODESPACES'):
url_preamble = f"https://{os.environ.get('CODESPACE_NAME')}-{active_port}.{os.environ.get('GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN')}"
else:
url_preamble = f"http://localhost:{active_port}"

SECONDS = 2

# Does this figure have annotations
if ignore_annotations or not view_path.with_suffix('.xml').exists():
# Don't worry about annotations so just open the SVG in a browser
file_rel_path = os.path.relpath(view_path, home_dir)
url = f'{url_preamble}/{file_rel_path}'
log.info(f'Opening webpage in {SECONDS} seconds at {url}')
SECONDS = 1
log.info(f'Opening webpage in {SECONDS} second at {url}')
time.sleep(SECONDS)
webbrowser.open(url)
else:
Expand All @@ -427,24 +427,14 @@ def view(filename, ignore_annotations, restart_server, port):
webbrowser.open(url)
log.setLevel(log_level)


def find_active_server(port, restart):
for proc in psutil.process_iter():
if not proc.name().startswith('python'):
continue

def port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
connections = proc.connections()
if len(connections) > 0:
active_port = str(connections[0].laddr.port)
if restart or active_port != port:
log.info('Restarting server')
proc.terminate()
return None
return active_port
except:
pass
log.warning("Unable to restart server")
return None
s.bind(('127.0.0.1', int(port)))
return False
except OSError:
return True

@main.command(
help="Validate a PreFigure XML source file against the schema"
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ python = "^3.8.5"
shapely = "^2.0.6"
lxml = "^5.3.0"
click = "^8.1.7"
psutil = "^6.0.0"
networkx = "^2.5"
scipy = [{version = ">=1.5, <=1.7", python = "3.8"}, {version = "^1.8", python = ">=3.9,<3.13"}, {version = "^1.14.1", python = "3.13"}]
numpy = [{version = ">=1.19, <=1.25", python = "3.8"}, {version = "^1.26", python = ">=3.9,<3.13"}, {version = "^2.1.0", python = "3.13"}]
Expand Down

0 comments on commit 51f67ea

Please sign in to comment.