-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
665 lines (522 loc) · 19.7 KB
/
main.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import socket
import time
from datetime import datetime
from typing import List
import pyalpm
import pycman
import typer
from rich import print, prompt
from rich.console import Console
from rich.padding import Padding
from rich.panel import Panel
from rich.text import Text
from rich.tree import Tree
from callbacks import dl_cb, log_cb
__version__ = "0.1.0"
app = typer.Typer()
console = Console()
pacman = pycman.config.init_with_config("/etc/pacman.conf") # type: ignore
# App Commands
@app.callback(invoke_without_command=True)
def callback(ctx: typer.Context, verbose: bool = False, version: bool = False):
if verbose == True:
pacman.logcb = log_cb
if version or ctx.invoked_subcommand is None:
console.print(
r"""[bold blue]
| |
_ _ __ _ | | __ _
| | | | / _` || | / _` |
| |_| || (_| || || (_| |
\__, | \__,_||_| \__,_|
__/ |
|___/
[/bold blue]""",
)
console.print(
f" [bold]- The Most Prettier Package Manager for Arch Linux -[/bold]"
)
console.print(f" [bold]Version:[/bold]{__version__}")
if not version:
print(ctx.get_help())
if version:
print()
@app.command(name="list")
def lis(upgradable: bool = False):
"""
List All Installed or Upgradable Packages
"""
if upgradable == True:
show_upgradable_packages()
else:
show_installed_packages()
@app.command()
def update(verbose: bool = False):
"""
Update System Databases
"""
dbs = pacman.get_syncdbs()
# pacman.dlcb = download_callback
# Is Verbose Enabled Show Log to the Console
if verbose == True:
pacman.logcb = log_cb
else:
pacman.logcb = None
update = ""
failed = 0
# Updating Databases
with console.status("[bold white]Updating Databases...[/bold white]") as status:
time.sleep(1)
for db in dbs:
time.sleep(1)
try:
db.update(False)
status.update(
f"[bold white]Updating....[/bold white] [blue]{db.name}[/blue]"
)
console.print(
f"[bold green]SUCCESS[/bold green] Updating: [blue]{db.name}[blue]"
)
except:
console.print(
f"[bold red]ERROR[/bold red] Updating: [blue]{db.name}[/blue]"
)
failed = failed + 1
if failed == len(dbs):
console.print(
"[bold yellow] HINT[/bold yellow] Set [white]--verbose[/white] flag to show log about updating process"
)
# zh = alpm_handle.init_transaction(booleans)
# print(zh)
@app.command()
def upgrade(downgrade: bool = False, verbose: bool = False):
"""
Execute System Upgrade
"""
if verbose == True:
pacman.logcb = log_cb
else:
pacman.logcb = None
# Initialize Transaction
t: pyalpm.Transaction = pacman.init_transaction()
t.sysupgrade(downgrade)
t.prepare()
# Show all upgradable packages
upgradable = get_upgradable_packages()
console.print(
f"There are {str(len(upgradable))} packages with a new version",
justify="center",
)
names = ", ".join([f"{pkg.name}" for pkg in upgradable])
text = Text(names, style="blue")
panel = Panel(text, height=10)
p = Padding(panel, 1)
console.print(p)
total_install_size = 0
total_download_size = 0
# Show total install and download size
for pkg in upgradable:
total_download_size += pkg.download_size
total_install_size += pkg.isize
print(
f" [bright_black]Total Download Size:[/bright_black] [blue]{relative_bytes_converter(total_download_size)}[blue]"
)
print(
f" [bright_black]Total Install Size:[/bright_black] [blue]{relative_bytes_converter(total_install_size)}[blue]"
)
console.print(
"[white]Type [green]yala list[/green] [blue]--upgradable[/blue] to se all upgradable packages[/white]",
justify="center",
)
if len(t.to_add) + len(t.to_remove) == 0:
print("nothing to do")
else:
try:
t.commit()
except:
print(f" [bold red]ERROR[/bold red] Upgrading the System [blue]")
print(
"[bold yellow] HINT[/bold yellow] Set [white]--verbose[/white] flag to show logs"
)
t.release()
@app.command()
def install(pkg: List[str], verbose: bool = False):
"""
Installs New Packages
"""
# Initialize Pacman
pacman.dlcb = dl_cb
# Configure verbosity of this command
if verbose == True:
pacman.logcb = log_cb
else:
pacman.logcb = None
# Get Sync DBs
dbs = pacman.get_syncdbs()
# Initialize New Transaction
try:
t: pyalpm.Transaction = pacman.init_transaction()
# Search for Packages To Install
packages_to_install = list(set(pkg))
pkgs = []
for package in packages_to_install:
pk = get_package(package, dbs)
if pk:
# Add package to the Transaction
t.add_pkg(pk)
pkgs.append(pk)
else:
print(f"[bold red]ERROR[/bold red] Package {package} not found")
# Install valid packages
if len(pkgs) != 0:
# Show to the user the packages to be installed
names = ", ".join([f"[blue]{pkg.name}[/blue]" for pkg in pkgs])
print(f" Do you want to install ({str(len(pkgs))}) packages: {names}?")
# Show download and install total size
total_download_size = 0
total_install_size = 0
for pkg in pkgs:
total_download_size += pkg.download_size # type: ignore
total_install_size += pkg.isize # type: ignore
print(
f" [bright_black]Total Download Size:[/bright_black] [blue]{relative_bytes_converter(total_download_size)}[blue]"
)
print(
f" [bright_black]Total Install Size:[/bright_black] [blue]{relative_bytes_converter(total_install_size)}[blue]"
)
print()
if prompt.Confirm.ask(" [white bold]Continue?[/white bold]"):
# Resolve Dependencies
result = t.prepare()
# if not result:
# print("[bold red]ERROR Resolving dependencies [/bold red]")
# Commit Transaction
try:
t.commit()
print(f" Package/s ({names}) are suceffuly installed")
# print(f" [bold red]ERROR[/bold red] No internet connection")
except Exception as error:
print(f" [bold red]ERROR[/bold red] Resolving Packages")
# Realease Transaction
t.release()
except pyalpm.error as error:
print(f" [bold red]ERROR[/bold red] {error.args[0]}")
@app.command()
def remove(pkg: List[str], verbose: bool = False):
"""
Remove Packages from your System
"""
# Initialize Pacman
pacman.dlcb = dl_cb
# Configure verbosity of this command
if verbose == True:
pacman.logcb = log_cb
else:
pacman.logcb = None
# Get Local DB
# dbs = pacman.get_syncdbs()
db = pacman.get_localdb()
# Initialize New Transaction
try:
t: pyalpm.Transaction = pacman.init_transaction()
# Search for Packages To Install
packages_to_remove = list(set(pkg))
pkgs = []
for package in packages_to_remove:
pk = db.get_pkg(package)
if pk:
# Add package to the Transaction
t.remove_pkg(pk)
pkgs.append(pk)
else:
print(f"[bold red]ERROR[/bold red] Package {package} not found")
# Install valid packages
if len(pkgs) != 0:
# Show to the user the packages to be removed
names = ", ".join([f"[blue]{pkg.name}[/blue]" for pkg in pkgs])
print(f" Do you want to remove ({str(len(pkgs))}) packages: {names}?")
if prompt.Confirm.ask(" [white bold]Continue?[/white bold]"):
# Resolve Dependencies
result = t.prepare()
# if not result:
# print("[bold red]ERROR Resolving dependencies [/bold red]")
# Commit Transaction
try:
t.commit()
print(
f" Package/s ({names}) are suceffuly removed from your system"
)
# print(f" [bold red]ERROR[/bold red] No internet connection")
except Exception as error:
print(f" [bold red]ERROR[/bold red] Resolving Packages")
# Realease Transaction
t.release()
except pyalpm.error as error:
print(f" [bold red]ERROR[/bold red] {error.args[0]}")
@app.command()
def search(query: str, local: bool = False, exact: bool = False):
"""
Search for Packages in Local and Sync dbs
"""
if local == True:
search_localdb(query, exact)
else:
search_syncdbs(query, exact)
@app.command()
def inspect(pkg: str):
"""
Inspect Packages
"""
db: pyalpm.DB = pacman.get_localdb()
package = db.get_pkg(pkg)
if package:
tree = Tree(f"[bold green]{package.name}[/bold green]")
tree.add(f"[white]{package.desc}[white]")
tree.add(
f"[bright_black]source[/bright_black] : [blue]{package.db.name}[/blue]"
)
tree.add(f"[bright_black]description[/bright_black] : {package.desc}")
tree.add(
f"[bright_black]build date[/bright_black] : [blue]{datetime.fromtimestamp(package.builddate)}[/blue]"
)
tree.add(f"[bright_black]arch[/bright_black] : {package.arch}")
tree.add(f"[bright_black]url[/bright_black] : {package.url}")
tree.add(f"[bright_black]packager[/bright_black] : {package.packager}")
tree.add(f"[bright_black]md5sum[/bright_black] : {package.md5sum}")
tree.add(f"[bright_black]sha256sum[/bright_black] : {package.sha256sum}")
tree.add(f"[bright_black]base64_sig[/bright_black] : {package.base64_sig}")
tree.add(
f"[bright_black]install date[/bright_black] : [blue]{datetime.fromtimestamp(package.installdate)}[/blue]"
)
tree.add(
f"[bright_black]archive size[/bright_black] : [blue]{relative_bytes_converter(package.size)}[/blue]"
)
tree.add(
f"[bright_black]install size[/bright_black] : [blue]{relative_bytes_converter(package.isize)}[/blue]"
)
tree.add(
f"[bright_black]has scriptlet[/bright_black] : [blue]{package.has_scriptlet}[/blue]"
)
tree.add(
f"[bright_black]licenses[/bright_black] : [blue]{' '.join(package.licenses)}[/blue]"
)
tree.add(f"[bright_black]version[/bright_black] : {package.version}")
tree.add(
f"[bright_black]dependencies[/bright_black] : {' '.join(package.depends)}"
)
tree.add(
f"[bright_black]optional dependencies[/bright_black] : {' '.join(package.optdepends)}"
)
tree.add(
f"[bright_black]replaces[/bright_black] : {' '.join(package.replaces) if len(package.replaces) > 0 else None}"
)
tree.add(
f"[bright_black]provides[/bright_black] : {' '.join(package.provides) if len(package.provides) > 0 else None}"
)
tree.add(
f"[bright_black]conflicts[/bright_black] : {' '.join(package.conflicts) if len(package.conflicts) > 0 else None}"
)
tree.add(
f"[bright_black]backup[/bright_black] : {' '.join(package.backup) if len(package.backup) > 0 else None}"
)
tree.add(
f"[bright_black]groups[/bright_black] : {' '.join(package.groups) if len(package.groups) > 0 else None }"
)
tree.add(
f"[bright_black]required by[/bright_black] : {' '.join(package.compute_requiredby()) if len(package.compute_requiredby()) > 0 else None}"
)
tree.add(
f"[bright_black]optional for[/bright_black] : {' '.join(package.compute_optionalfor()) if len(package.compute_optionalfor()) > 0 else None}"
)
p = Padding(tree, 1)
print(p)
else:
print("Sorry there are not any package with this name")
# Utils
def show_upgradable_packages():
upgradable = 0
for pkg in pacman.get_localdb().pkgcache:
for sync_db in pacman.get_syncdbs():
sync_pkg = sync_db.get_pkg(pkg.name)
if (
sync_pkg is not None
and pyalpm.vercmp(sync_pkg.version, pkg.version) > 0
):
tree = Tree(f"[bold green]{pkg.name}[/bold green]")
tree.add(f"[white]{pkg.desc}[white]")
tree.add(
f"[bright_black]source[/bright_black] : [blue]{pkg.db.name}[/blue]"
)
tree.add(
f"[bright_black]install size[/bright_black] : [blue]{relative_bytes_converter(pkg.isize)}[/blue]"
)
tree.add(
f"[bright_black]version[/bright_black] : {pkg.version} -> [bold blue]{sync_pkg.version}[/bold blue]"
)
p = Padding(tree, 1)
upgradable = upgradable + 1
print(p)
if upgradable == 0:
console.print(
"[bold white]Congrats your system is [blue]Up To Date[/blue][/bold white]",
justify="center",
)
console.print("[bold white]Nothing to do here[/bold white]", justify="center")
else:
console.print(
f"[bold white]Total Upgradable Packages: [/bold white][green]{upgradable}[/green]",
justify="center",
)
def get_upgradable_packages():
upgradable = []
for pkg in pacman.get_localdb().pkgcache:
for sync_db in pacman.get_syncdbs():
sync_pkg = sync_db.get_pkg(pkg.name)
if (
sync_pkg is not None
and pyalpm.vercmp(sync_pkg.version, pkg.version) > 0
):
upgradable.append(sync_pkg)
return upgradable
def show_installed_packages():
db = pacman.get_localdb()
for pkg in db.pkgcache:
tree = Tree(f"[bold green]{pkg.name}[/bold green]")
tree.add(f"[white]{pkg.desc}[white]")
tree.add(f"[bright_black]source[/bright_black] : [blue]{pkg.db.name}[/blue]")
tree.add(
f"[bright_black]install size[/bright_black] : [blue]{relative_bytes_converter(pkg.isize)}[/blue]"
)
tree.add(f"[bright_black]version[/bright_black] : {pkg.version}")
p = Padding(tree, 1)
print(p)
console.print(
f"[bold white]Total Installed Packages: [/bold white] [green] {len(db.pkgcache)} [/green]",
justify="center",
)
def get_package(package: str, dbs):
for db in dbs:
pk = db.get_pkg(package)
if pk:
return pk
return None
def search_localdb(query: str, exact: bool):
"""
Search Packages in Local DB
"""
db: pyalpm.DB = pacman.get_localdb()
if exact == True:
pkg = db.get_pkg(query)
if pkg:
tree = Tree(f"[bold green]{pkg.name}[/bold green]")
tree.add(f"[white]{pkg.desc}[white]")
tree.add(
f"[bright_black]source[/bright_black] : [blue]{pkg.db.name}[/blue]"
)
tree.add(
f"[bright_black]install size[/bright_black] : [blue]{relative_bytes_converter(pkg.isize)}[/blue]"
)
tree.add(f"[bright_black]version[/bright_black] : {pkg.version}")
p = Padding(tree, 1)
print(p)
else:
print("Sorry there are not package with this name")
else:
pkgs = db.search(query)
pkgs.reverse()
for pkg in pkgs:
tree = Tree(f"[bold green]{pkg.name}[/bold green]")
tree.add(f"[white]{pkg.desc}[white]")
tree.add(
f"[bright_black]source[/bright_black] : [blue]{pkg.db.name}[/blue]"
)
tree.add(
f"[bright_black]install size[/bright_black] : [blue]{relative_bytes_converter(pkg.isize)}[/blue]"
)
tree.add(f"[bright_black]version[/bright_black] : {pkg.version}")
p = Padding(tree, 1)
print(p)
def search_syncdbs(query: str, exact: bool):
"""
Search Packages in Sync DBs
"""
dbs = pacman.get_syncdbs()
pkgs = []
if exact == True:
pkg = get_package(query, dbs)
if pkg:
tree = Tree(f"[bold green]{pkg.name}[/bold green]")
tree.add(f"[white]{pkg.desc}[white]")
tree.add(
f"[bright_black]source[/bright_black] : [blue]{pkg.db.name}[/blue]"
)
tree.add(
f"[bright_black]install size[/bright_black] : [blue]{relative_bytes_converter(pkg.isize)}[/blue]"
)
tree.add(
f"[bright_black]download size[/bright_black] : [blue]{relative_bytes_converter(pkg.download_size)}[/blue]"
)
tree.add(f"[bright_black]version[/bright_black] : {pkg.version}")
p = Padding(tree, 1)
print(p)
else:
print("Sorry there are not package with this name")
else:
for db in dbs:
pkg = db.search(query)
pkgs.extend(pkg)
pkgs.reverse()
for pkg in pkgs:
tree = Tree(f"[bold green]{pkg.name}[/bold green]")
tree.add(f"[white]{pkg.desc}[white]")
tree.add(
f"[bright_black]source[/bright_black] : [blue]{pkg.db.name}[/blue]"
)
tree.add(
f"[bright_black]install size[/bright_black] : [blue]{relative_bytes_converter(pkg.isize)}[/blue]"
)
tree.add(
f"[bright_black]download size[/bright_black] : [blue]{relative_bytes_converter(pkg.download_size)}[/blue]"
)
tree.add(f"[bright_black]version[/bright_black] : {pkg.version}")
p = Padding(tree, 1)
print(p)
def get_download_size(pkg: pyalpm.Package):
dbs = pacman.get_syncdbs()
size = pkg.download_size
if pkg.depends:
for dep in pkg.depends:
pk = get_package(dep, dbs)
if pk is not None:
# size += get_dep_size(pk)
installed_pkg = pacman.get_localdb().get_pkg(pk.name)
if installed_pkg is None or installed_pkg.version != pk.version:
size += pk.download_size
return size
def get_install_size(pkg: pyalpm.Package):
dbs = pacman.get_syncdbs()
size = pkg.isize
if pkg.depends:
for dep in pkg.depends:
pk = get_package(dep, dbs)
if pk is not None:
# size += get_dep_size(pk)
installed_pkg = pacman.get_localdb().get_pkg(pk.name)
if installed_pkg is None or installed_pkg.version != pk.version:
size += pk.isize
return size
def relative_bytes_converter(num_bytes):
for unit in ["B", "KB", "MB", "GB", "TB"]:
if abs(num_bytes) < 1024.0:
return f"{num_bytes:.1f}{unit}"
num_bytes /= 1024
return f"{num_bytes:.1f}PB"
def is_connected():
try:
socket.create_connection(("www.google.com", 80))
return True
except OSError as error:
print(error)
return False
if __name__ == "__main__":
app()