Skip to content

Instantly share code, notes, and snippets.

@jorgenschaefer
Created December 13, 2016 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgenschaefer/786985eab0a28d32c3444bffe42ad82e to your computer and use it in GitHub Desktop.
Save jorgenschaefer/786985eab0a28d32c3444bffe42ad82e to your computer and use it in GitHub Desktop.
Modern form of self-modifying code
#!/usr/bin/env python3
import sys, os, subprocess, signal
#usage $~/lab/erc-notifier.py title message
### pop up the first notify window, and kill it when pop up the second notify window
controler=0
previous_pid=0
if controler == 0:
p0=subprocess.Popen(['/home/jusss/lab/notifier.py', sys.argv[1], sys.argv[2]], shell=False)
### just replace once! sed -i '0,/a/ s//e/' example
os.system("sed -i -e '0,/controler=0/ s//controler=1/' ~/lab/erc-notifier.py")
os.system("sed -i -e '0,/previous_pid=0/ s//previous_pid=" + str(p0.pid) + "/' ~/lab/erc-notifier.py")
if controler == 1:
try:
### consider if you click ok, and then process exit, so the process won't exist anymore
os.kill(previous_pid,signal.SIGTERM)
except Exception as e:
print(e)
p=subprocess.Popen(['/home/jusss/lab/notifier.py', sys.argv[1], sys.argv[2]], shell=False)
os.system("sed -i -e '0,/previous_pid=" + str(previous_pid) + "/ s//previous_pid=" + str(p.pid) + "/' ~/lab/erc-notifier.py")
@jorgenschaefer
Copy link
Author

Recently, a rookie came to our IRC channel and asked for help, because they extended an IRC client for desktop notifications, and "it does not work" (that was the full description of the problem). After some work, we got them to paste the code they wrote.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment