Skip to content

Commit 5a1c038

Browse files
committed
Catalog cache cleanup
1 parent c6d69c8 commit 5a1c038

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

AddonCatalogCacheCreator.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
ADDON_CATALOG_URL = (
4545
"https://raw.githubusercontent.com/FreeCAD/FreeCAD-addons/master/AddonCatalog.json"
4646
)
47-
BASE_DIRECTORY = "./"
47+
BASE_DIRECTORY = "./CatalogCache"
4848
MAX_COUNT = 10 # Do at most this many repos (for testing purposes)
4949

5050
# Repos that are too large, or that should for some reason not be cloned here
@@ -62,13 +62,17 @@ class CacheEntry:
6262
icon_data: str = ""
6363

6464

65-
class AddonCatalogCacheCreator:
65+
class CatalogFetcher:
66+
"""Fetches the addon catalog from the given URL and returns an AddonCatalog object. Separated
67+
from the main class for easy mocking during tests. Note that every instantiation of this class
68+
will run a new fetch of the catalog."""
6669

67-
def __init__(self, addon_catalog_url=ADDON_CATALOG_URL):
70+
def __init__(self, addon_catalog_url: str = ADDON_CATALOG_URL):
6871
self.addon_catalog_url = addon_catalog_url
6972
self.catalog = self.fetch_catalog()
7073

7174
def fetch_catalog(self) -> AddonCatalog.AddonCatalog:
75+
"""Fetch the addon catalog from the given URL and return an AddonCatalog object."""
7276
response = requests.get(self.addon_catalog_url)
7377
if response.status_code != 200:
7478
raise RuntimeError(
@@ -78,22 +82,30 @@ def fetch_catalog(self) -> AddonCatalog.AddonCatalog:
7882

7983

8084
class CacheWriter:
85+
"""Writes a JSON file containing a cache of all addon catalog entries. The cache is a copy of
86+
the package.xml, requirements.txt, and metadata.txt files from the addon repositories, as well
87+
as a base64-encoded icon image. The cache is written to the current working directory."""
8188

8289
def __init__(self):
8390
self.catalog: AddonCatalog = None
8491
if os.path.isabs(BASE_DIRECTORY):
8592
self.cwd = BASE_DIRECTORY
8693
else:
87-
self.cwd = os.path.normpath(os.path.join(os.path.curdir, BASE_DIRECTORY))
94+
self.cwd = os.path.normpath(os.path.join(os.getcwd(), BASE_DIRECTORY))
8895
self._cache = {}
8996

9097
def write(self):
98+
original_working_directory = os.getcwd()
99+
os.makedirs(self.cwd, exist_ok=True)
100+
os.chdir(self.cwd)
91101
self.create_local_copy_of_addons()
92102
with open("addon_catalog_cache.json", "w", encoding="utf-8") as f:
93103
f.write(json.dumps(self._cache, indent=" "))
104+
os.chdir(original_working_directory)
105+
print(f"Wrote cache to {os.path.join(self.cwd, 'addon_catalog_cache.json')}")
94106

95107
def create_local_copy_of_addons(self):
96-
self.catalog = AddonCatalogCacheCreator().catalog
108+
self.catalog = CatalogFetcher().catalog
97109
counter = 0
98110
for addon_id, catalog_entries in self.catalog.get_catalog().items():
99111
if addon_id in EXCLUDED_REPOS:
@@ -128,7 +140,7 @@ def create_local_copy_of_single_addon(
128140
def generate_cache_entry(
129141
self, addon_id: str, index: int, catalog_entry: AddonCatalog.AddonCatalogEntry
130142
) -> Optional[CacheEntry]:
131-
"""Create the cache entry for this catalog entry, if there is data to cache. If there is
143+
"""Create the cache entry for this catalog entry if there is data to cache. If there is
132144
nothing to cache, returns None."""
133145
path_to_package_xml = self.find_file("package.xml", addon_id, index, catalog_entry)
134146
cache_entry = None

AddonManagerTest/app/test_addon_catalog_cache_creator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def test_create_local_copy_of_single_addon_using_zip(
222222
self.assertEqual(3, len(writer._cache["TestMod"]))
223223

224224
@patch("AddonCatalogCacheCreator.CacheWriter.create_local_copy_of_single_addon")
225-
@patch("AddonCatalogCacheCreator.AddonCatalogCacheCreator.fetch_catalog")
225+
@patch("AddonCatalogCacheCreator.CatalogFetcher.fetch_catalog")
226226
def test_create_local_copy_of_addons(self, mock_fetch_catalog, mock_create_single_addon):
227227
"""Given a catalog, each addon is fetched and cached."""
228228

0 commit comments

Comments
 (0)