Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import uuid
|
9 |
+
|
10 |
+
def download_github_repo(url, local_path):
|
11 |
+
if os.path.exists(local_path):
|
12 |
+
shutil.rmtree(local_path)
|
13 |
+
Repo.clone_from(url, local_path)
|
14 |
+
|
15 |
+
def create_zip_file(source_dir, output_filename):
|
16 |
+
shutil.make_archive(output_filename, 'zip', source_dir)
|
17 |
+
|
18 |
+
def check_repo_exists(g, repo_name):
|
19 |
+
try:
|
20 |
+
g.get_repo(repo_name)
|
21 |
+
return True
|
22 |
+
except:
|
23 |
+
return False
|
24 |
+
|
25 |
+
def create_repo(g, repo_name):
|
26 |
+
user = g.get_user()
|
27 |
+
return user.create_repo(repo_name)
|
28 |
+
|
29 |
+
def delete_repo(g, repo_name):
|
30 |
+
repo = g.get_repo(repo_name)
|
31 |
+
repo.delete()
|
32 |
+
|
33 |
+
def push_to_github(local_path, repo, github_token):
|
34 |
+
repo_url = f"https://{github_token}@github.com/{repo.full_name}.git"
|
35 |
+
repo = Repo(local_path)
|
36 |
+
origin = repo.create_remote('origin', repo_url)
|
37 |
+
origin.push(refspec='{}:{}'.format('master', 'master'))
|
38 |
+
|
39 |
+
def main():
|
40 |
+
st.title("GitHub Repository Cloner and Uploader")
|
41 |
+
|
42 |
+
# Sidebar instructions (unchanged)
|
43 |
+
st.sidebar.title("How to Get Your GitHub Token")
|
44 |
+
st.sidebar.markdown("""
|
45 |
+
1. Go to your GitHub account settings
|
46 |
+
2. Click on 'Developer settings' in the left sidebar
|
47 |
+
3. Click on 'Personal access tokens' and then 'Tokens (classic)'
|
48 |
+
4. Click 'Generate new token' and select 'Generate new token (classic)'
|
49 |
+
5. Give your token a descriptive name
|
50 |
+
6. Select the following scopes:
|
51 |
+
- repo (all)
|
52 |
+
- delete_repo
|
53 |
+
7. Click 'Generate token' at the bottom of the page
|
54 |
+
8. Copy your new token immediately (you won't be able to see it again!)
|
55 |
+
|
56 |
+
**Important:** Keep your token secret and secure. Treat it like a password!
|
57 |
+
""")
|
58 |
+
|
59 |
+
# Input for source repository
|
60 |
+
source_repo = st.text_input("Source GitHub Repository URL",
|
61 |
+
value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit/")
|
62 |
+
|
63 |
+
# GitHub token input
|
64 |
+
github_token = os.environ.get("GITHUB")
|
65 |
+
if not github_token:
|
66 |
+
github_token = st.text_input("GitHub Personal Access Token", type="password",
|
67 |
+
help="Paste your GitHub token here. See sidebar for instructions on how to get it.")
|
68 |
+
|
69 |
+
if st.button("Clone and Upload"):
|
70 |
+
if not github_token:
|
71 |
+
st.error("Please enter a GitHub token. See the sidebar for instructions.")
|
72 |
+
return
|
73 |
+
|
74 |
+
with st.spinner("Processing..."):
|
75 |
+
try:
|
76 |
+
# Generate a unique local path
|
77 |
+
local_path = f"./temp_repo_{uuid.uuid4().hex}"
|
78 |
+
|
79 |
+
# Download the source repository
|
80 |
+
download_github_repo(source_repo, local_path)
|
81 |
+
|
82 |
+
# Create a zip file
|
83 |
+
zip_filename = f"repo_contents_{uuid.uuid4().hex}"
|
84 |
+
create_zip_file(local_path, zip_filename)
|
85 |
+
|
86 |
+
# Generate a unique repository name
|
87 |
+
new_repo_name = f"AIExample-Clone-{uuid.uuid4().hex[:8]}"
|
88 |
+
|
89 |
+
# Create the new repository
|
90 |
+
g = Github(github_token)
|
91 |
+
user = g.get_user()
|
92 |
+
new_repo = create_repo(g, new_repo_name)
|
93 |
+
|
94 |
+
# Push the contents to the new repository
|
95 |
+
push_to_github(local_path, new_repo, github_token)
|
96 |
+
|
97 |
+
# Clean up
|
98 |
+
shutil.rmtree(local_path)
|
99 |
+
os.remove(zip_filename + ".zip")
|
100 |
+
|
101 |
+
st.success(f"Repository cloned and uploaded successfully to {new_repo.html_url}!")
|
102 |
+
|
103 |
+
except Exception as e:
|
104 |
+
st.error(f"An error occurred: {str(e)}")
|
105 |
+
finally:
|
106 |
+
# Ensure cleanup even if an error occurs
|
107 |
+
if os.path.exists(local_path):
|
108 |
+
shutil.rmtree(local_path)
|
109 |
+
if os.path.exists(zip_filename + ".zip"):
|
110 |
+
os.remove(zip_filename + ".zip")
|
111 |
+
|
112 |
+
if __name__ == "__main__":
|
113 |
+
main()
|