def remove(name, folder, repo, conda, remove_all, **kwargs): # remove the project from the project file try: Project.project_exists(name) print("Project does not exist") if not kwargs['test']: sys.exit(1) except ProjectNameAlreadyExists: project = Project.remove_project(name) print("Successfully removed cobra project {} from the project file". format(name)) # load config Config.getInstance().load_config() # github user object user = get_github_user() if remove_all: repo = True conda = True folder = True if repo: try: repo = user.get_repo(project.repo_name) repo.delete() print("Successfully removed repo") except UnknownObjectException as e: if e.status == 404: print("Repo could not be deleted! Not existing") if e.status == 403: print( 'Permission problem, please reinstall cobra (--clean; --install)' ) if folder: delete_path(project.project_path) print("Successfully removed folders") if conda: if check_env_exists(project.conda_name): try: # remove env subprocess.check_output([ 'conda', 'env', 'remove', '-y', '--name', project.conda_name ]) print("Successfully removed conda environment") except subprocess.CalledProcessError: print('Environment was not deactivated!') print( 'Remove the environment by hand with the following command:\n' ) print('conda deactivate\n') print('conda env remove --name ' + project.conda_name + '-y') print('\n') else: print("Conda environment does not exist")
def pull_repo(project): # pull repo print("Pulling the repo...") cwd = os.getcwd() git_url = "{}{}/{}.git".format( Config.getInstance().connection_type.url, Config.getInstance().username, project.repo_name) git.Git(cwd).clone(git_url) print("Pulling done")
def test_load_config(self, tmp_path, monkeypatch): config_content = """!Config _connection_type: &id001 !ConnectionType _name: ssh _url: '[email protected]:' _used_vcs: !VCS _connection_types: - !ConnectionType _name: https _url: https://github.com/ - *id001 _name: Github _username: testUser""" conf_file_path = tmp_path / "load_config" conf_file_path.write_text(config_content) monkeypatch.setattr(Paths, "CONF_FILE_PATH", conf_file_path.__str__()) conf = Config.getInstance() conf.load_config() conf._sec = "testSec" assert conf.used_vcs.name == "Github" assert conf.connection_type.name == "ssh" assert conf.connection_type.url == "[email protected]:" assert conf.username == "testUser" assert conf._sec == "testSec" assert conf.vcses[0].name == "Github" conf.used_vcs = None
def user_password_dialog(error=None): if error is None: error = {} try: if not error: github, username, password = dialog_username_password() auth = github.get_user().create_authorization( scopes=Structures.AUTH_SCOPES, note='cobra') else: if error['key'] == 0: print(error['message']) github, username, password = dialog_username_password() auth = github.get_user().create_authorization( scopes=Structures.AUTH_SCOPES, note='cobra') if error['key'] == 1: print(error['message']) github, username, password = error['cred'] tfa = ask_questions(['input'], [Texts.TFA_TEXT], ['tfa'], [[]])['tfa'] auth = github.get_user().create_authorization( scopes=Structures.AUTH_SCOPES, onetime_password=tfa, note='cobra') sec = auth.token or "" github = Github(sec) # to test if the github object has a correct authentication github.get_user().id Config.getInstance().username = username Config.getInstance().sec = sec except GithubException as e: error_msg = {} if e.status == 422: error_msg['message'] = Texts.TOKEN_ALREADY_EXISTS_TEXT error_msg['key'] = 1 if e.status == 401: if e.data['message'] == 'Bad credentials': error_msg['key'] = 0 else: error_msg['key'] = 1 error_msg['message'] = e.data['message'] error_msg['cred'] = (github, username, password) return error_msg
def installation(): """ With this method the user installs the tool. That means he creates the config and the project file, enters the user credentials as well as defines the connection type. """ # ask for the vcs which are stored in the vc.yml file vcs_selection = ask_questions(['list'], [Texts.VCS_SELECT_TEXT], ['vcs'], [Config.getInstance().vcses]) used_vcs = Config.getInstance().used_vcs = get_object_from_list_by_name( vcs_selection['vcs'], Config.getInstance().vcses) # finish vcs install (ssh http selection; username and password or token) error = user_password_dialog() while error: error = user_password_dialog(error) or {} con_selection = ask_questions(['list'], [Texts.CON_SELECT_TEXT], ['con_type'], [used_vcs.connection_types]) Config.getInstance().connection_type = get_object_from_list_by_name( con_selection['con_type'], used_vcs.connection_types) # create files and folders create_files_folders() # save infos Config.getInstance().save_config()
def create(name, python_version, from_file, **kwargs): # load config Config.getInstance().load_config() if from_file: print("Creating project from file") project = Project.project_from_file() print(".cobra file found. Continue processing...") name = project.project_name if os.path.basename(os.getcwd()) != project.repo_name: delete_path(os.path.join(os.getcwd(), '.cobra')) project.project_path = os.path.join(os.getcwd(), project.repo_name) else: project.project_path = os.getcwd() else: project = Project( os.getcwd(), name, "repo_name" in kwargs if kwargs['repo_name'] else name, "conda_name" in kwargs if kwargs['conda_name'] else name, Config.getInstance().username, Config.getInstance().used_vcs, python_version) # check for if project exists Project.project_exists(name) # creat missing elements Project.create_project_parts(project, **kwargs) project.create_project_file() # save path, conda name, name, git link, python version into project file project.append_project() print("****************************************************") print("****************************************************") print("Successfully created project {}".format(project.project_name)) print("****************************************************") print("****************************************************")
def test_save_config(self, tmp_path, monkeypatch): conf = Config.getInstance() conf_file_path = tmp_path / "save_config" conf.username = "******" conf.connection_type = ConnectionType("TestConType", "TestUrl") monkeypatch.setattr(Paths, "CONF_FILE_PATH", conf_file_path.__str__()) conf.save_config() assert os.path.exists(conf_file_path.__str__()) import yaml with open(conf_file_path.__str__(), 'r') as f: config = yaml.load(f, Loader=yaml.Loader) assert config.username == "Test" assert config.connection_type.name == "TestConType" assert config.connection_type.url == "TestUrl" assert config.used_vcs is None
def get_github_user(): # get github user github = Github(Config.getInstance().sec) user = github.get_user() return user
def test_get_instance(self): config1 = Config.getInstance() config2 = Config.getInstance() assert config1 == config2