-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnexus_sync_raw_repository.py
66 lines (53 loc) · 1.78 KB
/
nexus_sync_raw_repository.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
#!/usr/bin/env python3
from os import getenv
from config import FROM_COOKIE, FROM_HOST_URL, FROM_REPO_NAME, TO_COOKIE, TO_HOST_URL, TO_REPO_NAME
import requests
TEMP_FILE = 'tmp_nexus_asset'
def get_assets(next_token=None):
headers = {
'Cookie': FROM_COOKIE,
'accept': 'application/json'
}
cnt_token_string = ''
if next_token is not None:
cnt_token_string = 'continuationToken=%s&' % next_token
req_url = '%s/service/rest/v1/assets?%srepository=%s' % (
FROM_HOST_URL, cnt_token_string, FROM_REPO_NAME)
res = requests.get(req_url, headers=headers)
return res.json()
def download_asset(download_url):
print('Downloading: %s' % download_url)
headers = {
'Cookie': FROM_COOKIE
}
with open(TEMP_FILE, 'wb') as file:
res = requests.get(download_url, headers=headers)
file.write(res.content)
def upload_asset(path):
directory = path.split('/')[0]
filename = path.split('/')[1]
headers = {
'Cookie': TO_COOKIE,
'accept': 'application/json'
}
files = {
'raw.directory': (None, directory),
'raw.asset1': (filename, open(TEMP_FILE, 'rb')),
'raw.asset1.filename': (None, filename)
}
post_url = '%s/service/rest/v1/components?repository=%s' % (
TO_HOST_URL, TO_REPO_NAME)
resp = requests.post(post_url, headers=headers, files=files)
print(resp)
items = []
assets = get_assets()
items.append(assets['items'])
while assets['continuationToken']:
assets = get_assets(assets['continuationToken'])
items.append(assets['items'])
print('Processed token: %s' % assets['continuationToken'])
for assets in items:
for asset in assets:
download_asset(asset['downloadUrl'])
upload_asset(asset['path'])
print('Done')