-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewrepo.py
executable file
·136 lines (94 loc) · 3.81 KB
/
newrepo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
import argparse
import os
import sys
from pathlib import Path
from subprocess import Popen, PIPE
DEFAULT_REPO_ENV_NAME = "REPONEW_DEFAULT_ORGANIZATION"
def parse_args():
prog = "newrepo"
parser = argparse.ArgumentParser(prog=prog, description="Creates a new Github repository. Tip: "
+ f"use \". {prog} <args>\" to change directory to cloned directory.")
parser.add_argument("repoId", type=str, help="Organization (optinal) and repository name. Example: myorg/myrepo")
parser.add_argument("-n", "--dry-run", action=argparse.BooleanOptionalAction, help="Don't make any changes")
parser.add_argument("-p", "--private", action=argparse.BooleanOptionalAction, help="Make the new repository private")
parser.add_argument("-t", "--template", type=str, help="repository for template. For instance 'myorg/mytemplaterepo'")
parser.add_argument("-d", "--description", type=str, help="the description for the repository")
return parser.parse_args()
def print_err(txt):
print(txt, file=sys.stderr)
def validate_args(cmd_args):
error = False
err = None
if len(cmd_args.repoId.split("/")) > 2:
err = "repoId must be on the form: org/repo"
error = True
if error:
print_err("ERROR: " + str(err))
sys.exit(1)
def get_organization(repo_id):
split = repo_id.split("/")
if len(split) == 1:
o = default_org()
if o is None:
print_err("ERROR: You didn't provide a organization, and you haven't set the environment variable "
+ DEFAULT_REPO_ENV_NAME + ", so it's impossible to figure out which organization to use.")
sys.exit(1)
return o
return split[0]
def default_org():
return os.environ.get("REPONEW_DEFAULT_ORGANIZATION")
def get_repository_name(repo_id):
return repo_id.split("/")[-1]
def git_dir():
d = os.environ.get("GCLONE_GIT_DIR")
if d is None:
raise "Missing environment variable GCLONE_GIT_DIR. Set it to the directory where you contain git " \
+ "repositories"
return d
def create_dir(d):
if os.path.exists(d):
print_err(f"ERROR: Directory already exists:")
print_err(d)
sys.exit(1)
# print_err(f"Creating directory {repoDir}")
Path(repoDir).mkdir(parents=True, exist_ok=True)
def get_private_public_arg(private_arg):
if private_arg:
return "--private"
return "--public"
def run_cmd(command, repo_dir):
print_err("Command: " + " ".join(command))
p = Popen(command, cwd=repo_dir, stdout=PIPE, stderr=PIPE)
output, error = p.communicate()
if p.returncode != 0:
print_err("ERROR, response from gh (exit code %d): %s %s" % (p.returncode, output, error))
sys.exit(1)
args = parse_args()
# print_err("ARGS:")
# print_err(args)
validate_args(args)
org = get_organization(args.repoId)
repo = get_repository_name(args.repoId)
gitDir = git_dir()
repoDir = Path(gitDir).joinpath(org).joinpath(repo)
gh_private_public_argh = get_private_public_arg(args.private)
# print_err("org: " + org)
# print_err("repo: " + repo)
if not args.dry_run:
create_dir(repoDir)
else:
print_err(f"DRY-RUN: Would create dir {repoDir}")
org_with_repo = f"{org}/{repo}"
cmd = ["gh", "repo", "create", "--clone", org_with_repo, gh_private_public_argh]
if args.template is not None and len(args.template) > 0:
cmd.append("--template")
cmd.append(args.template)
if args.description is not None and len(args.description) > 0:
cmd.append("--description")
cmd.append(args.description)
if not args.dry_run:
run_cmd(cmd, repoDir.parent)
private_or_public = "private" if args.private else "public"
print_err(f"Successfully created {private_or_public} repository in directory " + str(repoDir))
print(repoDir)