Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
[#2] - Added basic shell usage class
Browse files Browse the repository at this point in the history
Basic shell usage class for running bash commands or scripts

Resolves #2
  • Loading branch information
ZacBlanco committed Jun 16, 2016
1 parent 364b052 commit 347e6dc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ omit =
*/python?.?/*
*/site-packages/nose/*
*__init__*
tests/*
tests/*
env.py
46 changes: 46 additions & 0 deletions scripts/shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os, subprocess

# The shell class defines just a few functions which can make executing commands easier
# run
# This command has two possible ways to be called
# (command) which is just a single line with all arguments
# (command, args) which simply just joins a string with the command and arguments
#
# It is also possible to change the current working directory (cwd)
# for commands which are sensitive to file locations
# (Unfortunately 'cd' doesn't work)

# If no arguments are passed for args, then it is assumed to be a string of length 0
# If no arguments are passed to the constructor we assume default cwd

class Shell:

cwd = ''

def run(self, command, args=''):

if len(args) > 0:
command = ' '.join([command, args.join(' ')])

path = os.getcwd()
if len(self.cwd) > 0:
process = subprocess.Popen(command, shell=True, cwd=path, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
else:
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
output = process.communicate()
return output


# set_cwd
# equivalent to doing a 'cd' command at the command line
# Error if the directory doesn't exist
def set_cwd(self, new_cwd):
if not os.path.exists(new_cwd):
raise IOError(' '.join([self.cwd, 'does not exist']))
else:
self.cwd = new_cwd

def __init__(self, wd=''):
if not wd == '':
self.set_cwd(wd)

40 changes: 40 additions & 0 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from env import scripts
from scripts.shell import Shell

class TestShell(unittest.TestCase):
def test_default_cwd(self):
try:
cmd = Shell()
except IOError as e:
self.fail('No argument shell constructor should not raise IOError')

def test_nonexistent_cwd(self):
try:
cmd = Shell('/missing/directory')
self.fail('Should raise IOError on setting nonexistent directory')
except IOError as e:
return

def test_existing_directory(self):
try:
# Actual Directory
cmd = Shell('/tmp')
except IOError as e:
self.fail('Valid path should pass here')

def test_simple_run(self):
try:
cmd = Shell('/tmp');
out = cmd.run('bash --version')
out = cmd.run('bash', '--version')
except Exception:
self.fail('Exception should not have been raised')

def test_default_cwd(self):
try:
cmd = Shell('');
out = cmd.run('bash --version')
out = cmd.run('bash', '--version')
except Exception:
self.fail('Exception should not have been raised')

0 comments on commit 347e6dc

Please sign in to comment.