Repository: getnikola/nikola

Issue:
utils.get_root_dir() should not be run for import_* and init
Here’s a fun thing you can try at home:

``` console
$ cd /tmp
$ nikola init site_1
[2014-03-14T15:39:05Z] INFO: init: Created empty site at site_1.
$ cd site_1
$ mkdir subdir
$ cd subdir
$ nikola init site_2
[2014-03-14T15:39:41Z] INFO: init: Created empty site at site_2.
$ ls -l .
total 0
$ ls -l ..
total 32
-rw-r--r-- 1 kwpolska kwpolska 27413 Mar 14 16:39 conf.py
-rw-r--r-- 1 kwpolska kwpolska  1923 Mar 14 16:39 conf.pyc
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 files/
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 galleries/
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 listings/
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 posts/
drwxr-xr-x 7 kwpolska kwpolska   160 Mar 14 16:39 site_2/    ← wat
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 stories/
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 subdir/
$
```

The same behavior applies to `import_*`:

``` console
$ pwd
/tmp/site_1/subdir
$ nikola import_wordpress ~/git/nikola/tests/wordpress_unicode_export.xml
[2014-03-14T15:41:24Z] INFO: init: Created empty site at new_site.
[2014-03-14T15:41:24Z] INFO: Nikola: Configuration will be written to: new_site/conf.py
$ ls -l .
total 0
$ ls -l ..
total 32
-rw-r--r-- 1 kwpolska kwpolska 27413 Mar 14 16:39 conf.py
-rw-r--r-- 1 kwpolska kwpolska  1923 Mar 14 16:39 conf.pyc
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 files/
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 galleries/
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 listings/
drwxr-xr-x 7 kwpolska kwpolska   180 Mar 14 16:41 new_site/  ← wat
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 posts/
drwxr-xr-x 7 kwpolska kwpolska   160 Mar 14 16:39 site_2/    ← wat
drwxr-xr-x 2 kwpolska kwpolska    40 Mar 14 16:39 stories/
drwxr-xr-x 3 kwpolska kwpolska    40 Mar 14 16:39 subdir/
$ mkdir nesting_is_fun
$ cd nesting_is_fun
$ nikola import_wordpress ~/git/nikola/tests/wordpress_unicode_export.xml
[2014-03-14T15:42:05Z] NOTICE: Nikola: The folder new_site already exists - assuming that this is a already existing Nikola site.
[2014-03-14T15:42:05Z] INFO: Nikola: Configuration will be written to: new_site/conf.py.import_wordpress-20140314_164205
$
```

This can be dangerous — especially because one can put a file named `conf.py` anywhere higher in the directory structure and destroy his Nikola attempts.

How could we fix it?  We could just not run `utils.get_root_dir()` if the user wants to execute one of those commands (it’s executed in `__main__.py`).



Discussion:
I'd say backwards, only run it in commands where it makes sense.

It’s too much effort. `init`, `import_wordpress`, `import_feed`, `import_blogger` are the only things that require this (`version` doesn’t require `conf.py` at all, but that is not a problem) — and all the other 23 commands actually require this.


Resolving patch:
diff --git a/nikola/__main__.py b/nikola/__main__.py
--- a/nikola/__main__.py
+++ b/nikola/__main__.py
@@ -70,9 +70,16 @@ def main(args):
             if os.name == 'nt':
                 colorful = False
 
-    root = get_root_dir()
-    if root:
-        os.chdir(root)
+    # Those commands do not require a `conf.py`.  (Issue #1132)
+    # Moreover, actually having one somewhere in the tree can be bad, putting
+    # the output of that command (the new site) in an unknown directory that is
+    # not the current working directory.  (does not apply to `version`)
+    argname = args[0] if len(args) > 0 else None
+    if argname not in ['init', 'import_wordpress', 'import_feed',
+                       'import_blogger', 'version']:
+        root = get_root_dir()
+        if root:
+            os.chdir(root)
 
     sys.path.append('')
     try:


Test patch: