forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_koji.py
888 lines (696 loc) · 28.8 KB
/
utils_koji.py
1
2
3
4
5
6
7
8
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
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
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
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
607
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
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
import HTMLParser, ConfigParser, os, logging, urllib
from autotest.client import os_dep, utils
try:
import koji
KOJI_INSTALLED = True
except ImportError:
KOJI_INSTALLED = False
DEFAULT_KOJI_TAG = None
class KojiDirIndexParser(HTMLParser.HTMLParser):
'''
Parser for HTML directory index pages, specialized to look for RPM links
'''
def __init__(self):
'''
Initializes a new KojiDirListParser instance
'''
HTMLParser.HTMLParser.__init__(self)
self.package_file_names = []
def handle_starttag(self, tag, attrs):
'''
Handle tags during the parsing
This just looks for links ('a' tags) for files ending in .rpm
'''
if tag == 'a':
for k, v in attrs:
if k == 'href' and v.endswith('.rpm'):
self.package_file_names.append(v)
class RPMFileNameInfo:
'''
Simple parser for RPM based on information present on the filename itself
'''
def __init__(self, filename):
'''
Initializes a new RpmInfo instance based on a filename
'''
self.filename = filename
def get_filename_without_suffix(self):
'''
Returns the filename without the default RPM suffix
'''
assert self.filename.endswith('.rpm')
return self.filename[0:-4]
def get_filename_without_arch(self):
'''
Returns the filename without the architecture
This also excludes the RPM suffix, that is, removes the leading arch
and RPM suffix.
'''
wo_suffix = self.get_filename_without_suffix()
arch_sep = wo_suffix.rfind('.')
return wo_suffix[:arch_sep]
def get_arch(self):
'''
Returns just the architecture as present on the RPM filename
'''
wo_suffix = self.get_filename_without_suffix()
arch_sep = wo_suffix.rfind('.')
return wo_suffix[arch_sep+1:]
def get_nvr_info(self):
'''
Returns a dictionary with the name, version and release components
If koji is not installed, this returns None
'''
if not KOJI_INSTALLED:
return None
return koji.util.koji.parse_NVR(self.get_filename_without_arch())
class KojiClient(object):
"""
Stablishes a connection with the build system, either koji or brew.
This class provides convenience methods to retrieve information on packages
and the packages themselves hosted on the build system. Packages should be
specified in the KojiPgkSpec syntax.
"""
CMD_LOOKUP_ORDER = ['/usr/bin/brew', '/usr/bin/koji' ]
CONFIG_MAP = {'/usr/bin/brew': '/etc/brewkoji.conf',
'/usr/bin/koji': '/etc/koji.conf'}
def __init__(self, cmd=None):
"""
Verifies whether the system has koji or brew installed, then loads
the configuration file that will be used to download the files.
@type cmd: string
@param cmd: Optional command name, either 'brew' or 'koji'. If not
set, get_default_command() is used and to look for
one of them.
@raise: ValueError
"""
if not KOJI_INSTALLED:
raise ValueError('No koji/brew installed on the machine')
# Instance variables used by many methods
self.command = None
self.config = None
self.config_options = {}
self.session = None
# Set koji command or get default
if cmd is None:
self.command = self.get_default_command()
else:
self.command = cmd
# Check koji command
if not self.is_command_valid():
raise ValueError('Koji command "%s" is not valid' % self.command)
# Assuming command is valid, set configuration file and read it
self.config = self.CONFIG_MAP[self.command]
self.read_config()
# Setup koji session
server_url = self.config_options['server']
session_options = self.get_session_options()
self.session = koji.ClientSession(server_url,
session_options)
def read_config(self, check_is_valid=True):
'''
Reads options from the Koji configuration file
By default it checks if the koji configuration is valid
@type check_valid: boolean
@param check_valid: whether to include a check on the configuration
@raises: ValueError
@returns: None
'''
if check_is_valid:
if not self.is_config_valid():
raise ValueError('Koji config "%s" is not valid' % self.config)
config = ConfigParser.ConfigParser()
config.read(self.config)
basename = os.path.basename(self.command)
for name, value in config.items(basename):
self.config_options[name] = value
def get_session_options(self):
'''
Filter only options necessary for setting up a cobbler client session
@returns: only the options used for session setup
'''
session_options = {}
for name, value in self.config_options.items():
if name in ('user', 'password', 'debug_xmlrpc', 'debug'):
session_options[name] = value
return session_options
def is_command_valid(self):
'''
Checks if the currently set koji command is valid
@returns: True or False
'''
koji_command_ok = True
if not os.path.isfile(self.command):
logging.error('Koji command "%s" is not a regular file',
self.command)
koji_command_ok = False
if not os.access(self.command, os.X_OK):
logging.warn('Koji command "%s" is not executable: this is '
'not fatal but indicates an unexpected situation',
self.command)
if not self.command in self.CONFIG_MAP.keys():
logging.error('Koji command "%s" does not have a configuration '
'file associated to it', self.command)
koji_command_ok = False
return koji_command_ok
def is_config_valid(self):
'''
Checks if the currently set koji configuration is valid
@returns: True or False
'''
koji_config_ok = True
if not os.path.isfile(self.config):
logging.error('Koji config "%s" is not a regular file', self.config)
koji_config_ok = False
if not os.access(self.config, os.R_OK):
logging.error('Koji config "%s" is not readable', self.config)
koji_config_ok = False
config = ConfigParser.ConfigParser()
config.read(self.config)
basename = os.path.basename(self.command)
if not config.has_section(basename):
logging.error('Koji configuration file "%s" does not have a '
'section "%s", named after the base name of the '
'currently set koji command "%s"', self.config,
basename, self.command)
koji_config_ok = False
return koji_config_ok
def get_default_command(self):
'''
Looks up for koji or brew "binaries" on the system
Systems with plain koji usually don't have a brew cmd, while systems
with koji, have *both* koji and brew utilities. So we look for brew
first, and if found, we consider that the system is configured for
brew. If not, we consider this is a system with plain koji.
@returns: either koji or brew command line executable path, or None
'''
koji_command = None
for command in self.CMD_LOOKUP_ORDER:
if os.path.isfile(command):
koji_command = command
break
else:
koji_command_basename = os.path.basename(command)
try:
koji_command = os_dep.command(koji_command_basename)
break
except ValueError:
pass
return koji_command
def get_pkg_info(self, pkg):
'''
Returns information from Koji on the package
@type pkg: KojiPkgSpec
@param pkg: information about the package, as a KojiPkgSpec instance
@returns: information from Koji about the specified package
'''
info = {}
if pkg.build is not None:
info = self.session.getBuild(int(pkg.build))
elif pkg.tag is not None and pkg.package is not None:
builds = self.session.listTagged(pkg.tag,
latest=True,
inherit=True,
package=pkg.package)
if builds:
info = builds[0]
return info
def is_pkg_valid(self, pkg):
'''
Checks if this package is altogether valid on Koji
This verifies if the build or tag specified in the package
specification actually exist on the Koji server
@returns: True or False
'''
valid = True
if pkg.build:
if not self.is_pkg_spec_build_valid(pkg):
valid = False
elif pkg.tag:
if not self.is_pkg_spec_tag_valid(pkg):
valid = False
else:
valid = False
return valid
def is_pkg_spec_build_valid(self, pkg):
'''
Checks if build is valid on Koji
@param pkg: a Pkg instance
'''
if pkg.build is not None:
info = self.session.getBuild(int(pkg.build))
if info:
return True
return False
def is_pkg_spec_tag_valid(self, pkg):
'''
Checks if tag is valid on Koji
@type pkg: KojiPkgSpec
@param pkg: a package specification
'''
if pkg.tag is not None:
tag = self.session.getTag(pkg.tag)
if tag:
return True
return False
def get_pkg_rpm_info(self, pkg, arch=None):
'''
Returns a list of infomation on the RPM packages found on koji
@type pkg: KojiPkgSpec
@param pkg: a package specification
@type arch: string
@param arch: packages built for this architecture, but also including
architecture independent (noarch) packages
'''
if arch is None:
arch = utils.get_arch()
rpms = []
info = self.get_pkg_info(pkg)
if info:
rpms = self.session.listRPMs(buildID=info['id'],
arches=[arch, 'noarch'])
if pkg.subpackages:
rpms = [d for d in rpms if d['name'] in pkg.subpackages]
return rpms
def get_pkg_rpm_names(self, pkg, arch=None):
'''
Gets the names for the RPM packages specified in pkg
@type pkg: KojiPkgSpec
@param pkg: a package specification
@type arch: string
@param arch: packages built for this architecture, but also including
architecture independent (noarch) packages
'''
if arch is None:
arch = utils.get_arch()
rpms = self.get_pkg_rpm_info(pkg, arch)
return [rpm['name'] for rpm in rpms]
def get_pkg_rpm_file_names(self, pkg, arch=None):
'''
Gets the file names for the RPM packages specified in pkg
@type pkg: KojiPkgSpec
@param pkg: a package specification
@type arch: string
@param arch: packages built for this architecture, but also including
architecture independent (noarch) packages
'''
if arch is None:
arch = utils.get_arch()
rpm_names = []
rpms = self.get_pkg_rpm_info(pkg, arch)
for rpm in rpms:
arch_rpm_name = koji.pathinfo.rpm(rpm)
rpm_name = os.path.basename(arch_rpm_name)
rpm_names.append(rpm_name)
return rpm_names
def get_pkg_base_url(self):
'''
Gets the base url for packages in Koji
'''
if self.config_options.has_key('pkgurl'):
return self.config_options['pkgurl']
else:
return "%s/%s" % (self.config_options['topurl'],
'packages')
def get_scratch_base_url(self):
'''
Gets the base url for scratch builds in Koji
'''
one_level_up = os.path.dirname(self.get_pkg_base_url())
return "%s/%s" % (one_level_up, 'scratch')
def get_pkg_urls(self, pkg, arch=None):
'''
Gets the urls for the packages specified in pkg
@type pkg: KojiPkgSpec
@param pkg: a package specification
@type arch: string
@param arch: packages built for this architecture, but also including
architecture independent (noarch) packages
'''
info = self.get_pkg_info(pkg)
rpms = self.get_pkg_rpm_info(pkg, arch)
rpm_urls = []
base_url = self.get_pkg_base_url()
for rpm in rpms:
rpm_name = koji.pathinfo.rpm(rpm)
url = ("%s/%s/%s/%s/%s" % (base_url,
info['package_name'],
info['version'], info['release'],
rpm_name))
rpm_urls.append(url)
return rpm_urls
def get_pkgs(self, pkg, dst_dir, arch=None):
'''
Download the packages
@type pkg: KojiPkgSpec
@param pkg: a package specification
@type dst_dir: string
@param dst_dir: the destination directory, where the downloaded
packages will be saved on
@type arch: string
@param arch: packages built for this architecture, but also including
architecture independent (noarch) packages
'''
rpm_urls = self.get_pkg_urls(pkg, arch)
for url in rpm_urls:
utils.get_file(url,
os.path.join(dst_dir, os.path.basename(url)))
def get_scratch_pkg_urls(self, pkg, arch=None):
'''
Gets the urls for the scratch packages specified in pkg
@type pkg: KojiScratchPkgSpec
@param pkg: a scratch package specification
@type arch: string
@param arch: packages built for this architecture, but also including
architecture independent (noarch) packages
'''
rpm_urls = []
if arch is None:
arch = utils.get_arch()
arches = [arch, 'noarch']
index_url = "%s/%s/task_%s" % (self.get_scratch_base_url(),
pkg.user,
pkg.task)
index_parser = KojiDirIndexParser()
index_parser.feed(urllib.urlopen(index_url).read())
if pkg.subpackages:
for p in pkg.subpackages:
for pfn in index_parser.package_file_names:
r = RPMFileNameInfo(pfn)
info = r.get_nvr_info()
if (p == info['name'] and
r.get_arch() in arches):
rpm_urls.append("%s/%s" % (index_url, pfn))
else:
for pfn in index_parser.package_file_names:
if (RPMFileNameInfo(pfn).get_arch() in arches):
rpm_urls.append("%s/%s" % (index_url, pfn))
return rpm_urls
def get_scratch_pkgs(self, pkg, dst_dir, arch=None):
'''
Download the packages from a scratch build
@type pkg: KojiScratchPkgSpec
@param pkg: a scratch package specification
@type dst_dir: string
@param dst_dir: the destination directory, where the downloaded
packages will be saved on
@type arch: string
@param arch: packages built for this architecture, but also including
architecture independent (noarch) packages
'''
rpm_urls = self.get_scratch_pkg_urls(pkg, arch)
for url in rpm_urls:
utils.get_file(url,
os.path.join(dst_dir, os.path.basename(url)))
def set_default_koji_tag(tag):
'''
Sets the default tag that will be used
'''
global DEFAULT_KOJI_TAG
DEFAULT_KOJI_TAG = tag
def get_default_koji_tag():
return DEFAULT_KOJI_TAG
class KojiPkgSpec(object):
'''
A package specification syntax parser for Koji
This holds information on either tag or build, and packages to be fetched
from koji and possibly installed (features external do this class).
New objects can be created either by providing information in the textual
format or by using the actual parameters for tag, build, package and sub-
packages. The textual format is useful for command line interfaces and
configuration files, while using parameters is better for using this in
a programatic fashion.
The following sets of examples are interchangeable. Specifying all packages
part of build number 1000:
>>> from kvm_utils import KojiPkgSpec
>>> pkg = KojiPkgSpec('1000')
>>> pkg = KojiPkgSpec(build=1000)
Specifying only a subset of packages of build number 1000:
>>> pkg = KojiPkgSpec('1000:kernel,kernel-devel')
>>> pkg = KojiPkgSpec(build=1000,
subpackages=['kernel', 'kernel-devel'])
Specifying the latest build for the 'kernel' package tagged with 'dist-f14':
>>> pkg = KojiPkgSpec('dist-f14:kernel')
>>> pkg = KojiPkgSpec(tag='dist-f14', package='kernel')
Specifying the 'kernel' package using the default tag:
>>> kvm_utils.set_default_koji_tag('dist-f14')
>>> pkg = KojiPkgSpec('kernel')
>>> pkg = KojiPkgSpec(package='kernel')
Specifying the 'kernel' package using the default tag:
>>> kvm_utils.set_default_koji_tag('dist-f14')
>>> pkg = KojiPkgSpec('kernel')
>>> pkg = KojiPkgSpec(package='kernel')
If you do not specify a default tag, and give a package name without an
explicit tag, your package specification is considered invalid:
>>> print kvm_utils.get_default_koji_tag()
None
>>> print kvm_utils.KojiPkgSpec('kernel').is_valid()
False
>>> print kvm_utils.KojiPkgSpec(package='kernel').is_valid()
False
'''
SEP = ':'
def __init__(self, text='', tag=None, build=None,
package=None, subpackages=[]):
'''
Instantiates a new KojiPkgSpec object
@type text: string
@param text: a textual representation of a package on Koji that
will be parsed
@type tag: string
@param tag: a koji tag, example: Fedora-14-RELEASE
(see U{http://fedoraproject.org/wiki/Koji#Tags_and_Targets})
@type build: number
@param build: a koji build, example: 1001
(see U{http://fedoraproject.org/wiki/Koji#Koji_Architecture})
@type package: string
@param package: a koji package, example: python
(see U{http://fedoraproject.org/wiki/Koji#Koji_Architecture})
@type subpackages: list of strings
@param subpackages: a list of package names, usually a subset of
the RPM packages generated by a given build
'''
# Set to None to indicate 'not set' (and be able to use 'is')
self.tag = None
self.build = None
self.package = None
self.subpackages = []
self.default_tag = None
# Textual representation takes precedence (most common use case)
if text:
self.parse(text)
else:
self.tag = tag
self.build = build
self.package = package
self.subpackages = subpackages
# Set the default tag, if set, as a fallback
if not self.build and not self.tag:
default_tag = get_default_koji_tag()
if default_tag is not None:
self.tag = default_tag
def parse(self, text):
'''
Parses a textual representation of a package specification
@type text: string
@param text: textual representation of a package in koji
'''
parts = text.count(self.SEP) + 1
if parts == 1:
if text.isdigit():
self.build = text
else:
self.package = text
elif parts == 2:
part1, part2 = text.split(self.SEP)
if part1.isdigit():
self.build = part1
self.subpackages = part2.split(',')
else:
self.tag = part1
self.package = part2
elif parts >= 3:
# Instead of erroring on more arguments, we simply ignore them
# This makes the parser suitable for future syntax additions, such
# as specifying the package architecture
part1, part2, part3 = text.split(self.SEP)[0:3]
self.tag = part1
self.package = part2
self.subpackages = part3.split(',')
def _is_invalid_neither_tag_or_build(self):
'''
Checks if this package is invalid due to not having either a valid
tag or build set, that is, both are empty.
@returns: True if this is invalid and False if it's valid
'''
return (self.tag is None and self.build is None)
def _is_invalid_package_but_no_tag(self):
'''
Checks if this package is invalid due to having a package name set
but tag or build set, that is, both are empty.
@returns: True if this is invalid and False if it's valid
'''
return (self.package and not self.tag)
def _is_invalid_subpackages_but_no_main_package(self):
'''
Checks if this package is invalid due to having a tag set (this is Ok)
but specifying subpackage names without specifying the main package
name.
Specifying subpackages without a main package name is only valid when
a build is used instead of a tag.
@returns: True if this is invalid and False if it's valid
'''
return (self.tag and self.subpackages and not self.package)
def is_valid(self):
'''
Checks if this package specification is valid.
Being valid means that it has enough and not conflicting information.
It does not validate that the packages specified actually existe on
the Koji server.
@returns: True or False
'''
if self._is_invalid_neither_tag_or_build():
return False
elif self._is_invalid_package_but_no_tag():
return False
elif self._is_invalid_subpackages_but_no_main_package():
return False
return True
def describe_invalid(self):
'''
Describes why this is not valid, in a human friendly way
'''
if self._is_invalid_neither_tag_or_build():
return ('neither a tag nor a build were set, one of them '
'must be set')
elif self._is_invalid_package_but_no_tag():
return 'package name specified but no tag is set'
elif self._is_invalid_subpackages_but_no_main_package():
return 'subpackages specified but no main package is set'
return 'unkwown reason, seems to be valid'
def describe(self):
'''
Describe this package specification, in a human friendly way
@returns: package specification description
'''
if self.is_valid():
description = ''
if not self.subpackages:
description += 'all subpackages from %s ' % self.package
else:
description += ('only subpackage(s) %s from package %s ' %
(', '.join(self.subpackages), self.package))
if self.build:
description += 'from build %s' % self.build
elif self.tag:
description += 'tagged with %s' % self.tag
else:
raise ValueError, 'neither build or tag is set'
return description
else:
return ('Invalid package specification: %s' %
self.describe_invalid())
def to_text(self):
'''
Return the textual representation of this package spec
The output should be consumable by parse() and produce the same
package specification.
We find that it's acceptable to put the currently set default tag
as the package explicit tag in the textual definition for completeness.
@returns: package specification in a textual representation
'''
default_tag = get_default_koji_tag()
if self.build:
if self.subpackages:
return "%s:%s" % (self.build, ",".join(self.subpackages))
else:
return "%s" % self.build
elif self.tag:
if self.subpackages:
return "%s:%s:%s" % (self.tag, self.package,
",".join(self.subpackages))
else:
return "%s:%s" % (self.tag, self.package)
elif default_tag is not None:
# neither build or tag is set, try default_tag as a fallback
if self.subpackages:
return "%s:%s:%s" % (default_tag, self.package,
",".join(self.subpackages))
else:
return "%s:%s" % (default_tag, self.package)
else:
raise ValueError, 'neither build or tag is set'
def __repr__(self):
return ("<KojiPkgSpec tag=%s build=%s pkg=%s subpkgs=%s>" %
(self.tag, self.build, self.package,
", ".join(self.subpackages)))
class KojiScratchPkgSpec(object):
'''
A package specification syntax parser for Koji scratch builds
This holds information on user, task and subpackages to be fetched
from koji and possibly installed (features external do this class).
New objects can be created either by providing information in the textual
format or by using the actual parameters for user, task and subpackages.
The textual format is useful for command line interfaces and configuration
files, while using parameters is better for using this in a programatic
fashion.
This package definition has a special behaviour: if no subpackages are
specified, all packages of the chosen architecture (plus noarch packages)
will match.
The following sets of examples are interchangeable. Specifying all packages
from a scratch build (whose task id is 1000) sent by user jdoe:
>>> from kvm_utils import KojiScratchPkgSpec
>>> pkg = KojiScratchPkgSpec('jdoe:1000')
>>> pkg = KojiScratchPkgSpec(user=jdoe, task=1000)
Specifying some packages from a scratch build whose task id is 1000, sent
by user jdoe:
>>> pkg = KojiScratchPkgSpec('jdoe:1000:kernel,kernel-devel')
>>> pkg = KojiScratchPkgSpec(user=jdoe, task=1000,
subpackages=['kernel', 'kernel-devel'])
'''
SEP = ':'
def __init__(self, text='', user=None, task=None, subpackages=[]):
'''
Instantiates a new KojiScratchPkgSpec object
@type text: string
@param text: a textual representation of a scratch build on Koji that
will be parsed
@type task: number
@param task: a koji task id, example: 1001
@type subpackages: list of strings
@param subpackages: a list of package names, usually a subset of
the RPM packages generated by a given build
'''
# Set to None to indicate 'not set' (and be able to use 'is')
self.user = None
self.task = None
self.subpackages = []
# Textual representation takes precedence (most common use case)
if text:
self.parse(text)
else:
self.user = user
self.task = task
self.subpackages = subpackages
def parse(self, text):
'''
Parses a textual representation of a package specification
@type text: string
@param text: textual representation of a package in koji
'''
parts = text.count(self.SEP) + 1
if parts == 1:
raise ValueError('KojiScratchPkgSpec requires a user and task id')
elif parts == 2:
self.user, self.task = text.split(self.SEP)
elif parts >= 3:
# Instead of erroring on more arguments, we simply ignore them
# This makes the parser suitable for future syntax additions, such
# as specifying the package architecture
part1, part2, part3 = text.split(self.SEP)[0:3]
self.user = part1
self.task = part2
self.subpackages = part3.split(',')
def __repr__(self):
return ("<KojiScratchPkgSpec user=%s task=%s subpkgs=%s>" %
(self.user, self.task, ", ".join(self.subpackages)))