#!/usr/bin/python2
# pylint: disable=interruptible-system-call

from distutils.core import setup
import subprocess
import sys
import glob
import os
import re

AM_RE = r'(^.. automodule::.+?(?P<mo>^\s+?:member-order:.+?\n)?.+?:\n)\n(?(mo)NEVER)'

def generate_api_docs():
    if subprocess.call(["sphinx-apidoc", "-o", "doc", "blivet"]):
        sys.stderr.write("failed to generate API docs")

def add_member_order_option(files):
    """ Add an automodule option to preserve source code member order. """
    for fn in files:
        buf = open(fn).read()
        amended = re.sub(AM_RE,
                         r'\1    :member-order: bysource\n\n',
                         buf,
                         flags=re.DOTALL|re.MULTILINE)
        open(fn, "w").write(amended)

data_files = []
if os.environ.get("READTHEDOCS", False):
    generate_api_docs()
    rst_files = glob.glob("doc/*.rst")
    add_member_order_option(rst_files)
    api_doc_files = rst_files + ["doc/conf.py"]
    data_files.append(("docs/blivet", api_doc_files))

setup(name='blivet', version='1.15',
      description='Python module for system storage configuration',
      author='David Lehman', author_email='dlehman@redhat.com',
      url='http://fedoraproject.org/wiki/blivet',
      data_files=data_files,
      packages=['blivet', 'blivet.devices', 'blivet.devicelibs', 'blivet.formats', 'blivet.tasks'])

@@ -90,16 +90,47 @@
 t setup%0A
+from distutils import filelist%0A
 import s
@@ -183,16 +183,984 @@
 ort re%0A%0A
+# this is copied straight from distutils.filelist.findall , but with os.stat()%0A# replaced with os.lstat(), so S_ISLNK() can actually tell us something.%0Adef findall(dirname = os.curdir):%0A    from stat import ST_MODE, S_ISREG, S_ISDIR, S_ISLNK%0A%0A    file_list = %5B%5D%0A    stack = %5Bdirname%5D%0A    pop = stack.pop%0A    push = stack.append%0A%0A    while stack:%0A        dirname = pop()%0A        names = os.listdir(dirname)%0A%0A        for name in names:%0A            if dirname != os.curdir:        # avoid the dreaded %22./%22 syndrome%0A                fullname = os.path.join(dirname, name)%0A            else:%0A                fullname = name%0A%0A            # Avoid excess stat calls -- just one will do, thank you!%0A            stat = os.lstat(fullname)%0A            mode = stat%5BST_MODE%5D%0A            if S_ISREG(mode):%0A                file_list.append(fullname)%0A            elif S_ISDIR(mode) and not S_ISLNK(mode):%0A                push(fullname)%0A%0A    return file_list%0A%0Afilelist.findall = findall%0A%0A
 AM_RE = 

Make distutils.filelist.findall() do the right thing with symlinks maybe.