Skip to content

Commit

Permalink
Merge branches
Browse files Browse the repository at this point in the history
  • Loading branch information
you-n-g committed Aug 15, 2024
1 parent 856d045 commit dec7a43
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 132 deletions.
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ keywords = [
license = { text = "MIT" }
authors = [
{ email = "afe.young@gmail.com", name = "Xiao Yang" },
{ email = "zplongr@hotmail.com", name = "zhupr" },
{ email = "v-gazh@hotmail.com", name = "gazh" },
]
requires-python = ">=3.8"
classifiers = [
Expand All @@ -37,6 +39,13 @@ dynamic = [
dependencies = [
"pydantic-settings",
"typer[all]",
"loguru",
"notifiers",
"fire",
"psutil",
"iterfzf",
"PyYAML",
"watchdog",
]
urls.documentation = "https://you-n-g.github.io/wan"
urls.issue = "https://github.com/you-n-g/wan/issues"
Expand Down
132 changes: 131 additions & 1 deletion src/wan/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,131 @@
"""Init for the project."""
from pathlib import Path
import subprocess
import time
from typing import Optional, Union

import fire
from loguru import logger
import psutil

from .notify import Notifier
from .utils import get_pid_via_fzf, is_buzy
from .watch import watch_file


class CLI:
# TODO: process related features can decouple from CLI
def __init__(self, idle=False) -> None:
self.ntf = Notifier()
self._idle = idle

@staticmethod
def _get_process_info(pid):
process_info = ":"
try:
p = psutil.Process(pid)
except psutil.NoSuchProcess:
process_info = ""
else:
process_info = ":" + " ".join(p.cmdline())
return process_info

def wait(self, pid=None, message=None, idle=False, patience=20, sleep=3):
"""wait.
Parameters
----------
pid :
pid
message :
message
idle :
will it notify me if the process become idle
patience :
How many idle status is ignored before reguard the process as stopped
sleep :
sleep
"""
logger.debug(f"Idle: {self._idle or idle}; patience: {patience}; sleep: {sleep}")

if pid is None:
pid = get_pid_via_fzf()
if pid is None:
logger.info("No process selected, You can used --pid to specify the process")
return

process_info = self._get_process_info(pid)

logger.info(f"Process[{pid}{process_info}] selected")
start_time = time.time()

cp = 0
while True:
try:
p = psutil.Process(pid)
p_status = p.status()
# If the process has been stopped then we'll break the while loop
# 1) No such PID
# 2) Become Zombie
# 3) Idle for a long time
if p_status == psutil.STATUS_ZOMBIE:
break
except psutil.NoSuchProcess:
logger.info(f"The process[PID] has ended")
break
else:
logger.debug(f"status: {p_status}, patience: {cp}")
if (self._idle or idle) and not is_buzy(p):
cp += 1
if cp > patience:
logger.info(f"The process is idle, status: {p_status}")
break
else:
cp = 0
time.sleep(sleep)
if message is None:
message = f"The Process[{pid}{process_info}] has stopped or become idle now."
self.ntf(f"[{time.time() - start_time:.1f}s] {message}")

def cmd(self, *cmd):
"""
Run command directly and notify after cmd stop or become idle
"""
logger.info(f"run command: {cmd}")
if len(cmd) > 0:
jcmd = " ".join(str(c) for c in cmd)
proc = subprocess.Popen(jcmd, shell=True)
self.wait(proc.pid)
code = proc.wait()
return code

def wc(self, *cmd):
"""
Wait a process to end and then start a command
"""
logger.info(f"command queued: {cmd}")
if len(cmd) > 0:
self.wait()
return self.cmd(*cmd)

def pid(self):
return get_pid_via_fzf()

def watch(self, path: Union[str, Path], pattern: Optional[str] =None):
"""
watch the change of the file system
"""
path = Path(path)
watch_file(path, pattern=pattern)


def ntf(message, config_path: str = "~/.dotfiles/.notifiers.yaml"):
# notify with the call stack
Notifier(config_path=config_path)(message)


def run():
fire.Fire(CLI)


if __name__ == "__main__":
run()
File renamed without changes.
File renamed without changes.
File renamed without changes.
131 changes: 0 additions & 131 deletions wan/__init__.py

This file was deleted.

0 comments on commit dec7a43

Please sign in to comment.