# -*- coding: utf-8 -*-

__author__ = 'Marek Wywiał'
__email__ = 'onjinx@gmail.com'
__version__ = '0.1.4'

import sys
import subprocess
import os
import stat


def run(*args):
    """Load given `envfile` and run `command` with `params`"""

    if not args:
        args = sys.argv[1:]

    if len(args) < 2:
        print('Usage: runenv <envfile> <command> <params>')
        sys.exit(0)
    environ = create_env(args[0])
    runnable_path = args[1]

    try:
        if not os.path.isfile(runnable_path):
            print('File `{}` does not exist'.format(runnable_path))
            sys.exit(1)
        if not(stat.S_IXUSR & os.stat(runnable_path)[stat.ST_MODE]):
            print('File `{}` is not executable'.format(runnable_path))
            sys.exit(1)
        return subprocess.check_call(
            args[1:], env=environ
        )
    except subprocess.CalledProcessError as e:
        return e.returncode


def create_env(env_file):
    """Create environ dictionary from current os.environ and
    variables got from given `env_file`"""

    environ = os.environ
    with open(env_file, 'r') as f:
        for line in f.readlines():
            line = line.rstrip(os.linesep)
            if '=' not in line:
                continue
            if line.startswith('#'):
                continue
            key, value = line.split('=', 1)
            environ[key] = value
    return environ

@@ -524,19 +524,18 @@
 ('File %60
-%7B%7D%60
+%25s
  does no
@@ -542,24 +542,19 @@
 t exist'
-.format(
+ %25 
 runnable
@@ -551,33 +551,32 @@
 %25 runnable_path)
-)
 %0A            sys
@@ -682,11 +682,10 @@
 le %60
-%7B%7D%60
+%25s
  is 
@@ -703,16 +703,11 @@
 ble'
-.format(
+ %25 
 runn
@@ -716,17 +716,16 @@
 le_path)
-)
 %0A       

Make text formatting compatible with python 2.6