Skip to content
Browse files
Fix #2214 -- handle nikola init in existing dir nicely
Signed-off-by: Chris Warrick <>
  • Loading branch information
Kwpolska committed Jan 10, 2016
1 parent b7b94ce commit 4e9d8b076a5eb0f7428ee57d95e122dac55bfa92
Showing 1 changed file with 17 additions and 6 deletions.
@@ -220,6 +220,14 @@ def prepare_config(config):
p.update({k: str(v) for k, v in config.items() if isinstance(v, bool) or v is None})
return p

def test_destination(destination, demo=False):
if demo and os.path.exists(destination):
LOGGER.warning("The directory {0} already exists, and a new demo site cannot be initialized in an existing directory.".format(destination))
LOGGER.warning("Please remove the directory and try again, or use another directory.")"Hint: If you want to initialize a git repository in this directory, run `git init` in the directory after creating a Nikola site.")
return False
return True

class CommandInit(Command):
"""Create a new site."""
@@ -277,7 +285,7 @@ def create_empty_site(cls, target):
makedirs(os.path.join(target, folder))

def ask_questions(target):
def ask_questions(target, demo=False):
"""Ask some questions about Nikola."""
def urlhandler(default, toconf):
answer = ask('Site URL', '')
@@ -442,7 +450,7 @@ def chandler(default, toconf):
print("If you do not want to answer and want to go with the defaults instead, simply restart with the `-q` parameter.")

for query, default, toconf, destination in questions:
if target and destination == '!target':
if target and destination == '!target' and test_destination(target, demo):
# Skip the destination question if we know it already
@@ -459,8 +467,9 @@ def chandler(default, toconf):
if toconf:
SAMPLE_CONF[destination] = answer
if destination == '!target':
while not answer:
print(' ERROR: you need to specify a target directory.\n')
while not answer or not test_destination(answer, demo):
if not answer:
print(' ERROR: you need to specify a target directory.\n')
answer = ask(query, default)
STORAGE['target'] = answer

@@ -476,7 +485,7 @@ def _execute(self, options={}, args=None):
except IndexError:
target = None
if not options.get('quiet'):
st = self.ask_questions(target=target)
st = self.ask_questions(target=target, demo=options.get('demo'))
if not target:
target = st['target']
@@ -489,11 +498,13 @@ def _execute(self, options={}, args=None):
-q, --quiet Do not ask questions about config.
-d, --demo Create a site filled with example data.""")
return False
return 1
if not options.get('demo'):
self.create_empty_site(target)'Created empty site at {0}.'.format(target))
if not test_destination(target, True):
return 2
self.copy_sample_site(target)"A new site with example data has been created at "

0 comments on commit 4e9d8b0

Please sign in to comment.