"""Miscellaneous tasks that don't fit into one of the other groupings."""
import os
import pkgutil
import zipfile
from StringIO import StringIO

from paver.easy import dry, task

_docsdir = os.path.join(os.path.dirname(__file__), "docs")
if os.path.exists(_docsdir):
    @task
    def paverdocs():
        """Open your web browser and display Paver's documentation."""
        import webbrowser
        webbrowser.open("file://"
            + (os.path.join(os.path.abspath(_docsdir), 'index.html')))

@task
def minilib(options):
    """Create a Paver mini library that contains enough for a simple
    pavement.py to be installed using a generated setup.py. This
    is a good temporary measure until more people have deployed paver.
    The output file is 'paver-minilib.zip' in the current directory.

    Options:

    extra_files
        list of other paver modules to include (don't include the .py
        extension). By default, the following modules are included:
        defaults, path, release, setuputils, misctasks, options,
        tasks, easy
    """
    filelist = ['__init__', 'defaults', 'path', 'path25', 'release',
                'setuputils', "misctasks", "options", "tasks", "easy"]
    filelist.extend(options.get('extra_files', []))
    output_file = 'paver-minilib.zip'

    def generate_zip():
        # Write the mini library to a buffer.
        buf = StringIO()
        destfile = zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED)
        for filename in filelist:
            destfile.writestr(
                "paver/" + (filename + ".py"),
                pkgutil.get_data('paver', filename + ".py"))
        destfile.close()

        # Write the buffer to disk.
        f = open(output_file, "wb")
        f.write(buf.getvalue())
        f.close()
    dry("Generate %s" % output_file, generate_zip)

@task
def generate_setup():
    """Generates a setup.py file that uses paver behind the scenes. This
    setup.py file will look in the directory that the user is running it
    in for a paver-minilib.zip and will add that to sys.path if available.
    Otherwise, it will just assume that paver is available."""
    from paver.easy import dry
    def write_setup():
        setup = open("setup.py", "w")
        setup.write("""try:
    import paver.tasks
except ImportError:
    import os
    if os.path.exists("paver-minilib.zip"):
        import sys
        sys.path.insert(0, "paver-minilib.zip")
    import paver.tasks

paver.tasks.main()
""")
        setup.close()

    dry("Write setup.py", write_setup)


@@ -67,26 +67,16 @@
 ngs.%22%22%22%0A
-import os%0A
 import p
@@ -131,75 +131,141 @@
 gIO%0A
-%0Afrom paver.easy import dry, task%0A%0A_docsdir = os.path.join(os.path.
+from os.path import join, dirname, exists, abspath%0Afrom paver.easy import dry, task%0Afrom paver.tasks import VERSION%0A%0A_docsdir = join(
 dirn
@@ -290,24 +290,16 @@
 cs%22)%0Aif 
-os.path.
 exists(_
@@ -472,46 +472,20 @@
 e://
-%22%0A            + (os.path.join(os.path.
+%25s%22  %25 join(
 absp
@@ -512,19 +512,27 @@
 x.html')
-)
+ 
 )%0A
+        
 %0A@task%0Ad
@@ -1312,21 +1312,34 @@
 -minilib
+-%25s
 .zip'
+ %25 VERSION
 %0A%0A    de
@@ -2183,39 +2183,8 @@
 %22%22%22%0A
-    from paver.easy import dry%0A
     
@@ -2357,24 +2357,36 @@
 aver-minilib
+-%25(VERSION)s
 .zip%22):%0A    
@@ -2437,24 +2437,36 @@
 aver-minilib
+-%25(VERSION)s
 .zip%22)%0A    i
@@ -2506,16 +2506,39 @@
 in()%0A%22%22%22
+ %25 %7B'VERSION': VERSION%7D
 )%0A      
@@ -2593,9 +2593,8 @@
 _setup)%0A
-%0A

add version into minilib filename, fixes #46