nexuscli package¶
Subpackages¶
Submodules¶
nexuscli.exception module¶
-
exception
nexuscli.exception.
DownloadError
¶ Bases:
Exception
Error retrieving artefact from Nexus service.
-
exception
nexuscli.exception.
NexusClientAPIError
¶ Bases:
Exception
Unexpected response from Nexus service.
-
exception
nexuscli.exception.
NexusClientConnectionError
¶ Bases:
Exception
Generic network connector error.
-
exception
nexuscli.exception.
NexusClientCreateCleanupPolicyError
¶ Bases:
Exception
Used when a cleanup policy creation operation in Nexus fails.
-
exception
nexuscli.exception.
NexusClientCreateRepositoryError
¶ Bases:
Exception
Used when a repository creation operation in Nexus fails.
-
exception
nexuscli.exception.
NexusClientInvalidCleanupPolicy
¶ Bases:
Exception
The given cleanup policy does not exist in Nexus.
-
exception
nexuscli.exception.
NexusClientInvalidCredentials
¶ Bases:
Exception
Login credentials not accepted by Nexus service. Usually the result of a HTTP 401 response.
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
andpassword
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 ofNexusClient
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:
-
download_file
(download_url, destination)¶ Download an asset from Nexus artefact repository to local file system.
Parameters: Returns:
-
http_delete
(endpoint, **kwargs)¶ Performs a HTTP DELETE request on the given endpoint.
Parameters: - endpoint (str) – name of the Nexus REST API endpoint.
- kwargs – as per
requests.request()
.
Return type:
-
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: - endpoint (str) – name of the Nexus REST API endpoint.
- kwargs – as per
requests.request()
.
Return type:
-
http_put
(endpoint, **kwargs)¶ Performs a HTTP PUT request on the given endpoint.
Parameters: - endpoint (str) – name of the Nexus REST API endpoint.
- kwargs – as per
requests.request()
.
Return type:
-
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
andendpoint
. - kwargs – as per
requests.request()
.
Return type: - method – one of
-
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 ofNexusClient
to communicate with the Nexus service.Return type: RepositoryCollection
-
scripts
¶ Instance of
ScriptCollection
. This will automatically use the existing instance ofNexusClient
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 givencomponent_path
doesn’t represent a file, then thefilename
is set toNone
.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
andpassword
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
andnexus_verify
.
-
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
.
nexuscli.nexus_util module¶
-
nexuscli.nexus_util.
calculate_hash
(hash_name, file_path_or_handle)¶ Calculate a hash for the given file.
Parameters: Returns: the calculated hash
Return type:
-
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:
- the path dict key is a str
- 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: - artefact (dict) – as returned by