""" Library for interacting with the operating system """

import os
from subprocess import Popen

def cmd (command, workdir='.', input=''):
	# Detect people using the old API
	if not os.path.exists(workdir):
		raise Exception('ncos.cmd() has a new API.')

	file2 = os.tmpfile()
	file2.write(input)
	file2.flush()
	file2.seek(0)
	old = os.getcwd()
	os.chdir(workdir)
	file3 = os.tmpfile()
	proc = Popen(command, stdin=file2, stdout=file3, stderr=file3)
	proc.communicate()
	file2.close()
	file3.flush()
	file3.seek(0)
	output = file3.read()
	file3.close()
	os.chdir(old)
	return output[:-1]

def cmd2 (command, workdir='.'):
	old = os.getcwd()
	os.chdir(workdir)
	proc = Popen(command)
	proc.communicate()
	os.chdir(old)
