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
7 changes: 7 additions & 0 deletions .moban.cd/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: moban
organisation: moremoban
releases:
- changes:
- action: Added
details:
- "`#32`: option 1 copy a directory without its subdirectories."
- "`#30`: command line template option is ignore when a moban file is present"
date: 14-07-2018
version: 0.2.4
- changes:
- action: Added
details:
Expand Down
6 changes: 3 additions & 3 deletions .moban.cd/moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ organisation: moremoban
author: C. W.
contact: wangc_2011@hotmail.com
license: MIT
version: 0.2.3
current_version: 0.2.3
release: 0.2.3
version: 0.2.4
current_version: 0.2.4
release: 0.2.4
branch: master
command_line_interface: "moban"
entry_point: "moban.main:main"
Expand Down
1 change: 1 addition & 0 deletions .moban.d/travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{%block custom_python_versions%}
python:
- pypy-5.3.1
- 3.7-dev
- 3.6
- 3.5
- 3.4
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ notifications:
email: false
python:
- pypy-5.3.1
- 3.7-dev
- 3.6
- 3.5
- 3.4
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Change log
================================================================================

0.2.4 - 14-07-2018
--------------------------------------------------------------------------------

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

#. `#32 <https://github.com/moremoban/moban/issues/32>`_: option 1 copy a
directory without its subdirectories.
#. `#30 <https://github.com/moremoban/moban/issues/30>`_: command line template
option is ignore when a moban file is present

0.2.3 - 10-07-2018
--------------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
author = u'C. W.'

# The short X.Y version
version = u'0.2.3'
version = u'0.2.4'
# 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."
46 changes: 35 additions & 11 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,38 @@ 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}
)
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
27 changes: 27 additions & 0 deletions moban/mobanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def find_default_moban_file():

def handle_moban_file_v1(moban_file_configurations, command_line_options):
merged_options = None
target = extract_target(command_line_options)
if constants.LABEL_CONFIG in moban_file_configurations:
merged_options = merge(
command_line_options,
Expand All @@ -35,6 +36,10 @@ def handle_moban_file_v1(moban_file_configurations, command_line_options):

targets = moban_file_configurations.get(constants.LABEL_TARGETS)
if targets:
if target:
# if command line option exists, append its template to targets
# issue 30
targets += target
number_of_templated_files = handle_targets(merged_options, targets)
else:
number_of_templated_files = 0
Expand Down Expand Up @@ -101,3 +106,25 @@ def handle_plugin_dirs(plugin_dirs):
for plugin in plugins:
plugin_module = os.path.basename(plugin_dir) + "." + plugin
do_import(plugin_module)


def extract_target(options):
template = options.get(constants.LABEL_TEMPLATE)
config = options.get(constants.LABEL_CONFIG)
output = options.get(constants.LABEL_OUTPUT)
result = []
if template:
if output is None:
raise Exception(
"Please specify a output file name for %s." % template)
if config:
result = [
{
constants.LABEL_TEMPLATE: template,
constants.LABEL_CONFIG: config,
constants.LABEL_OUTPUT: output,
}
]
else:
result = [{output: template}]
return result
8 changes: 4 additions & 4 deletions 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 All @@ -23,7 +23,7 @@
'Yet another jinja2 cli command for static text generation'
)
URL = 'https://github.com/moremoban/moban'
DOWNLOAD_URL = '%s/archive/0.2.3.tar.gz' % URL
DOWNLOAD_URL = '%s/archive/0.2.4.tar.gz' % URL
FILES = ['README.rst', 'CHANGELOG.rst']
KEYWORDS = [
'jinja2',
Expand Down Expand Up @@ -58,8 +58,8 @@
# You do not need to read beyond this line
PUBLISH_COMMAND = '{0} setup.py sdist bdist_wheel upload -r pypi'.format(
sys.executable)
GS_COMMAND = ('gs moban v0.2.3 ' +
"Find 0.2.3 in changelog for more details")
GS_COMMAND = ('gs moban v0.2.4 ' +
"Find 0.2.4 in changelog for more details")
NO_GS_MESSAGE = ('Automatic github release is disabled. ' +
'Please install gease to enable it.')
UPLOAD_FAILED_MSG = (
Expand Down
45 changes: 45 additions & 0 deletions tests/integration_tests/test_command_line_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,51 @@ def test_single_command(self, fake_template_doer):
],
)

@patch("moban.engine.Engine.render_to_files")
def test_single_command_with_a_few_options(self, fake_template_doer):
test_args = ["moban", "-t", "abc.jj2", "-o", "xyz.output"]
with patch.object(sys, "argv", test_args):
main()
call_args = list(fake_template_doer.call_args[0][0])
eq_(
call_args,
[
("README.rst.jj2", "data.yaml", "README.rst"),
("setup.py.jj2", "data.yaml", "setup.py"),
("abc.jj2", "data.yaml", "xyz.output"),
],
)

@patch("moban.engine.Engine.render_to_files")
def test_single_command_with_options(self, fake_template_doer):
test_args = [
"moban",
"-t",
"abc.jj2",
"-c",
"new.yml",
"-o",
"xyz.output",
]
with patch.object(sys, "argv", test_args):
main()
call_args = list(fake_template_doer.call_args[0][0])
eq_(
call_args,
[
("README.rst.jj2", "new.yml", "README.rst"),
("setup.py.jj2", "new.yml", "setup.py"),
("abc.jj2", "new.yml", "xyz.output"),
],
)

@raises(Exception)
@patch("moban.engine.Engine.render_to_files")
def test_single_command_without_output_option(self, fake_template_doer):
test_args = ["moban", "-t", "abc.jj2"]
with patch.object(sys, "argv", test_args):
main()

def tearDown(self):
os.unlink(self.config_file)
os.unlink(self.data_file)
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)