-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
116 lines (97 loc) · 3.07 KB
/
main.py
File metadata and controls
116 lines (97 loc) · 3.07 KB
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
import os
import sys
import click
from git_cmd import useGit # pack: ignore
from project import ( # pack: ignore
_GitConfigurations, # pack: ignore
get_config, # pack: ignore
get_config_raw, # pack: ignore
new_proj, # pack: ignore
overwrite_config_raw, # pack: ignore
) # pack: ignore
from rich import get_console
console = get_console()
# Function to disable stdout and stderr
def disable_output():
# Redirect stdout and stderr to os.devnull
sys.stdout = open(os.devnull, "w")
sys.stderr = open(os.devnull, "w")
# Function to restore stdout and stderr
def restore_output():
# Restore stdout and stderr to their original values
sys.stdout.close()
sys.stderr.close()
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
@click.group
@click.option("--quiet", is_flag=True, help="Run in quiet mode.")
def cli(quiet):
"""
a package manager for git repositories
allows you to install, remove and update your libraries using their git repository locally
"""
if quiet:
disable_output()
@cli.command
@click.argument("url")
@click.argument("target_dir")
def add(url, target_dir):
"add a new package to the registry"
cfg: _GitConfigurations = get_config_raw()
for repo in cfg["repositories"]:
if repo["url"] == url:
print("repo already added to the list of packages.")
os._exit(0)
cfg["repositories"].append({"url": url, "dir": target_dir})
overwrite_config_raw(cfg)
os._exit(0)
@cli.command
@click.argument("url")
def remove(url):
"remove a package by its url"
cfg: _GitConfigurations = get_config_raw()
for index, repo in enumerate(cfg["repositories"]):
if repo["url"] == url:
del cfg["repositories"][index]
overwrite_config_raw(cfg)
print(f"removed the package with the url {url!r}.")
os._exit(0)
@cli.command
def init():
"create a new git package manager repository"
if os.path.exists("git-package.json"):
print("git configuration already exists in this directory.")
if not click.confirm("do you want to overwrite it"):
print("[canceled]")
return
new_proj()
useGit("init")
useGit("add", ".")
useGit("commit", "-m", "init repo")
print("successfully initiated a new git project.")
os._exit(0)
@cli.command
def sync():
"update all the packages and install the ones that aren't installed yet"
cfg = get_config()
with console.status("cloning the git repositories"):
for repo in cfg.repositories:
if os.path.exists(repo.dir):
os.rmdir(repo.dir)
print(f"updating: {repo.url!r}")
else:
print(f"installing: {repo.url!r}")
useGit("clone", repo.url, repo.dir)
print("installed all the git packages")
os._exit(0)
@cli.command
def list():
"list all the registered packages"
cfg = get_config()
for repo in cfg.repositories:
print(f"`{repo.url}` (at {repo.dir})")
os._exit(0)
if __name__ == "__main__":
cli()
restore_output()
# end main