glpkg 1.2.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: glpkg
3
- Version: 1.2.0
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
@@ -105,10 +105,18 @@ Where:
105
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`)
106
106
  - `mypackagename` is the name of the generic package
107
107
  - `1.0` is the version of the generic package to which the file is uploaded
108
- - `my-file.txt` is the file that is uploaded to the generic package. Currently, only relative paths are supported, and the relative path (e.g. `folder/file.txt`) is preserved when uploading the file to the registry.
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.
109
109
 
110
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.
111
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
+
112
120
  ### Use in GitLab pipelines
113
121
 
114
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`.
@@ -126,5 +134,4 @@ To use the `CI_JOB_TOKEN` with package registry of another projects, add `--proj
126
134
 
127
135
  The tool is not perfect (yet) and has limitations. The following limitations are known, but more can exist:
128
136
 
129
- - Uploading files must be done one-by-one.
130
137
  - Only project registries are supported for now.
@@ -92,10 +92,18 @@ Where:
92
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`)
93
93
  - `mypackagename` is the name of the generic package
94
94
  - `1.0` is the version of the generic package to which the file is uploaded
95
- - `my-file.txt` is the file that is uploaded to the generic package. Currently, only relative paths are supported, and the relative path (e.g. `folder/file.txt`) is preserved when uploading the file to the registry.
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.
96
96
 
97
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.
98
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
+
99
107
  ### Use in GitLab pipelines
100
108
 
101
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`.
@@ -113,5 +121,4 @@ To use the `CI_JOB_TOKEN` with package registry of another projects, add `--proj
113
121
 
114
122
  The tool is not perfect (yet) and has limitations. The following limitations are known, but more can exist:
115
123
 
116
- - Uploading files must be done one-by-one.
117
124
  - Only project registries are supported for now.
@@ -31,3 +31,9 @@ where = ["src"]
31
31
  [tool.pytest.ini_options]
32
32
  pythonpath = ["src"]
33
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,5 @@
1
+ """GitLab packages initialization module"""
2
+
3
+ from gitlab.packages import Packages
4
+
5
+ __version__ = "1.3.0"
@@ -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