Skip to content

Commit 0d97251

Browse files
committed
Add github_archiver and tests
1 parent 37a005d commit 0d97251

5 files changed

Lines changed: 233 additions & 54 deletions

File tree

funcs/github_archiver.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

funcs/replit_playwright.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

requirements.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,59 @@
1+
appnope==0.1.3
2+
asttokens==2.4.1
13
certifi==2023.11.17
4+
cffi==1.16.0
25
charset-normalizer==3.3.2
6+
comm==0.2.0
7+
cryptography==41.0.7
8+
debugpy==1.8.0
9+
decorator==5.1.1
10+
Deprecated==1.2.14
11+
executing==2.0.1
312
flake8==6.1.0
413
greenlet==3.0.1
514
idna==3.6
615
iniconfig==2.0.0
16+
ipykernel==6.27.1
17+
ipython==8.19.0
18+
jedi==0.19.1
19+
jupyter_client==8.6.0
20+
jupyter_core==5.5.1
21+
matplotlib-inline==0.1.6
722
mccabe==0.7.0
23+
nest-asyncio==1.5.8
824
packaging==23.2
25+
parso==0.8.3
26+
pexpect==4.9.0
27+
platformdirs==4.1.0
928
playwright==1.40.0
1029
playwright-stealth==1.0.6
1130
pluggy==1.3.0
31+
prompt-toolkit==3.0.43
32+
psutil==5.9.7
33+
ptyprocess==0.7.0
34+
pure-eval==0.2.2
1235
pycodestyle==2.11.0
36+
pycparser==2.21
1337
pyee==11.0.1
1438
pyflakes==3.1.0
39+
PyGithub==2.1.1
40+
Pygments==2.17.2
41+
PyJWT==2.8.0
42+
PyNaCl==1.5.0
1543
pytest==7.4.3
1644
pytest-base-url==2.0.0
1745
pytest-playwright==0.4.3
46+
python-dateutil==2.8.2
1847
python-dotenv==1.0.0
1948
python-slugify==8.0.1
49+
pyzmq==25.1.2
2050
requests==2.31.0
51+
six==1.16.0
52+
stack-data==0.6.3
2153
text-unidecode==1.3
54+
tornado==6.4
55+
traitlets==5.14.0
2256
typing_extensions==4.9.0
2357
urllib3==2.1.0
58+
wcwidth==0.2.12
59+
wrapt==1.16.0

tests/test_github_archiver.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from github import Github, Auth
2+
import unittest
3+
from funcs.github_archiver import GithubArchiver
4+
import os
5+
from dotenv import load_dotenv
6+
load_dotenv()
7+
8+
9+
class Test(unittest.TestCase):
10+
11+
def test_archiver_raise_error_if_target_folder_does_not_exist(self):
12+
archiver = GithubArchiver(
13+
project_name="directory_not_exist",
14+
github_access_token=os.environ['GITHUB_ACCESS_TOKEN']
15+
)
16+
with self.assertRaises(AssertionError) as ctx_manager:
17+
archiver.identify_target_files()
18+
self.assertEqual(str(ctx_manager.exception), "Target folder does not exist")
19+
20+
def test_archiver_raise_error_if_target_folder_is_empty(self):
21+
archiver = GithubArchiver(
22+
project_name="empty_folder",
23+
github_access_token=os.environ['GITHUB_ACCESS_TOKEN']
24+
)
25+
with self.assertRaises(AssertionError) as ctx_manager:
26+
archiver.identify_target_files()
27+
self.assertEqual(str(ctx_manager.exception), "Target folder is empty")
28+
29+
def test_archiver_return_list_of_target_files(self):
30+
target_list = [
31+
"SlipperyGargantuanDebuggers/test-README.md",
32+
"SlipperyGargantuanDebuggers/road.jpg",
33+
"SlipperyGargantuanDebuggers/test.py",
34+
"SlipperyGargantuanDebuggers/main.py",
35+
"SlipperyGargantuanDebuggers/Group-1/test-1.txt",
36+
"SlipperyGargantuanDebuggers/Group-2/test-2.txt",
37+
]
38+
39+
archiver = GithubArchiver(
40+
project_name="SlipperyGargantuanDebuggers",
41+
xxwgithub_access_token=os.environ['GITHUB_ACCESS_TOKEN']
42+
)
43+
archiver.identify_target_files()
44+
# https://stackoverflow.com/questions/12813633/how-to-assert-two-list-contain-the-same-elements-in-python
45+
self.assertCountEqual(archiver.get_target_files(), target_list)
46+
47+
def test_archiver_raise_error_if_target_files_not_set(self):
48+
archiver = GithubArchiver(
49+
project_name="SlipperyGargantuanDebuggers",
50+
github_access_token=os.environ['GITHUB_ACCESS_TOKEN']
51+
)
52+
with self.assertRaises(AssertionError) as ctx_manager:
53+
archiver.commit_to_github()
54+
55+
self.assertEqual(str(ctx_manager.exception), "Target files are not identified")
56+
57+
def test_archiver_upload_target_files_to_github(self):
58+
archiver = GithubArchiver(
59+
project_name="SlipperyGargantuanDebuggers",
60+
github_access_token=os.environ['GITHUB_ACCESS_TOKEN']
61+
)
62+
archiver.identify_target_files()
63+
archiver.commit_to_github()
64+
65+
auth = Auth.Token(os.environ['GITHUB_ACCESS_TOKEN'])
66+
g = Github(auth=auth)
67+
repo = g.get_user().get_repo('The-Archive')
68+
commit = repo.get_commit(archiver.get_commit_sha())
69+
target_list = list()
70+
for file in commit.files:
71+
target_list.append(file.filename)
72+
g.close()
73+
74+
self.assertCountEqual(archiver.get_target_files(), target_list)
75+
76+
77+
if __name__ == "__main__":
78+
unittest.main()

tests/test_replit_scrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from funcs.replit_scrapper import ReplitScrapper
3-
import os
3+
# import os
44
from dotenv import load_dotenv
55
load_dotenv()
66

@@ -20,7 +20,7 @@ def test_scrapper_return_replit_url(self):
2020
scrapper.set_replit_url(test_url)
2121
self.assertEqual(scrapper.get_replit_url(), test_url)
2222

23-
# Commented out to avoid acount freezes
23+
# Commented out to avoid replit acount freezes
2424
# def test_scrapper_login_with_invalid_credentials(self):
2525
# scrapper = ReplitScrapper(login_name = os.environ['EMAIL'], login_password = "ThisIsNotTheCorrectPassword")
2626
# with self.assertRaises(ValueError) as ctx_manager:

0 commit comments

Comments
 (0)