Skip to content

Instantly share code, notes, and snippets.

@bodqhrohro
Last active March 3, 2019 16:04
Show Gist options
  • Save bodqhrohro/b682a273d41efa4543349c867d15eb61 to your computer and use it in GitHub Desktop.
Save bodqhrohro/b682a273d41efa4543349c867d15eb61 to your computer and use it in GitHub Desktop.
Verbose log of upgradeable packages
# ported to Python, thanks to KillTheCat
import subprocess
def get_installed_size(block: str) -> int:
def size_in_byte(data: list) -> int:
if len(data) != 2:
return 0
inc = 1
if data[1] == 'kB':
inc = 1024
elif data[1] == 'MB':
inc = 1024**2
elif data[1] == 'GB':
inc = 1024**3
return int(float(data[0]) * inc)
size = 0
for line in block.split('\n'):
if line.startswith('Installed-Size: '):
size = size_in_byte(line.split(' ', 2)[1:])
return size
def human_readable_size(bytes: int, delta: bool = False) -> str:
FORMAT = '%.1f'
if delta:
FORMAT = '+' + FORMAT
abs_bytes = abs(bytes)
if abs_bytes >= 2 ** 30:
return (FORMAT + ' GB') % (bytes / 2 ** 30)
elif abs_bytes >= 2 ** 20:
return (FORMAT + ' MB') % (bytes / 2 ** 20)
elif abs_bytes >= 2 ** 10:
return (FORMAT + ' kB') % (bytes / 2 ** 10)
else:
return '%d B' % bytes
def generator() -> tuple:
result = []
for line in subprocess.run(
['apt', 'list', '--upgradeable'], stdout=subprocess.PIPE, stderr=subprocess.PIPE
).stdout.decode().split('\n'):
try:
name, line = line.split('/', 1)
line = line.split(' ')
result.append([name, line[5].rstrip(']')])
result.append([name, line[1]])
except (IndexError, ValueError):
continue
for idx, block in enumerate(subprocess.run(
['apt', 'show'] + ['{}={}'.format(*l) for l in result],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
).stdout.decode().strip('\n').split('\n\n')):
result[idx][1] = get_installed_size(block)
for old_size, new_size in zip(result[0::2], result[1::2]):
name = old_size[0]
old_size, new_size = old_size[1], new_size[1]
delta = new_size - old_size
if delta > 0:
color = 'f00'
elif delta < 0:
color = '0f0'
else:
color = '888'
old_size, new_size = (human_readable_size(s) for s in (old_size, new_size))
delta = human_readable_size(delta, True)
yield name, old_size, new_size, color, delta
def table_generator():
td = '<td>\n{}\n</td>'
td_color = '<td><font color="#{}">\n{}\n</font></td>'
tr = '\n'.join(('<tr>', td, td, td, td_color, '</tr>'))
print('<table>')
print('\n'.join([tr.format(*data) for data in generator()]))
print('</table>')
if __name__ == '__main__':
table_generator()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment