-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateRepo.py
More file actions
120 lines (98 loc) · 3.34 KB
/
createRepo.py
File metadata and controls
120 lines (98 loc) · 3.34 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
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
# Rob Casale
# This will:
# - Validate user does exist and if it does not throw Error
# - Validate repo does not exist and if it does throw Error
# - Create Repo
# Return codes:
# - 0 success
# - 1 user does not exist
# - 2 repo exist
# - 3 error create repo
# - 8 others
import requests, os, ssl, urllib2, base64, json, sys
# Another possible context option
# context=ssl._create_unverified_context()
# Get pass verification and get response
gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
# API url requests
test_url='https://www.google.com'
url_account='https://<dtr-hostname>/api/v0/accounts/'
url_repo='https://<dtr-hostname>/api/v0/repositories/'
# Login credentials
username='casaler'
password='<password>'
# Proxy settings
proxies={
"http":"http://<username>:<password>@<proxy-info>:8080",
"https":"http://<username>:<password>@<proxy-info>:8080",
}
def build_request(requestUrl):
# Build request
request=urllib2.Request(requestUrl)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
return request
def get_info(request):
info=urllib2.urlopen(request,context=gcontext).read()
return info
def get_code(request):
code=urllib2.urlopen(request,context=gcontext).getcode()
return code
def create_account_code(request,user):
data={'type':'user','name':user,'password':password}
request.add_header('Content-Type', 'application/json')
code=urllib2.urlopen(request,json.dumps(data),context=gcontext).getcode()
return code
def activate_account_code(request,user):
request.add_header('Content-Type', 'application/json')
request.get_method=lambda:'PUT'
code=urllib2.urlopen(request,json.dumps(user),context=gcontext).getcode()
return code
def create_repository_code(request,repo,visibility):
data={'name':repo,'visibility':visibility}
request.add_header('Content-Type', 'application/json')
code=urllib2.urlopen(request,json.dumps(data),context=gcontext).getcode()
return code
# Retrieve username and repo from user
if len(sys.argv) != 3:
print 'exit(8): enter username and repository only'
exit()
user=sys.argv[1]
repo=sys.argv[2]
user_code=None
url_user_account=url_account+user
try:
# validate username
user_code=get_code(build_request(url_user_account))
repo_code=None
url_user_repo=url_repo+user+'/'+repo
try:
# validate repository
repo_code=get_code(build_request(url_user_repo))
print 'exit(2): repository already exist'
except urllib2.HTTPError as e:
if e.code == 404:
# repo does not exist
#visibility=raw_input('Enter the visibility of the repository[public/private]: ').lower()
visibility='public'
try:
# create repo
create_repository_code(build_request(url_repo+user),repo,visibility)
print 'exit(0): success'
except urllib2.HTTPError as e:
if e.code == 400:
# repo exists - should not happen
print 'exit(2): repository already exist'
else:
# could not create repositroy
print 'exit(3): could not create repository'
else:
# could not access repo details
print 'exit(8): could not access repository details'
except urllib2.HTTPError as e:
if e.code == 404:
# username does not exist
print 'exit(1): username does not exist'
else:
# could not access account details
print 'exit(8): could not access account details'