awacke1 commited on
Commit
cc41871
·
verified ·
1 Parent(s): 1864e70

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import os
4
+ import shutil
5
+ import zipfile
6
+ from github import Github
7
+ from git import Repo
8
+
9
+ def download_github_repo(url, local_path):
10
+ # Clone the repository
11
+ Repo.clone_from(url, local_path)
12
+
13
+ def create_zip_file(source_dir, output_filename):
14
+ shutil.make_archive(output_filename, 'zip', source_dir)
15
+
16
+ def check_repo_exists(g, repo_name):
17
+ try:
18
+ g.get_repo(repo_name)
19
+ return True
20
+ except:
21
+ return False
22
+
23
+ def create_repo(g, repo_name):
24
+ user = g.get_user()
25
+ user.create_repo(repo_name.split('/')[-1])
26
+
27
+ def delete_repo(g, repo_name):
28
+ repo = g.get_repo(repo_name)
29
+ repo.delete()
30
+
31
+ def push_to_github(local_path, repo_url, github_token):
32
+ repo = Repo(local_path)
33
+ origin = repo.create_remote('origin', repo_url)
34
+ origin.push(refspec='{}:{}'.format('master', 'master'))
35
+
36
+ def main():
37
+ st.title("GitHub Repository Cloner and Uploader")
38
+
39
+ # Input for source repository
40
+ source_repo = st.text_input("Source GitHub Repository URL",
41
+ value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit/")
42
+
43
+ # Input for target repository
44
+ target_repo = st.text_input("Target GitHub Repository URL",
45
+ value="https://github.com/AaronCWacker/AIExample-Clone/")
46
+
47
+ # GitHub token input
48
+ github_token = st.text_input("GitHub Personal Access Token", type="password")
49
+
50
+ if st.button("Clone and Upload"):
51
+ with st.spinner("Processing..."):
52
+ # Download the source repository
53
+ local_path = "./temp_repo"
54
+ download_github_repo(source_repo, local_path)
55
+
56
+ # Create a zip file
57
+ zip_filename = "repo_contents"
58
+ create_zip_file(local_path, zip_filename)
59
+
60
+ # Check if the target repository exists
61
+ g = Github(github_token)
62
+ repo_name = '/'.join(target_repo.split('/')[-2:])
63
+ repo_exists = check_repo_exists(g, repo_name)
64
+
65
+ if repo_exists:
66
+ overwrite = st.radio("Repository already exists. Overwrite?", ("Yes", "No"))
67
+ if overwrite == "Yes":
68
+ delete_repo(g, repo_name)
69
+ create_repo(g, repo_name)
70
+ else:
71
+ st.warning("Operation cancelled. Target repository already exists.")
72
+ return
73
+ else:
74
+ create_repo(g, repo_name)
75
+
76
+ # Push the contents to the new repository
77
+ push_to_github(local_path, target_repo, github_token)
78
+
79
+ # Clean up
80
+ shutil.rmtree(local_path)
81
+ os.remove(zip_filename + ".zip")
82
+
83
+ st.success("Repository cloned and uploaded successfully!")
84
+
85
+ if __name__ == "__main__":
86
+ main()