Created
November 12, 2016 07:07
-
-
Save matejc/c65a0d66249a868b377f92f1f19e60a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from subprocess import Popen, PIPE, TimeoutExpired | |
from threading import Thread | |
import random | |
import json | |
def uniqueid(): | |
random.seed() | |
return '{0:0>6x}'.format(random.getrandbits(24)) | |
def stderr_reader(identity, pipe): | |
with pipe: | |
for line in iter(pipe.readline, ''): | |
line = line.rstrip() | |
print('{0}-err> {1}'.format(identity, line)) | |
def run(identity, executable, args, inputText=None, timeout=600): | |
with Popen( | |
[executable] + args, | |
stdin=PIPE, | |
stdout=PIPE, | |
stderr=PIPE, | |
universal_newlines=True, | |
bufsize=1) as p: | |
try: | |
print('{0}-run> {1} {2}'.format(identity, executable, ' '.join( | |
args))) | |
err_thread = Thread( | |
target=stderr_reader, args=[identity, p.stderr]) | |
err_thread.start() | |
if inputText: | |
p.stdin.write(inputText) | |
p.stdin.close() | |
out_lines = [] | |
with p.stdout as pipe: | |
for line in iter(pipe.readline, ''): | |
line = line.rstrip() | |
out_lines += [line] | |
print('{0}-out> {1}'.format(identity, line)) | |
p.wait(timeout=timeout) | |
err_thread.join() | |
print('{0}-returncode> {1}\n'.format(identity, p.returncode)) | |
return p.returncode, out_lines | |
except TimeoutExpired as e: | |
p.kill() | |
raise e | |
except Exception as e: | |
raise e | |
def nixEnv(args): | |
return run(uniqueid(), 'nix-env', args) | |
def nixInstantiate(args, inputText=None): | |
return run(uniqueid(), 'nix-instantiate', args, inputText) | |
def nixBuild(args): | |
return run(uniqueid(), 'nix-build', args) | |
print('########################## nix-env output ##########################') | |
print(nixEnv(['-q'])) | |
print('\n###################### nix-instantiate output ######################') | |
print(nixInstantiate(['--eval', '-E', '-'], 'let a = 12; in a')) | |
print('\n######################### nix-build output #########################') | |
print(nixBuild(['../nixpkgs', '-A', 'xpra', '--no-out-link'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment