def post(self, request, *args, **kwargs): obj = self.get_object() if obj.has_notebook: commit = request.data.get('commit') commit = to_bool(commit) if commit is not None else True if commit: # Commit changes git.commit(obj.repo.path, request.user.email, request.user.username) else: # Reset changes git.undo(obj.repo.path) stop_notebook.delay(project_id=obj.id) return Response(status=status.HTTP_200_OK)
def handle_new_files(user_id, repo_id, tar_file_name): if not tarfile.is_tarfile(tar_file_name): raise ValueError('Received wrong file format.') User = get_user_model() try: user = User.objects.get(id=user_id) except User.DoesNotExist: logger.warning( 'User with id `{}` does not exist anymore.'.format(user_id)) return try: repo = Repo.objects.get(id=repo_id) # Checkout to master git.checkout_commit(repo.path) except User.DoesNotExist: logger.warning( 'Repo with id `{}` does not exist anymore.'.format(repo_id)) return # Destination files new_repo_path = repo.get_tmp_tar_path() # clean the current path from all files path_files = os.listdir(repo.path) for member in path_files: if member == '.git': continue member = os.path.join(repo.path, member) if os.path.isfile(member): os.remove(member) else: delete_path(member) # Move the tar inside the repo path shutil.move(tar_file_name, new_repo_path) # Untar the file with tarfile.open(new_repo_path) as tar: tar.extractall(repo.path) # Delete the current tar os.remove(new_repo_path) # Get the git repo if not git.get_status(repo.path): return # commit changes git.commit(repo.path, user.email, user.username)
def post(self, request, *args, **kwargs): obj = self.get_object() if obj.has_notebook: commit = request.data.get('commit') commit = to_bool(commit) if commit is not None else True if commit: # Commit changes git.commit(obj.repo.path, request.user.email, request.user.username) else: # Reset changes git.undo(obj.repo.path) stop_notebook.delay(project_id=obj.id) elif obj.notebook and obj.notebook.is_running: obj.notebook.set_status(status=ExperimentLifeCycle.STOPPED, message='Notebook was stopped') return Response(status=status.HTTP_200_OK)
def test_checkout_commit_and_master(self): git_url = 'https://github.com/polyaxon/empty.git' # Create repo repo = ExternalRepo(project=self.project, git_url=git_url) repo.save() # Check last commit assert repo.last_commit is None # Add new file file_path = os.path.join(repo.path, 'file1.dat') open(file_path, 'w+') assert git.get_status(repo.path) is not None git.commit(repo.path, '*****@*****.**', 'username') # Check last commit commit1 = repo.last_commit[0] assert commit1 is not None # Add new file file_path = os.path.join(repo.path, 'file2.dat') open(file_path, 'w+') assert git.get_status(repo.path) is not None git.commit(repo.path, '*****@*****.**', 'username') # Check last commit commit2 = repo.last_commit[0] assert commit2 is not None # Commits are different assert commit1 != commit2 # Checkout to commit1 git.checkout_commit(repo_path=repo.path, commit=commit1) assert repo.last_commit[0] == commit1 # Checkout to master git.checkout_commit(repo_path=repo.path) assert repo.last_commit[0] == commit2