nexuscli package

Subpackages

Submodules

nexuscli.exception module

nexuscli.exception.API_ERROR_MAP = {'"EULA is not accepted"': {'class': 'ConfigError', 'msg': 'Please accept the Health Check EULA using your Nexus instance UI; once this is done for at least one repository, the API call will work for other repositories'}, 'Creating and updating scripts is disabled': {'class': 'ConfigError', 'msg': 'Please enable scripting in your Nexus instance; see https://support.sonatype.com/hc/en-us/articles/360045220393-Scripting-Nexus-Repository-Manager-3#how-to-enable'}}

Map from Nexus API response to an exception class and user-friendly error

class nexuscli.exception.CliReturnCode

Bases: enum.Enum

Error codes returned by nexuscli.cli

API_ERROR = 2
CAPABILITY_NOT_AVAILABLE = 6
CONFIG_ERROR = 40
CONNECTION_ERROR = 3
DOWNLOAD_ERROR = 4
INVALID_CREDENTIALS = 5
INVALID_SUBCOMMAND = 10
NOT_FOUND = 12
NOT_IMPLEMENTED = 7
NO_FILES = 1
POLICY_NOT_FOUND = 20
REPOSITORY_NOT_FOUND = 30
SUBCOMMAND_ERROR = 11
SUCCESS = 0
TASK_DISABLED = 13
UNKNOWN_ERROR = 99
exception nexuscli.exception.ConfigError(message: str)

Bases: nexuscli.exception.NexusClientBaseError

Configuration error.

exit_code = 40
exception nexuscli.exception.DownloadError(message: str)

Bases: nexuscli.exception.NexusClientBaseError

Error retrieving artefact from Nexus service.

exit_code = 4
exception nexuscli.exception.FeatureNotImplemented(message=None)

Bases: nexuscli.exception.NexusClientBaseError

The requested feature hasn’t been implemented in this client

exit_code = 7
exception nexuscli.exception.NexusClientAPIError(message_bytes=None)

Bases: nexuscli.exception.NexusClientBaseError

Unexpected response from Nexus service.

exit_code = 2
exception nexuscli.exception.NexusClientBaseError(message: str)

Bases: click.exceptions.ClickException

exit_code = 99
exception nexuscli.exception.NexusClientCapabilityUnsupported(message: str)

Bases: nexuscli.exception.NexusClientBaseError

Client tried to use a capability that doesn’t exist in this Nexus 3 server version

exit_code = 6
exception nexuscli.exception.NexusClientConnectionError(message: str)

Bases: nexuscli.exception.NexusClientBaseError

Generic network connector error.

exit_code = 3
exception nexuscli.exception.NexusClientCreateCleanupPolicyError(message: str)

Bases: nexuscli.exception.NexusClientBaseError

Used when a cleanup policy creation operation in Nexus fails.

exit_code = 11
exception nexuscli.exception.NexusClientCreateRepositoryError(message: str)

Bases: nexuscli.exception.NexusClientBaseError

Used when a repository creation operation in Nexus fails.

exit_code = 11
exception nexuscli.exception.NexusClientInvalidCleanupPolicy(message: str)

Bases: nexuscli.exception.NexusClientBaseError

The given cleanup policy does not exist in Nexus.

exit_code = 11
exception nexuscli.exception.NexusClientInvalidCredentials(message: str)

Bases: nexuscli.exception.NexusClientBaseError

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

exit_code = 5
exception nexuscli.exception.NexusClientInvalidRepository(message: str)

Bases: nexuscli.exception.NexusClientBaseError

The given repository does not exist in Nexus.

exit_code = 30
exception nexuscli.exception.NexusClientInvalidRepositoryPath(message: str)

Bases: nexuscli.exception.NexusClientBaseError

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

exception nexuscli.exception.NotFound(message)

Bases: nexuscli.exception.NexusClientBaseError

The requested object/item was not found on the server

exit_code = 12
exception nexuscli.exception.TaskDisabled(message)

Bases: nexuscli.exception.NexusClientBaseError

The requested task is disabled

exit_code = 13

nexuscli.nexus_client module

class nexuscli.nexus_client.NexusClient(config: nexuscli.nexus_config.NexusConfig = 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 – instance containing the configuration for the Nexus service used by this instance.

property blobstores

Instance of BlobstoreCollection

property cleanup_policies

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

create_scripts(scripts: List[str]) → None
property repositories

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

property scripts

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

property security_realms

This instance uses the existing instance of NexusClient to communicate with the Nexus service.

property tasks

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

nexuscli.nexus_config module

class nexuscli.nexus_config.NexusConfig(username='admin', password='', 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.

property api_version

Current API version in use.

Return type

str

property auth

Current username and password as a tuple.

Return type

tuple[str, str]

property config_file

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

Return type

str

property config_path

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

dump() → str

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.

return: name of the file written

dump_env()

Writes the current configuration to disk under property:config_file.env.

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

return: name of the file written

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.

property to_dict

Current instance configuration.

Return type

dict

property url

The Nexus service URL

Return type

str

property x509_verify

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

Return type

str

nexuscli.nexus_http module

class nexuscli.nexus_http.NexusHttp(config: nexuscli.nexus_config.NexusConfig = None)

Bases: object

get(endpoint: str, **kwargs) → requests.models.Response

Performs a HTTP GET request on the given endpoint.

Parameters

endpoint – name of the Nexus REST API endpoint.

request(method: str, endpoint: str, service_url: Optional[str] = None, **kwargs) → requests.models.Response

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

Parameters
  • method – one of get, put, post, delete.

  • endpoint – URI path to be appended to the service URL.

  • service_url – override the default URL to use for the request, which is created by joining rest_url and endpoint.

  • kwargs – as per requests.request().

property rest_url

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

Return type

str

property 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]

property service_url

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

Return type

str

nexuscli.nexus_util module

nexuscli.nexus_util.REMOTE_PATH_SEPARATOR = '/'

The character used by the Nexus server as a path separator

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: pathlib.Path, is_dir: bool = 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.get_dst_path_and_file(source: str, destination: str) → Tuple[str, str]

Given a source file and destination path, return a destination path and destination file name.

Parameters
  • source – the source file path. The name of this file is used as the returned file name when the given destination is a directory.

  • destination – the destination path. When this is a path to a file, the returned destination is the base path of this value and the returned file name matches this one.

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.pop_repository(component_path)

Helper for split_component_path. Returns the repository and the remainder of the component_path as a path_fragments list.

Parameters

component_path – the component path, as given to split_component_path.

Returns

tuple of (repository, path_fragments)

Return type

tuple(str, list)

nexuscli.nexus_util.remote_path_to_local(remote_src, local_dst, flatten, create=True)

Takes the remote path of an asset (without the repository name), the desired destination in the local file system, and creates the fully qualified path according to the instance settings.

If self.flatten is True, the remote_path isn’t reproduced locally.

If the remote is a directory, we’ll always assume the destination is also a directory, even if it doesn’t end with a /.

Parameters
  • remote_src – path to the artefact as reported by the artefact service (i.e.: the path attribute of an asset object).

  • local_dst – desired location in the local filesystem for the remote_path.

  • create – whether or not to create the local destination file or directory.

Returns

the local path to be used.

nexuscli.nexus_util.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]

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

class nexuscli.NexusConfig(username='admin', password='', 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.

property api_version

Current API version in use.

Return type

str

property auth

Current username and password as a tuple.

Return type

tuple[str, str]

property config_file

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

Return type

str

property config_path

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

dump() → str

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.

return: name of the file written

dump_env()

Writes the current configuration to disk under property:config_file.env.

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

return: name of the file written

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.

property to_dict

Current instance configuration.

Return type

dict

property url

The Nexus service URL

Return type

str

property x509_verify

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

Return type

str

class nexuscli.NexusClient(config: nexuscli.nexus_config.NexusConfig = 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 – instance containing the configuration for the Nexus service used by this instance.

property blobstores

Instance of BlobstoreCollection

property cleanup_policies

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

create_scripts(scripts: List[str]) → None
property repositories

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

property scripts

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

property security_realms

This instance uses the existing instance of NexusClient to communicate with the Nexus service.

property tasks

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