import os import base64 import requests import subprocess def repository_exists(repo_url, token, repo_name): headers = { 'Authorization': f'token {token}', 'Content-Type': 'application/json', } response = requests.get(f'{repo_url}/api/v1/user/repos', headers=headers) repos = response.json() for repo in repos: if repo['name'] == repo_name: return True return False def create_repository(repo_url, token, repo_name): headers = { 'Authorization': f'token {token}', 'Content-Type': 'application/json', } response = requests.post(f'{repo_url}/api/v1/user/repos', headers=headers, json={'name': repo_name}) if response.status_code == 201: print(f"Repository '{repo_name}' created successfully.") return True else: print(f"Failed to create repository '{repo_name}'.") return False def add_mit_license(local_path, language='english'): mit_license_text_english = """ MIT License Copyright (c) 2024 [Name] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ mit_license_text_german = """ MIT-Lizenz Urheberrecht (c) 2024 [Name] Hiermit wird unentgeltlich jeder Person, die eine Kopie der Software und der zugehörigen Dokumentationsdateien (die "Software") erhält, die Erlaubnis erteilt, sie uneingeschränkt zu nutzen, inklusive und ohne Ausnahme mit dem Recht, sie zu verwenden, zu kopieren, zu ändern, zusammenzufügen, zu veröffentlichen, zu verbreiten, zu unterlizenzieren und/oder zu verkaufen, und Personen, denen diese Software überlassen wird, diese Rechte zu gewähren, unter den folgenden Bedingungen: Der obige Urheberrechtsvermerk und dieser Erlaubnisvermerk sind in allen Kopien oder wesentlichen Teilen der Software beizulegen. DIE SOFTWARE WIRD "WIE BESEHEN" OHNE JEGLICHE AUSDRÜCKLICHE ODER IMPLIZIERTE GARANTIEN, EINSCHLIESSLICH DER GARANTIE DER MARKTFÄHIGKEIT, DER EIGNUNG FÜR EINEN BESTIMMTEN ZWECK UND DER NICHTVERLETZUNG VON RECHTEN DRITTER, BEREITGESTELLT. IN KEINEM FALL HAFTEN DIE AUTOREN ODER COPYRIGHT-INHABER FÜR JEGLICHEN SCHADEN ODER SONSTIGE ANSPRÜCHE, OB AUS EINEM VERTRAG, EINER UNERLAUBTEN HANDLUNG ODER ANDERWEITIG, DER SICH AUS, IN ODER IN VERBINDUNG MIT DER SOFTWARE ODER DER VERWENDUNG ODER ANDEREN GESCHÄFTEN IN DER SOFTWARE ERGIBT. """ mit_license_path = os.path.join(local_path, 'LICENSE') mit_license_text = mit_license_text_english if language == 'english' else mit_license_text_german with open(mit_license_path, 'w') as f: f.write(mit_license_text.strip()) def upload_to_gitea(repo_url, token, local_path, repo_name): headers = { 'Authorization': f'token {token}', 'Content-Type': 'application/json', } if not repository_exists(repo_url, token, repo_name): print("Repository does not exist. Creating...") if not create_repository(repo_url, token, repo_name): print("Repository creation failed. Aborting upload.") return if not os.path.exists(os.path.join(local_path, '.git')): subprocess.run(["git", "config", "--global", "user.email", "E-Mail Adresse"]) subprocess.run(["git", "config", "--global", "user.name", "Name"]) subprocess.run(["git", "init"], cwd=local_path) subprocess.run(["git", "checkout", "-b", "main"], cwd=local_path) subprocess.run(["git", "add", "."], cwd=local_path) subprocess.run(["git", "commit", "-m", "Add MIT License"], cwd=local_path) remote_exists = subprocess.run(["git", "remote", "get-url", "origin"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=local_path) if remote_exists.returncode != 0: subprocess.run(["git", "remote", "add", "origin", f"https://example.com/Name/{repo_name}.git"], cwd=local_path) subprocess.run(["git", "push", "-u", "origin", "main"], cwd=local_path) if __name__ == "__main__": repo_url = 'Gitea URL' token = 'token' local_path = r'local Pfad' repo_name = 'Name' #add_mit_license(local_path, language='english') # MIT License in English add_mit_license(local_path, language='german') # MIT License in German upload_to_gitea(repo_url, token, local_path, repo_name)