summaryrefslogtreecommitdiff
path: root/.local/bin/update
blob: 9cb752d1fa2efa51d66c173f5f11ccf138378257 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3

"""
nvim --headless "+Lazy! sync"
sudo pacman -Syu
rustup update
aur sync -unr
nix-channel --update && nix-env -u
"""

import sys, os
import argparse
import subprocess
from time import localtime, strftime


parser = argparse.ArgumentParser(prog = 'update')
parser.add_argument('--noconfirm', default = False, action = 'store_true')
args = parser.parse_args()


def log(message: str):
    print(f'[{strftime("%H:%M:%S", localtime())}] {message}')


lock_file = 'update.lock'
if os.path.exists(lock_file):
    log('Update already running')
    sys.exit(0)

open(lock_file, 'a').close()


try:
    commands_confirm = [
        lambda: log('Running `sudo pacman -Syu`...'),
        lambda: subprocess.run(['sudo', 'pacman', '-Syu']),
        lambda: log('Running `aur sync -unr`...'),
        lambda: subprocess.run(['aur', 'sync', '-unr']),
    ]

    commands_noconfirm = [
        lambda: log('Running `aur sync -unr`...'),
        lambda: subprocess.run(['aur', 'sync', '-unr']),
        lambda: log('Running `sudo pacman -Syu`...'),
        lambda: subprocess.run(['sudo', 'pacman', '-Syu', '--noconfirm']),
    ]

    if not args.noconfirm:
        for command in commands_confirm:
            command()
    else:
        for command in commands_noconfirm:
            command()

    log('Running `nvim --headless \'+Lazy! sync\'`...'),
    subprocess.run(['nvim', '--headless', '"+Lazy! sync"', '+qall']),

    log('Running `rustup update`...')
    subprocess.run(['rustup', 'update'])

    log('Running `nix-channel --update`...')
    subprocess.run(['nix-channel', '--update'])

    log('Running `nix-env -u`...')
    subprocess.run(['nix-env', '-u'])

except (KeyboardInterrupt):
    pass

log('Cleaning up...')
os.remove(lock_file)