import os
import sys

from snake.core import Snake


SNAKEFILE_LOADED = False


def abort(msg):
    print >> sys.stderr, "Error: %s" % msg
    sys.exit(1)


def load_snakefile(path, fail_silently=False):
    global SNAKEFILE_LOADED
    if not SNAKEFILE_LOADED:
        sys.path.insert(0, path)
        try:
            return __import__('snakefile')
        except ImportError:
            if not fail_silently:
                abort("couldn't find any snakefile.")
        else:
            SNAKEFILE_LOADED = True
        del sys.path[0]


def find_snakefile():
    global SNAKEFILE_LOADED
    path = os.getcwd()
    while True:
        filepath = os.path.join(path, 'snakefile.py')
        if os.path.isfile(filepath):
            return load_snakefile(path), filepath
        if not os.path.split(path)[1]:
            break
        path = os.path.split(path)[0]
    if not SNAKEFILE_LOADED:
        abort("couldn't find any snakefile.")


def main():
    snakefile, snakefilepath = find_snakefile()
    for name in dir(snakefile):
        attr = getattr(snakefile, name)
        if isinstance(attr, Snake):
            attr.run(snakefilepath)
            break
    else:
        abort("couldn't find any Snake instance in snakefile.")

import imp
import os
import sys
from snake.core import Snake


def abort(msg):
    print >> sys.stderr, "Error: %s" % msg
    sys.exit(1)


def get_ascending_paths(path):
    paths = []
    while True:
        paths.append(path)
        path, tail = os.path.split(path)
        if not tail:
            break
    return paths


def find_snakefile():
    paths = get_ascending_paths(os.getcwd())
    try:
        return imp.find_module('snakefile', paths)
    except:
        abort("couldn't find any snakefile.")


def get_snakefile():
    return imp.load_module('snakefile', *find_snakefile())


def main():
    snakefile = get_snakefile()
    for name in dir(snakefile):
        attr = getattr(snakefile, name)
        if isinstance(attr, Snake):
            attr.run(snakefile.__file__)
            break
    else:
        abort("couldn't find any Snake instance in snakefile.")

Improve the way snakefile loading works
Improve the way snakefile loading works

+ import imp
  import os
  import sys
- 
  from snake.core import Snake
- 
- 
- SNAKEFILE_LOADED = False
  
  
  def abort(msg):
      print >> sys.stderr, "Error: %s" % msg
      sys.exit(1)
  
  
+ def get_ascending_paths(path):
+     paths = []
+     while True:
+         paths.append(path)
+         path, tail = os.path.split(path)
+         if not tail:
+             break
+     return paths
- def load_snakefile(path, fail_silently=False):
-     global SNAKEFILE_LOADED
-     if not SNAKEFILE_LOADED:
-         sys.path.insert(0, path)
-         try:
-             return __import__('snakefile')
-         except ImportError:
-             if not fail_silently:
-                 abort("couldn't find any snakefile.")
-         else:
-             SNAKEFILE_LOADED = True
-         del sys.path[0]
  
  
  def find_snakefile():
+     paths = get_ascending_paths(os.getcwd())
+     try:
+         return imp.find_module('snakefile', paths)
+     except:
-     global SNAKEFILE_LOADED
-     path = os.getcwd()
-     while True:
-         filepath = os.path.join(path, 'snakefile.py')
-         if os.path.isfile(filepath):
-             return load_snakefile(path), filepath
-         if not os.path.split(path)[1]:
-             break
-         path = os.path.split(path)[0]
-     if not SNAKEFILE_LOADED:
          abort("couldn't find any snakefile.")
  
  
+ def get_snakefile():
+     return imp.load_module('snakefile', *find_snakefile())
+ 
+ 
  def main():
-     snakefile, snakefilepath = find_snakefile()
+     snakefile = get_snakefile()
      for name in dir(snakefile):
          attr = getattr(snakefile, name)
          if isinstance(attr, Snake):
-             attr.run(snakefilepath)
+             attr.run(snakefile.__file__)
              break
      else:
          abort("couldn't find any Snake instance in snakefile.")
  
Improve the way snakefile loading works
## Code Before:
import os
import sys

from snake.core import Snake


SNAKEFILE_LOADED = False


def abort(msg):
    print >> sys.stderr, "Error: %s" % msg
    sys.exit(1)


def load_snakefile(path, fail_silently=False):
    global SNAKEFILE_LOADED
    if not SNAKEFILE_LOADED:
        sys.path.insert(0, path)
        try:
            return __import__('snakefile')
        except ImportError:
            if not fail_silently:
                abort("couldn't find any snakefile.")
        else:
            SNAKEFILE_LOADED = True
        del sys.path[0]


def find_snakefile():
    global SNAKEFILE_LOADED
    path = os.getcwd()
    while True:
        filepath = os.path.join(path, 'snakefile.py')
        if os.path.isfile(filepath):
            return load_snakefile(path), filepath
        if not os.path.split(path)[1]:
            break
        path = os.path.split(path)[0]
    if not SNAKEFILE_LOADED:
        abort("couldn't find any snakefile.")


def main():
    snakefile, snakefilepath = find_snakefile()
    for name in dir(snakefile):
        attr = getattr(snakefile, name)
        if isinstance(attr, Snake):
            attr.run(snakefilepath)
            break
    else:
        abort("couldn't find any Snake instance in snakefile.")

## Instruction:
Improve the way snakefile loading works
## Code After:
import imp
import os
import sys
from snake.core import Snake


def abort(msg):
    print >> sys.stderr, "Error: %s" % msg
    sys.exit(1)


def get_ascending_paths(path):
    paths = []
    while True:
        paths.append(path)
        path, tail = os.path.split(path)
        if not tail:
            break
    return paths


def find_snakefile():
    paths = get_ascending_paths(os.getcwd())
    try:
        return imp.find_module('snakefile', paths)
    except:
        abort("couldn't find any snakefile.")


def get_snakefile():
    return imp.load_module('snakefile', *find_snakefile())


def main():
    snakefile = get_snakefile()
    for name in dir(snakefile):
        attr = getattr(snakefile, name)
        if isinstance(attr, Snake):
            attr.run(snakefile.__file__)
            break
    else:
        abort("couldn't find any Snake instance in snakefile.")
