def add_env(self, key, value): '''add a key/value pair to the environment. Should begin with SCIF to maintain proper namespace Parameters ========== key: the environment variable name. For SCIF, slashes should be removed and replaced with underscore. value: the value to set for the environment variable ''' if not hasattr(self,'environment'): self.environment = dict() if not key.startswith('SCIF'): msg = 'Environment variable outside SCIF namespace not recommended.' bot.warning(msg) # If the variable already exists, status is "update" action = 'new' if key in self.environment: action = 'update' self.environment[key] = value bot.debug('[environment:%s][%s=%s]' %(action, key, value))
def run(self, app=None, args=None): """run an app. This means the following: 1. Check that the app is valid for the client. Don't proceed otherwise 2. Set the client app to be active 3. update the environment to indicate the app is active 4. set the entry point and entry folder relevant to the app Parameters ========== app: the name of the scif app to run args: a list of one or more additional arguments to pass to runscript """ interactive = False config = self.app(app) if "apprun" not in config: bot.debug("%s does not have a runscript." % app) interactive = True self.activate(app, args=args) # checks for existence # sets _active to app's name # updates environment # sets entrypoint # sets entryfolder return self._exec(app, interactive=interactive)
def load_recipe(path): '''load will return a loaded in (user) scif configuration file Parameters ========== path: a path to a deid file Returns ======= config: a parsed recipe configuration for SCIF ''' path = os.path.abspath(path) if os.path.exists(path): # Read in spec, clean up extra spaces and newlines spec = [ x.strip('\n').strip(' ') for x in read_file(path) if x.strip('\n').strip(' ') not in [''] ] spec = [x for x in spec if x not in ['', None]] config = OrderedDict() section = None name = None while len(spec) > 0: # Clean up white trailing/leading space line = spec.pop(0).strip() # Comment if line.startswith("#"): continue # A new section? elif line.startswith('%'): # Remove any comments line = line.split('#', 1)[0].strip() # Is there a section name? parts = line.split(' ') if len(parts) > 1: name = ' '.join(parts[1:]) section = re.sub('[%]|(\s+)', '', parts[0]).lower() config = add_section(config=config, section=section, name=name) # If we have a section, and are adding it elif section is not None: spec = [line] + spec config = read_section(config=config, spec=spec, section=section, name=name) else: bot.debug("Cannot find recipe file %s" % path) return config
def add_section(config, section, name=None): '''add section will add a section (and optionally) section name to a config Parameters ========== config: the config (dict) parsed thus far section: the section type (e.g., appinstall) name: an optional name, added as a level (e.g., google-drive) Resulting data structure is: config['registry']['apprun'] config[name][section] ''' if section is None: bot.error( 'You must define a section (e.g. %appenv) before any action.') sys.exit(1) if section not in sections: bot.error("%s is not a valid section." % section) sys.exit(1) global_section = 'apps' # Add the global section, if doesn't exist if global_section not in config: config[global_section] = OrderedDict() if name is not None: if name not in config[global_section]: config[global_section][name] = OrderedDict() if section not in config[global_section][name]: config[global_section][name][section] = [] bot.debug("Adding section %s %s" % (name, section)) return config
def load_recipe(path): '''load will return a loaded in (user) scif configuration file Parameters ========== path: a path to a scif recipe file Returns ======= config: a parsed recipe configuration for SCIF ''' path = os.path.abspath(path) if os.path.exists(path): # Read in spec, skip empty lines, don't strip remaining spec = [ x.strip('\n') for x in read_file(path) if not re.match(r'^\s+$', x.strip('\n')) ] spec = [x for x in spec if x not in ['', None]] config = OrderedDict() section = None name = None while len(spec) > 0: # Clean up white trailing/leading space line = spec.pop(0) stripped = line.strip() # Comment if stripped.startswith("#"): continue # A new section? elif stripped.startswith('%'): # Remove any comments line = line.split('#', 1)[0].strip() # Is there a section name? parts = line.split(' ') if len(parts) > 1: name = ' '.join(parts[1:]) section = re.sub(r'[%]|(\s+)', '', parts[0]).lower() config = add_section(config=config, section=section, name=name) # If we have a section, and are adding it elif section is not None: spec = [line] + spec config = read_section(config=config, spec=spec, section=section, name=name) # Make sure app environments are sourced as first line of recipe config = finish_recipe(config) else: bot.debug("Cannot find recipe file %s" % path) return config