models.py 25.6 KB
Newer Older
1
import io
2
from typing import List, Optional, Tuple
3

David Runge's avatar
David Runge committed
4
5
from pyalpm import vercmp
from pydantic import BaseModel, validator
6

7
8
from repo_management import defaults

9

David Runge's avatar
David Runge committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Arch(BaseModel):
    """A model describing a single 'arch' attribute

    Attributes
    ----------
    arch: str
        The attribute can be used to describe the (required) data below an %ARCH% identifier in a 'desc' file, which
        identifies a package's architecture
    """

    arch: str


class Backup(BaseModel):
    """A model describing a single 'backup' attribute

    Attributes
    ----------
    backup: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %BACKUP% identifier in a 'desc' file, which
        identifies which file(s) of a package pacman will create backups for
    """

    backup: Optional[List[str]]


36
class Base(BaseModel):
David Runge's avatar
David Runge committed
37
    """A model describing a single 'base' attribute
38

David Runge's avatar
David Runge committed
39
40
    Attributes
    ----------
41
    base: str
David Runge's avatar
David Runge committed
42
43
44
        The attribute can be used to describe the (required) data below a %BASE% identifier in a 'desc' file, which
        identifies a package's pkgbase
    """
45

David Runge's avatar
David Runge committed
46
    base: str
47
48


David Runge's avatar
David Runge committed
49
50
class BuildDate(BaseModel):
    """A model describing a single 'builddate' attribute
51

David Runge's avatar
David Runge committed
52
53
54
55
56
57
    Attributes
    ----------
    builddate: int
        The attribute can be used to describe the (required) data below a %BUILDDATE% identifier in a 'desc' file,
        which identifies a package's build date (represented in seconds since the epoch)
    """
58

David Runge's avatar
David Runge committed
59
    builddate: int
60

David Runge's avatar
David Runge committed
61
62
63
64
65
66
67
    @validator("builddate")
    def builddate_greater_zero(cls, builddate: int) -> int:
        if builddate < 0:
            raise ValueError("The build date must be greater than zero.")

        return builddate

68
69

class CheckDepends(BaseModel):
David Runge's avatar
David Runge committed
70
71
72
73
74
75
76
77
    """A model describing a single 'checkdepends' attribute

    Attributes
    ----------
    checkdepends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %CHECKDEPENDS% identifier in a 'desc' file,
        which identifies a package's checkdepends
    """
78
79
80
81

    checkdepends: Optional[List[str]]


David Runge's avatar
David Runge committed
82
83
class Conflicts(BaseModel):
    """A model describing a single 'conflicts' attribute
84

David Runge's avatar
David Runge committed
85
86
87
88
89
90
    Attributes
    ----------
    conflicts: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %CONFLICTS% identifier in a 'desc' file, which
        identifies what other package(s) a package conflicts with
    """
91

David Runge's avatar
David Runge committed
92
    conflicts: Optional[List[str]]
93
94


David Runge's avatar
David Runge committed
95
96
97
98
99
100
101
102
103
104
105
106
class CSize(BaseModel):
    """A model describing a single 'csize' attribute

    Attributes
    ----------
    csize: int
        The attribute can be used to describe the (required) data below a %CSIZE% identifier in a 'desc' file, which
        identifies a package's size
    """

    csize: int

David Runge's avatar
David Runge committed
107
108
109
110
111
112
113
    @validator("csize")
    def csize_greater_equal_zero(cls, csize: int) -> int:
        if csize < 0:
            raise ValueError("The csize must be greater than or equal zero.")

        return csize

David Runge's avatar
David Runge committed
114
115
116
117
118
119
120
121
122
123
124
125

class Depends(BaseModel):
    """A model describing a single 'depends' attribute

    Attributes
    ----------
    depends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %DEPENDS% identifier in a 'desc' file, which
        identifies what other package(s) a package depends on
    """

    depends: Optional[List[str]]
126
127
128


class Desc(BaseModel):
David Runge's avatar
David Runge committed
129
    """A model describing a single 'desc' attribute
130

David Runge's avatar
David Runge committed
131
132
    Attributes
    ----------
133
    desc: str
David Runge's avatar
David Runge committed
134
135
136
        The attribute can be used to describe the (required) data below a %DESC% identifier in a 'desc' file, which
        identifies a package's description
    """
137

David Runge's avatar
David Runge committed
138
    desc: str
139
140


David Runge's avatar
David Runge committed
141
142
class FileName(BaseModel):
    """A model describing a single 'filename' attribute
143

David Runge's avatar
David Runge committed
144
145
146
147
148
149
    Attributes
    ----------
    filename: str
        The attribute can be used to describe the (required) data below a %FILENAME% identifier in a 'desc' file, which
        identifies a package's file name
    """
150

David Runge's avatar
David Runge committed
151
    filename: str
152
153


David Runge's avatar
David Runge committed
154
155
class Files(BaseModel):
    """A model describing a single 'files' attribute
156

David Runge's avatar
David Runge committed
157
158
159
160
161
162
    Attributes
    ----------
    files: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %FILES% identifier in a 'files' file, which
        identifies which file(s) belong to a package
    """
163

David Runge's avatar
David Runge committed
164
    files: Optional[List[str]]
165
166


David Runge's avatar
David Runge committed
167
168
class Groups(BaseModel):
    """A model describing a single 'groups' attribute
169

David Runge's avatar
David Runge committed
170
171
172
173
174
175
    Attributes
    ----------
    groups: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %GROUPS% identifier in a 'desc' file, which
        identifies a package's groups
    """
176

David Runge's avatar
David Runge committed
177
    groups: Optional[List[str]]
178
179


David Runge's avatar
David Runge committed
180
181
class ISize(BaseModel):
    """A model describing a single 'isize' attribute
182

David Runge's avatar
David Runge committed
183
184
185
186
    Attributes
    ----------
    isize: int
        The attribute can be used to describe the (required) data below an %ISIZE% identifier in a 'desc' file, which
David Runge's avatar
David Runge committed
187
        identifies a package's installed size
David Runge's avatar
David Runge committed
188
    """
189

David Runge's avatar
David Runge committed
190
    isize: int
191

David Runge's avatar
David Runge committed
192
193
194
195
196
197
198
    @validator("isize")
    def isize_greater_equal_zero(cls, isize: int) -> int:
        if isize < 0:
            raise ValueError("The isize must be greater than or equal zero.")

        return isize

199

David Runge's avatar
David Runge committed
200
201
class License(BaseModel):
    """A model describing a single 'license' attribute
202

David Runge's avatar
David Runge committed
203
204
205
206
207
208
    Attributes
    ----------
    license: List[str]
        The attribute can be used to describe the (required) data below a %LICENSE% identifier in a 'desc' file, which
        identifies a package's license(s)
    """
209

David Runge's avatar
David Runge committed
210
    license: List[str]
211
212


David Runge's avatar
David Runge committed
213
214
class MakeDepends(BaseModel):
    """A model describing a single 'makedepends' attribute
215

David Runge's avatar
David Runge committed
216
217
218
219
220
221
    Attributes
    ----------
    makedepends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %MAKEDEPENDS% identifier in a 'desc' file,
        which identifies a package's makedepends
    """
222

David Runge's avatar
David Runge committed
223
    makedepends: Optional[List[str]]
224
225


David Runge's avatar
David Runge committed
226
227
class Md5Sum(BaseModel):
    """A model describing a single 'md5sum' attribute
228

David Runge's avatar
David Runge committed
229
230
231
232
233
234
    Attributes
    ----------
    md5sum: str
        The attribute can be used to describe the (required) data below an %MD5SUM% identifier in a 'desc' file, which
        identifies a package's md5 checksum
    """
235

David Runge's avatar
David Runge committed
236
    md5sum: str
237

David Runge's avatar
David Runge committed
238
239
240
241
242
243
244
245
246
247
248
249

class Name(BaseModel):
    """A model describing a single 'name' attribute

    Attributes
    ----------
    name: str
        The attribute can be used to describe the (required) data below a %NAME% identifier in a 'desc' file, which
        identifies a package's name
    """

    name: str
250

David Runge's avatar
David Runge committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    @validator("name")
    def name_contains_only_allowed_chars(cls, name: str) -> str:
        disallowed_start_chars = [".", "-"]
        for char in disallowed_start_chars:
            if name.startswith(char):
                raise ValueError(f"The package name '{name}' can not start with any of '{disallowed_start_chars}'.")

        allowed_chars = ["@", ".", "_", "+", "-"]
        remaining_chars: List[str] = []
        for char in name:
            if (not char.isalnum() or (not char.isdigit() and not char.islower())) and char not in allowed_chars:
                remaining_chars += [char]
        if remaining_chars:
            raise ValueError(
                f"The package name '{name}' can not contain '{remaining_chars}' but must consist only of alphanumeric "
                f"chars and any of '{allowed_chars}'."
            )

        return name

271
272

class Packager(BaseModel):
David Runge's avatar
David Runge committed
273
274
275
276
277
278
279
280
    """A model describing a single 'packager' attribute

    Attributes
    ----------
    packager: str
        The attribute can be used to describe the (required) data below a %PACKAGER% identifier in a 'desc' file, which
        identifies a package's packager
    """
281
282
283
284

    packager: str


David Runge's avatar
David Runge committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
class PgpSig(BaseModel):
    """A model describing a single 'pgpsig' attribute

    Attributes
    ----------
    pgpsig: str
        The attribute can be used to describe the (required) data below a %PGPSIG% identifier in a 'desc' file, which
        identifies a package's PGP signature
    """

    pgpsig: str


class Provides(BaseModel):
    """A model describing a single 'provides' attribute

    Attributes
    ----------
    provides: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %PROVIDES% identifier in a 'desc' file, which
        identifies what other package(s) a package provides
    """

    provides: Optional[List[str]]


311
class Replaces(BaseModel):
David Runge's avatar
David Runge committed
312
    """A model describing a single 'replaces' attribute
313

David Runge's avatar
David Runge committed
314
315
    Attributes
    ----------
316
    replaces: Optional[List[str]]
David Runge's avatar
David Runge committed
317
318
319
        The attribute can be used to describe the (optional) data below a %REPLACES% identifier in a 'desc' file, which
        identifies what other package(s) a package replaces
    """
320

David Runge's avatar
David Runge committed
321
    replaces: Optional[List[str]]
322
323


David Runge's avatar
David Runge committed
324
325
326
class RepoDbMemberType(BaseModel):
    """A model describing a single 'member_type' attribute, which is used to identify/ distinguish different types of
    repository database file types (e.g. 'desc' and 'files' files, which are contained in a repository database file).
327

David Runge's avatar
David Runge committed
328
329
330
331
332
    Attributes
    ----------
    member_type: defaults.RepoDbMemberType
        A member of the IntEnum defaults.RepoDbMemberType
    """
333

David Runge's avatar
David Runge committed
334
    member_type: defaults.RepoDbMemberType
335
336


David Runge's avatar
David Runge committed
337
338
class Sha256Sum(BaseModel):
    """A model describing a single 'sha256sum' attribute
339

David Runge's avatar
David Runge committed
340
341
342
343
344
345
    Attributes
    ----------
    sha256sum: str
        The attribute can be used to describe the (required) data below an %SHA256SUM% identifier in a 'desc' file,
        which identifies a package's sha256 checksum
    """
346

David Runge's avatar
David Runge committed
347
    sha256sum: str
348
349
350


class OptDepends(BaseModel):
David Runge's avatar
David Runge committed
351
    """A model describing a single 'optdepends' attribute
352

David Runge's avatar
David Runge committed
353
354
    Attributes
    ----------
355
    optdepends: Optional[List[str]]
David Runge's avatar
David Runge committed
356
357
358
        The attribute can be used to describe the (optional) data below a %OPTDEPENDS% identifier in a 'desc' file,
        which identifies what other package(s) a package optionally depends on
    """
359

David Runge's avatar
David Runge committed
360
    optdepends: Optional[List[str]]
361
362


David Runge's avatar
David Runge committed
363
364
class Url(BaseModel):
    """A model describing a single 'url' attribute
365

David Runge's avatar
David Runge committed
366
367
368
369
370
371
    Attributes
    ----------
    url: str
        The attribute can be used to describe the (required) data below a %URL% identifier in a 'desc' file, which
        identifies a package's URL
    """
372

David Runge's avatar
David Runge committed
373
    url: str
374
375


David Runge's avatar
David Runge committed
376
377
class Version(BaseModel):
    """A model describing a single 'version' attribute
378

David Runge's avatar
David Runge committed
379
380
381
382
383
384
385
386
    Attributes
    ----------
    version: str
        The attribute can be used to describe the (required) data below a %VERSION% identifier in a 'desc' file, which
        identifies a package's version (this is the accumulation of epoch, pkgver and pkgrel)
    """

    version: str
387

David Runge's avatar
David Runge committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
    @validator("version")
    def version_is_valid(cls, version: str) -> str:
        allowed_chars = [":", ".", "_", "+", "-"]

        if version.endswith("-0"):
            raise ValueError("The first pkgrel of a package release always needs to start at 1.")
        for char in allowed_chars:
            if version.startswith(char):
                raise ValueError("The first character of a package version must not be '{char}'.")

        remaining_chars: List[str] = []
        for char in version:
            if not char.isalnum() and char not in allowed_chars:
                remaining_chars += [char]
        if remaining_chars:
            raise ValueError(
                f"Package versions can not contain '{remaining_chars}' but must consist of alphanumeric chars and any "
                f"of '{allowed_chars}'."
            )

        return version

    def is_older_than(self, version: str) -> bool:
        """Check whether the version is older than a provided version

        Parameters
        ----------
        version: str
            Another version string to compare that of self to

        Returns
        -------
        True if self.version is older than the provided version, False otherwise.
        """

        if vercmp(self.version, version) < 0:
            return True
        else:
            return False

    def is_newer_than(self, version: str) -> bool:
        """Check whether the version is newer than a provided version

        Parameters
        ----------
        version: str
            Another version string to compare that of self to

        Returns
        -------
        True if self.version is newer than the provided version, False otherwise.
        """

        if vercmp(self.version, version) > 0:
            return True
        else:
            return False

446

447
class OutputPackage(
448
449
450
    Arch,
    Backup,
    BuildDate,
David Runge's avatar
David Runge committed
451
    CheckDepends,
452
453
454
455
456
    Conflicts,
    CSize,
    Depends,
    Desc,
    FileName,
457
    Files,
458
459
460
461
462
463
464
465
466
467
468
469
    Groups,
    ISize,
    License,
    Md5Sum,
    Name,
    OptDepends,
    PgpSig,
    Provides,
    Replaces,
    Sha256Sum,
    Url,
):
David Runge's avatar
David Runge committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
    """A model describing all required attributes that define a package in the context of an output file

    Attributes
    ----------
    arch: str
        The attribute can be used to describe the (required) data below an %ARCH% identifier in a 'desc' file, which
        identifies a package's architecture
    backup: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %BACKUP% identifier in a 'desc' file, which
        identifies which file(s) of a package pacman will create backups for
    builddate: int
        The attribute can be used to describe the (required) data below a %BUILDDATE% identifier in a 'desc' file,
        which identifies a package's build date (represented in seconds since the epoch)
    checkdepends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %CHECKDEPENDS% identifier in a 'desc' file,
        which identifies a package's checkdepends
    conflicts: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %CONFLICTS% identifier in a 'desc' file, which
        identifies what other package(s) a package conflicts with
    csize: int
        The attribute can be used to describe the (required) data below a %CSIZE% identifier in a 'desc' file, which
        identifies a package's size
    depends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %DEPENDS% identifier in a 'desc' file, which
        identifies what other package(s) a package depends on
    desc: str
        The attribute can be used to describe the (required) data below a %DESC% identifier in a 'desc' file, which
        identifies a package's description
    filename: str
        The attribute can be used to describe the (required) data below a %FILENAME% identifier in a 'desc' file, which
        identifies a package's file name
    files: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %FILES% identifier in a 'files' file, which
        identifies which file(s) belong to a package
    groups: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %GROUPS% identifier in a 'desc' file, which
        identifies a package's groups
    isize: int
        The attribute can be used to describe the (required) data below an %ISIZE% identifier in a 'desc' file, which
David Runge's avatar
David Runge committed
509
        identifies a package's installed size
David Runge's avatar
David Runge committed
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
    license: List[str]
        The attribute can be used to describe the (required) data below a %LICENSE% identifier in a 'desc' file, which
        identifies a package's license(s)
    md5sum: str
        The attribute can be used to describe the (required) data below an %MD5SUM% identifier in a 'desc' file, which
        identifies a package's md5 checksum
    name: str
        The attribute can be used to describe the (required) data below a %NAME% identifier in a 'desc' file, which
        identifies a package's name
    optdepends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %OPTDEPENDS% identifier in a 'desc' file,
        which identifies what other package(s) a package optionally depends on
    pgpsig: str
        The attribute can be used to describe the (required) data below a %PGPSIG% identifier in a 'desc' file, which
        identifies a package's PGP signature
    provides: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %PROVIDES% identifier in a 'desc' file, which
        identifies what other package(s) a package provides
    replaces: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %REPLACES% identifier in a 'desc' file, which
        identifies what other package(s) a package replaces
    sha256sum: str
        The attribute can be used to describe the (required) data below an %SHA256SUM% identifier in a 'desc' file,
        which identifies a package's sha256 checksum
    url: str
        The attribute can be used to describe the (required) data below a %URL% identifier in a 'desc' file, which
        identifies a package's URL
537
538
    """

539
    pass
540
541


542
class PackageDesc(
543
544
    Arch,
    Backup,
545
    Base,
546
    BuildDate,
David Runge's avatar
David Runge committed
547
    CheckDepends,
548
549
550
551
552
553
554
555
    Conflicts,
    CSize,
    Depends,
    Desc,
    FileName,
    Groups,
    ISize,
    License,
556
    MakeDepends,
557
558
559
    Md5Sum,
    Name,
    OptDepends,
560
    Packager,
561
562
563
564
565
    PgpSig,
    Provides,
    Replaces,
    Sha256Sum,
    Url,
566
    Version,
567
):
David Runge's avatar
David Runge committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
    """A model describing all identifiers in a 'desc' file

    Attributes
    ----------
    arch: str
        The attribute can be used to describe the (required) data below an %ARCH% identifier in a 'desc' file, which
        identifies a package's architecture
    backup: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %BACKUP% identifier in a 'desc' file, which
        identifies which file(s) of a package pacman will create backups for
    base: str
        The attribute can be used to describe the (required) data below a %BASE% identifier in a 'desc' file, which
        identifies a package's pkgbase
    builddate: int
        The attribute can be used to describe the (required) data below a %BUILDDATE% identifier in a 'desc' file,
        which identifies a package's build date (represented in seconds since the epoch)
    checkdepends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %CHECKDEPENDS% identifier in a 'desc' file,
        which identifies a package's checkdepends
    conflicts: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %CONFLICTS% identifier in a 'desc' file, which
        identifies what other package(s) a package conflicts with
    csize: int
        The attribute can be used to describe the (required) data below a %CSIZE% identifier in a 'desc' file, which
        identifies a package's size
    depends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %DEPENDS% identifier in a 'desc' file, which
        identifies what other package(s) a package depends on
    desc: str
        The attribute can be used to describe the (required) data below a %DESC% identifier in a 'desc' file, which
        identifies a package's description
    filename: str
        The attribute can be used to describe the (required) data below a %FILENAME% identifier in a 'desc' file, which
        identifies a package's file name
    groups: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %GROUPS% identifier in a 'desc' file, which
        identifies a package's groups
    isize: int
        The attribute can be used to describe the (required) data below an %ISIZE% identifier in a 'desc' file, which
David Runge's avatar
David Runge committed
607
        identifies a package's installed size
David Runge's avatar
David Runge committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    license: List[str]
        The attribute can be used to describe the (required) data below a %LICENSE% identifier in a 'desc' file, which
        identifies a package's license(s)
    makedepends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %MAKEDEPENDS% identifier in a 'desc' file,
        which identifies a package's makedepends
    md5sum: str
        The attribute can be used to describe the (required) data below an %MD5SUM% identifier in a 'desc' file, which
        identifies a package's md5 checksum
    name: str
        The attribute can be used to describe the (required) data below a %NAME% identifier in a 'desc' file, which
        identifies a package's name
    optdepends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %OPTDEPENDS% identifier in a 'desc' file,
        which identifies what other package(s) a package optionally depends on
    packager: str
        The attribute can be used to describe the (required) data below a %PACKAGER% identifier in a 'desc' file, which
        identifies a package's packager
    pgpsig: str
        The attribute can be used to describe the (required) data below a %PGPSIG% identifier in a 'desc' file, which
        identifies a package's PGP signature
    provides: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %PROVIDES% identifier in a 'desc' file, which
        identifies what other package(s) a package provides
    replaces: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %REPLACES% identifier in a 'desc' file, which
        identifies what other package(s) a package replaces
    sha256sum: str
        The attribute can be used to describe the (required) data below an %SHA256SUM% identifier in a 'desc' file,
        which identifies a package's sha256 checksum
    url: str
        The attribute can be used to describe the (required) data below a %URL% identifier in a 'desc' file, which
        identifies a package's URL
    version: str
        The attribute can be used to describe the (required) data below a %VERSION% identifier in a 'desc' file, which
        identifies a package's version (this is the accumulation of epoch, pkgver and pkgrel)
    """
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671

    def get_output_package(self, files: Optional[Files]) -> OutputPackage:
        """Transform the PackageDesc model and an optional Files model into an OutputPackage model

        Parameters
        ----------
        files: Optional[Files]:
            A pydantic model, that represents the list of files, that belong to the package described by self

        Returns
        -------
        OutputPackage
            A pydantic model, that describes a package and its list of files
        """

        desc_dict = self.dict()
        # remove attributes, that are represented on the pkgbase level
        for name in ["base", "makedepends", "packager", "version"]:
            if desc_dict.get(name):
                del desc_dict[name]

        if files:
            return OutputPackage(**desc_dict, **files.dict())
        else:
            return OutputPackage(**desc_dict)


David Runge's avatar
David Runge committed
672
673
class RepoDbMemberData(Name, RepoDbMemberType):
    """A model describing a set of attributes to provide the data of a 'desc' or 'files' file
674

David Runge's avatar
David Runge committed
675
676
677
678
    Attributes
    ----------
    name: str
        A package name
679
    member_type: defaults.RepoDbMemberType
David Runge's avatar
David Runge committed
680
681
682
683
        A member of the IntEnum defaults.RepoDbMemberType
    data: io.StringIO
        The contents of a 'desc' or 'files' file provided as a StringIO instance
    """
684
685
686
687
688

    data: io.StringIO

    class Config:
        arbitrary_types_allowed = True
689
690
691


class OutputPackageBase(
692
    Base,
693
694
695
696
697
698
    MakeDepends,
    Packager,
    Version,
):
    """A model describing all required attributes for an output file, that describes a list of packages based upon a
    pkgbase
David Runge's avatar
David Runge committed
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715

    Attributes
    ----------
    base: str
        The attribute can be used to describe the (required) data below a %BASE% identifier in a 'desc' file, which
        identifies a package's pkgbase
    makedepends: Optional[List[str]]
        The attribute can be used to describe the (optional) data below a %MAKEDEPENDS% identifier in a 'desc' file,
        which identifies a package's makedepends
    packager: str
        The attribute can be used to describe the (required) data below a %PACKAGER% identifier in a 'desc' file, which
        identifies a package's packager
    packages: List[OutputPackage]
        A list of OutputPackage instances that belong to the pkgbase identified by base
    version: str
        The attribute can be used to describe the (required) data below a %VERSION% identifier in a 'desc' file, which
        identifies a package's version (this is the accumulation of epoch, pkgver and pkgrel)
716
717
718
    """

    packages: List[OutputPackage]
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760

    def get_packages_as_models(self) -> List[Tuple[PackageDesc, Files]]:
        """Return the list of packages as tuples of PackageDesc and Files models

        Returns
        -------
        List[Tuple[PackageDesc, Files]]
            A list of tuples with one PackageDesc and one Files each
        """

        return [
            (
                PackageDesc(
                    arch=package.arch,
                    backup=package.backup,
                    base=self.base,
                    builddate=package.builddate,
                    checkdepends=package.checkdepends,
                    conflicts=package.conflicts,
                    csize=package.csize,
                    depends=package.depends,
                    desc=package.desc,
                    filename=package.filename,
                    groups=package.groups,
                    isize=package.isize,
                    license=package.license,
                    makedepends=self.makedepends,
                    md5sum=package.md5sum,
                    name=package.name,
                    optdepends=package.optdepends,
                    packager=self.packager,
                    pgpsig=package.pgpsig,
                    provides=package.provides,
                    replaces=package.replaces,
                    sha256sum=package.sha256sum,
                    url=package.url,
                    version=self.version,
                ),
                Files(files=package.files),
            )
            for package in self.packages
        ]