-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from MichaelAquilina/cleanup
Code clean up and refactoring
- Loading branch information
Showing
7 changed files
with
186 additions
and
167 deletions.
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
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,53 @@ | ||
#! -*- encoding: utf8 -*- | ||
|
||
import os | ||
import shutil | ||
import subprocess | ||
import tempfile | ||
|
||
|
||
def show_diff(client_1, client_2, key): | ||
if shutil.which("diff") is None: | ||
print('Missing required "diff" executable.') | ||
print("Install this using your distribution's package manager") | ||
return | ||
|
||
if shutil.which("less") is None: | ||
print('Missing required "less" executable.') | ||
print("Install this using your distribution's package manager") | ||
return | ||
|
||
so1 = client_1.get(key) | ||
data1 = so1.fp.read() | ||
so1.fp.close() | ||
|
||
so2 = client_2.get(key) | ||
data2 = so2.fp.read() | ||
so2.fp.close() | ||
|
||
fd1, path1 = tempfile.mkstemp() | ||
fd2, path2 = tempfile.mkstemp() | ||
fd3, path3 = tempfile.mkstemp() | ||
|
||
with open(path1, 'wb') as fp: | ||
fp.write(data1) | ||
with open(path2, 'wb') as fp: | ||
fp.write(data2) | ||
|
||
# This is a lot faster than the difflib found in python | ||
with open(path3, 'wb') as fp: | ||
subprocess.call([ | ||
'diff', '-u', | ||
'--label', client_1.get_uri(key), path1, | ||
'--label', client_2.get_uri(key), path2, | ||
], stdout=fp) | ||
|
||
subprocess.call(['less', path3]) | ||
|
||
os.close(fd1) | ||
os.close(fd2) | ||
os.close(fd3) | ||
|
||
os.remove(path1) | ||
os.remove(path2) | ||
os.remove(path3) |
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,20 @@ | ||
#! -*- encoding: utf8 -*- | ||
|
||
try: | ||
from os import scandir | ||
except ImportError: | ||
from scandir import scandir | ||
|
||
from inotify_simple import INotify | ||
|
||
|
||
class INotifyRecursive(INotify): | ||
def add_watches(self, path, mask): | ||
results = {} | ||
results[self.add_watch(path, mask)] = path | ||
|
||
for item in scandir(path): | ||
if item.is_dir(): | ||
results.update(self.add_watches(item.path, mask)) | ||
|
||
return results |
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,24 @@ | ||
#! -*- encoding: utf-8 -*- | ||
|
||
import tqdm | ||
|
||
|
||
class ProgressBar(object): | ||
""" | ||
Singleton wrapper around tqdm | ||
""" | ||
pbar = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if cls.pbar: | ||
cls.pbar.close() | ||
|
||
cls.pbar = tqdm.tqdm(*args, **kwargs) | ||
|
||
@classmethod | ||
def update(cls, value): | ||
cls.pbar.update(value) | ||
|
||
@classmethod | ||
def close(cls): | ||
cls.pbar.close() |
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
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,44 @@ | ||
#! -*- encoding: utf8 -*- | ||
|
||
import mock | ||
|
||
from s4 import diff | ||
from tests import utils | ||
|
||
|
||
class TestShowDiff(object): | ||
@mock.patch('shutil.which') | ||
def test_diff_not_found(self, which, capsys, local_client, s3_client): | ||
which.return_value = None | ||
diff.show_diff(local_client, s3_client, "something") | ||
|
||
out, err = capsys.readouterr() | ||
assert out == ( | ||
'Missing required "diff" executable.\n' | ||
"Install this using your distribution's package manager\n" | ||
) | ||
|
||
@mock.patch('shutil.which') | ||
def test_less_not_found(self, which, capsys, local_client, s3_client): | ||
def missing_less(value): | ||
return None if value == 'less' else 'something' | ||
|
||
which.side_effect = missing_less | ||
diff.show_diff(local_client, s3_client, "something") | ||
|
||
out, err = capsys.readouterr() | ||
assert out == ( | ||
'Missing required "less" executable.\n' | ||
"Install this using your distribution's package manager\n" | ||
) | ||
|
||
@mock.patch('subprocess.call') | ||
def test_diff(self, call, local_client, s3_client): | ||
utils.set_local_contents(local_client, "something", 4000, "wow") | ||
utils.set_s3_contents(s3_client, "something", 3000, "nice") | ||
|
||
diff.show_diff(local_client, s3_client, "something") | ||
|
||
assert call.call_count == 2 | ||
assert call.call_args_list[0][0][0][0] == "diff" | ||
assert call.call_args_list[1][0][0][0] == "less" |
Oops, something went wrong.