# coding=utf-8 import glob import os import sys import subprocess import logging logger = logging.getLogger("esales") class CommandException(Exception): pass class Cluster(object): def __init__(self, url=None): self.url = url def run_command(self, command): """ execute a command against the esales cluster """ try: # find the location of the jar file (provided by apptus) that is # used to execute the commands jar = os.path.abspath(os.path.dirname( __file__) + "/command/command.jar") subprocess.check_output( "java -jar {0} {1}".format(jar, command).split(), stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: message = "Could not execute command {0}. Error: {1}".format( command, e.output) logger.error(message) raise CommandException(message) except OSError: sys.exit("Java not found on this system") def import_products_and_categories(self, pattern): """ imports products and categories into the eSales cluster """ i = 0 for f in sorted(glob.iglob(pattern)): if os.path.isfile(f): logger.info("Importing {}".format(f)) try: self.run_command("import -p {0} {1}".format(self.url, f)) i += 1 except CommandException as e: logger.error("Failed to import {}".format(f)) finally: os.remove(f) logger.info("Imported {} file(s)".format(i)) return i