This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2] - Added basic shell usage class
Basic shell usage class for running bash commands or scripts Resolves #2
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ omit = | |
*/python?.?/* | ||
*/site-packages/nose/* | ||
*__init__* | ||
tests/* | ||
tests/* | ||
env.py |
This file contains 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
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) | ||
|
This file contains 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
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') |