1
0
Fork 0
mirror of https://codeberg.org/Mo8it/dotfiles.git synced 2024-12-29 03:33:42 +00:00

Replace Xonsh scripts

This commit is contained in:
Mo 2022-05-04 22:45:52 +02:00
parent 114a99f4ff
commit 2089e825f8
4 changed files with 82 additions and 60 deletions

View file

@ -1 +0,0 @@
vim.wo.wrap = true

View file

@ -0,0 +1 @@
asciidoc.lua

View file

@ -1,11 +1,21 @@
#!/usr/bin/env xonsh
import os
import json
import subprocess
from os import environ
from pathlib import Path
def run(command, **kwargs):
return subprocess.run(command, shell=True, check=True, **kwargs)
def cap_run(command, **kwargs):
return run(
command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, **kwargs
).stdout
def rpm_ostree_packages(output_dir):
out = $(rpm-ostree status --json)
out = cap_run("rpm-ostree status --json")
json_out = json.loads(out)
deployments = json_out["deployments"]
@ -25,8 +35,9 @@ def rpm_ostree_packages(output_dir):
dict = {"Removed": removals, "Added": additions}
json.dump(dict, f, indent=4)
def npm_packages(output_dir):
out = $(npm list --json -g)
out = cap_run("npm list --json -g")
json_out = json.loads(out)
deps = json_out["dependencies"].keys()
@ -35,8 +46,9 @@ def npm_packages(output_dir):
for dep in deps:
f.write(dep + "\n")
def pipx_packages(output_dir):
out = $(pipx list --json)
out = cap_run("pipx list --json")
json_out = json.loads(out)
venvs = json_out["venvs"]
@ -51,16 +63,17 @@ def pipx_packages(output_dir):
file = output_dir / "pipx.json"
with file.open("w") as f:
dict = {p: i for p, i in zip(packages, injections)}
json.dump(dict, f, indent = 4)
json.dump(dict, f, indent=4)
output_dir = Path(f"{$HOME}/bk/installed_packages_info")
output_dir = Path(f"{environ['HOME']}/bk/installed_packages_info")
output_dir.mkdir(parents=True, exist_ok=True)
rpm_ostree_packages(output_dir)
flatpak list --app --columns=name,application,origin >@(output_dir)/flatpak.csv
run(f"flatpak list --app --columns=name,application,origin > {output_dir}/flatpak.csv")
cargo install --list | rg '(.+) .+:.*' -r '$1' >@(output_dir)/cargo.txt
run(f"cargo install --list | rg '(.+) .+:.*' -r '$1' > {output_dir}/cargo.txt")
pipx_packages(output_dir)

58
.scripts/update.py Normal file
View file

@ -0,0 +1,58 @@
import subprocess
from getpass import getpass
from sys import argv
def run(command, **kwargs):
return subprocess.run(command, shell=True, **kwargs)
def cap_live_lines(command, **kwargs):
lines = []
with subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, text=True, **kwargs
) as proc:
for line in proc.stdout:
print(line, end="")
lines.append(line.strip())
return lines
def press_to_exit():
getpass("Exit")
def main(input):
if input == "rpm-ostree":
lines = cap_live_lines("rpm-ostree upgrade")
if "no upgrade" not in lines[-1].lower():
press_to_exit()
elif input == "flatpak":
lines = cap_live_lines("flatpak update -y")
done_updates = "nothing to do" not in lines[-1].lower()
run("flatpak remove --delete-data --unused")
if done_updates:
press_to_exit()
elif input == "pipx":
lines = cap_live_lines("pipx upgrade-all --include-injected")
if "did not change" not in out[-1].lower():
press_to_exit()
elif input == "cargo":
lines = cap_live_lines("cargo install-update -a")
if "no packages need updating" not in lines[-2].lower():
press_to_exit()
elif input == "npm":
run("npm update -g")
elif input == "nvim":
run('nvim -c "autocmd User PackerComplete quitall" -c "PackerSync"')
else:
print(f"Input {input} not valid!")
main(argv[1])

View file

@ -1,49 +0,0 @@
#!/usr/bin/env xonsh
import re
def get_out(out_obj):
for line in out_obj:
print(line, end="")
return out_obj.out.split("\n")
def press_to_exit():
read -s -n 1 -p "Exit"
def main():
if $ARG1 == "rpm-ostree":
out = get_out(!(rpm-ostree upgrade | tee))
if out[-2] != "No upgrade available.":
press_to_exit()
elif $ARG1 == "flatpak":
flatpak update -y
flatpak remove --delete-data --unused
elif $ARG1 == "pipx":
out = get_out(!(pipx upgrade-all --include-injected | tee))
if "did not change" not in out[-2]:
press_to_exit()
elif $ARG1 == "cargo":
out = get_out(!(cargo install-update -a | tee))
pattern = r"updated (\d+) packages"
match = re.search(pattern, out[-2])
if match is None:
press_to_exit()
return
num_updated_packages = match.group(1)
if num_updated_packages != "0":
press_to_exit()
elif $ARG1 == "npm":
npm update -g
elif $ARG1 == "nvim":
nvim --headless -c "autocmd User PackerComplete quitall" -c "PackerSync"
else:
print(f"{$ARG1} not found!")
main()