Merge ~alexmurray/ubuntu-cve-tracker:source-map-performance-improvements into ubuntu-cve-tracker:master

Proposed by Alex Murray
Status: Rejected
Rejected by: Alex Murray
Proposed branch: ~alexmurray/ubuntu-cve-tracker:source-map-performance-improvements
Merge into: ubuntu-cve-tracker:master
Diff against target: 141 lines (+33/-14)
4 files modified
scripts/cve_lib.py (+3/-2)
scripts/source_map.py (+16/-9)
scripts/test_source_map.py (+11/-2)
test/esm-fake-supported.txt (+3/-1)
Reviewer Review Type Date Requested Status
Alex Murray Disapprove
Review via email: mp+447863@code.launchpad.net

Description of the change

This is currently a work-in-progress

To post a comment you must log in.
e76ede6... by Alex Murray

scripts/test_source_map.py: test get_packages_from_generic_name()

Signed-off-by: Alex Murray <email address hidden>

1d08db8... by Alex Murray

scripts/test_source_map.py: test get_aliases_of_ubuntu_package()

Signed-off-by: Alex Murray <email address hidden>

Revision history for this message
Alex Murray (alexmurray) wrote :

Closing this since the current approach of using a global variable to store the cache directly in source_map doesn't make sense given that source_map in general doesn't store global state like this. Instead will either need to encapsulate the cache in the returned map, or return it in a separate variable.

review: Disapprove

Unmerged commits

1d08db8... by Alex Murray

scripts/test_source_map.py: test get_aliases_of_ubuntu_package()

Signed-off-by: Alex Murray <email address hidden>

Succeeded
[SUCCEEDED] unit-tests:0 (build)
[SUCCEEDED] check-cves:0 (build)
12 of 2 results
e76ede6... by Alex Murray

scripts/test_source_map.py: test get_packages_from_generic_name()

Signed-off-by: Alex Murray <email address hidden>

Succeeded
[SUCCEEDED] unit-tests:0 (build)
[SUCCEEDED] check-cves:0 (build)
12 of 2 results
24c7dec... by Alex Murray

scripts/source_map.py: add comments about future proposed caches

Signed-off-by: Alex Murray <email address hidden>

Succeeded
[SUCCEEDED] unit-tests:0 (build)
[SUCCEEDED] check-cves:0 (build)
12 of 2 results
73c31b7... by Alex Murray

scripts/source_map.py: add a cache for generic name lookups

Signed-off-by: Alex Murray <email address hidden>

54f07e4... by Alex Murray

scripts: load aliases when loading a subproject

Signed-off-by: Alex Murray <email address hidden>

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/scripts/cve_lib.py b/scripts/cve_lib.py
index 120dd8f..6439bd5 100755
--- a/scripts/cve_lib.py
+++ b/scripts/cve_lib.py
@@ -950,8 +950,9 @@ def load_external_subprojects():
950950
951 # check if aliases for packages exist951 # check if aliases for packages exist
952 if os.path.isfile(supported_txt[:-len("supported.txt")] + 'aliases.yaml'):952 if os.path.isfile(supported_txt[:-len("supported.txt")] + 'aliases.yaml'):
953 subprojects[rel].setdefault("aliases",953 # load aliases
954 supported_txt[:-len("supported.txt")] + 'aliases.yaml')954 aliases = yaml.safe_load(open(supported_txt[:-len("supported.txt")] + 'aliases.yaml'))
955 subprojects[rel].setdefault("aliases", aliases)
955956
956 try:957 try:
957 # use config to populate other parts of the958 # use config to populate other parts of the
diff --git a/scripts/source_map.py b/scripts/source_map.py
index 58ef5c3..65f0bdd 100755
--- a/scripts/source_map.py
+++ b/scripts/source_map.py
@@ -17,7 +17,6 @@ import re
17import subprocess17import subprocess
18import sys18import sys
19import cve_lib19import cve_lib
20import yaml
2120
22built_using_tags = ["Built-Using", "Static-Built-Using", "X-Cargo-Built-Using"]21built_using_tags = ["Built-Using", "Static-Built-Using", "X-Cargo-Built-Using"]
23apt_pkg.init_system()22apt_pkg.init_system()
@@ -388,37 +387,43 @@ def get_built_using_header():
388 header += "\n" + "-" * 120387 header += "\n" + "-" * 120
389 return header388 return header
390389
390# TODO - add a cache which we update when we load the sources data so that we
391# don't have to loop through all the pkgs in the source map
391def get_all_aliases(sources, rel):392def get_all_aliases(sources, rel):
392 aliases = []393 aliases = []
393394
394 if not rel in sources:395 if not rel in sources:
395 return aliases396 return aliases
396397
398 # this is very expensive
397 for pkg in sources[rel]:399 for pkg in sources[rel]:
398 if 'aliases' in sources[rel][pkg]:400 if 'aliases' in sources[rel][pkg]:
399 for alias in sources[rel][pkg]['aliases']:401 aliases += sources[rel][pkg]['aliases']
400 aliases.append(alias)
401 return aliases402 return aliases
402403
404# TODO - add a cache which we update when we load the sources data so that we
405# don't have to loop through all the pkgs in the source map
403def get_aliases_of_ubuntu_package(sources, pkg_name, rel):406def get_aliases_of_ubuntu_package(sources, pkg_name, rel):
404 aliases = []407 aliases = []
405 if not rel in sources:408 if not rel in sources:
406 return aliases409 return aliases
407410
411 # this is very expensive
408 for pkg in sources[rel]:412 for pkg in sources[rel]:
409 if 'aliases' in sources[rel][pkg]:413 if 'aliases' in sources[rel][pkg]:
410 if pkg_name in sources[rel][pkg]['aliases']:414 if pkg_name in sources[rel][pkg]['aliases']:
411 aliases.append(pkg)415 aliases.append(pkg)
412 return aliases416 return aliases
413417
418generic_name_cache = {}
414def get_packages_from_generic_name(sources, generic_name, rel):419def get_packages_from_generic_name(sources, generic_name, rel):
415 pkgs = []420 pkgs = []
416 if not rel in sources:421 if not rel in sources:
417 return pkgs422 return pkgs
418423
419 for pkg in sources[rel]:424 if generic_name in generic_name_cache and rel in generic_name_cache[generic_name]:
420 if 'generic_name' in sources[rel][pkg] and sources[rel][pkg]['generic_name'] == generic_name:425 return generic_name_cache[generic_name][rel]
421 pkgs.append(pkg)426
422 return pkgs427 return pkgs
423428
424def load_subprojects_lists(releases=None):429def load_subprojects_lists(releases=None):
@@ -484,11 +489,13 @@ def load_subprojects_lists(releases=None):
484 if '|' in pkg:489 if '|' in pkg:
485 main_package_name = pkg.split('|')[0]490 main_package_name = pkg.split('|')[0]
486 map[rel][pkg]['generic_name'] = main_package_name491 map[rel][pkg]['generic_name'] = main_package_name
492 generic_name_cache.setdefault(main_package_name, {})
493 generic_name_cache[main_package_name].setdefault(rel, [])
494 generic_name_cache[main_package_name][rel].append(pkg)
487495
488 if 'aliases' in details:
489 with open(details['aliases'], 'r') as file:
490 aliases = yaml.safe_load(file)
491496
497 if 'aliases' in details:
498 aliases = details['aliases']
492 for pkg in aliases:499 for pkg in aliases:
493 for src_pkg in map[rel]:500 for src_pkg in map[rel]:
494 if pkg == src_pkg or \501 if pkg == src_pkg or \
diff --git a/scripts/test_source_map.py b/scripts/test_source_map.py
index 8e99825..04316d3 100755
--- a/scripts/test_source_map.py
+++ b/scripts/test_source_map.py
@@ -15,6 +15,9 @@ mock_subprojects = {
15 "codename": "Fabulous Fake",15 "codename": "Fabulous Fake",
16 "ppa": "ubuntu-esm/esm-bar-security/ubuntu",16 "ppa": "ubuntu-esm/esm-bar-security/ubuntu",
17 "parent": "ubuntu/bar",17 "parent": "ubuntu/bar",
18 "aliases": {
19 "fooishbar": ["bar"],
20 },
18 "description": "Available with Ubuntu Pro: https://ubuntu.com/pro",21 "description": "Available with Ubuntu Pro: https://ubuntu.com/pro",
19 "stamp": 1618963200,22 "stamp": 1618963200,
20 }23 }
@@ -26,7 +29,13 @@ class TestSubprojects:
26 def test_load_subproject_supported(self, _find_sources_mock):29 def test_load_subproject_supported(self, _find_sources_mock):
27 _find_sources_mock.return_value = []30 _find_sources_mock.return_value = []
28 map = source_map.load(releases=['esm-foo/bar'])31 map = source_map.load(releases=['esm-foo/bar'])
29 assert set(map['esm-foo/bar'].keys()) == set(['foo', 'bar'])32 assert set(map['esm-foo/bar'].keys()) == set(['foo', 'baz|1.0', 'bar', 'fooishbar'])
33
34 pkgs = source_map.get_packages_from_generic_name(map, "baz", "esm-foo/bar")
35 assert pkgs == ['baz|1.0']
36
37 aliases = source_map.get_aliases_of_ubuntu_package(map, "bar", "esm-foo/bar")
38 assert aliases == ['fooishbar']
3039
31 @pytest.mark.parametrize("release", cve_lib.get_active_esm_releases())40 @pytest.mark.parametrize("release", cve_lib.get_active_esm_releases())
32 @mock.patch("source_map._find_sources")41 @mock.patch("source_map._find_sources")
@@ -52,4 +61,4 @@ class TestSubprojects:
52 packages_clean.add(package)61 packages_clean.add(package)
5362
54 map = source_map.load(releases=[release])63 map = source_map.load(releases=[release])
55 assert set(map[release].keys()) == packages_clean
56\ No newline at end of file64\ No newline at end of file
65 assert set(map[release].keys()) == packages_clean
diff --git a/test/esm-fake-supported.txt b/test/esm-fake-supported.txt
index a907ec3..9dcc581 100644
--- a/test/esm-fake-supported.txt
+++ b/test/esm-fake-supported.txt
@@ -1,2 +1,4 @@
1foo1foo
2bar
3\ No newline at end of file2\ No newline at end of file
3bar
4baz|1.0
5fooishbar

Subscribers

People subscribed via source and target branches