#!/usr/bin/env python

"""
Creates or regenerates a Makefile with special planex-init comments
"""

import os
import logging

MAKEFILE_PATH = "/usr/share/planex"

log = logging.getLogger(__name__)
logging.basicConfig(level=logging.ERROR)


def create_makefile():
    """ Checks if a Makefile exists with special planex-init comments in it.
    If not, it creates or regenerates the Makefile while preserving its
    existing contents.
    """
    name = "Makefile"

    firstline = "# Start generated by planex-init\n"
    autogen = "include %s/Makefile.rules\n" % (MAKEFILE_PATH)
    endline = "# End generated by planex-init\n"

    if not os.path.exists(name):
        log.debug("Creating Makefile")
        with open(name, 'w') as makefile:
            makefile.write(firstline)
            makefile.write(autogen)
            makefile.write(endline)
        return

    with open(name, 'r') as makefile:
        lines = makefile.readlines()

    try:
        start = lines.index(firstline)
        end = lines.index(endline)
        lines = lines[:start+1] + [autogen] + lines[end:]

    except ValueError:
        log.error("Couldn't find planex-init autogenerated stanza in Makefile")

    with open(name, 'w') as makefile:
        makefile.writelines(lines)


def main():
    """
    Main entry point
    """
    create_makefile()

if __name__ == "__main__":
    main()

#!/usr/bin/env python

"""
Creates or regenerates a Makefile with special planex-init comments
"""

import os
import logging

MAKEFILE_PATH = "/usr/share/planex"

log = logging.getLogger(__name__)
logging.basicConfig(level=logging.ERROR)


def create_makefile():
    """ Checks if a Makefile exists with special planex-init comments in it.
    If not, it creates or regenerates the Makefile while preserving its
    existing contents.
    """
    name = "Makefile"

    firstline = "# Start generated by planex-init\n"
    autogen = "include %s/Makefile.rules\n" % (MAKEFILE_PATH)
    endline = "# End generated by planex-init\n"

    if not os.path.exists(name):
        log.debug("Creating Makefile")
        with open(name, 'w') as makefile:
            makefile.write(firstline)
            makefile.write(autogen)
            makefile.write(endline)
        return

    with open(name, 'r') as makefile:
        lines = makefile.readlines()

    try:
        start = lines.index(firstline)
        end = lines.index(endline)
        lines = lines[:start + 1] + [autogen] + lines[end:]

    except ValueError:
        log.error("Couldn't find planex-init autogenerated stanza in Makefile")

    with open(name, 'w') as makefile:
        makefile.writelines(lines)


def main():
    """
    Main entry point
    """
    create_makefile()

if __name__ == "__main__":
    main()

Fix warning reported by older pep8