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

from pydantic import BaseModel

6
7
from repo_management import defaults

8

David Runge's avatar
David Runge committed
9
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
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]]


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

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

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


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

David Runge's avatar
David Runge committed
51
52
53
54
55
56
    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)
    """
57

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


class CheckDepends(BaseModel):
David Runge's avatar
David Runge committed
62
63
64
65
66
67
68
69
    """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
    """
70
71
72
73

    checkdepends: Optional[List[str]]


David Runge's avatar
David Runge committed
74
75
class Conflicts(BaseModel):
    """A model describing a single 'conflicts' attribute
76

David Runge's avatar
David Runge committed
77
78
79
80
81
82
    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
    """
83

David Runge's avatar
David Runge committed
84
    conflicts: Optional[List[str]]
85
86


David Runge's avatar
David Runge committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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


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]]
111
112
113


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

David Runge's avatar
David Runge committed
116
117
    Attributes
    ----------
118
    desc: str
David Runge's avatar
David Runge committed
119
120
121
        The attribute can be used to describe the (required) data below a %DESC% identifier in a 'desc' file, which
        identifies a package's description
    """
122

David Runge's avatar
David Runge committed
123
    desc: str
124
125


David Runge's avatar
David Runge committed
126
127
class FileName(BaseModel):
    """A model describing a single 'filename' attribute
128

David Runge's avatar
David Runge committed
129
130
131
132
133
134
    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
    """
135

David Runge's avatar
David Runge committed
136
    filename: str
137
138


David Runge's avatar
David Runge committed
139
140
class Files(BaseModel):
    """A model describing a single 'files' attribute
141

David Runge's avatar
David Runge committed
142
143
144
145
146
147
    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
    """
148

David Runge's avatar
David Runge committed
149
    files: Optional[List[str]]
150
151


David Runge's avatar
David Runge committed
152
153
class Groups(BaseModel):
    """A model describing a single 'groups' attribute
154

David Runge's avatar
David Runge committed
155
156
157
158
159
160
    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
    """
161

David Runge's avatar
David Runge committed
162
    groups: Optional[List[str]]
163
164


David Runge's avatar
David Runge committed
165
166
class ISize(BaseModel):
    """A model describing a single 'isize' attribute
167

David Runge's avatar
David Runge committed
168
169
170
171
172
173
    Attributes
    ----------
    isize: int
        The attribute can be used to describe the (required) data below an %ISIZE% identifier in a 'desc' file, which
        identifies a package's size
    """
174

David Runge's avatar
David Runge committed
175
    isize: int
176
177


David Runge's avatar
David Runge committed
178
179
class License(BaseModel):
    """A model describing a single 'license' attribute
180

David Runge's avatar
David Runge committed
181
182
183
184
185
186
    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)
    """
187

David Runge's avatar
David Runge committed
188
    license: List[str]
189
190


David Runge's avatar
David Runge committed
191
192
class MakeDepends(BaseModel):
    """A model describing a single 'makedepends' attribute
193

David Runge's avatar
David Runge committed
194
195
196
197
198
199
    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
    """
200

David Runge's avatar
David Runge committed
201
    makedepends: Optional[List[str]]
202
203


David Runge's avatar
David Runge committed
204
205
class Md5Sum(BaseModel):
    """A model describing a single 'md5sum' attribute
206

David Runge's avatar
David Runge committed
207
208
209
210
211
212
    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
    """
213

David Runge's avatar
David Runge committed
214
    md5sum: str
215

David Runge's avatar
David Runge committed
216
217
218
219
220
221
222
223
224
225
226
227

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
228
229
230


class Packager(BaseModel):
David Runge's avatar
David Runge committed
231
232
233
234
235
236
237
238
    """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
    """
239
240
241
242

    packager: str


David Runge's avatar
David Runge committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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]]


269
class Replaces(BaseModel):
David Runge's avatar
David Runge committed
270
    """A model describing a single 'replaces' attribute
271

David Runge's avatar
David Runge committed
272
273
    Attributes
    ----------
274
    replaces: Optional[List[str]]
David Runge's avatar
David Runge committed
275
276
277
        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
    """
278

David Runge's avatar
David Runge committed
279
    replaces: Optional[List[str]]
280
281


David Runge's avatar
David Runge committed
282
283
284
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).
285

David Runge's avatar
David Runge committed
286
287
288
289
290
    Attributes
    ----------
    member_type: defaults.RepoDbMemberType
        A member of the IntEnum defaults.RepoDbMemberType
    """
291

David Runge's avatar
David Runge committed
292
    member_type: defaults.RepoDbMemberType
293
294


David Runge's avatar
David Runge committed
295
296
class Sha256Sum(BaseModel):
    """A model describing a single 'sha256sum' attribute
297

David Runge's avatar
David Runge committed
298
299
300
301
302
303
    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
    """
304

David Runge's avatar
David Runge committed
305
    sha256sum: str
306
307
308


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

David Runge's avatar
David Runge committed
311
312
    Attributes
    ----------
313
    optdepends: Optional[List[str]]
David Runge's avatar
David Runge committed
314
315
316
        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
    """
317

David Runge's avatar
David Runge committed
318
    optdepends: Optional[List[str]]
319
320


David Runge's avatar
David Runge committed
321
322
class Url(BaseModel):
    """A model describing a single 'url' attribute
323

David Runge's avatar
David Runge committed
324
325
326
327
328
329
    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
    """
330

David Runge's avatar
David Runge committed
331
    url: str
332
333


David Runge's avatar
David Runge committed
334
335
class Version(BaseModel):
    """A model describing a single 'version' attribute
336

David Runge's avatar
David Runge committed
337
338
339
340
341
342
343
344
    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
345
346


347
class OutputPackage(
348
349
350
    Arch,
    Backup,
    BuildDate,
David Runge's avatar
David Runge committed
351
    CheckDepends,
352
353
354
355
356
    Conflicts,
    CSize,
    Depends,
    Desc,
    FileName,
357
    Files,
358
359
360
361
362
363
364
365
366
367
368
369
    Groups,
    ISize,
    License,
    Md5Sum,
    Name,
    OptDepends,
    PgpSig,
    Provides,
    Replaces,
    Sha256Sum,
    Url,
):
David Runge's avatar
David Runge committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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
    """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
        identifies a package's size
    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
437
438
    """

439
    pass
440
441


442
class PackageDesc(
443
444
    Arch,
    Backup,
445
    Base,
446
    BuildDate,
David Runge's avatar
David Runge committed
447
    CheckDepends,
448
449
450
451
452
453
454
455
    Conflicts,
    CSize,
    Depends,
    Desc,
    FileName,
    Groups,
    ISize,
    License,
456
    MakeDepends,
457
458
459
    Md5Sum,
    Name,
    OptDepends,
460
    Packager,
461
462
463
464
465
    PgpSig,
    Provides,
    Replaces,
    Sha256Sum,
    Url,
466
    Version,
467
):
David Runge's avatar
David Runge committed
468
469
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
509
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
537
538
539
540
541
542
543
544
    """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
        identifies a package's size
    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)
    """
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571

    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
572
573
class RepoDbMemberData(Name, RepoDbMemberType):
    """A model describing a set of attributes to provide the data of a 'desc' or 'files' file
574

David Runge's avatar
David Runge committed
575
576
577
578
    Attributes
    ----------
    name: str
        A package name
579
    member_type: defaults.RepoDbMemberType
David Runge's avatar
David Runge committed
580
581
582
583
        A member of the IntEnum defaults.RepoDbMemberType
    data: io.StringIO
        The contents of a 'desc' or 'files' file provided as a StringIO instance
    """
584
585
586
587
588

    data: io.StringIO

    class Config:
        arbitrary_types_allowed = True
589
590
591


class OutputPackageBase(
592
    Base,
593
594
595
596
597
598
    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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615

    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)
616
617
618
    """

    packages: List[OutputPackage]
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660

    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
        ]