Skip to content

Commit

Permalink
fix: use cache dir to store files over the package dir
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Dec 23, 2023
1 parent 5595ed8 commit 580026d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies = [
"packaging >= 22.0",
"setuptools >= 45.0", # for pkg_resources. Otherwise get LegacyVersion error
"gitpython >= 3.1.24",
"platformdirs >= 4.0.0",
]

[project.optional-dependencies]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ persist-queue==0.8.0
packaging==23.0
setuptools==66.0
gitpython==3.1.30
platformdirs==4.0.0
7 changes: 4 additions & 3 deletions src/jupynium/definitions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from pathlib import Path

PACKAGE_DIR = Path(os.path.dirname(os.path.abspath(__file__)))
import platformdirs

persist_queue_path = PACKAGE_DIR / "jupynium_persist_queue"
CACHE_DIR = Path(platformdirs.user_cache_dir("jupynium"))
persist_queue_path = CACHE_DIR / "jupynium_persist_queue"
jupynium_pid_path = CACHE_DIR / "jupynium_pid.txt"
14 changes: 7 additions & 7 deletions src/jupynium/process.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# https://stackoverflow.com/questions/36799192/check-if-python-script-is-already-running
from os import getpid
from os.path import exists
from psutil import pid_exists, Process

from .definitions import PACKAGE_DIR
from psutil import Process, pid_exists

from .definitions import jupynium_pid_path

def already_running_pid(name="jupynium"):
pidpath = PACKAGE_DIR / f"{name}_pid.txt"

def already_running_pid(name="jupynium", pid_path=jupynium_pid_path):
my_pid = getpid()
if exists(pidpath):
with open(pidpath) as f:
if exists(pid_path):
with open(pid_path) as f:
pid = f.read()
pid = int(pid) if pid.isnumeric() else None
if pid is not None and pid_exists(pid):
Expand All @@ -20,6 +20,6 @@ def already_running_pid(name="jupynium"):
return pid
elif Process(pid).cmdline() == Process(my_pid).cmdline():
return pid
with open(pidpath, "w") as f:
with open(pid_path, "w") as f:
f.write(str(my_pid))
return 0

0 comments on commit 580026d

Please sign in to comment.