Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #15463: Remove tabulate python lib from rudder pkg #2389

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion relay/sources/rudder-pkg/lib/rudder-pkg/rpkg.py
Expand Up @@ -177,6 +177,6 @@ def show(self):
print(json.dumps(self.metadata, indent=4, sort_keys=True))

def toTabulate(self):
return [self.longName, self.version.mode, self.version.pluginLongVersion, self.isCompatible()]
return [self.longName, self.version.mode, self.version.pluginLongVersion, str(self.isCompatible())]


55 changes: 43 additions & 12 deletions relay/sources/rudder-pkg/lib/rudder-pkg/rudderPkg.py
Expand Up @@ -8,11 +8,11 @@
import shutil
import requests as requests
import logging
from tabulate import tabulate
import plugin
import rpkg
import rudderPkgUtils as utils
from lxml import html
import traceback


"""
Expand Down Expand Up @@ -49,33 +49,49 @@ def package_list_installed():
toPrint = []
printLatest = os.path.isfile(utils.INDEX_PATH)

pluginName = []
version = []
latestRelease = []

for p in utils.DB["plugins"].keys():
pluginName.append(p)
currentVersion = rpkg.PluginVersion(utils.DB["plugins"][p]["version"])
version.append(currentVersion)
extra = ""
if printLatest:
pkgs = plugin.Plugin(p)
pkgs.getAvailablePackages()
latestVersion = pkgs.getLatestCompatibleRelease().version
if currentVersion < latestVersion:
extra = "version %s is available"%(latestVersion.pluginLongVersion)
toPrint.append([p, currentVersion.pluginLongVersion, latestVersion.pluginLongVersion + " " + extra])
latestRelease.append(latestVersion.pluginLongVersion + " " + extra)
else:
toPrint.append([p, currentVersion.pluginLongVersion])
latestRelease.append("")

table = { "Plugin Name" : pluginName,
"Version" : version,
}
if printLatest:
print(tabulate(toPrint, headers=['Plugin Name', 'Version', 'Latest release'], tablefmt='orgtbl'))
else:
print(tabulate(toPrint, headers=['Plugin Name', 'Version'], tablefmt='orgtbl'))
table["Latest release"] = latestRelease
print(utils.dictToAsciiTable(table))

"""
List available plugin names.
"""
def package_list_name():
pluginDict = utils.list_plugin_name()
toPrint = []
pluginName = []
shortName = []
description = []
for p in pluginDict.keys():
toPrint.append([p, pluginDict[p][0], pluginDict[p][1]])
print(tabulate(toPrint, headers=['Plugin Name', 'Plugin Short Name', 'Description'], tablefmt='orgtbl'))
pluginName.append(str(p))
shortName.append(str(pluginDict[p][0]))
description.append(str(pluginDict[p][1]))
table = { "Plugin Name" : pluginName,
"Plugin Short Name" : shortName,
"Description" : description,
}
print(utils.dictToAsciiTable(table))

"""
Given a name, a version, and a mode, print associated plugin metadata.
Expand Down Expand Up @@ -105,10 +121,24 @@ def package_search(name):
utils.readConf()
pkgs = plugin.Plugin(name[0])
pkgs.getAvailablePackages()
toPrint = []
pluginName = []
releaseMode = []
version = []
compatible = []

for iRpkg in sorted(pkgs.packagesInfo):
toPrint.append(iRpkg.toTabulate())
print(tabulate(toPrint, headers=['Name', 'release mode', 'Version', 'Compatible'], tablefmt='orgtbl'))
data = iRpkg.toTabulate()
pluginName.append(data[0])
releaseMode.append(data[1])
version.append(data[2])
compatible.append(data[3])

table = { "Plugin Name" : pluginName,
"Release Mode" : releaseMode,
"Version" : version,
"Compatible" : compatible,
}
print(utils.dictToAsciiTable(table))

"""
Install the package for a given plugin in a specific version.
Expand Down Expand Up @@ -250,6 +280,7 @@ def update():
try:
utils.download(utils.URL + "/" + "rpkg.index")
except Exception as e:
traceback.print_exc(file=sys.stdout)
if os.path.isfile(utils.INDEX_PATH + ".bkp"):
logging.debug("restoring %s from %s"%(utils.INDEX_PATH, utils.INDEX_PATH + ".bkp"))
os.rename(utils.INDEX_PATH + ".bkp", utils.INDEX_PATH)
Expand Down
46 changes: 45 additions & 1 deletion relay/sources/rudder-pkg/lib/rudder-pkg/rudderPkgUtils.py
Expand Up @@ -3,8 +3,16 @@
from pprint import pprint
from pkg_resources import parse_version
from subprocess import Popen, PIPE
import fcntl, termios, struct

# See global variables at the end of the file
""" Get Terminal width """
def terminal_size():
h, w, hp, wp = struct.unpack('HHHH',
fcntl.ioctl(0, termios.TIOCGWINSZ,
struct.pack('HHHH', 0, 0, 0, 0)))
return w, h


"""
Start two different loggers:
Expand Down Expand Up @@ -85,6 +93,42 @@ def createPath(path):
except OSError:
if not os.path.isdir(path):
fail("Could not create dir %s"%(path))
"""
Print dict list in a fancy manner
Assume the dict is following the format:
{ "strkey1" : [ "str", "str2", ... ],
"strkey2" : [ "str", "str2", ... ],
}
"""
def dictToAsciiTable(data):
# Get maximum text length to print
lengths = [ len(max(data.get(k) + [k], key=len)) for k in data.keys() ]
lenstr = "| " + " | ".join("{:<%s}" % m for m in lengths) + " |"
lenstr += "\n"

# Define sep bar for header
sepBar = ""
for iSep in lengths:
sepBar += "+" + "-" * (int(iSep) + 2)
sepBar += "+\n"

outmsg = sepBar + lenstr.format(*data.keys()) + sepBar

# Write rows, at least an empty one if everything is empty
printedRows = 0
maxRows = max([len(data.get(k)) + 1 for k in data.keys()])
while True:
row = []
for k in data.keys():
if len(data.get(k)) > printedRows:
row.append(data.get(k)[printedRows])
else:
row.append("")
outmsg += lenstr.format(*row)
printedRows = printedRows + 1
if printedRows >= maxRows - 1:
break
return outmsg

"""
From a complete url, try to download a file. The destination path will be determined by the complete url
Expand All @@ -101,7 +145,7 @@ def download(completeUrl, dst=""):
fileDir = os.path.dirname(fileDst)
createPath(fileDir)
r = requests.get(completeUrl, auth=(USERNAME, PASSWORD), stream=True)
columns, rows = os.get_terminal_size(0)
columns = terminal_size()[1]
with open(fileDst, 'wb') as f:
bar_length = int(r.headers.get('content-length'))
if r.status_code == 200:
Expand Down