glpkg 1.1.0__tar.gz → 1.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {glpkg-1.1.0/src/glpkg.egg-info → glpkg-1.3.0}/PKG-INFO +27 -16
- {glpkg-1.1.0 → glpkg-1.3.0}/README.md +25 -8
- {glpkg-1.1.0 → glpkg-1.3.0}/pyproject.toml +7 -10
- glpkg-1.3.0/src/gitlab/__init__.py +5 -0
- glpkg-1.3.0/src/gitlab/__main__.py +29 -0
- glpkg-1.3.0/src/gitlab/cli_handler.py +361 -0
- glpkg-1.3.0/src/gitlab/packages.py +457 -0
- {glpkg-1.1.0 → glpkg-1.3.0/src/glpkg.egg-info}/PKG-INFO +27 -16
- {glpkg-1.1.0 → glpkg-1.3.0}/src/glpkg.egg-info/SOURCES.txt +0 -1
- {glpkg-1.1.0 → glpkg-1.3.0}/test/test_packages.py +30 -6
- glpkg-1.1.0/src/gitlab/__init__.py +0 -3
- glpkg-1.1.0/src/gitlab/__main__.py +0 -12
- glpkg-1.1.0/src/gitlab/cli_handler.py +0 -150
- glpkg-1.1.0/src/gitlab/packages.py +0 -163
- glpkg-1.1.0/src/glpkg.egg-info/requires.txt +0 -8
- {glpkg-1.1.0 → glpkg-1.3.0}/LICENSE.md +0 -0
- {glpkg-1.1.0 → glpkg-1.3.0}/setup.cfg +0 -0
- {glpkg-1.1.0 → glpkg-1.3.0}/src/glpkg.egg-info/dependency_links.txt +0 -0
- {glpkg-1.1.0 → glpkg-1.3.0}/src/glpkg.egg-info/entry_points.txt +0 -0
- {glpkg-1.1.0 → glpkg-1.3.0}/src/glpkg.egg-info/top_level.txt +0 -0
- {glpkg-1.1.0 → glpkg-1.3.0}/test/test_cli_handler.py +0 -0
|
@@ -1,25 +1,19 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: glpkg
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.0
|
|
4
4
|
Summary: Tool to make GitLab generic package registry operations easy.
|
|
5
5
|
Author-email: bugproduction <bugproduction@outlook.com>
|
|
6
6
|
License-Expression: MIT
|
|
7
7
|
Project-URL: Repository, https://gitlab.com/bugproduction/glpkg.git
|
|
8
8
|
Keywords: GitLab,packages,registry,generic
|
|
9
|
+
Requires-Python: >=3.9
|
|
9
10
|
Description-Content-Type: text/markdown
|
|
10
11
|
License-File: LICENSE.md
|
|
11
|
-
Provides-Extra: dev
|
|
12
|
-
Requires-Dist: black; extra == "dev"
|
|
13
|
-
Requires-Dist: build; extra == "dev"
|
|
14
|
-
Requires-Dist: pytest; extra == "dev"
|
|
15
|
-
Requires-Dist: pytest-cov; extra == "dev"
|
|
16
|
-
Requires-Dist: setuptools; extra == "dev"
|
|
17
|
-
Requires-Dist: twine; extra == "dev"
|
|
18
12
|
Dynamic: license-file
|
|
19
13
|
|
|
20
|
-
# glpkg - GitLab Generic Package
|
|
14
|
+
# glpkg - GitLab Generic Package tools
|
|
21
15
|
|
|
22
|
-
glpkg is a tool that makes it easy to work with [GitLab generic
|
|
16
|
+
glpkg is a tool that makes it easy to work with [GitLab generic packages](https://docs.gitlab.com/user/packages/generic_packages/).
|
|
23
17
|
|
|
24
18
|
|
|
25
19
|
## Installation
|
|
@@ -47,7 +41,11 @@ By default, the used GitLab host is gitlab.com. If you use a self-hosted GitLab,
|
|
|
47
41
|
|
|
48
42
|
To authenticate with the package registry in any of the commands below, use `--token readapitoken123` argument where the `readapitoken123` is a [personal](https://docs.gitlab.com/user/profile/personal_access_tokens/#create-a-personal-access-token) or [project](https://docs.gitlab.com/user/project/settings/project_access_tokens/#create-a-project-access-token) access token, with read API scope. In case the package registry is public, you can omit this argument.
|
|
49
43
|
|
|
50
|
-
|
|
44
|
+
Alternatively you can use a token stored in your `.netrc` file by setting `--netrc` argument.
|
|
45
|
+
|
|
46
|
+
> If you use the tool in GitLab CI, read [below](#Use-in-GitLab-pipelines) on how to use the `CI_JOB_TOKEN`.
|
|
47
|
+
|
|
48
|
+
The arguments related to the GitLab host or authentication (`--token`, `--netrc`, and `--ci`) are omitted in the examples below to focus on the commands.
|
|
51
49
|
|
|
52
50
|
In general, run `glpkg --help` when needed.
|
|
53
51
|
|
|
@@ -74,7 +72,7 @@ mypackagename 2.0
|
|
|
74
72
|
|
|
75
73
|
### Download generic package
|
|
76
74
|
|
|
77
|
-
To download
|
|
75
|
+
To download all files from a specific version of a generic package, run
|
|
78
76
|
|
|
79
77
|
```bash
|
|
80
78
|
glpkg download --project 12345 --name mypackagename --version 1.0
|
|
@@ -85,7 +83,13 @@ Where:
|
|
|
85
83
|
- `mypackagename` is the name of the generic package
|
|
86
84
|
- `1.0` is the version of the generic package from which the files are downloaded
|
|
87
85
|
|
|
88
|
-
|
|
86
|
+
By default the files will be downloaded in the current working directory. To download the files to another directory, add argument `--destination` to the command. In all cases, as long as you have permissions to the destination directory, any pre-existing files will be overridden without warning.
|
|
87
|
+
|
|
88
|
+
To download only a specific file from the package, add `--file` argument.
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
glpkg download --project 12345 --name mypackagename --version 1.5 --file the_only_one --destination /temp
|
|
92
|
+
```
|
|
89
93
|
|
|
90
94
|
> If a package has multiple files with the same filename, the tool can only download the newest file. This is a restriction of GitLab API.
|
|
91
95
|
|
|
@@ -101,13 +105,21 @@ Where:
|
|
|
101
105
|
- `12345` is your projects ID ([Find the Project ID](https://docs.gitlab.com/user/project/working_with_projects/#find-the-project-id)) or the path of the project (like `namespace/project`)
|
|
102
106
|
- `mypackagename` is the name of the generic package
|
|
103
107
|
- `1.0` is the version of the generic package to which the file is uploaded
|
|
104
|
-
- `my-file.txt` is the file that is uploaded to the generic package.
|
|
108
|
+
- `my-file.txt` is the file that is uploaded to the generic package. Only relative paths are supported, and the relative path (e.g. `folder/file.txt`) is preserved when uploading the file to the package.
|
|
105
109
|
|
|
106
110
|
> A GitLab generic package may have multiple files with the same file name. However, it likely is not a great idea, as they cannot be downloaded separately from the GitLab API.
|
|
107
111
|
|
|
112
|
+
To upload multiple files, or to upload a single file from a different directory, use `--source` argument. If no `--file` argument is set, all of the files in the source directory are uploaded, recursively. As an example, to upload all files from a `upload` folder to the package:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
glpkg upload --project 12345 --name mypackagename --version 1.0 --source upload
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Note: a file in `upload` folder will be uploaded to the root of the package. If you want to upload the file to a `dir` folder in the package, make a structure in the upload folder, like `upload/dir/`.
|
|
119
|
+
|
|
108
120
|
### Use in GitLab pipelines
|
|
109
121
|
|
|
110
|
-
If you use the tool in a GitLab pipeline,
|
|
122
|
+
If you use the tool in a GitLab pipeline, setting argument `--ci` uses [GitLab predefined variables](https://docs.gitlab.com/ci/variables/predefined_variables/) to configure the tool. In this case `CI_SERVER_HOST`, `CI_PROJECT_ID`, and `CI_JOB_TOKEN` environment variables are used. The `--project`, and `--token` arguments can still be used to override the project ID or to use a personal or project access token instead of `CI_JOB_TOKEN`.
|
|
111
123
|
|
|
112
124
|
In other words, you don't need to give the `--host`, `--project`, or `--token` arguments if you are interacting with the package registry of the project where the pipeline is running. Example: uploading `my-file.txt` to generic package `mypackagename` version `1.0` in the project package registry in CI:
|
|
113
125
|
|
|
@@ -122,5 +134,4 @@ To use the `CI_JOB_TOKEN` with package registry of another projects, add `--proj
|
|
|
122
134
|
|
|
123
135
|
The tool is not perfect (yet) and has limitations. The following limitations are known, but more can exist:
|
|
124
136
|
|
|
125
|
-
- Uploading files must be done one-by-one.
|
|
126
137
|
- Only project registries are supported for now.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# glpkg - GitLab Generic Package
|
|
1
|
+
# glpkg - GitLab Generic Package tools
|
|
2
2
|
|
|
3
|
-
glpkg is a tool that makes it easy to work with [GitLab generic
|
|
3
|
+
glpkg is a tool that makes it easy to work with [GitLab generic packages](https://docs.gitlab.com/user/packages/generic_packages/).
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
## Installation
|
|
@@ -28,7 +28,11 @@ By default, the used GitLab host is gitlab.com. If you use a self-hosted GitLab,
|
|
|
28
28
|
|
|
29
29
|
To authenticate with the package registry in any of the commands below, use `--token readapitoken123` argument where the `readapitoken123` is a [personal](https://docs.gitlab.com/user/profile/personal_access_tokens/#create-a-personal-access-token) or [project](https://docs.gitlab.com/user/project/settings/project_access_tokens/#create-a-project-access-token) access token, with read API scope. In case the package registry is public, you can omit this argument.
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
Alternatively you can use a token stored in your `.netrc` file by setting `--netrc` argument.
|
|
32
|
+
|
|
33
|
+
> If you use the tool in GitLab CI, read [below](#Use-in-GitLab-pipelines) on how to use the `CI_JOB_TOKEN`.
|
|
34
|
+
|
|
35
|
+
The arguments related to the GitLab host or authentication (`--token`, `--netrc`, and `--ci`) are omitted in the examples below to focus on the commands.
|
|
32
36
|
|
|
33
37
|
In general, run `glpkg --help` when needed.
|
|
34
38
|
|
|
@@ -55,7 +59,7 @@ mypackagename 2.0
|
|
|
55
59
|
|
|
56
60
|
### Download generic package
|
|
57
61
|
|
|
58
|
-
To download
|
|
62
|
+
To download all files from a specific version of a generic package, run
|
|
59
63
|
|
|
60
64
|
```bash
|
|
61
65
|
glpkg download --project 12345 --name mypackagename --version 1.0
|
|
@@ -66,7 +70,13 @@ Where:
|
|
|
66
70
|
- `mypackagename` is the name of the generic package
|
|
67
71
|
- `1.0` is the version of the generic package from which the files are downloaded
|
|
68
72
|
|
|
69
|
-
|
|
73
|
+
By default the files will be downloaded in the current working directory. To download the files to another directory, add argument `--destination` to the command. In all cases, as long as you have permissions to the destination directory, any pre-existing files will be overridden without warning.
|
|
74
|
+
|
|
75
|
+
To download only a specific file from the package, add `--file` argument.
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
glpkg download --project 12345 --name mypackagename --version 1.5 --file the_only_one --destination /temp
|
|
79
|
+
```
|
|
70
80
|
|
|
71
81
|
> If a package has multiple files with the same filename, the tool can only download the newest file. This is a restriction of GitLab API.
|
|
72
82
|
|
|
@@ -82,13 +92,21 @@ Where:
|
|
|
82
92
|
- `12345` is your projects ID ([Find the Project ID](https://docs.gitlab.com/user/project/working_with_projects/#find-the-project-id)) or the path of the project (like `namespace/project`)
|
|
83
93
|
- `mypackagename` is the name of the generic package
|
|
84
94
|
- `1.0` is the version of the generic package to which the file is uploaded
|
|
85
|
-
- `my-file.txt` is the file that is uploaded to the generic package.
|
|
95
|
+
- `my-file.txt` is the file that is uploaded to the generic package. Only relative paths are supported, and the relative path (e.g. `folder/file.txt`) is preserved when uploading the file to the package.
|
|
86
96
|
|
|
87
97
|
> A GitLab generic package may have multiple files with the same file name. However, it likely is not a great idea, as they cannot be downloaded separately from the GitLab API.
|
|
88
98
|
|
|
99
|
+
To upload multiple files, or to upload a single file from a different directory, use `--source` argument. If no `--file` argument is set, all of the files in the source directory are uploaded, recursively. As an example, to upload all files from a `upload` folder to the package:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
glpkg upload --project 12345 --name mypackagename --version 1.0 --source upload
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Note: a file in `upload` folder will be uploaded to the root of the package. If you want to upload the file to a `dir` folder in the package, make a structure in the upload folder, like `upload/dir/`.
|
|
106
|
+
|
|
89
107
|
### Use in GitLab pipelines
|
|
90
108
|
|
|
91
|
-
If you use the tool in a GitLab pipeline,
|
|
109
|
+
If you use the tool in a GitLab pipeline, setting argument `--ci` uses [GitLab predefined variables](https://docs.gitlab.com/ci/variables/predefined_variables/) to configure the tool. In this case `CI_SERVER_HOST`, `CI_PROJECT_ID`, and `CI_JOB_TOKEN` environment variables are used. The `--project`, and `--token` arguments can still be used to override the project ID or to use a personal or project access token instead of `CI_JOB_TOKEN`.
|
|
92
110
|
|
|
93
111
|
In other words, you don't need to give the `--host`, `--project`, or `--token` arguments if you are interacting with the package registry of the project where the pipeline is running. Example: uploading `my-file.txt` to generic package `mypackagename` version `1.0` in the project package registry in CI:
|
|
94
112
|
|
|
@@ -103,5 +121,4 @@ To use the `CI_JOB_TOKEN` with package registry of another projects, add `--proj
|
|
|
103
121
|
|
|
104
122
|
The tool is not perfect (yet) and has limitations. The following limitations are known, but more can exist:
|
|
105
123
|
|
|
106
|
-
- Uploading files must be done one-by-one.
|
|
107
124
|
- Only project registries are supported for now.
|
|
@@ -12,21 +12,12 @@ license-files = ["LICENSE.md"]
|
|
|
12
12
|
authors = [
|
|
13
13
|
{ name="bugproduction", email="bugproduction@outlook.com" }
|
|
14
14
|
]
|
|
15
|
+
requires-python = ">= 3.9"
|
|
15
16
|
dependencies = []
|
|
16
17
|
|
|
17
18
|
[project.urls]
|
|
18
19
|
Repository = "https://gitlab.com/bugproduction/glpkg.git"
|
|
19
20
|
|
|
20
|
-
[project.optional-dependencies]
|
|
21
|
-
dev = [
|
|
22
|
-
"black",
|
|
23
|
-
"build",
|
|
24
|
-
"pytest",
|
|
25
|
-
"pytest-cov",
|
|
26
|
-
"setuptools",
|
|
27
|
-
"twine"
|
|
28
|
-
]
|
|
29
|
-
|
|
30
21
|
[project.scripts]
|
|
31
22
|
glpkg = "gitlab.__main__:cli"
|
|
32
23
|
|
|
@@ -40,3 +31,9 @@ where = ["src"]
|
|
|
40
31
|
[tool.pytest.ini_options]
|
|
41
32
|
pythonpath = ["src"]
|
|
42
33
|
addopts = "--cov=src --cov-report term"
|
|
34
|
+
|
|
35
|
+
[tool.pylint.'MESSAGES CONTROL']
|
|
36
|
+
min-public-methods = 1
|
|
37
|
+
max-args = 6
|
|
38
|
+
max-positional-arguments = 6
|
|
39
|
+
ignore = ["build", "test"]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""GitLab packages main module"""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from gitlab.cli_handler import CLIHandler
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def cli() -> int:
|
|
10
|
+
"""
|
|
11
|
+
Runs the main program of the glpkg.
|
|
12
|
+
|
|
13
|
+
Uses arguments from command line and executes the given command.
|
|
14
|
+
|
|
15
|
+
Return
|
|
16
|
+
------
|
|
17
|
+
int
|
|
18
|
+
Zero when everything is fine, non-zero otherwise.
|
|
19
|
+
"""
|
|
20
|
+
logging.basicConfig(
|
|
21
|
+
level=logging.DEBUG,
|
|
22
|
+
handlers=[logging.FileHandler("glpkg.log")], # , logging.StreamHandler()],
|
|
23
|
+
)
|
|
24
|
+
handler = CLIHandler()
|
|
25
|
+
return handler.do_it()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if __name__ == "__main__":
|
|
29
|
+
sys.exit(cli())
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
"""CLI handler for glpkg"""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
from netrc import netrc
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
import urllib
|
|
8
|
+
from gitlab import Packages, __version__
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class CLIHandler:
|
|
12
|
+
"""Class to parse CLI arguments and run the requested command"""
|
|
13
|
+
|
|
14
|
+
def __init__(self):
|
|
15
|
+
"""
|
|
16
|
+
Creates a new instance of CLIHandler.
|
|
17
|
+
|
|
18
|
+
Parses the arguments from command line and prepares everything
|
|
19
|
+
to be ready to executed. Just use do_it to run it!
|
|
20
|
+
"""
|
|
21
|
+
parser = argparse.ArgumentParser(
|
|
22
|
+
description="Toolbox for GitLab generic packages"
|
|
23
|
+
)
|
|
24
|
+
parser.add_argument("-v", "--version", action="store_true")
|
|
25
|
+
parser.set_defaults(action=self._print_version)
|
|
26
|
+
subparsers = parser.add_subparsers()
|
|
27
|
+
list_parser = subparsers.add_parser(
|
|
28
|
+
name="list",
|
|
29
|
+
description="Lists the available version of a package from the "
|
|
30
|
+
"package registry.",
|
|
31
|
+
)
|
|
32
|
+
self._register_list_parser(list_parser)
|
|
33
|
+
download_parser = subparsers.add_parser(
|
|
34
|
+
name="download",
|
|
35
|
+
description="Downloads all files from a specific package version "
|
|
36
|
+
"to the current directory.",
|
|
37
|
+
)
|
|
38
|
+
self._register_download_parser(download_parser)
|
|
39
|
+
upload_parser = subparsers.add_parser(
|
|
40
|
+
name="upload", description="Uploads file to a specific package version."
|
|
41
|
+
)
|
|
42
|
+
self._register_upload_parser(upload_parser)
|
|
43
|
+
self.args = parser.parse_args()
|
|
44
|
+
|
|
45
|
+
def _print_version(self, _args: argparse.Namespace) -> int:
|
|
46
|
+
"""
|
|
47
|
+
A handler for printing the version of the tool to the console.
|
|
48
|
+
|
|
49
|
+
Parameters
|
|
50
|
+
----------
|
|
51
|
+
_args : argparse.Namespace
|
|
52
|
+
Unused.
|
|
53
|
+
|
|
54
|
+
Return
|
|
55
|
+
------
|
|
56
|
+
int
|
|
57
|
+
Zero when printing to console succeeded.
|
|
58
|
+
"""
|
|
59
|
+
print(__version__)
|
|
60
|
+
return 0
|
|
61
|
+
|
|
62
|
+
def do_it(self) -> int:
|
|
63
|
+
"""
|
|
64
|
+
Executes the requested command.
|
|
65
|
+
|
|
66
|
+
In case of error, prints to stderr.
|
|
67
|
+
|
|
68
|
+
Return
|
|
69
|
+
------
|
|
70
|
+
int
|
|
71
|
+
Zero when everything went fine, non-zero otherwise.
|
|
72
|
+
"""
|
|
73
|
+
ret = 1
|
|
74
|
+
try:
|
|
75
|
+
ret = self.args.action(self.args)
|
|
76
|
+
except urllib.error.HTTPError as e:
|
|
77
|
+
# GitLab API returns 404 when a resource is not found
|
|
78
|
+
# but also when the user has no access to the resource
|
|
79
|
+
print("Oops! Something did go wrong.", file=sys.stderr)
|
|
80
|
+
print(e, file=sys.stderr)
|
|
81
|
+
print(
|
|
82
|
+
"Note that Error 404 may also indicate authentication issues with GitLab API.",
|
|
83
|
+
file=sys.stderr,
|
|
84
|
+
)
|
|
85
|
+
print("Check your arguments and credentials.", file=sys.stderr)
|
|
86
|
+
return ret
|
|
87
|
+
|
|
88
|
+
def _register_common_arguments(self, parser: argparse.ArgumentParser) -> None:
|
|
89
|
+
"""
|
|
90
|
+
Registers common arguments to the parser:
|
|
91
|
+
- host
|
|
92
|
+
- ci
|
|
93
|
+
- project
|
|
94
|
+
- name
|
|
95
|
+
- token
|
|
96
|
+
- netrc
|
|
97
|
+
|
|
98
|
+
Parameters
|
|
99
|
+
----------
|
|
100
|
+
parser:
|
|
101
|
+
The argparser where to register the common arguments.
|
|
102
|
+
"""
|
|
103
|
+
group = parser.add_mutually_exclusive_group()
|
|
104
|
+
group.add_argument(
|
|
105
|
+
"-H",
|
|
106
|
+
"--host",
|
|
107
|
+
default="gitlab.com",
|
|
108
|
+
type=str,
|
|
109
|
+
help="The host address of GitLab instance without scheme, "
|
|
110
|
+
"for example gitlab.com. Note that only https scheme is supported.",
|
|
111
|
+
)
|
|
112
|
+
group.add_argument(
|
|
113
|
+
"-c",
|
|
114
|
+
"--ci",
|
|
115
|
+
action="store_true",
|
|
116
|
+
help="Use this in GitLab jobs. In this case CI_SERVER_HOST, CI_PROJECT_ID, "
|
|
117
|
+
"and CI_JOB_TOKEN variables from the environment are used. --project and --token "
|
|
118
|
+
"can be used to override project ID and the CI_JOB_TOKEN to a personal or project "
|
|
119
|
+
"access token.",
|
|
120
|
+
)
|
|
121
|
+
parser.add_argument(
|
|
122
|
+
"-p",
|
|
123
|
+
"--project",
|
|
124
|
+
type=str,
|
|
125
|
+
help="The project ID or path. For example 123456 or namespace/project.",
|
|
126
|
+
)
|
|
127
|
+
parser.add_argument("-n", "--name", type=str, help="The package name.")
|
|
128
|
+
group2 = parser.add_mutually_exclusive_group()
|
|
129
|
+
group2.add_argument(
|
|
130
|
+
"-t",
|
|
131
|
+
"--token",
|
|
132
|
+
type=str,
|
|
133
|
+
help="Private or project access token that is used to authenticate with "
|
|
134
|
+
"the package registry. Leave empty if the registry is public. The token "
|
|
135
|
+
"must have 'read API' or 'API' scope.",
|
|
136
|
+
)
|
|
137
|
+
group2.add_argument(
|
|
138
|
+
"--netrc",
|
|
139
|
+
action="store_true",
|
|
140
|
+
help="Set to use a token from .netrc file (~/.netrc) for the host. The "
|
|
141
|
+
".netrc username is ignored due to API restrictions. PRIVATE-TOKEN is used "
|
|
142
|
+
"instead. Note that .netrc file access rights must be correct.",
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
def _register_download_parser(self, parser: argparse.ArgumentParser):
|
|
146
|
+
"""
|
|
147
|
+
Registers the download command related arguments to the parser:
|
|
148
|
+
- version
|
|
149
|
+
- file
|
|
150
|
+
- destination
|
|
151
|
+
- action
|
|
152
|
+
|
|
153
|
+
Additionally, registers the common args.
|
|
154
|
+
|
|
155
|
+
Parameters
|
|
156
|
+
----------
|
|
157
|
+
parser:
|
|
158
|
+
The argparser where to register the download arguments.
|
|
159
|
+
"""
|
|
160
|
+
self._register_common_arguments(parser)
|
|
161
|
+
parser.add_argument("-v", "--version", type=str, help="The package version.")
|
|
162
|
+
parser.add_argument(
|
|
163
|
+
"-f",
|
|
164
|
+
"--file",
|
|
165
|
+
type=str,
|
|
166
|
+
help="The file to download from the package. If not defined, all "
|
|
167
|
+
"files are downloaded.",
|
|
168
|
+
)
|
|
169
|
+
parser.add_argument(
|
|
170
|
+
"-d",
|
|
171
|
+
"--destination",
|
|
172
|
+
default="",
|
|
173
|
+
type=str,
|
|
174
|
+
help="The path where the file(s) are downloaded. If not defined, "
|
|
175
|
+
"the current working directory is used.",
|
|
176
|
+
)
|
|
177
|
+
parser.set_defaults(action=self._download_handler)
|
|
178
|
+
|
|
179
|
+
def _args(self, args) -> tuple[str, str, str, str, str]:
|
|
180
|
+
"""
|
|
181
|
+
Returns the connection parameters according to the args
|
|
182
|
+
|
|
183
|
+
Parameters
|
|
184
|
+
----------
|
|
185
|
+
args:
|
|
186
|
+
The args that are used to determined the connection parameters
|
|
187
|
+
|
|
188
|
+
Returns
|
|
189
|
+
-------
|
|
190
|
+
host : str
|
|
191
|
+
The GitLab host name
|
|
192
|
+
project : str
|
|
193
|
+
The Project ID or name to use
|
|
194
|
+
name : str
|
|
195
|
+
The package name
|
|
196
|
+
token_user : str
|
|
197
|
+
The token user according to the args. If ci is used, returns `JOB-TOKEN`, else
|
|
198
|
+
`PRIVATE-TOKEN`.
|
|
199
|
+
token : str
|
|
200
|
+
The token according to the args. If token is set, returns it. If netrc is set,
|
|
201
|
+
reads the token from the .netrc file. If ci is set, reads the environment
|
|
202
|
+
variable CI_JOB_TOKEN. Otherwise returns None.
|
|
203
|
+
"""
|
|
204
|
+
if args.ci:
|
|
205
|
+
host = os.environ["CI_SERVER_HOST"]
|
|
206
|
+
project = os.environ["CI_PROJECT_ID"]
|
|
207
|
+
token = os.environ["CI_JOB_TOKEN"]
|
|
208
|
+
token_user = "JOB-TOKEN"
|
|
209
|
+
if args.project:
|
|
210
|
+
project = args.project
|
|
211
|
+
if args.token:
|
|
212
|
+
token = args.token
|
|
213
|
+
token_user = "PRIVATE-TOKEN"
|
|
214
|
+
else:
|
|
215
|
+
host = args.host
|
|
216
|
+
project = args.project
|
|
217
|
+
token = args.token
|
|
218
|
+
token_user = "PRIVATE-TOKEN"
|
|
219
|
+
if args.netrc:
|
|
220
|
+
_, _, token = netrc().authenticators(host)
|
|
221
|
+
token_user = "PRIVATE-TOKEN"
|
|
222
|
+
name = args.name
|
|
223
|
+
return host, project, name, token_user, token
|
|
224
|
+
|
|
225
|
+
def _download_handler(self, args: argparse.Namespace) -> int:
|
|
226
|
+
"""
|
|
227
|
+
Downloads package file(s) from GitLab package registry.
|
|
228
|
+
|
|
229
|
+
Parameters
|
|
230
|
+
----------
|
|
231
|
+
args : argparse.Namespace
|
|
232
|
+
The parsed arguments
|
|
233
|
+
|
|
234
|
+
Returns
|
|
235
|
+
-------
|
|
236
|
+
int
|
|
237
|
+
Zero if everything goes well, non-zero otherwise
|
|
238
|
+
"""
|
|
239
|
+
ret = 1
|
|
240
|
+
host, project, name, token_user, token = self._args(args)
|
|
241
|
+
version = args.version
|
|
242
|
+
destination = args.destination
|
|
243
|
+
gitlab = Packages(host, token_user, token)
|
|
244
|
+
package_id = gitlab.get_package_id(project, name, version)
|
|
245
|
+
if package_id:
|
|
246
|
+
files = []
|
|
247
|
+
if args.file:
|
|
248
|
+
files.append(args.file)
|
|
249
|
+
else:
|
|
250
|
+
files = gitlab.list_files(project, package_id)
|
|
251
|
+
for file in files:
|
|
252
|
+
ret = gitlab.download_file(project, name, version, file, destination)
|
|
253
|
+
if ret:
|
|
254
|
+
print("Failed to download file " + file)
|
|
255
|
+
break
|
|
256
|
+
else:
|
|
257
|
+
print("No package " + name + " version " + version + " found!")
|
|
258
|
+
return ret
|
|
259
|
+
|
|
260
|
+
def _register_list_parser(self, parser: argparse.ArgumentParser):
|
|
261
|
+
"""
|
|
262
|
+
Registers the list command related arguments to the parser:
|
|
263
|
+
- action
|
|
264
|
+
|
|
265
|
+
Additionally, registers the common args.
|
|
266
|
+
|
|
267
|
+
Parameters
|
|
268
|
+
----------
|
|
269
|
+
parser:
|
|
270
|
+
The argparser where to register the list arguments.
|
|
271
|
+
"""
|
|
272
|
+
self._register_common_arguments(parser)
|
|
273
|
+
parser.set_defaults(action=self._list_packages)
|
|
274
|
+
|
|
275
|
+
def _list_packages(self, args: argparse.Namespace) -> int:
|
|
276
|
+
"""
|
|
277
|
+
List package versions from GitLab package registry.
|
|
278
|
+
|
|
279
|
+
Parameters
|
|
280
|
+
----------
|
|
281
|
+
args : argparse.Namespace
|
|
282
|
+
The parsed arguments
|
|
283
|
+
|
|
284
|
+
Returns
|
|
285
|
+
-------
|
|
286
|
+
int
|
|
287
|
+
Zero if everything goes well, non-zero otherwise
|
|
288
|
+
"""
|
|
289
|
+
host, project, name, token_user, token = self._args(args)
|
|
290
|
+
gitlab = Packages(host, token_user, token)
|
|
291
|
+
packages = gitlab.list_packages(project, name)
|
|
292
|
+
print("Name" + "\t\t" + "Version")
|
|
293
|
+
for package in packages:
|
|
294
|
+
print(package["name"] + "\t" + package["version"])
|
|
295
|
+
|
|
296
|
+
def _register_upload_parser(self, parser: argparse.ArgumentParser):
|
|
297
|
+
"""
|
|
298
|
+
Registers the upload command related arguments to the parser:
|
|
299
|
+
- version
|
|
300
|
+
- file
|
|
301
|
+
- action
|
|
302
|
+
|
|
303
|
+
Additionally, registers the common args.
|
|
304
|
+
|
|
305
|
+
Parameters
|
|
306
|
+
----------
|
|
307
|
+
parser:
|
|
308
|
+
The argparser where to register the upload arguments.
|
|
309
|
+
"""
|
|
310
|
+
self._register_common_arguments(parser)
|
|
311
|
+
parser.add_argument("-v", "--version", type=str, help="The package version.")
|
|
312
|
+
parser.add_argument(
|
|
313
|
+
"-f",
|
|
314
|
+
"--file",
|
|
315
|
+
type=str,
|
|
316
|
+
help="The file to be uploaded, for example my_file.txt. Note that "
|
|
317
|
+
"only relative paths (to the source) are supported and the relative "
|
|
318
|
+
"path is preserved when uploading the file. If left undefined, all files "
|
|
319
|
+
"of the source directory are uploaded. For example --source=temp --file=myfile "
|
|
320
|
+
"will upload myfile to the GitLab generic package root. However using --source=. "
|
|
321
|
+
"(or omittinge source) --file=temp/myfile will upload the file to temp folder "
|
|
322
|
+
"in the GitLab package.",
|
|
323
|
+
)
|
|
324
|
+
parser.add_argument(
|
|
325
|
+
"-s",
|
|
326
|
+
"--source",
|
|
327
|
+
type=str,
|
|
328
|
+
default="",
|
|
329
|
+
help="The source directory of the uploaded file(s). Defaults to current"
|
|
330
|
+
"working directory.",
|
|
331
|
+
)
|
|
332
|
+
parser.set_defaults(action=self._upload)
|
|
333
|
+
|
|
334
|
+
def _upload(self, args: argparse.Namespace) -> int:
|
|
335
|
+
"""
|
|
336
|
+
Uploads a file to a GitLab package registry
|
|
337
|
+
|
|
338
|
+
Parameters
|
|
339
|
+
----------
|
|
340
|
+
args : argparse.Namespace
|
|
341
|
+
The arguments from command line
|
|
342
|
+
|
|
343
|
+
Returns
|
|
344
|
+
-------
|
|
345
|
+
int
|
|
346
|
+
Zero if everything went fine, non-zero otherwise.
|
|
347
|
+
"""
|
|
348
|
+
ret = 0
|
|
349
|
+
host, project, name, token_user, token = self._args(args)
|
|
350
|
+
version = args.version
|
|
351
|
+
file = args.file
|
|
352
|
+
source = args.source
|
|
353
|
+
if file:
|
|
354
|
+
# Check if the uploaded file exists.
|
|
355
|
+
if not os.path.isfile(os.path.join(source, file)):
|
|
356
|
+
print("File " + file + " does not exist!")
|
|
357
|
+
ret = 1
|
|
358
|
+
if not ret:
|
|
359
|
+
gitlab = Packages(host, token_user, token)
|
|
360
|
+
ret = gitlab.upload_file(project, name, version, file, source)
|
|
361
|
+
return ret
|