Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ better error handler for file permission denied in copying… #144

Merged
merged 2 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .moban.cd/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: moban
organisation: moremoban
releases:
- changes:
- action: Updated
details:
- "`#143`: moban shall report permission error and continue the rest of the copying task."
date: unreleased
version: 0.3.6
- changes:
- action: Updated
details:
Expand Down
4 changes: 2 additions & 2 deletions .moban.cd/moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ organisation: moremoban
author: C. W.
contact: wangc_2011@hotmail.com
license: MIT
version: 0.3.5
current_version: 0.3.5
version: 0.3.6
current_version: 0.3.6
release: 0.3.5
branch: master
command_line_interface: "moban"
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Change log
================================================================================

0.3.6 - unreleased
--------------------------------------------------------------------------------

Updated
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#. `#143 <https://github.com/moremoban/moban/issues/143>`_: moban shall report
permission error and continue the rest of the copying task.

0.3.5 - 10-12-2018
--------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# The short X.Y version
version = u'0.3.5'
# The full version, including alpha/beta/rc tags
release = u'0.3.5'
release = u'0.3.6'


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion moban/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.3.5"
__version__ = "0.3.6"
__author__ = "C. W."
12 changes: 10 additions & 2 deletions moban/copier.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import os
import sys
import shutil

import moban.utils as utils
import moban.reporter as reporter
from moban.hashstore import HASH_STORE

PY2 = sys.version_info[0] == 2
if PY2:
PermissionError = IOError


class Copier(object):
def __init__(self, template_dirs):
Expand Down Expand Up @@ -88,8 +93,11 @@ def _copy(self, src_path, dest):
if dest_folder:
utils.mkdir_p(dest_folder)
reporter.report_copying(src_path, dest)
shutil.copy(src_path, dest)
self._count = self._count + 1
try:
shutil.copy(src_path, dest)
self._count = self._count + 1
except PermissionError:
reporter.report_error_message("No permission to write %s" % dest)

def _increment_file_count(self):
self._file_count += 1
Expand Down
4 changes: 2 additions & 2 deletions moban/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def expand_directories(file_list, template_dirs):
break
if os.path.isdir(true_template_file):
for file_name in os.listdir(true_template_file):
template_file = '/'.join([template_file, file_name])
template_file = template_file.replace('\\', '/')
template_file = "/".join([template_file, file_name])
template_file = template_file.replace("\\", "/")
base_output_name, _ = os.path.splitext(file_name)
yield (
(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

NAME = 'moban'
AUTHOR = 'C. W.'
VERSION = '0.3.5'
VERSION = '0.3.6'
EMAIL = 'wangc_2011@hotmail.com'
LICENSE = 'MIT'
ENTRY_POINTS = {
Expand Down
15 changes: 15 additions & 0 deletions tests/test_copier.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import shutil

from mock import patch
Expand All @@ -7,6 +8,10 @@
from moban.copier import Copier
from moban.mobanfile import handle_copy

PY2 = sys.version_info[0] == 2
if PY2:
PermissionError = IOError


class TestCopier:
def setUp(self):
Expand All @@ -32,6 +37,16 @@ def test_copy_files_file_not_found(self, reporter):
"copier-test-not-found.csv cannot be found"
)

@patch("moban.reporter.report_error_message")
def test_no_permission_to_write(self, reporter):
copier = Copier([os.path.join("tests", "fixtures")])
file_list = [{"/tmp/test_cannot_write": "copier-test01.csv"}]
self.fake_copy.side_effect = PermissionError
copier.copy_files(file_list)
reporter.assert_called_with(
"No permission to write /tmp/test_cannot_write"
)

def test_number_of_files(self):
copier = Copier([os.path.join("tests", "fixtures")])
file_list = [{"/tmp/test": "copier-test04.csv"}]
Expand Down
7 changes: 4 additions & 3 deletions tests/test_hash_store.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import sys

from moban.hashstore import HashStore
from nose import SkipTest

from moban.hashstore import HashStore


class TestHashStore:
def setUp(self):
Expand Down Expand Up @@ -79,8 +80,8 @@ def test_dest_file_file_permision_changed(self):
Save as above, but this time,
the generated file had file permision change
"""
if sys.platform == 'win32':
raise SkipTest('No actual chmod on windows')
if sys.platform == "win32":
raise SkipTest("No actual chmod on windows")
hs = HashStore()
flag = hs.is_file_changed(*self.fixture)
if flag:
Expand Down
19 changes: 7 additions & 12 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from shutil import rmtree

from mock import patch
from nose.tools import eq_, raises
from nose import SkipTest
from nose.tools import eq_, raises

from moban.utils import (
mkdir_p,
get_repo_name,
Expand All @@ -27,8 +28,8 @@ def create_file(test_file, permission):


def test_file_permission_copy():
if sys.platform == 'win32':
raise SkipTest('No actual chmod on windows')
if sys.platform == "win32":
raise SkipTest("No actual chmod on windows")
test_source = "test_file_permission_copy1"
test_dest = "test_file_permission_copy2"
create_file(test_source, 0o046)
Expand All @@ -48,8 +49,8 @@ def test_file_permissions_file_not_found():


def test_file_permission_copy_symlink():
if sys.platform == 'win32':
raise SkipTest('No symlink on windows')
if sys.platform == "win32":
raise SkipTest("No symlink on windows")
test_source = "test_file_permission_copy1"
test_dest = "test_file_permission_copy2"
test_symlink = "test_file_permission_symlink"
Expand Down Expand Up @@ -98,13 +99,7 @@ def test_expand_dir():
file_list = [("template-tests", "abc", "abc")]
template_dirs = [os.path.join("tests", "fixtures")]
results = list(expand_directories(file_list, template_dirs))
expected = [
(
"template-tests/a.jj2",
"abc",
os.path.join("abc", "a"),
)
]
expected = [("template-tests/a.jj2", "abc", os.path.join("abc", "a"))]
eq_(results, expected)


Expand Down