#!/usr/bin/env python
import sys
import json
import os
import requests
import multiprocessing


def list_files(directory):
    for root, subdirs, files in os.walk(directory):
        print("ROOT: {}".format(root))
        for file in files:
            yield os.path.abspath(os.path.join(root, file))

        for subdir in subdirs:
            for subfile in list_files(subdir):
                yield subfile


if __name__ == "__main__":
    _, base_url, access_token, listing_dir = sys.argv

    endpoint = "{}/services".format(base_url)
    print("Base URL: {}".format(base_url))
    print("Access token: {}".format(access_token))
    print("Listing dir: {}".format(listing_dir))

    def put_file(file_path):
        with open(file_path) as f:
            data = json.load(f)
            data = {'services': data}
            url = '{}/{}'.format(endpoint, data['services']['id'])
            response = requests.put(
                url,
                data=json.dumps(data),
                headers={
                    "content-type": "application/json",
                    "authorization": "Bearer {}".format(access_token),
                })
            return response

    pool = multiprocessing.Pool(10)

    for result in pool.imap(put_file, list_files(listing_dir)):
        print(result)

@@ -15,16 +15,54 @@
  python%0A
+from __future__ import print_function%0A
 import s
@@ -125,16 +125,46 @@
 cessing%0A
+from datetime import datetime%0A
 %0A%0Adef li
@@ -287,24 +287,28 @@
     for file
+name
  in files:%0A 
@@ -363,16 +363,20 @@
 ot, file
+name
 ))%0A%0A    
@@ -485,136 +485,129 @@
 e%0A%0A%0A
-if __name__ == %22__main__%22:%0A    _, base_url, access_token, listing_dir = sys.argv%0A%0A    endpoint = %22%7B%7D/services%22.format(base_url)%0A
+def print_progress(counter, start_time):%0A    if counter %25 100 == 0:%0A        time_delta = datetime.now() - start_time%0A    
     
@@ -617,139 +617,196 @@
 nt(%22
-Base URL: %7B%7D%22.format(base_url))%0A    print(%22Access token: %7B%7D%22.format(access_token))%0A    print(%22Listing dir: %7B%7D%22.format(listing_dir))
+%7B%7D in %7B%7D (%7B%7D/s)%22.format(counter,%0A                                       time_delta,%0A                                       counter / time_delta))%0A%0A%0Adef file_putter(endpoint, access_token):
 %0A%0A  
@@ -1291,16 +1291,27 @@
   return
+ file_path,
  respons
@@ -1321,123 +1321,818 @@
     
-pool = multiprocessing.Pool(10)%0A%0A    for result in pool.imap(put_file, list_files(listing_dir)):%0A        print(result
+return put_file%0A%0A%0Adef do_import(base_url, access_token, listing_dir):%0A    endpoint = %22%7B%7D/services%22.format(base_url)%0A    put_file = file_putter(endpoint, access_token)%0A    print(%22Base URL: %7B%7D%22.format(base_url))%0A    print(%22Access token: %7B%7D%22.format(access_token))%0A    print(%22Listing dir: %7B%7D%22.format(listing_dir))%0A%0A    pool = multiprocessing.Pool(10)%0A%0A    counter = 0%0A    start_time = datetime.now()%0A    for file_path, response in pool.imap(put_file, list_files(listing_dir)):%0A        if response.status_code / 100 != 2:%0A            print(%22ERROR: %7B%7D on %7B%7D%22.format(response.status_code, file_path),%0A                  file=sys.stderr)%0A        else:%0A            counter += 1%0A            print_progress(counter, start_time)%0A%0A    print_progress(counter, start_time)%0A%0Aif __name__ == %22__main__%22:%0A    do_import(*sys.argv%5B1:%5D
 )%0A

Refactor import script