|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +import os |
| 3 | +from github import Github, Auth, InputGitTreeElement |
| 4 | +import base64 |
| 5 | + |
| 6 | + |
| 7 | +class GithubArchiverInterface(ABC): |
| 8 | + @abstractmethod |
| 9 | + def identify_target_files(): |
| 10 | + """ |
| 11 | + Read list of target files to be pushed to github, excluding replit's system files. |
| 12 | + Raise error if target folder does not exist or is empty. |
| 13 | + """ |
| 14 | + pass |
| 15 | + |
| 16 | + @abstractmethod |
| 17 | + def commit_to_github(): |
| 18 | + """ |
| 19 | + Commit target files to github. |
| 20 | + """ |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +class GithubArchiver(GithubArchiverInterface): |
| 25 | + |
| 26 | + def __init__(self, project_name, github_access_token, commit_message="Auto-archive") -> None: |
| 27 | + self._project_name = project_name |
| 28 | + self._file_paths = dict() |
| 29 | + self._file_list = list() |
| 30 | + self._commit_sha = "" |
| 31 | + self.__github_access_token = github_access_token |
| 32 | + self._commit_message = commit_message |
| 33 | + |
| 34 | + def get_project_name(self) -> str: |
| 35 | + return self._project_name |
| 36 | + |
| 37 | + def identify_target_files(self) -> None: |
| 38 | + download_folder_path = "./screen-shots" |
| 39 | + extracted_folder_path = os.path.join(download_folder_path, self.get_project_name()) |
| 40 | + assert os.path.isdir(extracted_folder_path) is True, "Target folder does not exist" |
| 41 | + assert len(os.listdir(extracted_folder_path)) != 0, "Target folder is empty" |
| 42 | + |
| 43 | + replit_junk = [ |
| 44 | + '.cache', |
| 45 | + '.upm', |
| 46 | + '.replit', |
| 47 | + 'poetry.lock', |
| 48 | + 'pyproject.toml', |
| 49 | + 'replit_zip_error_log.txt', |
| 50 | + 'replit.nix', |
| 51 | + ] |
| 52 | + |
| 53 | + # Walk through the directory and its subdirectories |
| 54 | + for root, dirs, files in os.walk(extracted_folder_path): |
| 55 | + for file in files: |
| 56 | + file_full_path = os.path.join(root, file) |
| 57 | + file_relative_path = file_full_path.replace(extracted_folder_path, self.get_project_name()) |
| 58 | + if not any(excluded in file_relative_path for excluded in replit_junk): |
| 59 | + self._file_paths[file_relative_path] = file_full_path |
| 60 | + self._file_list.append(file_relative_path) |
| 61 | + |
| 62 | + def get_target_files(self) -> list: |
| 63 | + return self._file_list |
| 64 | + |
| 65 | + def commit_to_github(self) -> None: |
| 66 | + assert len(self._file_list) != 0, "Target files are not identified" |
| 67 | + auth = Auth.Token(self.__github_access_token) |
| 68 | + g = Github(auth=auth) |
| 69 | + repo = g.get_user().get_repo('The-Archive') |
| 70 | + main_branch = repo.get_branch("main") |
| 71 | + main_tree = repo.get_git_tree(sha=main_branch.commit.sha) |
| 72 | + |
| 73 | + tree = list() |
| 74 | + for file_relative_path, file_full_path in self._file_paths.items(): |
| 75 | + |
| 76 | + with open(file_full_path, "rb") as file: |
| 77 | + file_content = file.read() |
| 78 | + |
| 79 | + file_content_based64 = base64.b64encode(file_content) |
| 80 | + |
| 81 | + blob = repo.create_git_blob( |
| 82 | + content=file_content_based64.decode('utf-8'), |
| 83 | + encoding="base64" |
| 84 | + ) |
| 85 | + |
| 86 | + tree.append( |
| 87 | + InputGitTreeElement( |
| 88 | + path=file_relative_path, |
| 89 | + mode="100644", |
| 90 | + type="blob", |
| 91 | + sha=blob.sha, |
| 92 | + ) |
| 93 | + ) |
| 94 | + |
| 95 | + new_tree = repo.create_git_tree( |
| 96 | + tree=tree, |
| 97 | + base_tree=main_tree |
| 98 | + ) |
| 99 | + |
| 100 | + commit = repo.create_git_commit( |
| 101 | + message=self._commit_message, |
| 102 | + tree=repo.get_git_tree(sha=new_tree.sha), |
| 103 | + parents=[repo.get_git_commit(main_branch.commit.sha)], |
| 104 | + ) |
| 105 | + |
| 106 | + archive_ref = repo.get_git_ref(ref='heads/main') |
| 107 | + print(f"Archive_ref: {archive_ref}") |
| 108 | + self._commit_sha = commit.sha |
| 109 | + |
| 110 | + # Commit to Github |
| 111 | + archive_ref.edit(sha=commit.sha) |
| 112 | + print("Upload complete") |
| 113 | + |
| 114 | + g.close() |
| 115 | + |
| 116 | + def get_commit_sha(self) -> str: |
| 117 | + return self._commit_sha |
0 commit comments