def file_path(self): """ Returns the complete file path for the level. """ shell = Shell() if self.depth == -1: return shell.getenv('@JOBS_ROOT') path = "" joiner = "" for n in self.branch(): path = n.name + joiner + path joiner = os.sep # sys.stderr.write(str(self.name) + " at " + str(self.depth) + ": " + path + "\n") # parent_id = self.parent_id # path = self.name # tmp_level = self # while parent_id: # tmp_level = tmp_level.parent # parent_id = tmp_level.parent_id # if tmp_level.name: # path = tmp_level.name + os.sep + path return shell.getenv('@JOBS_ROOT') + os.sep + path
def write_last(self, level): # Write information out to .last file in users HOME # Only if it is different to the current one # FIXME: Should use shell object shell = Shell() try: last_file = shell.getenv("@LAST_FILE") except EnvVarNotFound: last_file = os.getenv("HOME") + os.sep + ".last" old_syntax = "" try: last = open(last_file, "r") old_syntax = last.readline() last.close() except IOError: pass # FIXME: Have to do this test because JOB levels don't have a parent! if level: # If new is different to old then write it out new_syntax = level.syntax() if new_syntax != old_syntax: last = open(last_file, "w") last.write(level.syntax()) last.close()
def status(self): shell = Shell() path = "" joiner = "" for index, level_name in enumerate(settings.hierarchy()): try: path += joiner + shell.getenv("@" + level_name.upper()) except EnvVarNotFound: continue joiner = ":" sys.stderr.write("%s\n" % path)
def template(self, level): """ Generic template method for making the base content of terminal levels and non-terminal "share" folders. """ shell = Shell() module_name = self.__class__.__name__.split("Controller")[0].lower() template_path = os.path.join(shell.getenv("PIPELINE"), "modules", "resources", module_name) destination_path = level.file_path() if level.depth == pipeline.settings.depth(): # Template terminal folder template_path += os.sep + "terminal" elif level.depth < pipeline.settings.depth(): # Template internal folder template_path += os.sep + "internal" destination_path += os.sep + 'share' else: pipeline.utils.report("Error - Invalid level depth") contents = os.listdir(template_path) for item in contents: item_source = template_path + os.sep + item item_destination = os.path.join(destination_path, item) if not os.path.exists(item_destination): mode = os.stat(item_source)[stat.ST_MODE] if stat.S_ISDIR(mode): shutil.copytree(item_source, item_destination) else: shutil.copy(item_source, item_destination) # If parent level has a non-zero id if level.parent: if level.parent.id: self.template(level.parent)
def from_env(depth): """ Returns the syntax up to "depth" as is currently set in the environment. """ shell = Shell() path = "" joiner = "" for index, level_name in enumerate(pipeline.settings.hierarchy()): if index > depth: return path try: path += joiner + shell.getenv("@" + level_name.upper()) except EnvVarNotFound: if not index == depth: raise PathError(level_name) return path joiner = ":" return path
def set(self, args): """ Sets the environment to correspond to this level. """ # Get full syntax syntax, depth = self.syntax_and_depth(args) # Check to see if level exists level_list = Level.find_by_syntax(syntax) if not level_list: pipeline.utils.report("Error - Syntax does not match any levels") return if len(level_list) > 1: pipeline.utils.report("Error - Syntax matches multiple levels") return # Get the level we've found level = level_list[0] shell = Shell() # Grab settings info for reference hierarchy = settings.hierarchy() abbr = settings.abbreviations() # Clean out the any pipeline paths from PATH so we can reset it pattern = shell.getenv("@JOBS_ROOT") + r'/[\w/]*/share/bin' shell.clean('PATH', pattern) # Clean prod from PATH as well pattern = shell.getenv("@JOBS_ROOT") + r'/prod/[\w/]*' shell.clean('PATH', pattern) aliases = ['', 'bin'] # Remove all environment variables and aliases # associated with levels below this one depth = level.depth + 1 if level.depth < len(hierarchy) - 1: for level_name in hierarchy[level.depth + 1:]: shell.unset("@" + level_name.upper()) for alias in aliases: shell.unalias(abbr[depth] + alias) depth += 1 bin_path = "" # Set environment from level for l in level.branch(): shell.set("@" + hierarchy[l.depth], l.name) bin_path += "%s/share/bin:" % l.file_path() # Set aliases cd_path = "cd $@JOBS_ROOT" for index, level_name in enumerate(hierarchy[:level.depth + 1]): # alias current level cd_path += os.sep + "$@" + level_name.upper() shell.alias(abbr[index], cd_path) prod = shell.getenv("@PROD") bin_path += "%s/bin" % prod shell.insert("PATH", bin_path) self.write_last(level) shell.commit()
def remove(self, args): """ Removes the level and all it's children from the filesystem and the database. """ # Get full syntax syntax, depth = self.syntax_and_depth(args) # Check to see if level exists level_list = Level.find_by_syntax(syntax) if not level_list: pipeline.utils.report("Error - Syntax does not match any levels") return if len(level_list) > 1: pipeline.utils.report("Error - Syntax matches multiple levels") return level = level_list[0] # Get settings info hierarchy = settings.hierarchy() abbr = settings.abbreviations() aliases = ['', 'bin'] shell = Shell() # If we're removing a level in our current environment clean_env = True for n in level.branch(): # Check each level of the env against our level branch clean_env = True try: clean_env = (n.name != shell.getenv("@" + hierarchy[n.depth].upper())) except EnvVarNotFound: clean_env = False break # Necessary? if not clean_env: break # If necessary unset all environment variables and aliases # associated with this level and those below if clean_env: depth = level.depth for level_name in hierarchy[level.depth:]: shell.unset("@" + level_name.upper()) for alias in aliases: shell.unalias(abbr[depth] + alias) depth += 1 # Write out a syntax file so it is up-to-date with current env self.write_last(level.parent) shell.commit() # Remove the level from disk shutil.rmtree(level.file_path()) # Remove the level and all its children from the database level.destroy_children() session.delete(level) session.flush()
from sqlalchemy import create_engine, MetaData from sqlalchemy.orm import create_session from pipeline.shell import Shell import pipeline.settings import os import sys # db_address = "mysql://%s:%s@%s/%s" % (settings.db_user, # settings.db_password, # settings.db_host, # settings.db_name) shell = Shell() pipeline_folder = shell.getenv("PIPELINE") db_file = os.path.join(pipeline_folder, "db", "pipeline.db") # Triple slash for absolute path db_address = "sqlite:///%s" % db_file # sys.stderr.write("Address: " + db_address + "\n") db = create_engine(db_address) # db.echo = True metadata = MetaData(db) session = create_session()