Created
December 17, 2020 22:53
-
-
Save vectorsize/9a99741817ca09c878bb948262705f50 to your computer and use it in GitHub Desktop.
one file git based package manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import subprocess | |
import json | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKCYAN = '\033[96m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
gum_file = ".gum.json" | |
def add(namespace, name): | |
if not os.path.isdir(f"./{name}"): | |
subprocess.run(["git", "clone", f"{url}.git", name]) | |
subprocess.call(["git", "add", f"./{name}/"]) | |
latest_version = subprocess.getoutput(f"cd {name} && git rev-parse --short HEAD").split()[0] | |
update_gumfile(namespace, name, latest_version) | |
subprocess.call(["git", "add", f"{gum_file}"]) | |
subprocess.call(["git", "commit", "-m", f"added dep {name}"]) | |
else: | |
print(exists_warning(f"@{name}")) | |
def exists_warning(name): | |
return f"{bcolors.FAIL}'NOTICE: {name} is already in the dependency list\ntry using the update command instead." | |
def not_exists_warning(name): | |
return f"{bcolors.FAIL}'NOTICE: {name} is not installed\ntry installing it first." | |
def update_deps(data, existing, newdep, override=False): | |
if not override: | |
existing.update(newdep) | |
data["dependencies"] = existing | |
# save | |
with open(gum_file, "w") as outfile: | |
outfile.write(json.dumps(data)) | |
def update_gumfile(namespace, name, version, action="added"): | |
dep_name = f"{namespace}/{dest_name}" | |
dep = { | |
f"{dep_name}": { | |
"url": url, | |
"version": f"#{version}" | |
} | |
} | |
if not os.path.isfile(gum_file): | |
data ={ | |
"dependencies": dep | |
} | |
# save | |
with open(gum_file, "w") as outfile: | |
outfile.write(json.dumps(data)) | |
else: | |
# parse and add | |
with open(gum_file, "r") as openfile: | |
gum_data = json.load(openfile) | |
existing_deps = gum_data["dependencies"] | |
at_dep = f"@{dep_name}" | |
if at_dep in existing_deps: | |
curr_dep = existing_deps[at_dep] | |
if curr_dep["version"] == f"#{version}": | |
if action == "added": | |
print(exists_warning(f"@{dep_name}#{version}")) | |
else: | |
print((f"{bcolors.WARNING}nothing to update for @{dep_name}#{version}")) | |
sys.exit() | |
else: | |
if action == "added": | |
add(namespace, name) | |
subprocess.call(["git", add, f"{gum_file}"]) | |
subprocess.call(["git", "commit", "-m", f"added dep {dest_name}"]) | |
if action == "synched": | |
existing_deps[at_dep] = dep[dep_name] | |
update_deps(gum_data, existing_deps, dep, override=True) | |
else: | |
update_deps(gum_data, existing_deps, dep) | |
if __name__ == "__main__": | |
action = sys.argv[1] | |
url = f"https://github.com/{sys.argv[2]}" | |
# init repo if needed | |
# -- | |
if not os.path.isdir(".git"): | |
subprocess.run(["git", "init"]) | |
# Install | |
# -- | |
if action == 'install': | |
try: | |
dest_name = sys.argv[3] | |
try: | |
dest_namespace = dest_name.split('/')[-2] | |
except: | |
dest_namespace = "" | |
except: | |
dest_name = url.split('/')[-1] | |
try: | |
dest_namespace = f"@{url.split('/')[-2]}" | |
except: | |
dest_namespace = "" | |
add(dest_namespace, dest_name) | |
# Update | |
# -- | |
if action == 'update': | |
try: | |
name_arg = sys.argv[2] | |
except: | |
print(f"please provide a package name") | |
try: | |
dest_name = name_arg.split('/')[-1] | |
dest_namespace = name_arg.split('/')[-2] | |
except: | |
print(f"please provide a package name in the form of namespace/package") | |
if os.path.isdir(f"./{dest_name}"): | |
subprocess.call(["cd", f"./{dest_name}", "&&", "git", "pull"]) | |
latest_version = subprocess.getoutput(f"cd {dest_name} && git rev-parse --short HEAD").split()[0] | |
update_gumfile(dest_namespace, dest_name, latest_version, "updated") | |
else: | |
print(not_exists_warning(f@"{dest_namespace}/{dest_name}")) | |
# Sync | |
# -- | |
if action == 'sync': | |
try: | |
name_arg = sys.argv[2] | |
except: | |
print(f"please provide a package name") | |
try: | |
dest_name = name_arg.split('/')[-1] | |
dest_namespace = name_arg.split('/')[-2] | |
except: | |
print(f"please provide a package name in the form of namespace/package") | |
if os.path.isdir(f"./{dest_name}"): | |
latest_version = subprocess.getoutput(f"cd {dest_name} && git rev-parse --short HEAD").split()[0] | |
update_gumfile(dest_namespace, dest_name, latest_version, "synched") | |
else: | |
print(not_exists_warning(f@"{dest_namespace}/{dest_name}")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment