Skip to content
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: Added
details:
- "`#32`: option 1 copy a directory without its subdirectories."
date: pending release
version: 0.2.4
- changes:
- action: Added
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.2.3
current_version: 0.2.3
version: 0.2.4
current_version: 0.2.4
release: 0.2.3
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.2.4 - pending release
--------------------------------------------------------------------------------

Added
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#. `#32 <https://github.com/moremoban/moban/issues/32>`_: option 1 copy a
directory without its subdirectories.

0.2.3 - 10-07-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.2.3'
# The full version, including alpha/beta/rc tags
release = u'0.2.3'
release = u'0.2.4'


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions docs/misc-1-copying-templates/.moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ configuration:
copy:
- simple.file.copy: file-in-template-sources-folder.txt
- "misc-1-copying/can-create-folder/if-not-exists.txt": file-in-template-sources-folder.txt
- "test-dir": dir-for-copying
- "test-recursive-dir": dir-for-recusive-copying/**
22 changes: 22 additions & 0 deletions docs/misc-1-copying-templates/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,26 @@ Misc 1: copying templates
With `.moban.yml`, you can copy templates to your destination.


Please be aware that, your templates and template folder have to be inside
declared template folders. It does not copy any file or folder.


Here is example moban file for copying::

configuration:
template_dir:
- template-sources
copy:
- simple.file.copy: file-in-template-sources-folder.txt
- "misc-1-copying/can-create-folder/if-not-exists.txt": file-in-template-sources-folder.txt
- "test-dir": dir-for-copying
- "test-recursive-dir": dir-for-recusive-copying/**


template copy does:


#. copies any template inside pre-declared template directory to anywhere. moban will
create directory if needed.
#. copies any directory to anywhere. If "**" is followed, moban attempts to do
recursive copying.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dir for copying
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Please look at .moban.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
everything is copied
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dest_directory: source_directory/**
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.2.3"
__version__ = "0.2.4"
__author__ = "C. W."
44 changes: 36 additions & 8 deletions moban/copier.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@ def __init__(self, template_dirs):

def copy_files(self, file_list):
for dest, src in _iterate_list_of_dicts(file_list):
self._file_count += 1
src_path = self._get_src_file(src)
if src_path is None:
reporter.report_error_message(
"{0} cannot be found".format(src)
)
if src.endswith('**'):
source_dir = src[:-3]
src_path = self._get_src_file(source_dir)
if src_path:
self._copy_dir_recursively(src[:-3], src_path, dest)
else:
reporter.report_error_message(
"{0} cannot be found".format(source_dir)
)
else:
reporter.report_error_message(
"{0} cannot be found".format(src)
)
elif os.path.isdir(src_path):
self._do_copy_dir(src, src_path, dest)
self._copy_dir(src, src_path, dest)
elif HASH_STORE.are_two_file_different(src_path, dest):
self._do_copy(src_path, dest)
self._increment_file_count()
self._copy(src_path, dest)

def number_of_copied_files(self):
return self._count
Expand All @@ -42,24 +52,42 @@ def _get_src_file(self, src):
else:
return None

def _do_copy_dir(self, source, actual_source_path, dest):
def _copy_dir(self, source, actual_source_path, dest):
new_file_pair = []
for file_name in os.listdir(actual_source_path):
real_src_file = os.path.join(actual_source_path, file_name)
if os.path.isfile(real_src_file):
self._increment_file_count()
src_file_under_dir = os.path.join(source, file_name)
dest_file_under_dir = os.path.join(dest, file_name)
new_file_pair.append(
{dest_file_under_dir: src_file_under_dir}
)
if len(new_file_pair) > 0:
self.copy_files(new_file_pair)

def _copy_dir_recursively(self, source, actual_source_path, dest):
new_file_pair = []
for file_name in os.listdir(actual_source_path):
self._increment_file_count()
src_file_under_dir = os.path.join(source, file_name)
dest_file_under_dir = os.path.join(dest, file_name)
new_file_pair.append(
{dest_file_under_dir: src_file_under_dir}
)
self.copy_files(new_file_pair)

def _do_copy(self, src_path, dest):
def _copy(self, src_path, dest):
dest_folder = os.path.dirname(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

def _increment_file_count(self):
self._file_count += 1


def _iterate_list_of_dicts(list_of_dict):
for adict in list_of_dict:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

NAME = 'moban'
AUTHOR = 'C. W.'
VERSION = '0.2.3'
VERSION = '0.2.4'
EMAIL = 'wangc_2011@hotmail.com'
LICENSE = 'MIT'
ENTRY_POINTS = {
Expand Down
29 changes: 29 additions & 0 deletions tests/test_copier.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,34 @@ def test_copy_dir(reporter):
file_list = [{test_dir: "copier-directory"}]
copier.copy_files(file_list)
copier.copy_files(file_list) # not called the second time
eq_(reporter.call_count, 1)
shutil.rmtree(test_dir)


@patch("moban.reporter.report_error_message")
def test_copy_dir_with_error(reporter):
test_dir = "/tmp/copy-a-directory"
copier = Copier([os.path.join("tests", "fixtures")])
file_list = [{test_dir: "copier-directory-not-exist"}]
copier.copy_files(file_list)
eq_(reporter.call_count, 1)


@patch("moban.reporter.report_copying")
def test_copy_dir_recusively(reporter):
test_dir = "/tmp/copy-a-directory"
copier = Copier([os.path.join("tests", "fixtures")])
file_list = [{test_dir: "copier-directory/**"}]
copier.copy_files(file_list)
copier.copy_files(file_list) # not called the second time
eq_(reporter.call_count, 2)
shutil.rmtree(test_dir)


@patch("moban.reporter.report_error_message")
def test_copy_dir_recusively_with_error(reporter):
test_dir = "/tmp/copy-a-directory"
copier = Copier([os.path.join("tests", "fixtures")])
file_list = [{test_dir: "copier-directory-not-exist/**"}]
copier.copy_files(file_list)
eq_(reporter.call_count, 1)