-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscrub-deck.py
More file actions
executable file
·87 lines (64 loc) · 2.5 KB
/
Copy pathscrub-deck.py
File metadata and controls
executable file
·87 lines (64 loc) · 2.5 KB
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
#!/usr/bin/env python3
import sys
import re
import boatswain_env as benv
from github import Github
CMD_NAME = 'scrub'
DESC = 'Delete all repos from a GitHub org'
def scrub_deco(parser):
parser.add_argument('-f', '--filter',
default=r'.*',
type=re.compile,
help='only scrub repos matching regular expression',
metavar='<regex>',
)
parser.add_argument('-i', '--invert',
action='store_true',
help='invert regex filter',
)
parser.add_argument('org_name',
type=str,
help='organization to be scrubbed',
metavar='<org-name>',
)
def do_scrub(repo, opt):
if opt.noop:
opt.log('--noop option specified; not deleting {}'.format(repo.name))
return
opt.warn('Deleting {}...'.format(repo.name))
repo.delete()
def scrub_org(opt):
g = Github(opt.githubToken())
org = g.get_organization(opt.org_name)
opt.warn('Scrubbing Github Org: {} ({})'.format(org.login, org.url))
if not opt.promptYes('Are you sure you want to scrub this org?', False):
# deliberately disallow --yes to work
opt.info('Backing out of scrub because user was indecisive')
return
if not opt.promptYes('You realize this means every repo in this org will be'
' removed, right?', True):
opt.info('Backing out of scrub because confused user needs a moment')
return
for repo in org.get_repos():
repo_name = repo.full_name.split('/')[-1]
if opt.invert:
if opt.filter.fullmatch(repo_name):
opt.info('Skipping {}; matched with inverted filter: {}'
.format(repo_name, opt.filter))
continue
else:
if not opt.filter.fullmatch(repo_name):
opt.info('Skipping {}; did not match with filter: {}'
.format(repo_name, opt.filter))
continue
do_scrub(repo, opt)
opt.warn('Double-check all repos have actually been removed.')
opt.warn('Sometimes you need to re-run this a couple of times.')
def main(args=None, config_path=None, verbose=True):
if args is None:
args = sys.argv[1:]
opt = benv.ParseOption(args, section=CMD_NAME, config_path=config_path,
desc=DESC, parse_deco=scrub_deco, req_github=True)
scrub_org(opt)
if __name__ == '__main__':
main()