nexuscli package

Submodules

nexuscli.exception module

class nexuscli.exception.CliReturnCode

Bases: enum.Enum

Error codes returned by nexuscli.cli

API_ERROR = 2
CONNECTION_ERROR = 3
DOWNLOAD_ERROR = 4
INVALID_CREDENTIALS = 5
INVALID_SUBCOMMAND = 10
NO_FILES = 1
POLICY_NOT_FOUND = 20
REPOSITORY_NOT_FOUND = 30
SUBCOMMAND_ERROR = 11
SUCCESS = 0
UNKNOWN_ERROR = 99
exception nexuscli.exception.DownloadError(*args, cli_return_code=None)

Bases: nexuscli.exception.NexusClientBaseError

Error retrieving artefact from Nexus service.

DEFAULT_CLI_RETURN_CODE = 4
exception nexuscli.exception.NexusClientAPIError(*args, cli_return_code=None)

Bases: nexuscli.exception.NexusClientBaseError

Unexpected response from Nexus service.

DEFAULT_CLI_RETURN_CODE = 2
exception nexuscli.exception.NexusClientBaseError(*args, cli_return_code=None)

Bases: Exception

DEFAULT_CLI_RETURN_CODE = 99
exception nexuscli.exception.NexusClientConnectionError(*args, cli_return_code=None)

Bases: nexuscli.exception.NexusClientBaseError

Generic network connector error.

DEFAULT_CLI_RETURN_CODE = 3
exception nexuscli.exception.NexusClientCreateCleanupPolicyError(*args, cli_return_code=None)

Bases: nexuscli.exception.NexusClientBaseError

Used when a cleanup policy creation operation in Nexus fails.

DEFAULT_CLI_RETURN_CODE = 11
exception nexuscli.exception.NexusClientCreateRepositoryError(*args, cli_return_code=None)

Bases: nexuscli.exception.NexusClientBaseError

Used when a repository creation operation in Nexus fails.

DEFAULT_CLI_RETURN_CODE = 11
exception nexuscli.exception.NexusClientInvalidCleanupPolicy(*args, cli_return_code=None)

Bases: nexuscli.exception.NexusClientBaseError

The given cleanup policy does not exist in Nexus.

DEFAULT_CLI_RETURN_CODE = 11
exception nexuscli.exception.NexusClientInvalidCredentials(*args, cli_return_code=None)

Bases: nexuscli.exception.NexusClientBaseError

Login credentials not accepted by Nexus service. Usually the result of a HTTP 401 response.

DEFAULT_CLI_RETURN_CODE = 5
exception nexuscli.exception.NexusClientInvalidRepository(*args, cli_return_code=None)

Bases: nexuscli.exception.NexusClientBaseError

The given repository does not exist in Nexus.

DEFAULT_CLI_RETURN_CODE = 30
exception nexuscli.exception.NexusClientInvalidRepositoryPath(*args, cli_return_code=None)

Bases: nexuscli.exception.NexusClientBaseError

Used when an operation against the Nexus service uses an invalid or non-existent path.

nexuscli.nexus_client module

class nexuscli.nexus_client.NexusClient(config=None)

Bases: object

A class to interact with Nexus 3’s API.

Unless all keyword arguments url, user and password are supplied, the class will attempt to read the configuration file and, if unsuccessful, use defaults.

Parameters:config (NexusConfig) – instance containing the configuration for the Nexus service used by this instance.
cleanup_policies

Instance of CleanupPolicyCollection . This will automatically use the existing instance of NexusClient to communicate with the Nexus service.

delete(repository_path)

Delete artefacts, recursively if repository_path is a directory.

Parameters:repository_path (str) – location on the repository service.
Returns:number of deleted files. Negative number for errors.
Return type:int
download(source, destination, flatten=False, nocache=False)

Process a download. The source must be a valid Nexus 3 repository path, including the repository name as the first component of the path.

The destination must be a local file name or directory.

If a file name is given as destination, the asset may be renamed. The final destination will depend on flatten.

Parameters:
  • source (str) – location of artefact or directory on the repository service.
  • destination (str) – path to the local file or directory.
  • flatten (bool) – if True, the remote path isn’t reproduced locally.
  • nocache (bool) – if True, force download of a directory or artefact, ignoring an existing local copy. If false, it will not re-download an existing copy if its checksum matches the one in Nexus (as determined by nexuscli.nexus_util.has_same_hash()).
Returns:

number of downloaded files.

Return type:

int

download_file(download_url, destination)

Download an asset from Nexus artefact repository to local file system.

Parameters:
  • download_url (str) – fully-qualified URL to asset being downloaded.
  • destination (str) – file or directory location to save downloaded asset. Must be an existing directory; any exiting file in this location will be overwritten.
Returns:

http_delete(endpoint, **kwargs)

Performs a HTTP DELETE request on the given endpoint.

Parameters:
Return type:

requests.Response

http_get(endpoint)

Performs a HTTP GET request on the given endpoint.

Parameters:endpoint (str) – name of the Nexus REST API endpoint.
Return type:requests.Response
http_head(endpoint)

Performs a HTTP HEAD request on the given endpoint.

Parameters:endpoint (str) – name of the Nexus REST API endpoint.
Return type:requests.Response
http_post(endpoint, **kwargs)

Performs a HTTP POST request on the given endpoint.

Parameters:
Return type:

requests.Response

http_put(endpoint, **kwargs)

Performs a HTTP PUT request on the given endpoint.

Parameters:
Return type:

requests.Response

http_request(method, endpoint, service_url=None, **kwargs)

Performs a HTTP request to the Nexus REST API on the specified endpoint.

Parameters:
  • method – one of get, put, post, delete.
  • endpoint (str) – URI path to be appended to the service URL.
  • service_url (str) – override the default URL to use for the request, which is created by joining rest_url and endpoint.
  • kwargs – as per requests.request().
Return type:

requests.Response

list(repository_path)

List all the artefacts, recursively, in a given repository_path.

Parameters:repository_path (str) – location on the repository service.
Returns:artefacts under repository_path.
Return type:typing.Iterator[str]
list_raw(repository_path)

As per list() but yields raw Nexus artefacts as dicts.

Parameters:repository_path (str) – location on the repository service.
Return type:typing.Iterator[dict]
repositories

Instance of RepositoryCollection. This will automatically use the existing instance of NexusClient to communicate with the Nexus service.

Return type:RepositoryCollection
rest_url

Full URL to the Nexus REST API, based on the url and version from config.

Return type:str
scripts

Instance of ScriptCollection. This will automatically use the existing instance of NexusClient to communicate with the Nexus service.

server_version

Parse the Server header from a Nexus request response and return as version information. The method expects the header Server to be present and formatted as, e.g., ‘Nexus/3.19.1-01 (OSS)’

Returns:the parsed version. If it can’t be determined, return None.
Return type:Union[None,semver.VersionInfo]
split_component_path(component_path)

Splits a given component path into repository, directory, filename.

A Nexus component path for a raw directory must have this format:

repository_name/directory[(/subdir1)...][/|filename]

A path ending in / represents a directory; otherwise it represents a filename.

>>> dst0 = 'myrepo0/dir/'
>>> dst1 = 'myrepo1/dir/subdir/'
>>> dst2 = 'myrepo2/dir/subdir/file'
>>> dst3 = 'myrepo3/dir/subdir/etc/file.ext'
>>> split_component_path(dst0)
>>> ('myrepo0', 'dir', None)
>>> split_component_path(dst1)
>>> ('myrepo1', 'dir/subdir', None)
>>> split_component_path(dst2)
>>> ('myrepo2', 'dir/subdir', 'file')
>>> split_component_path(dst3)
>>> ('myrepo3', 'dir/subdir/etc', 'file.ext')
Parameters:component_path (str) – the Nexus component path, as described above.
Returns:tuple of (repository_name, directory, filename). If the given component_path doesn’t represent a file, then the filename is set to None.
Return type:tuple[str, str, str]
upload(source, destination, recurse=True, flatten=False)

Process an upload. The source must be either a local file name or directory. The flatten and recurse options are honoured for directory uploads.

The destination must be a valid Nexus 3 repository path, including the repository name as the first component of the path.

Parameters:
  • source (str) – location of file or directory to be uploaded.
  • destination (str) – destination path in Nexus, including repository name and, if required, directory name (e.g. raw repos require a directory).
  • recurse (bool) – do not process sub directories for uploads to remote
  • flatten (bool) – Flatten directory structure by not reproducing local directory structure remotely
Returns:

number of files uploaded.

nexuscli.nexus_config module

class nexuscli.nexus_config.NexusConfig(username='admin', password='admin123', url='http://localhost:8081', x509_verify=True, api_version='v1', config_path=None)

Bases: object

A class to hold Nexus 3’s configuration.

Unless keyword arguments url, user and password are supplied, the class will attempt to read the configuration file and, if unsuccessful, use defaults.

Parameters:
  • username (str) – username for Nexus service at given url.
  • password (str) – password for username above.
  • url (str) – URL to Nexus 3 OSS service.
  • x509_verify (bool) – toggle certificate validation.
  • api_version (str) – Nexus REST API version to be used.
  • config_path (str) – local file containing configuration above in JSON format with these keys: nexus_url, nexus_user, nexus_pass and nexus_verify.
api_version

Current API version in use.

Return type:str
auth

Current username and password as a tuple.

Return type:tuple[str, str]
config_file

Path to configuration file, as given by config_path during instantiation.

Return type:str
dump()

Writes the current configuration to disk under property:config_file.

If a file already exists, it will be overwritten. The permission will be set to read/write to the owner only.

load()

Load the configuration settings from the file specified by config_file.

The configuration file is in JSON format and expects these keys: nexus_user, nexus_pass, nexus_url, nexus_verify.

to_dict

Current instance configuration.

Return type:dict
url

The Nexus service URL

Return type:str
x509_verify

Whether to validate the x509 certificate when using https to access the Nexus service

Return type:str

nexuscli.nexus_util module

nexuscli.nexus_util.calculate_hash(hash_name, file_path_or_handle)

Calculate a hash for the given file.

Parameters:
  • hash_name (str) – name of the hash algorithm in hashlib
  • file_path_or_handle (str) – source file name (str) or file handle (file-like) for the hash algorithm.
Returns:

the calculated hash

Return type:

str

nexuscli.nexus_util.ensure_exists(path, is_dir=False)

Ensures a path exists.

Parameters:
  • path (pathlib.Path) – the path to ensure
  • is_dir (bool) – whether the path is a directory.
nexuscli.nexus_util.filtered_list_gen(raw_response, term=None, partial_match=True)
Iterates over items yielded by raw_response_gen, validating that:
  1. the path dict key is a str
  2. the path value starts with starts_with (if provided)
>>> r = [{
>>>     'checksum': {
>>>         'md5': 'd94b865aa7620c46ef8faef7059a311c',
>>>         'sha1': '2186934d880cf24dd9ecc578335e290026695522',
>>>         'sha256': 'b7bb3424a6a6(...)4113bc38fd7807528481a8ffe3cf',
>>>         'sha512': 'e7806f3caa3e(...)3caeb9bbc54bbde286c07f837fdc'
>>>     },
>>>     'downloadUrl': 'http://nexus/repository/repo_name/a/file.ext',
>>>     'format': 'yum',
>>>     'id': 'Y2xvdWRlcmEtbWFuYWdlcj(...)mRiNWU0YjllZWQzMg',
>>>     'path': 'a/fake.rpm',
>>>     'repository': 'cloudera-manager'}]
>>>
>>> for i in filtered_list_gen(r, starts_with='a/fake.rpm')
>>>     print(i['path'])
a/fake.rpm
>>> for i in filtered_list_gen(r, starts_with='b')
>>>     print(i['path'])
# (nothing printed)
Parameters:
  • raw_response (iterable) – an iterable that yields one element of a nexus search response at a time, such as the one returned by _paginate_get().
  • term (str) – if defined, only items with an attribute path that starts with the given parameter are returned.
  • partial_match (bool) – if True, include items whose artefact path starts with the given term.
Yields:

dict – items that matched the filter.

nexuscli.nexus_util.groovy_script(script_name)

Returns the content for a groovy script located in the package installation path under script/groovy.

E.g.: groovy_script(‘foo’) returns the content for the file at .../site-packages/nexuscli/script/groovy/foo.groovy.

Parameters:script_name – file name of the groovy script; .groovy is appended to this parameter to form the file name.
Returns:content for the groovy script
Return type:str
nexuscli.nexus_util.has_same_hash(artefact, filepath)

Checks if a Nexus artefact has the same hash as a local filepath.

Parameters:
  • artefact (dict) – as returned by list_raw()
  • filepath – local file path
Returns:

True if artefact and filepath have the same hash.

Return type:

bool

nexuscli.nexus_util.validate_strings(*args)

Checks that all given arguments have a string type (e.g. str, basestring, unicode etc)

Parameters:*args – values to be validated.
Returns:True if all arguments are of a string type. False otherwise.
Return type:bool

Module contents