def is_right_context(self, dest_path, verbose=True): """Check if we are in the right context for launching the command. If you are using this to introspect available commands, then set verbose to False. """ # check if dest_path check outside or inside only project :) if self.inside_project and not self.outside_project: try: project_path = tools.get_root_project_path(dest_path) except tools.project_path_not_found: if verbose: print( _("ERROR: Can't find project in %s.\nEnsure you launch " "this command from a quickly project directory.") % dest_path) print _("Aborting") return False if self.outside_project and not self.inside_project: try: project_path = tools.get_root_project_path(dest_path) if verbose: print _( "ERROR: %s is a project. You can't launch %s command " "within a project. Please choose another path." % (project_path, self.command)) print _("Aborting") return False except tools.project_path_not_found: pass return True
def loadConfig(can_stop=True, config_file_path=None): """ load configuration from path/.quickly or pwd/.quickly file""" # retrieve .quickly file global project_config project_config = {} # reset project_config try: if config_file_path is None: root_conf_dir = tools.get_root_project_path() else: root_conf_dir = tools.get_root_project_path(config_file_path) quickly_file_path = root_conf_dir + "/.quickly" config = project_config except tools.project_path_not_found: if can_stop: print _("ERROR: Can't load configuration in current path or its parent ones.") sys.exit(1) else: return 1 try: fileconfig = file(quickly_file_path, "rb") for line in fileconfig: fields = line.split("#")[0] # Suppress commentary after the value in configuration file and in full line fields = fields.split("=", 1) # Separate variable from value # normally, we have two fields in "fields" if len(fields) == 2: config[fields[0].strip()] = fields[1].strip() fileconfig.close() except (OSError, IOError), e: print _("ERROR: Can't load configuration in %s: %s" % (quickly_file_path, e)) sys.exit(1)
def is_right_context(self, dest_path, verbose=True): """Check if we are in the right context for launching the command. If you are using this to introspect available commands, then set verbose to False. """ # check if dest_path check outside or inside only project :) if self.inside_project and not self.outside_project: try: project_path = tools.get_root_project_path(dest_path) except tools.project_path_not_found: if verbose: print (_( "ERROR: Can't find project in %s.\nEnsure you launch " "this command from a quickly project directory.") % dest_path) print _("Aborting") return False if self.outside_project and not self.inside_project: try: project_path = tools.get_root_project_path(dest_path) if verbose: print _( "ERROR: %s is a project. You can't launch %s command " "within a project. Please choose another path." % (project_path, self.command)) print _("Aborting") return False except tools.project_path_not_found: pass return True
def saveConfig(config_file_path=None): """ save the configuration file from config dictionnary in project or global quuickly file path is optional (needed by the create command, for instance). getcwd() is taken by default. keep commentaries and layout from original file """ # retrieve .quickly file try: if config_file_path is None: root_conf_dir = tools.get_root_project_path() else: root_conf_dir = tools.get_root_project_path(config_file_path) quickly_file_path = root_conf_dir + "/.quickly" # if no .quickly, create it using config_file_path or cwd except tools.project_path_not_found: if config_file_path: quickly_file_path = os.path.abspath(config_file_path) + "/.quickly" else: quickly_file_path = os.getcwd() + "/.quickly" config = project_config try: filedest = file(quickly_file_path + ".new", "w") try: fileconfig = file(quickly_file_path, "rb") remaingconfigtosave = config.copy() for line in fileconfig: fields = line.split("#")[ 0 ] # Suppress commentary after the value in configuration file and in full line fieldsafter = line.split("#")[1:] fields = fields.split("=", 1) # Separate variable from value # normally, we have two fields in "fields" and it should be used by config tabular if len(fields) == 2: if fields[0].strip() in remaingconfigtosave: line = fields[0].strip() + " = " + str(remaingconfigtosave.pop(fields[0].strip())) if len(fieldsafter) > 0: line = line + " #" + "#".join(fieldsafter) # fieldsafter already contains \n else: line = line + "\n" else: # old config value, no more on the dictionary. So, remove it: line = "" filedest.write( line ) # commentaries or empty lines, anything other things which is not useful will be printed unchanged # write remaining data if some (new data not in the old config file). filedest.write( "".join(elem + " = " + str(remaingconfigtosave[elem]) + "\n" for elem in remaingconfigtosave) ) # \n here for last element (and not be added, when no iteration to do) # print "\n".join(elem + " = " + remaingconfigtosave[elem] for elem in remaingconfigtosave) fileconfig.close() except (OSError, IOError), e: # write config file from scratch (no previous file found) filedest.write("\n".join(elem + " = " + str(config[elem]) for elem in config) + "\n") finally: filedest.close() os.rename(filedest.name, quickly_file_path)
def launch(self, current_dir, command_args, project_template=None): """Launch command and hooks for it This command will perform the right action (insider function or script execution) after having checked the context. """ if not self.is_right_context(current_dir): # check in verbose mode return 1 # get root project dir try: project_path = tools.get_root_project_path(current_dir) inside_project = True except tools.project_path_not_found: # launch in current project project_path = current_dir inside_project = False # transition if we are inside a project and template is not None (builtins) # (call upgrade from native template) if inside_project and self.name != "upgrade" and self.template: (project_version, template_version) = templatetools.get_project_and_template_versions(self.template) if project_version < template_version: try: return_code = get_all_commands()[self.template]['upgrade'].launch( current_dir, [project_version, template_version], project_template) if return_code == 0: templatetools.update_version_in_project_file(template_version, self.template) else: sys.exit(return_code) except KeyError: # if KeyError, no upgrade command for this template pass if self.prehook: return_code = self.prehook(self.template, project_template, project_path, command_args) if return_code != 0: self._errmsg(self.prehook.__name__, return_code) return return_code if callable(self.command): # Internal function return_code = self.command(project_template, project_path, command_args) else: # External command return_code = subprocess.call( [self.command] + command_args, cwd=project_path) if return_code != 0: self._errmsg(self.name, return_code) return return_code if self.posthook: return_code = self.posthook(project_template, project_path, command_args) if return_code != 0: self._errmsg(self.posthook.__name__, return_code) return return_code return 0
def loadConfig(can_stop=True, config_file_path=None): """ load configuration from path/.quickly or pwd/.quickly file""" # retrieve .quickly file global project_config project_config = {} # reset project_config try: if config_file_path is None: root_conf_dir = tools.get_root_project_path() else: root_conf_dir = tools.get_root_project_path(config_file_path) quickly_file_path = root_conf_dir + '/.quickly' config = project_config except tools.project_path_not_found: if can_stop: print _( "ERROR: Can't load configuration in current path or its parent ones." ) sys.exit(1) else: return (1) try: fileconfig = file(quickly_file_path, 'rb') for line in fileconfig: fields = line.split( '#' )[0] # Suppress commentary after the value in configuration file and in full line fields = fields.split('=', 1) # Separate variable from value # normally, we have two fields in "fields" if len(fields) == 2: config[fields[0].strip()] = fields[1].strip() fileconfig.close() except (OSError, IOError), e: print _("ERROR: Can't load configuration in %s: %s" % (quickly_file_path, e)) sys.exit(1)
def launch(self, current_dir, command_args, project_template=None): """Launch command and hooks for it This command will perform the right action (insider function or script execution) after having checked the context. """ if not self.is_right_context(current_dir): # check in verbose mode return 1 # get root project dir try: project_path = tools.get_root_project_path(current_dir) inside_project = True except tools.project_path_not_found: # launch in current project project_path = current_dir inside_project = False # transition if we are inside a project and template is not None (builtins) # (call upgrade from native template) if inside_project and self.name != "upgrade" and self.template: (project_version, template_version ) = templatetools.get_project_and_template_versions(self.template) if project_version < template_version: try: return_code = get_all_commands()[ self.template]['upgrade'].launch( current_dir, ["--internal", project_version, template_version], project_template) if return_code != 0: sys.exit(return_code) except KeyError: # if KeyError, no upgrade command for this template pass if self.prehook: return_code = self.prehook(self.template, project_template, project_path, command_args) if return_code != 0: self._errmsg(self.prehook.__name__, return_code) return return_code if callable(self.command): # Internal function return_code = self.command(project_template, project_path, command_args) else: # External command return_code = subprocess.call([self.command] + command_args, cwd=project_path) if return_code != 0: self._errmsg(self.name, return_code) return return_code if self.posthook: return_code = self.posthook(project_template, project_path, command_args) if return_code != 0: self._errmsg(self.posthook.__name__, return_code) return return_code return 0
def get_root_project_path(path=None): '''return project path if found. Return None elsewhere''' try: return tools.get_root_project_path(path) except tools.project_path_not_found, e: return None
def saveConfig(config_file_path=None): """ save the configuration file from config dictionnary in project or global quuickly file path is optional (needed by the create command, for instance). getcwd() is taken by default. keep commentaries and layout from original file """ # retrieve .quickly file try: if config_file_path is None: root_conf_dir = tools.get_root_project_path() else: root_conf_dir = tools.get_root_project_path(config_file_path) quickly_file_path = root_conf_dir + '/.quickly' # if no .quickly, create it using config_file_path or cwd except tools.project_path_not_found: if config_file_path: quickly_file_path = os.path.abspath(config_file_path) + '/.quickly' else: quickly_file_path = os.getcwd() + "/.quickly" config = project_config try: filedest = file(quickly_file_path + '.new', 'w') try: fileconfig = file(quickly_file_path, 'rb') remaingconfigtosave = config.copy() for line in fileconfig: fields = line.split( '#' )[0] # Suppress commentary after the value in configuration file and in full line fieldsafter = line.split('#')[1:] fields = fields.split('=', 1) # Separate variable from value # normally, we have two fields in "fields" and it should be used by config tabular if len(fields) == 2: if fields[0].strip() in remaingconfigtosave: line = fields[0].strip() + " = " + str( remaingconfigtosave.pop(fields[0].strip())) if len(fieldsafter) > 0: line = line + " #" + "#".join( fieldsafter) # fieldsafter already contains \n else: line = line + "\n" else: # old config value, no more on the dictionary. So, remove it: line = "" filedest.write( line ) # commentaries or empty lines, anything other things which is not useful will be printed unchanged # write remaining data if some (new data not in the old config file). filedest.write( "".join(elem + " = " + str(remaingconfigtosave[elem]) + '\n' for elem in remaingconfigtosave) ) #\n here for last element (and not be added, when no iteration to do) # print "\n".join(elem + " = " + remaingconfigtosave[elem] for elem in remaingconfigtosave) fileconfig.close() except (OSError, IOError), e: # write config file from scratch (no previous file found) filedest.write("\n".join(elem + " = " + str(config[elem]) for elem in config) + "\n") finally: filedest.close() os.rename(filedest.name, quickly_file_path)