Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from datetime import datetime
|
9 |
+
import base64
|
10 |
+
|
11 |
+
def download_github_repo(url, local_path):
|
12 |
+
if os.path.exists(local_path):
|
13 |
+
shutil.rmtree(local_path)
|
14 |
+
Repo.clone_from(url, local_path)
|
15 |
+
|
16 |
+
def create_zip_file(source_dir, output_filename):
|
17 |
+
shutil.make_archive(output_filename, 'zip', source_dir)
|
18 |
+
|
19 |
+
def create_repo(g, repo_name):
|
20 |
+
user = g.get_user()
|
21 |
+
return user.create_repo(repo_name)
|
22 |
+
|
23 |
+
def push_to_github(local_path, repo, github_token):
|
24 |
+
repo_url = f"https://{github_token}@github.com/{repo.full_name}.git"
|
25 |
+
local_repo = Repo(local_path)
|
26 |
+
|
27 |
+
if 'origin' in [remote.name for remote in local_repo.remotes]:
|
28 |
+
origin = local_repo.remote('origin')
|
29 |
+
origin.set_url(repo_url)
|
30 |
+
else:
|
31 |
+
origin = local_repo.create_remote('origin', repo_url)
|
32 |
+
|
33 |
+
if not local_repo.heads:
|
34 |
+
local_repo.git.checkout('-b', 'main')
|
35 |
+
current_branch = 'main'
|
36 |
+
else:
|
37 |
+
current_branch = local_repo.active_branch.name
|
38 |
+
|
39 |
+
local_repo.git.add(A=True)
|
40 |
+
|
41 |
+
if local_repo.is_dirty():
|
42 |
+
local_repo.git.commit('-m', 'Initial commit')
|
43 |
+
|
44 |
+
origin.push(refspec=f'{current_branch}:{current_branch}')
|
45 |
+
|
46 |
+
def get_base64_download_link(file_path, file_name):
|
47 |
+
with open(file_path, "rb") as file:
|
48 |
+
contents = file.read()
|
49 |
+
base64_encoded = base64.b64encode(contents).decode()
|
50 |
+
return f'<a href="data:application/zip;base64,{base64_encoded}" download="{file_name}">Download {file_name}</a>'
|
51 |
+
|
52 |
+
def main():
|
53 |
+
st.title("GitHub Repository Cloner and Uploader")
|
54 |
+
|
55 |
+
st.sidebar.title("How to Get Your GitHub Token")
|
56 |
+
st.sidebar.markdown("""
|
57 |
+
1. Go to your GitHub account settings
|
58 |
+
2. Click on 'Developer settings' in the left sidebar
|
59 |
+
3. Click on 'Personal access tokens' and then 'Tokens (classic)'
|
60 |
+
4. Click 'Generate new token' and select 'Generate new token (classic)'
|
61 |
+
5. Give your token a descriptive name
|
62 |
+
6. Select the following scopes:
|
63 |
+
- repo (all)
|
64 |
+
- delete_repo
|
65 |
+
7. Click 'Generate token' at the bottom of the page
|
66 |
+
8. Copy your new token immediately (you won't be able to see it again!)
|
67 |
+
|
68 |
+
**Important:** Keep your token secret and secure. Treat it like a password!
|
69 |
+
""")
|
70 |
+
|
71 |
+
source_repo = st.text_input("Source GitHub Repository URL",
|
72 |
+
value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit/")
|
73 |
+
|
74 |
+
github_token = os.environ.get("GITHUB")
|
75 |
+
if not github_token:
|
76 |
+
github_token = st.text_input("GitHub Personal Access Token", type="password",
|
77 |
+
help="Paste your GitHub token here. See sidebar for instructions on how to get it.")
|
78 |
+
|
79 |
+
checkin_clone = st.checkbox("Checkin Clone", value=False)
|
80 |
+
|
81 |
+
if st.button("Clone"):
|
82 |
+
if not github_token:
|
83 |
+
st.error("Please enter a GitHub token. See the sidebar for instructions.")
|
84 |
+
return
|
85 |
+
|
86 |
+
with st.spinner("Processing..."):
|
87 |
+
try:
|
88 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
89 |
+
new_repo_name = f"AIExample-Clone-{timestamp}"
|
90 |
+
local_path = f"./temp_repo_{timestamp}"
|
91 |
+
|
92 |
+
download_github_repo(source_repo, local_path)
|
93 |
+
|
94 |
+
zip_filename = f"{new_repo_name}.zip"
|
95 |
+
create_zip_file(local_path, zip_filename[:-4]) # Remove .zip extension for make_archive
|
96 |
+
|
97 |
+
st.markdown(get_base64_download_link(zip_filename, zip_filename), unsafe_allow_html=True)
|
98 |
+
|
99 |
+
if checkin_clone:
|
100 |
+
g = Github(github_token)
|
101 |
+
new_repo = create_repo(g, new_repo_name)
|
102 |
+
push_to_github(local_path, new_repo, github_token)
|
103 |
+
|
104 |
+
# Upload zip file to the new repository
|
105 |
+
repo = g.get_repo(new_repo.full_name)
|
106 |
+
with open(zip_filename, 'rb') as file:
|
107 |
+
content = file.read()
|
108 |
+
repo.create_file(zip_filename, f"Add {zip_filename}", content, branch="main")
|
109 |
+
|
110 |
+
st.success(f"Repository cloned and uploaded successfully to {new_repo.html_url}!")
|
111 |
+
else:
|
112 |
+
st.success("Repository cloned successfully! Use the download link above to get the zip file.")
|
113 |
+
|
114 |
+
except Exception as e:
|
115 |
+
st.error(f"An error occurred: {str(e)}")
|
116 |
+
finally:
|
117 |
+
if os.path.exists(local_path):
|
118 |
+
shutil.rmtree(local_path)
|
119 |
+
if os.path.exists(zip_filename):
|
120 |
+
os.remove(zip_filename)
|
121 |
+
|
122 |
+
if __name__ == "__main__":
|
123 |
+
main()
|