""" Hello World accsyn compute engine script. Example engine that does not invoke an external application. Instead it overrides _execute and iterates the assigned item / item range, printing a greeting and sleeping briefly for each frame. Changelog: * v1r1; [Henrik Norin, 26.10.07] Initial version. This software is provided "as is" - the author and distributor can not be held responsible for any damage caused by executing this script in any means. Author: Henrik Norin, accsyn/HDR AB """ import traceback import sys import os import time try: if 'ACCSYN_COMPUTE_COMMON_PATH' in os.environ: sys.path.append(os.environ['ACCSYN_COMPUTE_COMMON_PATH']) from common import Common except ImportError as e: sys.stderr.write( 'Cannot import accsyn common engine (required), ' 'make sure to name it "common.py" add its parent directory to ' ' PYTHONPATH. Details: {}\n'.format(e) ) raise class Engine(Common): """Minimal accsyn compute engine that greets each assigned frame.""" __revision__ = 1 # Increment this after each update # Engine configuration # IMPORTANT NOTE: # This section defines engine behaviour and should not be refactored or moved # away from the enclosing START/END markers. Read into memory by backend at # launch and publish of new engine. # -- ENGINE CONFIG START -- SETTINGS = { "items": True, "default_range": "1001-1100", "default_bucketsize": 5, "filename_extensions": ".txt", "binary_filename_extensions": "", "color": "100,180,100", "vendor": "accsyn" } PARAMETERS = {"input_conversion": "never"} ENVS = {} # -- ENGINE CONFIG END -- def __init__(self, argv): super(Engine, self).__init__(argv) @staticmethod def get_path_version_name(): p = os.path.realpath(__file__) parent = os.path.dirname(p) return os.path.dirname(parent), os.path.basename(parent), os.path.splitext(os.path.basename(p))[0] @staticmethod def usage(): (unused_cp, cv, cn) = Common.get_path_version_name() (unused_p, v, n) = Engine.get_path_version_name() Common.log( ' accsyn compute engine "{}" v{}-{}(common: v{}-{}) '.format( n, v, Engine.__revision__, cv, Common.__revision__ ) ) Common.log('') Common.log(' Usage: python %s {--probe|}' % n) Common.log('') Common.log(' --probe Have engine check if it is found and of correct version.') Common.log('') Common.log( ' Execute engine on data provided in ' 'the JSON and ACCSYN_xxx environment variables.' ) Common.log('') def probe(self): """Always available — no external binary required.""" return True def _iter_items(self, item): """Yield frame numbers from a single item ('1080') or range ('1001-1004').""" item = str(item).strip() if not item or item == 'all': yield item or 'all' return if '-' in item: parts = item.split('-', 1) try: start = int(parts[0]) end = int(parts[1]) except ValueError: yield item return if end < start: raise Exception("Invalid item range '{}': end < start".format(item)) for frame in range(start, end + 1): yield frame else: yield item def _execute(self, item, additional_envs=None): """Process each frame in *item* without spawning an external command.""" items = list(self._iter_items(item)) input_path = self.normalize_path(self.data['compute']['input']) Common.log("Hello World engine processing input file {} for {} item/frame(s): {}".format(input_path, len(items), item)) for item in items: Common.log("Hello World, processing frame: {}".format(item)) # Mark frame as started so farm progress updates for bucketed ranges try: self.task_started(str(item)) except Exception: pass time.sleep(2) Common.log("Hello World engine finished.") return 0 if __name__ == "__main__": if "--help" in sys.argv: Engine.usage() else: # Common.set_debug(True) try: engine = Engine(sys.argv) if "--probe" in sys.argv: engine.probe() else: engine.load() # Load data engine.execute() # Run except: print(traceback.format_exc()) Engine.usage() time.sleep(2) sys.exit(1)