Skip to content

Instantly share code, notes, and snippets.

@eliaxelang007
Created February 9, 2022 03:49
Show Gist options
  • Save eliaxelang007/911ce38b93fbc5430386796dfbc27760 to your computer and use it in GitHub Desktop.
Save eliaxelang007/911ce38b93fbc5430386796dfbc27760 to your computer and use it in GitHub Desktop.
from subprocess import run
from pathlib import Path
from re import sub
from sys import stdout, stderr, argv
from time import sleep
from traceback import print_exc
username = ""
def create_remote_repo(repo_folder_path: str):
repo_folder = Path(repo_folder_path)
assert repo_folder.is_dir()
if (all([child.name.startswith('.') for child in repo_folder.iterdir()])):
print("The folder is empty; there's nothing to commit.")
return
def parse(repo_name: str):
return sub(r"[^A-Za-z0-9.-]", '-', repo_name)
repo_name = parse(repo_folder.name)
def build_arguments(prefix: str, argument_string: str):
arguments: list[str] = []
grouping = False
buffer: list[str] = []
def add_to_arguments():
argument = "".join(buffer).strip()
if (argument):
arguments.append(argument)
buffer.clear()
for character in argument_string:
buffer.append(character)
if (character == '"'):
grouping = not grouping
if (character.isspace() and not grouping):
add_to_arguments()
add_to_arguments()
return [prefix, *arguments]
def git(argument_string: str):
return build_arguments("git", argument_string)
def gh(argument_string: str):
return build_arguments("gh", argument_string)
commands = [git("init"), git("add ."), git("commit -m \"Hello, world\""), gh(f"repo create {repo_name} --private"), git(f"remote add origin https://github.com/{username}/{repo_name}.git"), git("push origin master")]
print(f"\n\nCreating repo for '{repo_name}'")
for command in commands:
print(f"\n\nRunning {' '.join(command)}")
run(command, cwd=repo_folder_path, stdout=stdout, stderr=stderr)
sleep(1)
print(f"\n\nDone")
root_path = Path(argv[1])
for repo_name in argv[2:]:
try:
create_remote_repo(f"{root_path / repo_name}")
except:
print_exc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment