Skip to content

Commit

Permalink
Add --all flag to command 'rebuild-index'
Browse files Browse the repository at this point in the history
Fix #35
  • Loading branch information
HerManNav committed Jun 15, 2023
1 parent cb40bdd commit 2a4a7f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
7 changes: 6 additions & 1 deletion pypickup/cmd/rebuildIndexEP.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def init_subparser(parser: argparse.ArgumentParser):
parser.add_argument("packageName", type=str, nargs="?", default="", help="[OPTIONAL] Python package for which the index will be rebuilt")
parser.add_argument("-p", "--index-path", dest="pypiLocalPath", type=str, default=os.getenv("PYPICKUP_INDEX_PATH", default="./.pypickup/"), help="Local root path in which the specified package is expected to be.")

# ToDo: add --recursive option in order to rebuild all the indices.
parser.add_argument("-a", "--all", dest="rebuildAllIndices", default=False, action="store_true", help="Rebuild all indices for all the available packages, besides the main one.")

@staticmethod
def run(args: argparse.Namespace):
Expand All @@ -24,5 +24,10 @@ def run(args: argparse.Namespace):
else:
if args.packageName != "" and not controllerInstance.packageExists():
print("Package " + controllerInstance.packageName + " has not been added to the local repository yet. Run the 'add' command first.")
elif args.rebuildAllIndices:
if args.packageName != "":
print("-a flag enabled, ignoring specified package.")

controllerInstance.rebuildAllIndices()
else:
controllerInstance.rebuildIndex()
53 changes: 40 additions & 13 deletions pypickup/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import argparse
import shutil
from typing import Dict, List, Tuple
from typing import Dict, List

from tqdm import tqdm

Expand Down Expand Up @@ -660,21 +660,48 @@ def __getDirectoriesInLocal(self):
dirsAndFileNames: List[str] = os.listdir(self.pypiLocalPath)
return [el for el in dirsAndFileNames if el != "settings" and "index.html" not in el]

def __getSubpackagesForPackage(self):
subpackagesList: List[str] = os.listdir(self.packageLocalPath)
def __rebuildMainIndex(self):
baseHTML: str = self._htmlManager.getBaseHTML()

directories: List[str] = self.__getDirectoriesInLocal()

with open(self.baseHTMLFileFullName, "r+") as baseHTMLFile:
self._addPackagesToIndex(baseHTML, baseHTMLFile, {"./" + dir:dir for dir in directories})

print("Main index rebuilt.")

return directories

def __getSubpackagesForPackage(self, packageLocalPath: str):
subpackagesList: List[str] = os.listdir(packageLocalPath)
return [file for file in subpackagesList if re.match(self._regexZIPAndTars, file) or ".whl" in file]

def rebuildIndex(self):
def __rebuildIndexForPackage(self, package: str):
packageLocalPath: str = os.path.join(self.pypiLocalPath, package) + "/"
packageHTMLFileFullName: str = os.path.join(packageLocalPath, self._packageHTMLFileName)

baseHTML: str = self._htmlManager.getBaseHTML()

if self.packageName == "":
directories: List[str] = self.__getDirectoriesInLocal()
subpackages: List[str] = self.__getSubpackagesForPackage(packageLocalPath)

with open(self.baseHTMLFileFullName, "r+") as baseHTMLFile:
self._addPackagesToIndex(baseHTML, baseHTMLFile, {"./" + dir:dir for dir in directories})
else:
subpackages: List[str] = self.__getSubpackagesForPackage()
_, baseHTML = self._htmlManager.insertHTMLEntry(baseHTML, "h1", "Links for " + self.packageName, {})
with open(packageHTMLFileFullName, "w") as packageHTML_file:
self._addPackagesToIndex(baseHTML, packageHTML_file, {"./" + subpackage:subpackage for subpackage in subpackages})

print("Index for '" + package + "' rebuilt.")

def rebuildAllIndices(self):
print()

_, baseHTML = self._htmlManager.insertHTMLEntry(baseHTML, "h1", "Links for " + self.packageName, {})
with open(self.packageHTMLFileFullName, "w") as packageHTML_file:
self._addPackagesToIndex(baseHTML, packageHTML_file, {"./" + subpackage:subpackage for subpackage in subpackages})
currentPackages: List[str] = self.__rebuildMainIndex()

for package in currentPackages:
self.__rebuildIndexForPackage(package)

def rebuildIndex(self):
print()

if self.packageName == "":
self.__rebuildMainIndex()
else:
self.__rebuildIndexForPackage(self.packageName)

0 comments on commit 2a4a7f2

Please sign in to comment.