Skip to content

Commit

Permalink
modified code to work in python 2 and python 3. (+1 squashed commit)
Browse files Browse the repository at this point in the history
Squashed commits:
[5384b01] tests passing in python 2 and python 3 (+1 squashed commit)
Squashed commits:
[8bdac3d] tests passing in python 2 and python 3
  • Loading branch information
asampat3090 committed Apr 7, 2018
1 parent c73d16a commit 558b675
Show file tree
Hide file tree
Showing 32 changed files with 262 additions and 159 deletions.
2 changes: 2 additions & 0 deletions datmo/cli/command/snapshot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function

from datmo.util.i18n import get as _
from datmo.cli.command.project import ProjectCommand
from datmo.controller.snapshot import SnapshotController
Expand Down
3 changes: 1 addition & 2 deletions datmo/cli/command/test/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def setup_class(self):
"--name", "foobar",
"--description", "test model"])
self.init.execute()
self.snapshot = SnapshotCommand(self.temp_dir, self.cli_helper,
self.init.controller.dal.driver)
self.snapshot = SnapshotCommand(self.temp_dir, self.cli_helper)

def teardown_class(self):
shutil.rmtree(self.temp_dir)
Expand Down
15 changes: 12 additions & 3 deletions datmo/cli/driver/helper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
from builtins import input

import sys
import importlib
import inspect
from datmo.util.i18n import get as _
from datmo.util.exceptions import ArgumentException


class Helper():
def __init__(self):
pass
Expand All @@ -12,16 +18,19 @@ def echo(self, message):
print(message)

def prompt(self, msg):
return raw_input(msg)
try:
return input(msg)
except EOFError:
pass

def prompt_bool(self, msg):
val = raw_input(msg).lower()
val = input(msg).lower()
return val in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']

def prompt_validator(self, msg, validate_function, tries=5, error_message="Invalid input"):
if not callable(validate_function):
raise ArgumentException('validate_function argument must be function')
val = raw_input(msg).lower()
val = input(msg).lower()
if not validate_function(val) and tries >= 0:
tries -= 1
return self.prompt_validator(msg, validate_function, tries, error_message)
Expand Down
22 changes: 21 additions & 1 deletion datmo/cli/driver/test/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from __future__ import print_function
from __future__ import unicode_literals

import os
import sys

# TODO: include builtin libraries for the appropriate Python
# try:
# import __builtin__
Expand All @@ -16,13 +19,30 @@
from datmo.cli.driver.helper import Helper

class TestHelper():
# https://stackoverflow.com/questions/35851323/pytest-how-to-test-a-function-with-input-call/36377194

def setup_method(self):
self.orig_stdin = sys.stdin

def teardown_method(self):
sys.stdin = self.orig_stdin

def test_init(self):
cli = Helper()
assert cli != None

def test_prompt(self):
cli = Helper()
test_message = 'foobar'
with open("test.txt", "w") as f:
f.write(test_message)

with open("test.txt", "r") as f:
sys.stdin = f
i = cli.prompt("what is this test?")
assert i == test_message
os.remove("test.txt")

# TODO: figure out how to replace "print" with a testable function
# https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python
# assert cli.echo(test_message) == test_message
# assert cli.prompt(test_message)
70 changes: 50 additions & 20 deletions datmo/controller/code/driver/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ def __init__(self, filepath, execpath, remote_url=None):
stderr=subprocess.PIPE,
cwd=self.filepath)
out, err = p.communicate()
out, err = out.decode(), err.decode()
if err:
raise GitExecutionException("exception.code_driver.git", {
"exception": err
})
if not semver.match(out.split()[2], ">=1.9.7"):
if not semver.match(str(out.split()[2]), ">=1.9.7"):
raise GitExecutionException("exception.code_driver.git", {
"git version": out.split()[2],
"exception": "Git version must be later than 1.9.7"
Expand Down Expand Up @@ -161,18 +162,29 @@ def commit(self, options):
"""
Git commit wrapper
Args:
options: list of options, e.g. ["-m", "hello"]
Parameters
----------
options: list
List of strings for the command (e.g. ["-m", "hello"])
Returns
-------
bool
True if success else False
Raises
------
GitExecutionException
If any errors occur in running git
Returns:
True for success
"""
try:
p = subprocess.Popen([self.execpath, "commit"] + options,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.filepath)
out, err = p.communicate()
out, err = out.decode(), err.decode()
if err:
raise GitExecutionException("exception.code_driver.git.commit", {
"options": options,
Expand Down Expand Up @@ -235,7 +247,8 @@ def stash_list(self, regex="datmo"):
try:
git_stash_list = subprocess.check_output([self.execpath, "stash", "list", "|",
"grep", """ + regex + """],
cwd=self.filepath).strip()
cwd=self.filepath)
git_stash_list = git_stash_list.decode().strip()
except Exception as e:
raise GitExecutionException("exception.code_driver.git.stash_list", {
"exception": e
Expand All @@ -247,10 +260,12 @@ def stash_pop(self, regex=None):
try:
if regex:
git_stash_pop = subprocess.check_output([self.execpath, "stash", "pop", "stash^{/"+regex+"}"],
cwd=self.filepath).strip()
cwd=self.filepath)
git_stash_pop = git_stash_pop.decode().strip()
else:
git_stash_pop = subprocess.check_output([self.execpath, "stash", "pop"],
cwd=self.filepath).strip()
cwd=self.filepath)
git_stash_pop = git_stash_pop.decode().strip()
except Exception as e:
raise GitExecutionException("exception.code_driver.git.stash_pop", {
"exception": e
Expand All @@ -262,10 +277,12 @@ def stash_apply(self, regex=None):
try:
if regex:
git_stash_apply = subprocess.check_output([self.execpath,"stash", "apply", "stash^{/"+regex+"}"],
cwd=self.filepath).strip()
cwd=self.filepath)
git_stash_apply = git_stash_apply.decode().strip()
else:
git_stash_apply = subprocess.check_output([self.execpath,"stash", "apply", "stash^{0}"],
cwd=self.filepath).strip()
cwd=self.filepath)
git_stash_apply = git_stash_apply.decode().strip()
except Exception as e:
raise GitExecutionException("exception.code_driver.git.stash_apply", {
"exception": e
Expand All @@ -275,7 +292,8 @@ def stash_apply(self, regex=None):
def hash(self, command, branch):
try:
result = subprocess.check_output([self.execpath, command, branch],
cwd=self.filepath).strip()
cwd=self.filepath)
result = result.decode().strip()
except Exception as e:
raise GitExecutionException("exception.code_driver.git.hash", {
"command": command,
Expand All @@ -287,7 +305,8 @@ def hash(self, command, branch):
def latest_commit(self):
try:
git_commit = subprocess.check_output([self.execpath, "log", "--format=%H", "-n" , "1"],
cwd=self.filepath).strip()
cwd=self.filepath)
git_commit = git_commit.decode().strip()
except Exception as e:
raise GitExecutionException("exception.code_driver.git.latest_commit", {
"exception": e
Expand All @@ -307,7 +326,8 @@ def reset(self, git_commit):
def get_absolute_git_dir(self):
try:
git_dir = subprocess.check_output([self.execpath,"rev-parse", "--absolute-git-dir"],
cwd=self.filepath).strip()
cwd=self.filepath)
git_dir = git_dir.decode().strip()
except Exception as e:
raise GitExecutionException("exception.code_driver.git.get_git_dir", {
"exception": e
Expand All @@ -317,7 +337,8 @@ def get_absolute_git_dir(self):
def check_git_work_tree(self):
try:
git_work_tree_exists = subprocess.check_output([self.execpath,"rev-parse", "--is-inside-work-tree"],
cwd=self.filepath).strip()
cwd=self.filepath)
git_work_tree_exists = git_work_tree_exists.decode().strip()
except Exception as e:
raise GitExecutionException("exception.code_driver.git.check_git_work_tree", {
"exception": e
Expand All @@ -333,12 +354,14 @@ def remote(self, mode, origin, git_url):
stderr=subprocess.PIPE,
cwd=self.filepath)
out, err = p.communicate()
out, err = out.decode(), err.decode()
elif mode == "add":
p = subprocess.Popen([self.execpath, "remote", "add", origin, git_url],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.filepath)
out, err = p.communicate()
out, err = out.decode(), err.decode()
else:
raise GitExecutionException("exception.code_driver.git.remote", {
"mode": mode,
Expand Down Expand Up @@ -366,7 +389,8 @@ def get_remote_url(self):
try:
# TODO: handle multiple remote urls
git_url = subprocess.check_output([self.execpath, "config", "--get", "remote.origin.url"],
cwd=self.filepath).strip()
cwd=self.filepath)
git_url = git_url.decode().strip()
except Exception as e:
raise GitExecutionException("exception.code_driver.git.get_remote_url", {
"exception": e
Expand Down Expand Up @@ -398,25 +422,29 @@ def push(self, origin, option=None, name=None):
stderr=subprocess.PIPE,
cwd=self.filepath)
out, err = p.communicate()
out, err = out.decode(), err.decode()
else:
p = subprocess.Popen([self.execpath, "push", option, origin],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.filepath)
out, err = p.communicate()
out, err = out.decode(), err.decode()
else:
if name:
p = subprocess.Popen([self.execpath,"push", origin, name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.filepath)
out, err = p.communicate()
out, err = out.decode(), err.decode()
else:
p = subprocess.Popen([self.execpath, "push", origin],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.filepath)
out, err = p.communicate()
out, err = out.decode(), err.decode()
if err:
raise GitExecutionException("exception.code_driver.git.push", {
"origin": origin,
Expand All @@ -442,7 +470,7 @@ def ensure_gitignore_exists(self):
def __create_filehash(single_filepath):
BUFF_SIZE = 65536
sha1 = hashlib.md5()
with open(single_filepath, "rb") as f:
with open(single_filepath, "r") as f:
while True:
data = f.read(BUFF_SIZE)
if not data:
Expand All @@ -462,9 +490,9 @@ def __create_filehash(single_filepath):
current_filehash = __create_filehash(current_gitignore_filepath)
template_filehash = __create_filehash(template_gitignore_filepath)
if current_filehash != template_filehash:
with open(os.path.join(self.filepath, ".tempgitignore"), "wb") as f:
shutil.copyfileobj(open(current_gitignore_filepath, "rb"), f)
shutil.copyfileobj(open(template_gitignore_filepath, "rb"), f)
with open(os.path.join(self.filepath, ".tempgitignore"), "w") as f:
shutil.copyfileobj(open(current_gitignore_filepath, "r"), f)
shutil.copyfileobj(open(template_gitignore_filepath, "r"), f)
shutil.move(os.path.join(self.filepath, ".tempgitignore"),
current_gitignore_filepath)
except Exception as e:
Expand Down Expand Up @@ -630,15 +658,17 @@ def _check_for_ssh(self):
stderr=subprocess.PIPE,
cwd=self.home)
out, err = p.communicate()
out, err = out.decode(), err.decode()
if "Error" in out or "Error" in err or "denied" in err:
self._ssh_enabled = False
cmd = "ssh -i %s/.ssh/id_rsa -T git@%s" % (self.home, self.host)
p = subprocess.Popen(cmd,
p = subprocess.Popen(str(cmd),
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.home)
out, err = p.communicate()
out, err = out.decode(), err.decode()
error_strings = [
"Error",
"denied",
Expand Down
19 changes: 10 additions & 9 deletions datmo/controller/code/driver/test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import os
import shutil
import tempfile
from ..git import GitCodeManager, GitHostManager
from datmo.controller.code.driver.git import GitCodeManager, \
GitHostManager

class TestGitManager():
"""
Expand Down Expand Up @@ -68,7 +69,7 @@ def test_commit(self):
test_filepath = os.path.join(self.git_code_manager.filepath,
"test.txt")

with open(test_filepath, "wb") as f:
with open(test_filepath, "w") as f:
f.write(str("test"))
self.git_code_manager.add(test_filepath)
result = self.git_code_manager.commit(["-m", "test"])
Expand Down Expand Up @@ -146,7 +147,7 @@ def test_get_remote_url(self):
# test_filepath = os.path.join(self.git_code_manager.filepath,
# "test.txt")
#
# with open(test_filepath, "wb") as f:
# with open(test_filepath, "w") as f:
# f.write(str("test"))
# self.git_code_manager.add(test_filepath)
# self.git_code_manager.commit(["-m", "test"])
Expand Down Expand Up @@ -209,7 +210,7 @@ def test_create_code_ref(self):
self.git_code_manager.init()
test_filepath = os.path.join(self.git_code_manager.filepath,
"test.txt")
with open(test_filepath, "wb") as f:
with open(test_filepath, "w") as f:
f.write(str("test"))
code_id = self.git_code_manager.create_code_ref()
code_ref_path = os.path.join(self.git_code_manager.filepath,
Expand All @@ -222,7 +223,7 @@ def test_exists_code_ref(self):
self.git_code_manager.init()
test_filepath = os.path.join(self.git_code_manager.filepath,
"test.txt")
with open(test_filepath, "wb") as f:
with open(test_filepath, "w") as f:
f.write(str("test"))
code_id = self.git_code_manager.create_code_ref()
code_ref_path = os.path.join(self.git_code_manager.filepath,
Expand All @@ -236,7 +237,7 @@ def test_delete_code_ref(self):
self.git_code_manager.init()
test_filepath = os.path.join(self.git_code_manager.filepath,
"test.txt")
with open(test_filepath, "wb") as f:
with open(test_filepath, "w") as f:
f.write(str("test"))
code_id = self.git_code_manager.create_code_ref()
code_ref_path = os.path.join(self.git_code_manager.filepath,
Expand All @@ -250,7 +251,7 @@ def test_list_code_refs(self):
self.git_code_manager.init()
test_filepath = os.path.join(self.git_code_manager.filepath,
"test.txt")
with open(test_filepath, "wb") as f:
with open(test_filepath, "w") as f:
f.write(str("test"))
code_id = self.git_code_manager.create_code_ref()
code_refs = self.git_code_manager.list_code_refs()
Expand All @@ -268,10 +269,10 @@ def test_checkout_code_ref(self):
self.git_code_manager.init()
test_filepath = os.path.join(self.git_code_manager.filepath,
"test.txt")
with open(test_filepath, "wb") as f:
with open(test_filepath, "w") as f:
f.write(str("test1"))
code_id_1 = self.git_code_manager.create_code_ref()
with open(test_filepath, "wb") as f:
with open(test_filepath, "w") as f:
f.write(str("test2"))
_ = self.git_code_manager.create_code_ref()
result = self.git_code_manager.checkout_code_ref(code_id_1)
Expand Down

0 comments on commit 558b675

Please sign in to comment.