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

from paver.easy import dry, path, 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
    """
    import paver
    paverdir = path(paver.__file__).dirname()
    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():
        import zipfile
        destfile = zipfile.ZipFile(output_file, "w", zipfile.ZIP_DEFLATED)
        for filename in filelist:
            destfile.write(
                paverdir / (filename + ".py"),
                "paver/" + (filename + ".py"))
        destfile.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)
    
@@ -76,16 +76,76 @@
 mport os
+%0Aimport pkgutil%0Aimport zipfile%0Afrom StringIO import StringIO
 %0A%0Afrom p
@@ -165,22 +165,16 @@
 ort dry,
- path,
  task%0A%0A_
@@ -1080,71 +1080,8 @@
 %22%22%22%0A
-    import paver%0A    paverdir = path(paver.__file__).dirname()%0A
     
@@ -1339,30 +1339,78 @@
         
-import zipfile
+# Write the mini library to a buffer.%0A        buf = StringIO()
 %0A       
@@ -1437,27 +1437,19 @@
 ZipFile(
-output_file
+buf
 , %22w%22, z
@@ -1529,16 +1529,19 @@
 le.write
+str
 (%0A      
@@ -1554,18 +1554,18 @@
     
+%22
 paver
-dir /
+/%22 +
  (fi
@@ -1593,36 +1593,50 @@
             
-%22paver/%22 + (
+pkgutil.get_data('paver', 
 filename + %22
@@ -1662,24 +1662,146 @@
 file.close()
+%0A%0A        # Write the buffer to disk.%0A        f = open(output_file, %22w%22)%0A        f.write(buf.getvalue())%0A        f.close()
 %0A    dry(%22Ge
@@ -2564,12 +2564,13 @@
 _setup)%0A    
+%0A

Fix minilib task to work when run from paver-minilib.zip.