Skip to content

Commit 1e044b5

Browse files
committed
Handle downloading and deleting index archive using constructor/destructor #660
Signed-off-by: Jono Yang <[email protected]>
1 parent eb4a4fb commit 1e044b5

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

minecode_pipelines/pipes/maven.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -585,19 +585,25 @@ class MavenNexusCollector:
585585
WARNING: Processing is rather long: a full index is ~600MB.
586586
"""
587587

588-
def __init__(self, index_properties_location=None):
589-
if index_properties_location:
590-
content = index_properties_location
591-
else:
592-
content = self.fetch_index_properties()
593-
with open(content) as config_file:
594-
self.index_properties = javaproperties.load(config_file) or {}
588+
def __init__(self, index_location=None, index_properties_location=None):
589+
self.set_index_properties(index_properties_location=index_properties_location)
590+
self.index_location = index_location
591+
self.index_location_given = bool(index_location)
592+
self.index_increment_locations = []
593+
594+
def __del__(self):
595+
if self.index_location and self.index_location_given:
596+
os.remove(self.index_location)
597+
if self.index_increment_locations:
598+
for loc in self.index_increment_locations:
599+
os.remove(loc)
595600

596601
def _fetch_index(self, uri=MAVEN_INDEX_URL):
597602
"""
598603
Return a temporary location where the maven index was saved.
599604
"""
600605
index = fetch_http(uri)
606+
self.index_location = index.path
601607
return index.path
602608

603609
def _fetch_index_properties(self, uri=MAVEN_INDEX_PROPERTIES_URL):
@@ -607,6 +613,16 @@ def _fetch_index_properties(self, uri=MAVEN_INDEX_PROPERTIES_URL):
607613
index_properties = fetch_http(uri)
608614
return index_properties.path
609615

616+
def set_index_properties(self, index_properties_location=None):
617+
if index_properties_location:
618+
content = index_properties_location
619+
else:
620+
content = self._fetch_index_properties()
621+
with open(content) as config_file:
622+
self.index_properties = javaproperties.load(config_file) or {}
623+
if not index_properties_location:
624+
os.remove(content)
625+
610626
def _fetch_index_increments(self, last_incremental):
611627
"""
612628
Yield maven index increments
@@ -617,6 +633,7 @@ def _fetch_index_increments(self, last_incremental):
617633
if key.startswith("nexus.index.incremental"):
618634
index_increment_url = MAVEN_INDEX_INCREMENT_BASE_URL.format(index=increment_index)
619635
index_increment = fetch_http(index_increment_url)
636+
self.index_increment_locations.append(index_increment.path)
620637
yield index_increment.path
621638

622639
def _get_packages(self, content=None):
@@ -690,16 +707,14 @@ def _get_packages_from_index_increments(self, last_incremental):
690707
for index_increment in self._fetch_index_increments(last_incremental=last_incremental):
691708
return self._get_packages(content=index_increment)
692709

693-
def get_packages(self, content=None, last_incremental=None):
710+
def get_packages(self, last_incremental=None):
694711
"""Yield Package objects from maven index"""
695712
if last_incremental:
696713
packages = chain(self._get_packages_from_index_increments(last_incremental=last_incremental))
697714
else:
698-
if content:
699-
index_location = content
700-
else:
701-
index_location = self.fetch_index()
702-
packages = self._get_packages(content=index_location)
715+
if not self.index_location:
716+
self._fetch_index()
717+
packages = self._get_packages(content=self.index_location)
703718
return packages
704719

705720

0 commit comments

Comments
 (0)