-
Notifications
You must be signed in to change notification settings - Fork 29
/
nreqs.py
executable file
·1250 lines (1106 loc) · 47.5 KB
/
nreqs.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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
#
# Copyright (c) 2021 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
# FIXME/PENDING:
#
# - support version requirement in pip
#
# - support version top level field
#
# - support simple operators for distro version comparision (fedora:<29)
#
# - support multiple matches in top level
# distro|METHODNAME|DISTRO[:[OPERATOR?]VERSION]: eg:
# fedora:<29|centos:<7|rhel:<7
#
# - redact passwords in URLs when logging with something like
# https://relaxdiego.com/2014/07/logging-in-python.html (for all
# $PASSWORD or so things, redact them)
#
# - support setting default method data? eg to set PIP URLs
"""
Handle package requirements covering multiple systems and OSes
Given a top level list of packages needed, this script tries to
install them using a native OS framework first (eg: DNF, APT) and then
falling back to other things like 'pip --user'.
No need to call as superuser! Will try to sudo by default
"""
__epilog__ = """\
Allows covering cases such when a PIP package (eg: paramiko) in RHEL
is called python3-paramiko but in Fedora 29 is called python-paramiko.
The list is specified as one or more multiple yaml files (callde
NAME.nreqs.yaml) which be as simple as::
package1:
package2:
package3:
Running *install* on this will try to install *package1*, *package2*
and *package3* with the system's package manager (eg: DNF or APT) and
fallback to other methods, such as *pip* when that doesn't work.
If some package needs a rename for distro packages, you can do::
package2: python3-package2
which is the same as::
package2:
distro:
name: python3-package2
(*distro* here meaning, in which ever distribution this is being
installed in, when using the package manager, call it
python3-package2)
If you need specific renames for a given distro::
package2:
fedora:29:
name: python-package2
distro:
name: python3-package2
This would, on Fedora 29, try to install *python-package2* before
falling back to pip as *package2*; on other distros, it would try to
install *python3-package2* before falling back to pip.
You can also force names for specific methods:
package2:
dnf:
name: python3-package2
apt:
name: python-package2
Hackish yaml schema description::
REQUIREMENTNAME:
REQUIREMENTNAME: DISTROPACKAGENAME
REQUIREMENTNAME:
description|reason: "DESCRIPTION" # O: needed for this and that
license: SPDX-LICENSE-HEADER # O: licensing info
distro|METHODNAME|DISTRO[:[OPERATOR?]VERSION]:
name: "NAME" # O: package name in disto (defaults to REQUIREMENT)
exclusive: true|false # O: install exclusively with this method
method: "NAME" # O: use a given method (data,dnf, apt, pip) instead of guessing based on distro
license: "SPDX-LICENSE-HEADER" # O: overrides top
description|reason: "DESCRIPTION" # O: overrides top
pip: # plus these ones for pop
version: "[OPERATOR]VERSION" # O: version requirement (eg: for pip)
index: "URL" # O: URL of file to download, forces method to url
indexes:
- "URL" # O: URL of file to download, forces method to url
- "URL" # O: URL of file to download, forces method to url
- "URL" # O: URL of file to download, forces method to url
skip: true|false # O: skip in this distro (default: false)
skip_platform: linux
skip_platform:
- macos
- win32s # platform names - win32, linux, macos
require_platform: linux
require_platform:
- macos
- win32 # platform names - win32, linux, macos
Platforms: win32, macos, linux, ... (as python's sys.platform)
METHODNAMES: dnf, apt, data, pip (more if other drivers have been instantiated)
DISTRO: fedora, rhel, centos, ubuntu, debian (other drivers can create more)
URLs: can contain $NAME or ${NAME} to get from the environment (for
password/usernames)
"""
import argparse
import collections
import ctypes
import hashlib
import json
import logging
import os
import platform
import pprint
import re
import subprocess
import sys
# We want to minimize the amount of dependencies for this script, so
# avoid adding any additional packages unless absolutely necessary
import yaml
class method_abc:
description = None
# this is a class variable
distro_to_method = dict()
# eg: pip, cpan, etc
generic_methods = dict()
methods_by_name = collections.defaultdict(set)
# append space, so we compose a command...bleh
sudo = "sudo "
# dictionary with a spec of things that need to be installed
# first--this is the same yaml format, but in dict. See
# method_pip_c for an example
prerequisites = None
def __init__(self, name, distros = None, exclusive = False):
"""
:param bool exclusive: (optional) fallback setting to decide
if a package that declares this method exclusively with it
instead of trying others first.
"""
assert isinstance(name, str)
assert isinstance(exclusive, bool)
self.name = name
self.exclusive = exclusive
self.origin = "FIXME"
if name in self.methods_by_name:
raise RuntimeError(
f"{name}: method already registered by"
f" {self.methods_by_name[name].origin}")
self.methods_by_name[name] = self
if distros == None:
method_abc.generic_methods[self.name] = self
else:
for distro in distros:
# all versions
# FIXME support centos >= version -- so that centos:7 does
# yum, centos>=8 does DNF
method_abc.distro_to_method[distro] = self
@classmethod
def get_method_for_distro(cls):
global distro
global distro_version
for _distro, _method in cls.distro_to_method.items():
if _distro == distro:
return _method.name
raise RuntimeError(
f"there is no known distro install method for distro {distro}")
def install(self, package, package_alternate, package_data, method_details):
# return
# None: didn't install, not supported
# True: installed
# False (or anything else): didn't install, try something else
#
# raise exception for a "stop right now" problem
raise NotImplementedError
# Handy methods for drivers
@staticmethod
def is_admin():
"""
Return *True* if current users is a sysadmin, *False* otherwise
"""
if hasattr(os, "geteuid"): # most Unix platforms
return os.geteuid() == 0
if hasattr(ctypes, "windll"): # windowsy
return ctypes.windll.shell32.IsUserAnAdmin()
logging.warning(
"non-Unix nor windosy platform? [{sys.platform},"
" can't tell if running user is a root/admin;"
" assuming not")
return False
class method_apt_c(method_abc):
# FIXME: how to add extra repos?
description = "Install packages with APT"
def __init__(self):
method_abc.__init__(self, "apt", [ 'debian', 'ubuntu' ])
def install(self, package, package_alternate, package_data, method_details):
apt_command = os.environ.get("APT_COMMAND", "apt-get")
cmdline = f"{self.sudo}{apt_command} install -y".split()
# Force default locale, so we can parse messages
os.environ["LC_ALL"] = "C"
os.environ["LC_LANG"] = "C"
try:
output = subprocess.check_output(
cmdline + [ package_alternate ],
stdin = subprocess.DEVNULL, stderr = subprocess.STDOUT)
output = output.decode('utf-8', errors = 'backslashreplace').replace('\n', '\n ')
logging.info(
f"{package} [apt/{package_alternate}]: installed\n {output}")
return True
except subprocess.CalledProcessError as e:
output = e.stdout.decode('utf-8', errors = 'backslashreplace')
if "are you root?" in output:
# can't install, try something else -- it is possible
# the user doesn't have privs, but can then fallback
# to pip --user, for example
logging.error(
f"{package} [apt/{package_alternate}]: no permission to"
f" install (need superuser, add --sudo?)")
return False
# APT prints one message, microapt another
if "Unable to locate package" in output:
# package does not exist, try something else
logging.error(
f"{package} [apt/{package_alternate}]: not available from APT (missing repo?)")
return False
# welp, what's this? no idea
logging.error(
f"{package} [apt/{package_alternate}]: command failed '{cmdline}':"
f" {output}")
raise RuntimeError(
f"{package} [apt/{package_alternate}]: command failed '{cmdline}':"
f" {output}")
class method_dnf_c(method_abc):
# FIXME: how to add extra repos?
description = "Install packages with DNF"
def __init__(self):
method_abc.__init__(self, "dnf", [ 'fedora', 'centos', 'rhel' ])
def install(self, package, package_alternate, package_data, method_details):
dnf_command = os.environ.get("DNF_COMMAND", "dnf")
cmdline = f"{self.sudo}{dnf_command} install -y".split()
# Force default locale, so we can parse messages
os.environ["LC_ALL"] = "C"
os.environ["LC_LANG"] = "C"
try:
output = subprocess.check_output(
cmdline + [ package_alternate ],
stdin = subprocess.DEVNULL, stderr = subprocess.STDOUT)
output = output.decode('utf-8', errors = 'backslashreplace').replace('\n', '\n ')
logging.info(
f"{package} [dnf/{package_alternate}]: installed\n {output}")
return True
except subprocess.CalledProcessError as e:
output = e.stdout.decode('utf-8', errors = 'backslashreplace')
if "This command has to be run with superuser privileges" in output:
# can't install, try something else -- it is possible
# the user doesn't have privs, but can then fallback
# to pip --user, for example
logging.error(
f"{package} [dnf/{package_alternate}]: no permission to"
f" install (need superuser, add --sudo?)")
return False
# DNF prints one message, microdnf another
if "Error: Unable to find a match" in output \
or "error: No package matches" in output:
# package does not exist, try something else
logging.error(
f"{package} [dnf/{package_alternate}]: not available from DNF (missing repo?)")
return False
# welp, what's this? no idea
logging.error(
f"{package} [dnf/{package_alternate}]: command failed '{cmdline}':"
f" {output}")
raise RuntimeError(
f"{package} [dnf/{package_alternate}]: command failed '{cmdline}':"
f" {output}")
class method_windows_c(method_abc):
# FIXME: Windows doesn't have packages like Linux, so we just skip
# to fall back to pip
description = "(does not) install packages with Windows" \
" (non existing) package manager"
def __init__(self):
method_abc.__init__(self, "windows", [ 'windows' ])
def install(self, package, package_alternate, package_data, method_details):
logging.info(
f"{package} [windows/{package_alternate}]: not installing"
f" as windows package because Windows can't")
return None
class method_pip_c(method_abc):
description = "Install packages with Python's PIP"
prerequisites = {
"pip": {
"distro": {
"name": "python3-pip",
"exclusive": True
}
}
}
def __init__(self):
method_abc.__init__(self, "pip") # empty means "generic"
def install(self, package, package_alternate, package_data, method_details):
# sys.executable is the current python interpreter; this way
# if we run with different versions we get it installed for
# that version
# FIXME: Issue with retrieving valid executable path when running
# in certian container environments (eg. kaniko) using the default
# method sys.executable. Currently using fallback methods depending
# on platform: os.readlink on linux and the python launcher on windows
if sys.executable:
executable = sys.executable
elif sys.platform == 'linux':
executable = os.readlink("/proc/self/exe")
elif sys.platform == 'win32':
executable = f'py -${sys.version_info[0]}.${sys.version_info[1]}'
else:
raise RuntimeError("Failed to determine current python executable")
cmdline = [ executable, "-m", "pip", "install" ]
admin = self.is_admin()
# To deps or --no-deps, this is the question.
# We can't tell ahead of time if we are able to install
# dependencies as distro packages (which we prefer), and we
# can't force the installation order (since the info could
# come from multiple files). If we did --no-deps, it'd be
# clustermess on packages we know are only available on
# pip/whichever, which would require the user to unravel the
# depednenciy rathole.
#
# FIXME: an approach might be to do some sort of ordering in
# which we install in this order:
#
# - general packages
# - packages which are exclusive to some method (without
# --no-deps)
#
# this balances the need, since the user can ensure the main
# distro packages are installed before doing a pip something
# that has distro-dependencies. Might not solve all, but gets
# close.
if not admin:
if os.getenv('VIRTUAL_ENV') != None:
logging.info("pip not using --user due to venv")
else:
cmdline.append("--user")
# Check which options are available in python pip install --
# note this is not supposed to fail--we only look at the output
p = subprocess.run(cmdline + [ "--help" ],
capture_output = True, text = True, check = False)
if '--break-system-packages' in p.stdout:
# add --break-system-packages to newer Pyhton installs,
# because even with packages that are not available as
# system packages we get this and is the only way I've
# found we can install
cmdline.append("--break-system-packages")
_indexes = []
_index = method_details.get('index', None)
if _index != None:
_indexes.append(_index)
# FIXME: verify this is a list of valid URLs
_indexes += method_details.get('indexes', [])
indexes = []
first_index = True
for index in _indexes:
if not isinstance(index, str):
raise ValueError(
f"{package}/pip: invalid indexes entry; expect an index URL or a list of them\n")
# expand $VAR, ${VAR}
index_url_expanded = os.path.expandvars(index)
# FIXME: validate is a valid URL
# FIXME: check if a password is being given, add it to the
# redact list
if first_index:
cmdline += [ "--index-url", index_url_expanded ]
first_index = False
else:
cmdline += [ "--extra-index-url", index_url_expanded ]
# Force default locale, so we can parse messages
os.environ["LC_ALL"] = "C"
os.environ["LC_LANG"] = "C"
try:
output = subprocess.check_output(
cmdline + [ package_alternate ],
stdin = subprocess.DEVNULL, stderr = subprocess.STDOUT)
output = output.decode('utf-8', errors = 'backslashreplace').replace('\n', '\n ')
logging.info(
f"{package} [pip/{package_alternate}]: installed\n {output}")
return True
except subprocess.CalledProcessError as e:
output = e.stdout.decode('utf-8', errors = 'backslashreplace')
if "ERROR: No matching distribution found for " in output:
# package does not exist, try something else
logging.error(
f"{package} [pip/{package_alternate}]: not available from PIP (missing repo?)")
logging.info(
f"{package} [pip/{package_alternate}]: output: {output}")
return False
# welp, what's this? no idea
logging.error(
f"{package} [pip/{package_alternate}]: command failed '{cmdline}':"
f" {output}")
raise RuntimeError(
f"{package} [pip/{package_alternate}]: command failed '{cmdline}':"
f" {output}")
class method_data_c(method_abc):
description = "Download data files"
def __init__(self):
method_abc.__init__(self, "data", exclusive = True)
def install(self, package, package_alternate, package_data, method_details):
url = method_details.get(
'URL', method_details.get('name', None))
destination = method_details.get('destination', None)
if url == None:
logging.error(
f"{package} [data/{package_alternate}]: skipping;"
" no URL in method data")
return False
# --location: follow 301 redirects
cmdline = f"curl --location".split()
if destination:
cmdline += [ '--output', destination ]
else:
cmdline += [ '--remote-name' ]
# Force default locale, so we can parse messages
os.environ["LC_ALL"] = "C"
os.environ["LC_LANG"] = "C"
try:
output = subprocess.check_output(
cmdline + [ url ],
stdin = subprocess.DEVNULL, stderr = subprocess.STDOUT)
output = output.decode('utf-8', errors = 'backslashreplace').replace('\n', '\n ')
logging.info(
f"{package} [data/{package_alternate}]: installed\n {output}")
return True
except subprocess.CalledProcessError as e:
output = e.stdout.decode('utf-8', errors = 'backslashreplace')
# welp, what's this? no idea
logging.error(
f"{package} [pip/{package_alternate}]: command failed '{cmdline}':"
f" {output}")
raise RuntimeError(
f"{package} [pip/{package_alternate}]: command failed '{cmdline}':"
f" {output}")
class method_default_c(method_abc):
description = "FIXME default distro"
def __init__(self):
method_abc.__init__(self, [ "default" ])
def _distro_version_get(distro, version):
data = {}
if distro:
data['ID'] = distro
if version:
data['VERSION_ID'] = version
# pull in the rest of the data -- Linux only, for now
if sys.platform == "linux":
with open("/etc/os-release") as f:
for line in f:
line = line.strip()
if not '=' in line:
continue
key, val = line.split('=', 1)
# some distros put values with quotes at beg/end...some don't,
# in anycase, we don't need'em
val = val.strip('"')
data.setdefault(key, val)
logging.log(8, f"/etc/os-release: sets {key}={val}")
elif sys.platform == 'win32':
data['ID'] = "windows"
data['VERSION_ID'] = platform.release()
else:
raise RuntimeError(
f"Can't figure out distro or version for"
f" platform {sys.platform}")
return data['ID'], data['VERSION_ID']
def _distro_match(req, req_distro, req_distro_version):
if req_distro != distro:
logging.debug(f"{req}: {req_distro}:"
f" ignored because req distro {req_distro} "
f" doesn't match target system's distro {distro}")
return False
if req_distro_version != None and req_distro_version != distro_version:
logging.info(f"{req}: {req_distro}:{req_distro_version}:"
f" ignored because req distro version {req_distro_version} "
f" doesn't match target system version {distro_version}")
return False
return True
def _skip_platform_check(req, req_distro_spec, skips):
# this is a list of distros to ignore
## skip: SOMEDISTRO
## skip:
## - DISTRO1
## - DISTRO2
## - DISTRO3[:VERSION]
if isinstance(skips, str):
skips = [ skips ]
else:
assert isinstance(skips, (list, set)), \
f"ERROR: {req}/{req_distro_spec}: expected list of distros to exclude;" \
f" got {type(skips)}: {skips}"
assert all(isinstance(i, str) for i in skips)
# FIXME: add :VERSION support
r = sys.platform in skips
if r:
logging.warning(
f"{req}: skipping because current platform"
f" '{sys.platform}' matches 'skip_platform'"
f" list: {' '.join(skips)}")
return r
def _reqs_grok(packages, req_method_details, filename, y):
global distro
global distro_version
global distro_method
logging.debug(f"{filename}: parsing {json.dumps(y, indent = 4, skipkeys = True)}")
for req, req_data in y.items():
logging.debug(f"new requirement: {req}, data {req_data}")
skip_platform = False
require_platform = False
skip_distro = False
if req_data == None:
# this is the most simple of the requirement specifications
#
## PACKAGNAME:
#
# is shorthand for:
#
# "I just need this package with the default
# distro/version system or whatever can get it installed"
#
# try dnf/apt, then pip, then this, then that
logging.debug(f"{req}: associating to distro '{distro}' (empty reference)")
req_data = { distro: {} }
elif isinstance(req_data, str):
# Translate shorthand
#
## PACKAGENAME: DISTROPACKAGENAME
#
# To:
#
## PACKAGENAME:
## distro:
## name: DISTROPACKAGENAME
req_data = {
"distro": {
"name": req_data
}
}
req_alternate = None
# need at least a basic definition
method_name = None
method_data = {}
description = None
spdx_license = None
for req_distro_spec, req_distro_data in req_data.items():
logging.log(9, f"{req}/{req_distro_spec}:"
f" req_distro_data {req_distro_data}")
if req_distro_spec == "skip_platform":
# the "skip" distro is just to tell us in which
# platforms we do not want to install
if _skip_platform_check(req, req_distro_spec, req_distro_data):
# FIXME: wipe from reqs and stop further
# processing -- or do not commit the info to req
skip_platform = True
logging.info(f"{req}/{req_distro_spec}: will skip due to platform check")
break
# no need to keep processing, since this req_distro
# does not specify a distro
continue
if req_distro_spec == "require_platform":
if not _skip_platform_check(req, req_distro_spec, req_distro_data):
# FIXME: wipe from reqs and stop further
# processing -- or do not commit the info to req
skip_platform = True
logging.info(
f"{req}/{req_distro_spec}:"
" will skip, not meeting required platform check")
break
# no need to keep processing, since this req_distro
# does not specify a distro
continue
if req_distro_spec == "license":
spdx_license = req_distro_data
logging.debug(f"{req}: top-level setting license to: {spdx_license}")
continue
if req_distro_spec in ( "description", "reason" ):
logging.debug(f"{req}: top-level setting description to: {description}")
description = req_distro_data
continue
# This might be a distro/package method specification
# Split distro|DISTRONAME[:VERSION]|METHODNAME
if req_distro_spec == "distro":
req_distro, req_distro_version = distro, distro_version
logging.debug(f"{req}/{req_distro_spec}:"
f" setting to distro version {req_distro_version}")
elif ':' in req_distro_spec:
# DISTRONAME[:VERSION]
req_distro, req_distro_version = req_distro_spec.split(":", 1)
logging.debug(f"{req}/{req_distro_spec}:"
f" applies to distro version {req_distro_version}")
else:
req_distro = req_distro_spec
logging.debug(f"{req}/{req_distro_spec}:"
" applies to all distro versions")
req_distro_version = None
if req_distro not in method_abc.generic_methods \
and req_distro not in method_abc.distro_to_method \
and req_distro not in method_abc.methods_by_name:
logging.error(
f"{req}: unknown distro or method"
f" '{req_distro}'; expected:"
f" {', '.join(method_abc.generic_methods.keys())}"
f" {', '.join(method_abc.distro_to_method.keys())}")
## PACKAGE:
## distro|DISTRO[:VERSION]|METHOD: NAME
#
# is shorthand for:
#
## PACKAGE:
## distro:
## name: NAME
if isinstance(req_distro_data, str):
method_data = { "name": req_distro_data }
elif req_distro_data == None:
method_data = {}
else:
assert isinstance(req_distro_data, dict), \
f"req_distro_data expected dict, got {type(req_distro_data)}"
method_data = dict(req_distro_data)
logging.debug(f"{req}/{req_distro_spec}: method_data {method_data}")
if req_distro in method_abc.generic_methods:
method_name = req_distro
logging.info(f"{req}: RECORD generic method '{req_distro}'"
f" -> {method_data}")
# if this "distro" is naming a generic method, just
# record it
if method_data:
# if we have some specific method data, record it,
# otherwise all defaults
req_method_details[req_distro][req] = method_data
# this is a generic method
continue
if req_distro_spec in method_abc.methods_by_name:
# This is a method name:
## PACKAGE:
## METHODNAME:
## ...
method_name = req_distro
elif req_distro in method_abc.distro_to_method.keys():
# This is a distro match name:
## PACKAGE:
## DISTRO[:VERSION]:
## ...
# valid distro
if not _distro_match(req, req_distro, req_distro_version):
# this requirement subspec doesn't apply to this distro
continue
# Is this specifying how to install in some distro?
# FIXME: consider versions when getting method_name
method_name = method_abc.distro_to_method[req_distro].name
else:
raise RuntimeError(
f"BUG? specification {req_distro_spec} not understood"
f" as a distribution ({','.join(method_abc.distro_to_method.keys())})"
f" or install method ({','.join(method_abc.methods_by_name.keys())})")
# We have an installation method name, either specified as
# a top level or derived from a distro specification or
# defaults
# Parse method details
#
## DISTRO:
## skip[: True|yes]
skip_distro = bool(method_data.get('skip', False))
if skip_distro:
logging.info(f"{req}: boolean skip field"
f" for distro {distro}:{distro_version}")
method_data = None
continue
if req_distro_version:
# record distro_version if we have it, because
# this means it is a more specific match
method_data['distro_version'] = distro_version
# FIXME: call method driver to parse info into method?
# (eg: pip can do version filters and URLs to index,
# dnf/apt can't)
# Overrides from method data?
req_alternate = method_data.get('name', req)
if 'description' in method_data:
description = method_data['description']
logging.info(
f"{req} [{method_name}/{req_alternate}]:"
f" {req_distro_spec}: overriding description to:"
f" {description}")
if 'license' in method_data:
spdx_license = method_data['license']
logging.info(
f"{req} [{method_name}/{req_alternate}]:"
f" {req_distro_spec}: overriding license to:"
f" {spdx_license}")
# if we have some specific method data, record it,
# otherwise all defaults
if not method_data:
logging.debug(
f"{req} [{method_name}/{req_alternate}]:"
f" -> install on '{distro}' with default methods")
continue
# If there are no entries, create it
existing_method_data = req_method_details[method_name].get(req, None)
if not existing_method_data:
logging.info(
f"{req}: RECORD [{method_name}/{req_alternate}] -> {method_data}")
req_method_details[method_name][req] = method_data
continue
# existing entry? only update if it has no version
# info and we do; in case of conflict, keep existing
_existing_distro_version = existing_method_data.get('distro_version', None)
_distro_version = method_data.get('distro_version', None)
if _existing_distro_version and _distro_version:
logging.warning(
f"{req} [{req_distro_spec}]: skipping; got more specific"
f" entry for {distro}:{_existing_distro_version}")
continue
if _existing_distro_version and not _distro_version:
# don't override
logging.warning(
f"{req} [{req_distro_spec}]: skipping; got more specific"
f" entry for {distro}:{_existing_distro_version}")
continue
# ok, this is more version-specifc than what we have, so
# record it
logging.info(
f"{req}: RECORD [{distro_method}/{req_alternate}]:"
f" (distro version {distro_version} override)"
f" will install first with method {distro_method} {method_data}")
req_method_details[distro_method][req] = method_data
continue
if skip_platform:
# well, it was determined this has to be skipped
logging.info(f"{req}: skipped because of require/skip_platform tag")
continue
# At this point we have req, req_alternate and method_data
if skip_distro:
logging.info(f"{req}: skipped because of distro/skip tag")
continue
packages.setdefault(req, {})
if description:
packages[req]['description'] = description
if spdx_license:
packages[req]['license'] = spdx_license
if method_name == None:
# no method? default to the distro's default
method_name = method_abc.distro_to_method[distro].name
# do we have to install exclusively with any method? if so,
# mark it -- this applies mostly to 'data' methods, where we
# just download some file
exclusive = method_data.get(
'exclusive',
method_abc.methods_by_name[method_name].exclusive)
if exclusive:
logging.info(
f"{req}: RECORD package will be exclusively installed with"
f" method '{method_name}'")
packages[req]['method'] = method_name
else:
logging.info(
f"{req}: RECORD package will be installed with any available method")
_filename_regex = re.compile(r"^.*\.nreqs\.yaml$")
def _parse_file(filename, packages, method_details):
# regular filename
basename = os.path.basename(filename)
m = _filename_regex.search(basename)
if not m:
logging.info(f"{filename}: ignoring, doesn't match NAME.nreqs.yaml")
return
logging.warning(f"{filename}: parsing")
try:
with open(filename) as f:
y = yaml.safe_load(f)
# FIXME: validate YAML
_reqs_grok(packages, method_details, filename, y)
except Exception as e:
logging.exception(f"{filename}: can't process: {e}")
# FIXME: ack -k?
def _parse_files(args):
#
# Load all the requirements into the reqs array
#
# reqs[METHOD] = [set of packages to install]
method_details = collections.defaultdict(collections.defaultdict)
packages = collections.OrderedDict()
# pre-install packages that might be needed by methods
#
# this has to happen first thing so they show up in the right
# order and are installed first.
#
# eg: pip defines python3-pip has to be dnf/apt installed
for method_name, method in method_abc.methods_by_name.items():
if method.prerequisites:
_reqs_grok(packages, method_details,
f"prerequisites for {method_name}",
method.prerequisites)
for path in args.filenames:
logging.debug(f"{path}: considering parsing")
if os.path.isfile(path):
_parse_file(path, packages, method_details)
elif os.path.isdir(path):
for file_path, _dirnames, filenames in os.walk(path):
logging.log(5, "%s: scanning directory", path)
for filename in filenames:
filename = os.path.join(file_path, filename)
_parse_file(filename, packages, method_details)
else:
logging.warning(f"{path}: invalid input file")
return packages, method_details
def _command_hash(args):
packages, method_details = _parse_files(args)
# FIXME: preprocess packages and method_details and remove fields
# that should not affect the dependency trees:
# - description
# - license
s = json.dumps(packages, indent = 4, skipkeys = True) \
+ json.dumps(method_details, indent = 4, skipkeys = True)
m = hashlib.sha512(s.encode('utf-8'))
# add any files passed with -e
for filename in args.extra_file:
with open(filename, 'rb') as f:
logging.warning(f"hash: adding contents of {filename}")
m.update(f.read())
for salt in args.salt:
m.update(salt.encode('utf-8'))
print(m.hexdigest()[:16])
def _command_json(args):
packages, method_details = _parse_files(args)
toplevel = {
"packages": packages,
"method_details": method_details,
}
json.dump(toplevel, sys.stdout, indent = 4, skipkeys = True)
def _command_install(args):
logging.warning(f"installing for: {distro} v{distro_version},"
f" default install with {distro_method}")
if args.sudo and not method_abc.is_admin():
# only enable sudo if not running as root, makes no sense
# otherwise; it also allows this to run smoothly on machines
# (eg: containers) that don't install sudo
method_abc.sudo = "sudo "
else:
method_abc.sudo = ""
packages, method_details = _parse_files(args)
if os.getenv('VIRTUAL_ENV') != None:
# if we are in a virtual environment, we first need to try to
# install with pip (eg: generic methods) in the virtual env,
# since the distro methods won't install in the venv
if args.generic_first == None:
logging.error(
"WARNING! python virtual_env active, enabling --generic-first"
" (you can forcibly disable with --generic-first)")
args.generic_first = True
if args.generic_first:
methods = list(method_abc.generic_methods.values()) + \
[ method_abc.methods_by_name[distro_method] ]
else:
methods = [ method_abc.methods_by_name[distro_method] ] \
+ list(method_abc.generic_methods.values())
skip_regexes = []
for pattern in args.skip_package:
skip_regexes.append(re.compile(pattern))
for package, package_data in packages.items():
# Gotta skip?
for skip_regex in skip_regexes:
m = skip_regex.search(package)
if m:
break
else:
m = None
if m:
logging.error(f"{package}: skipping, matches"
f" --skip-packages {skip_regex.pattern}")
continue
# clear, let's go
logging.warning(f"{package}: installing")
logging.info(f"{package}: package data {package_data}")
methods_tried = []
for method in methods:
try:
# FIXME: acknowledge exclusive -> if
# package_data['method'] != method, skip
_method_details = method_details.get(method.name, {}).get(package, {})