Nexus Repository Auto Python file

Hey guys, I have a question about an automated python file to perform execution to download dependencies in nexus and send it back to the physical computer, how do i do that?

This is about the bare minimum to get you started. You’ll need error handling and other things, and I’m sure there are more elegant/pythonic ways of writing this code (I’m not really a developer, I just like to automate parts of my job). Hopefully it’s helpful.

#!/usr/bin/env python3
import json
import urllib.request
    
# change baseurl and downloadRepository to your NXRM server and pypi repo
baseurl = 'http://nxrm.myorg.com:8080/service/rest/v1/'
downloadRepository = 'pypi-proxy'
url = baseurl + 'components?repository=' + downloadRepository

go = 1
while go
    response = urllib.request.urlopen(url)
    data = json.load(response)
    for item in data["items"]:
        for asset in item["assets"]:
            fileurl = asset["downloadUrl"]
            filename = downloadPath + fileurl.split('/')[-1] # '\' for Windows
            outfile = open(filename, "w")
            outfile.write(str(urllib.request.urlopen(fileurl).read()))
            outfile.close()
    if data["continuationToken"] is None:
        break
    else:
        # construct pagination url and loop
        url = baseurl + 'components?continuationToken=' + data["continuationToken"] + '&repository=' + downloadRepository
1 Like

Sorry for the late reply, could you explain what does these lines of code do? For loop of asset , items and the last line. Appreciate the help!

Hello,

The nexus server returns the data from the REST query in JSON format as a dictionary. You can see it if you do

print data

after this line

data = json.load(response)

You’ll see items is the key with all of the python packages as values. Inside items, you’ll see an assets key. One of the values in assets is the download URL for the package. This python program downloads the packages using those URLs.

Some repositories may be large, so the Nexus repository manager server API only returns 10 items for each REST call, and it also returns a pagination token to let you know there are more items. You can construct another REST call and include the pagination token to see the next page of items. If data["continuationToken"]=none, then there are no more items in the repository.

1 Like

Hello Austin,

I have completed coding my scripts from internal to server to list down the dependencies list needed for downloading, right now I needed to establish the API towards nexus repository I would require your assistance from you with this one, do you have discord maybe we could chat from there?
That would be very helpful :slight_smile:

Hello Skybright,

I’m sorry, I don’t have discord. It sounds like you have a list of dependencies you want to download from the repository. If that’s true, you could do the following:

for each of the item in data["items"] in the example loop I gave, search through your list and compare item["name"] and item["version"] in data to components in your list, then go into the for asset in item["assets"] to get the URL to download the component/package. That might look like this inserted into the code above…

while go
response = urllib.request.urlopen(url)
data = json.load(response)
for item in data["items"]:

<insert code here to compare your dependency name to item["name"] and your dependency version to item["version"]>

    <if item matches something in your dependencies, then do the following>
        for asset in item["assets"]:
            fileurl = asset["downloadUrl"]
            filename = downloadPath + fileurl.split('/')[-1] # '\' for Windows
            outfile = open(filename, "w")
            outfile.write(str(urllib.request.urlopen(fileurl).read()))

Hello austin,

I have the following code that brings in the dependencies list,

loggin.debug("dependencies list %s", str(dList))

reponame=args.repo

<insert code here to compare your dependency name to item[“name”] and your dependency version to item[“version”]>

how should I compare it with this, I’m quite new with this, pardon me.

Appreciate the help!

Hello Austin, I have experienced errors while executing,

File “/usr/lib64/python3.6/json/init.py”, line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File “/usr/lib64/python3.6/json/init.py”, line 354, in loads
return _default_decoder.decode(s)
File “/usr/lib64/python3.6/json/decoder.py”, line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib64/python3.6/json/decoder.py”, line 357, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

any clue on what has happened?