Skip to content

Commit

Permalink
Write packages in index only after having actually downloaded them.
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
HerManNav committed Sep 13, 2022
1 parent 7867553 commit 5ce2b87
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions pypi_cache/pypiCacheManager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#! /usr/bin/python
from fileinput import filelineno
from io import TextIOWrapper
import os

Expand Down Expand Up @@ -69,12 +68,8 @@ def packageLocalFileName(self, new_PackageLocalFileName: str):

### Common methods ###

def __writeFileFromTheStart(self, file: TextIOWrapper, textToWrite: str):
file.seek(0)
file.truncate(0)
file.write(textToWrite)

def __getLink(self, linkURL: str) -> Tuple[bool, str, bytes]:
response: requests.Response = requests.Response()
try:
response = requests.get(linkURL)
response.raise_for_status()
Expand All @@ -89,7 +84,18 @@ def __getLink(self, linkURL: str) -> Tuple[bool, str, bytes]:

return True, "200 OK", response.content

def __downloadFilesInLocalPath(self, packagesToDownload: Dict[str, str], indexHTML: str = "", addPackageFilesToIndex: bool = False) -> str:
def __writeFileFromTheStart(self, file: TextIOWrapper, textToWrite: str):
file.seek(0)
file.truncate(0)
file.write(textToWrite)

def __addPackageToIndex(self, indexHTML: str, file: TextIOWrapper, href: str, entryText: str) -> str:
_, updatedHTML = self._htmlManager.insertHTMLEntry(indexHTML, "a", {"href": href}, entryText)
self.__writeFileFromTheStart(file, updatedHTML)

return updatedHTML

def __downloadFilesInLocalPath(self, packagesToDownload: Dict[str, str], indexHTML: str, file: TextIOWrapper):
updatedHTML: str = indexHTML

if len(packagesToDownload) == 0:
Expand All @@ -109,8 +115,7 @@ def __downloadFilesInLocalPath(self, packagesToDownload: Dict[str, str], indexHT
with open(self.packageLocalFileName + fileName, "wb") as f:
f.write(content)

if addPackageFilesToIndex:
_, updatedHTML = self._htmlManager.insertHTMLEntry(updatedHTML, "a", {"href": fileLink}, fileName)
updatedHTML = self.__addPackageToIndex(updatedHTML, file, fileLink, fileName)

actuallyDownloadedPackages += 1

Expand All @@ -119,8 +124,6 @@ def __downloadFilesInLocalPath(self, packagesToDownload: Dict[str, str], indexHT
print()
print(str(actuallyDownloadedPackages) + "/" + str(len(packagesToDownload)) + " downloaded.")

return updatedHTML

### 'Add' command methods ###

def __initRegexs(self):
Expand All @@ -142,8 +145,9 @@ def parseScriptArguments(self, args: argparse.ArgumentParser):
def validPackageName(self) -> bool:
"""Checks whether the package link exists or not. If not, it returns False. True otherwise."""

ok, _, _ = self.__getLink(self._remotePypiBaseDir + self.packageName)
ok, status, _ = self.__getLink(self._remotePypiBaseDir + self.packageName)
if not ok:
print(status)
return False

return True
Expand Down Expand Up @@ -176,6 +180,7 @@ def __addEntryToBaseHTMLFile(self, baseHTMLFilePath: str) -> bool:

needToDownloadFiles: bool = False
if not entryAlreadyExists:
# ToDo: refactor these lines with the method __writeFileFromTheStart()
baseHTML_file.seek(0)
baseHTML_file.truncate()
baseHTML_file.write(htmlUpdated)
Expand Down Expand Up @@ -204,13 +209,14 @@ def addPackage(self):
pypiPackageHTMLStr: str = pypiPackageHTML.decode("utf-8")

pypiPackageHTMLStr = self._htmlManager.filterInHTML(pypiPackageHTMLStr, self._regexZIPAndTars, self.packageName, self.onlySources)
packageHTML_file = open(self.packageLocalFileName + self._packageHTMLFileName, "w")
packageHTML_file.write(pypiPackageHTMLStr)
packageHTML_file.close()

linksToDownload: Dict[str, str] = self._htmlManager.getHRefsList(pypiPackageHTMLStr)

self.__downloadFilesInLocalPath(linksToDownload)
packageBaseHTML: str = self._htmlManager.getBaseHTML()
_, packageBaseHTML = self._htmlManager.insertHTMLEntry(packageBaseHTML, "h1", {}, "Links for " + self.packageName)
with open(self.packageLocalFileName + self._packageHTMLFileName, "w") as packageHTML_file:
packageHTML_file.write(packageBaseHTML)

self.__downloadFilesInLocalPath(linksToDownload, packageBaseHTML, packageHTML_file)

### 'Update' command methods ###

Expand Down Expand Up @@ -252,10 +258,6 @@ def __getNewPackagesInRemote(self, remoteIndexHRefs: Dict[str, str], localIndexH

return resultingDict

def __overwritePackageIndexFile(self, textToWrite: str):
with open(self.packageLocalFileName + self._packageHTMLFileName, "r+") as pypiLocalIndexFile:
self.__writeFileFromTheStart(pypiLocalIndexFile, textToWrite)

def synchronizeWithRemote(self):
"""Synchronize the self.packageName against the PyPI remote repository. It adds the new available packages to the packageName/index.html and download them. Assumes the folders exists."""

Expand All @@ -275,6 +277,5 @@ def synchronizeWithRemote(self):
localIndexHRefs: Dict[str, str] = self._htmlManager.getHRefsList(pypiLocalIndex)
newPackagesToDownload: Dict[str, str] = self.__getNewPackagesInRemote(remoteIndexHRefs, localIndexHRefs)

pypiLocalIndexUpdated = self.__downloadFilesInLocalPath(newPackagesToDownload, pypiLocalIndex, True)

self.__overwritePackageIndexFile(pypiLocalIndexUpdated)
with open(self.packageLocalFileName + self._packageHTMLFileName, "w") as pypiLocalIndexFile:
self.__downloadFilesInLocalPath(newPackagesToDownload, pypiLocalIndex, pypiLocalIndexFile)

0 comments on commit 5ce2b87

Please sign in to comment.