Skip to content

Instantly share code, notes, and snippets.

@maphew
Last active July 11, 2020 08:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maphew/9c2b505f5f550f8b674058bb71b2cb60 to your computer and use it in GitHub Desktop.
Save maphew/9c2b505f5f550f8b674058bb71b2cb60 to your computer and use it in GitHub Desktop.
WIP: management scripts for History Tracer plugin for Leo
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1" >
<leo_header file_format="2"/>
<vnodes>
<v t="mhw.20200710235109.1"><vh>History Tracer</vh>
<v t="mhw.20200308223025.1"><vh>@int history-tracer-port=8088</vh></v>
<v t="mhw.20200710235122.1"><vh>@button leo-ver-serv-start</vh></v>
<v t="mhw.20200710235134.1"><vh>@button leo-ver-serv-list</vh></v>
<v t="mhw.20200710235410.1"><vh>@button leo-ver-serv-stop</vh></v>
<v t="mhw.20200711011130.6"><vh>g.</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="mhw.20200308223025.1"></t>
<t tx="mhw.20200710235109.1"># History Tracer plugin
Add or uncomment `history_tracer.py` in @settings--&gt;@enabled-plugins.
Set `@int history-tracer-port` to match the port used when launching `leo-ver-serv`
Psutil:
to support Leo launching and managing leo-ver-serv automatically. (Is my goal. We'll see if it's reasonable to accomplish.)
</t>
<t tx="mhw.20200710235122.1">@language python
'''Start 'leo-ver-serv' for History Tracer plugin'''
import os
import subprocess
watchfiles = os.path.join(g.computeHomeDir(), '.leo/.leoRecentFiles.txt')
if os.path.exists(watchfiles):
g.es_print(f"Running: leo-ver-serv {watchfiles} 8088")
#os.spawnlp(os.P_NOWAIT, 'leo-ver-serv', watchfiles, '8088')
subprocess.Popen([r'C:\Users\mattw\.cargo\bin\leo-ver-serv.exe', watchfiles, '8088'], close_fds=True)
else:
g.es_print(f"Couldn't find {watchfiles}")
'''TODO: see if can use psutils instead for better communication/monitoring
https://psutil.readthedocs.io/en/latest/#popen-class'''</t>
<t tx="mhw.20200710235134.1">@language python
g.es_print("====", c.p.h, "====")
g.es_print("Looking for 'leo-ver-serv' processes...")
import psutil
process = [proc for proc in psutil.process_iter() if "leo-ver-serv" in proc.name()]
#g.es_print(f"Process: {process}") # debug
#g.es_print([proc for proc in psutil.process_iter()])
g.es_print("{:&gt;6} {:&lt;20} {:&lt;10}".format('PID', 'Name', 'Status'))
for p in process:
g.es_print("{:&gt;6} {:&lt;20} {:&lt;10}".format(p.pid, p.name(), p.status()))
#g.es_print(p.open_files()) # raises AccessDenied if not run as root
#g.es_print(p.children()) # not needed, always empty (so far)
g.es_print("=" * 40)</t>
<t tx="mhw.20200710235410.1">@language python
'''Gracefully shutdown any processes named 'leo-ver-serv'
Adapted from https://psutil.readthedocs.io/en/latest/#psutil.wait_procs
'''
import psutil
def on_terminate(proc):
print("process {} terminated with exit code {}".format(proc, proc.returncode))
procs = [proc for proc in psutil.process_iter() if "leo-ver-serv" in proc.name()]
for p in procs:
p.terminate()
gone, alive = psutil.wait_procs(procs, timeout=3, callback=on_terminate)
for p in alive:
p.kill()</t>
<t tx="mhw.20200711011130.6">g.es_print(
#g.computeHomeDir(),
#g.computeLeoDir(),
g.computeGlobalConfigDir()
)</t>
</tnodes>
</leo_file>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment