Skip to content

Instantly share code, notes, and snippets.

@NanoAi
Last active July 31, 2018 00:31
Show Gist options
  • Save NanoAi/5ab51cc3cb82b3695e63c8ca932937a8 to your computer and use it in GitHub Desktop.
Save NanoAi/5ab51cc3cb82b3695e63c8ca932937a8 to your computer and use it in GitHub Desktop.
q.py aka. My First Python Script!
#!/usr/bin/python3
from filelock import Timeout, FileLock
from time import sleep
from sys import argv
from os import getpid
import argparse
import subprocess
path = "/tmp/.qpy_lock"
lock = FileLock( path, timeout=12 )
def cmd(var):
process = subprocess.Popen(
var, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Wait for the command execution to finish.
process.wait()
out, err = process.communicate()
if out:
out = out.decode('utf-8')
print( out )
if err:
err = err.decode('utf-8')
print( '!> ' + err )
exit()
if argv and len( argv ) > 1:
for k, v in enumerate( argv ):
if ' ' in v:
argv[k] = "\"" + v + "\""
_input = ( " " ).join( argv[1:] )
while True:
try:
with lock.acquire():
print( "> " + _input )
cmd( _input )
break
except Timeout:
print( "!> Request Timed out for " + str( getpid() ) )
break
sleep(1)
else:
print( "!> Please enter a command to queue..." )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment