Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import os | |
| import shutil | |
| import zipfile | |
| from github import Github | |
| from git import Repo | |
| def download_github_repo(url, local_path): | |
| # Clone the repository | |
| Repo.clone_from(url, local_path) | |
| def create_zip_file(source_dir, output_filename): | |
| shutil.make_archive(output_filename, 'zip', source_dir) | |
| def check_repo_exists(g, repo_name): | |
| try: | |
| g.get_repo(repo_name) | |
| return True | |
| except: | |
| return False | |
| def create_repo(g, repo_name): | |
| user = g.get_user() | |
| user.create_repo(repo_name.split('/')[-1]) | |
| def delete_repo(g, repo_name): | |
| repo = g.get_repo(repo_name) | |
| repo.delete() | |
| def push_to_github(local_path, repo_url, github_token): | |
| repo = Repo(local_path) | |
| origin = repo.create_remote('origin', repo_url) | |
| origin.push(refspec='{}:{}'.format('master', 'master')) | |
| def main(): | |
| st.title("GitHub Repository Cloner and Uploader") | |
| # Instructions for obtaining GitHub token | |
| st.sidebar.title("How to Get Your GitHub Token") | |
| st.sidebar.markdown(""" | |
| 1. Go to your GitHub account settings | |
| 2. Click on 'Developer settings' in the left sidebar | |
| 3. Click on 'Personal access tokens' and then 'Tokens (classic)' | |
| 4. Click 'Generate new token' and select 'Generate new token (classic)' | |
| 5. Give your token a descriptive name | |
| 6. Select the following scopes: | |
| - repo (all) | |
| - delete_repo | |
| 7. Click 'Generate token' at the bottom of the page | |
| 8. Copy your new token immediately (you won't be able to see it again!) | |
| **Important:** Keep your token secret and secure. Treat it like a password! | |
| """) | |
| # Input for source repository | |
| source_repo = st.text_input("Source GitHub Repository URL", | |
| value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit/") | |
| # Input for target repository | |
| target_repo = st.text_input("Target GitHub Repository URL", | |
| value="https://github.com/AaronCWacker/AIExample-Clone/") | |
| # GitHub token input | |
| #github_token = st.text_input("GitHub Personal Access Token", type="password", | |
| #help="Paste your GitHub token here. See sidebar for instructions on how to get it.") | |
| github_token = os.environ.get("GITHUB") | |
| if st.button("Clone and Upload"): | |
| if not github_token: | |
| st.error("Please enter a GitHub token. See the sidebar for instructions.") | |
| return | |
| with st.spinner("Processing..."): | |
| # Download the source repository | |
| local_path = "./temp_repo" | |
| download_github_repo(source_repo, local_path) | |
| # Create a zip file | |
| zip_filename = "repo_contents" | |
| create_zip_file(local_path, zip_filename) | |
| # Check if the target repository exists | |
| g = Github(github_token) | |
| repo_name = '/'.join(target_repo.split('/')[-2:]) | |
| repo_exists = check_repo_exists(g, repo_name) | |
| if repo_exists: | |
| overwrite = st.radio("Repository already exists. Overwrite?", ("Yes", "No")) | |
| if overwrite == "Yes": | |
| delete_repo(g, repo_name) | |
| create_repo(g, repo_name) | |
| else: | |
| st.warning("Operation cancelled. Target repository already exists.") | |
| return | |
| else: | |
| create_repo(g, repo_name) | |
| # Push the contents to the new repository | |
| push_to_github(local_path, target_repo, github_token) | |
| # Clean up | |
| shutil.rmtree(local_path) | |
| os.remove(zip_filename + ".zip") | |
| st.success("Repository cloned and uploaded successfully!") | |
| if __name__ == "__main__": | |
| main() |