def make(self, args): """ This method is responsible for: - Creating the level folder - Creating the "share" folder - Templating the share folder - Entering the level into the database It will exit if the level already exists or if the path is in some way invalid. """ # Get full syntax syntax, depth = self.syntax_and_depth(args) # Check to see if level exists level_list = Level.find_by_syntax(syntax) if level_list: pipeline.utils.report("Error - Level %s already exists." % syntax) return # Try to get the parent level parent_syntax = ":".join(syntax.split(":")[:-1]) parent = None if parent_syntax: # Retrieve parent in a list object parent = Level.find_by_syntax(parent_syntax) if not parent: pipeline.utils.report("Error - Level %s does not exist. Cannot create a child level." % parent_syntax) return parent = parent[0] # Create and setup new level new_level = Level() new_level.name = syntax.split(":")[-1] new_level.parent = parent new_level.depth = depth new_level.created_at = datetime.datetime.today() # Create level folder on disk file_path = new_level.file_path() os.mkdir(file_path) # Create the share folder os.mkdir(file_path + os.sep + "share") # Process template self.template(new_level) # Create the level in the database session.save(new_level) session.flush() # Set up the environment for the level self.set(args)
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()
def destroy_children(self): """ Destroys all child levels of the instance. """ if self.depth == pipeline.settings.depth(): # Level is a terminal level and has no children return query = session.query(Level) # subquery = select([Taggable.c.id], query._criterion, # from_obj= query._from_obj).as_scalar() # query = query.filter( ) filters = [] subquery = Level.parent_id == self.id final = subquery # Somehow this shit works! # for i in range(self.depth, len(pipeline.settings.hierarchy()) - 1, 1): # alias = Level._table.alias() # subquery = Level.parent_id.in_(select([alias.c.parent_id], subquery)) # final = final | subquery for i in range(self.depth, len(pipeline.settings.hierarchy()) - 1, 1): alias = Level._table.alias() subquery = alias.c.parent_id == self.id for j in range(self.depth, i, 1): next_alias = Level._table.alias() subquery = next_alias.c.parent_id.in_(select([alias.c.id], subquery)) alias = next_alias subquery = Level.parent_id.in_(select([alias.c.id], subquery)) final = final | subquery # alias = Level._table.alias() # # alias2 = Level._table.alias() # subquery = alias.c.parent_id == self.id # subquery = Level.parent_id.in_(select([alias.c.id], subquery)) # final = final | subquery # alias2 = Level._table.alias() # subquery = alias2.c.parent_id == self.id # subquery = alias.c.parent_id.in_(select([alias2.c.id], subquery)) # subquery = Level.parent_id.in_(select([alias.c.id], subquery)) # final = final | subquery # # alias3 = Level._table.alias() # subquery = alias3.c.parent_id == self.id # subquery = alias2.c.parent_id.in_(select([alias3.c.id], subquery)) # subquery = alias.c.parent_id.in_(select([alias2.c.id], subquery)) # subquery = Level.parent_id.in_(select([alias.c.id], subquery)) # final = final | subquery query = query.filter(final) # sys.stderr.write(str(query) + "\n\n") level_list = query.all() # Must be a better way of doing this! for level in level_list: session.delete(level) session.flush()