Repository: mkdocs/mkdocs

Issue:
Unicode paths break clean_directory on server
Discovered a weird error with the server and Unicode characters in paths. Run the following command and everything seems fine:

    mkdocs serve -f mkdocs/tests/integration/unicode/mkdocs.yml

However, once a file is edited and saved, the second attempt at a build results in the following error:

```
Exception in callback <bound method type.poll_tasks of <class 'livereload.handlers.LiveReloadHandler'>>
    Traceback (most recent call last):
      File "/Users/waylan/VirtualEnvs/mkdocs/lib/python2.7/site-packages/tornado/ioloop.py", line 1209, in _run
        return self.callback()
      File "/Users/waylan/VirtualEnvs/mkdocs/lib/python2.7/site-packages/livereload/handlers.py", line 67, in poll_tasks
        filepath, delay = cls.watcher.examine()
      File "/Users/waylan/VirtualEnvs/mkdocs/lib/python2.7/site-packages/livereload/watcher.py", line 73, in examine
        func and func()
      File "/Users/waylan/Code/mkdocs/mkdocs/commands/serve.py", line 112, in builder
        build(config, live_server=live_server, dirty=dirty)
      File "/Users/waylan/Code/mkdocs/mkdocs/commands/build.py", line 265, in build
        utils.clean_directory(config['site_dir'])
      File "/Users/waylan/Code/mkdocs/mkdocs/utils/__init__.py", line 144, in clean_directory
        if entry.startswith('.'):
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 1: ordinal not in range(128)
```

It would seem that somewhere between the call to `utils.clean_directory(config['site_dir'])` on the first build, and the same call on the second build, the `site_dir` config setting is converted from a Unicode string to a byte string.

I'm assuming the server code is the culprit.


Discussion:

Resolving patch:
diff --git a/mkdocs/commands/serve.py b/mkdocs/commands/serve.py
--- a/mkdocs/commands/serve.py
+++ b/mkdocs/commands/serve.py
@@ -92,7 +92,9 @@ def serve(config_file=None, dev_addr=None, strict=None, theme=None,
     """
 
     # Create a temporary build directory, and set some options to serve it
-    tempdir = tempfile.mkdtemp()
+    # PY2 returns a byte string by default. The Unicode prefix ensures a Unicode
+    # string is returned. And it makes MkDocs temp dirs easier to identify.
+    site_dir = tempfile.mkdtemp(prefix='mkdocs_')
 
     def builder():
         log.info("Building documentation...")
@@ -101,10 +103,10 @@ def builder():
             dev_addr=dev_addr,
             strict=strict,
             theme=theme,
-            theme_dir=theme_dir
+            theme_dir=theme_dir,
+            site_dir=site_dir
         )
         # Override a few config settings after validation
-        config['site_dir'] = tempdir
         config['site_url'] = 'http://{0}/'.format(config['dev_addr'])
 
         live_server = livereload in ['dirty', 'livereload']
@@ -119,8 +121,8 @@ def builder():
         host, port = config['dev_addr']
 
         if livereload in ['livereload', 'dirty']:
-            _livereload(host, port, config, builder, tempdir)
+            _livereload(host, port, config, builder, site_dir)
         else:
-            _static_server(host, port, tempdir)
+            _static_server(host, port, site_dir)
     finally:
-        shutil.rmtree(tempdir)
+        shutil.rmtree(site_dir)


Test patch: