#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

"""
Tensorflow CLI Fabric File
"""

__author__ = 'Dongjoon Hyun (dongjoon@apache.org)'
__copyright__ = 'Copyright (c) 2016'
__license__ = 'Apache License'
__version__ = '0.1'

from fabric.api import *

@task
def lm(inpath, outpath, step, maxiter):
    """
    fab tensorflow.lm:/sample/sample_regression,/tmp/lm_result,0.5,10
    """
    if not (outpath.startswith('/tmp/') or outpath.startswith('/user/hadoop/')):
        print 'Unauthorized path: %(outpath)s' % locals()
        return
    run('mkdir %s' % env.dir)
    with cd(env.dir):
        run('''cat <<'EOF' > tensorflow.lm.py
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np

# Data. (Note: -1 < x < 1)
data = np.loadtxt('%(inpath)s')
x_ = data[...,0]
y_ = data[...,1]
print len(data), 'instances loaded.'

# Model.
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_ + b

# Train.
train = tf.train.GradientDescentOptimizer(%(step)s).minimize(tf.reduce_mean(tf.square(y - y_)))

# Start.
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for step in xrange(%(maxiter)s):
    sess.run(train)
print(step, sess.run(W), sess.run(b))

EOF''' % locals())
        cmd = '/usr/bin/env python tensorflow.lm.py 2> /dev/null'
        run(cmd)

@@ -1055,17 +1055,8 @@
 ath,
- outpath,
  ste
@@ -1128,29 +1128,15 @@
 ion,
-/tmp/lm_result,
 0.
-5
+1
 ,10
+0
 %0A   
@@ -1144,162 +1144,8 @@
 %22%22%22%0A
-    if not (outpath.startswith('/tmp/') or outpath.startswith('/user/hadoop/')):%0A        print 'Unauthorized path: %25(outpath)s' %25 locals()%0A        return%0A
     
@@ -1781,15 +1781,33 @@
 rint
-(step, 
+ %22%25%25.2f * x + %25%25.2f%22 %25%25 (
 sess
@@ -1828,17 +1828,16 @@
 run(b))%0A
-%0A
 EOF''' %25

Remove output folder