Noah Vogt 3 vuotta sitten
sitoutus
75e13d5213
6 muutettua tiedostoa jossa 124 lisäystä ja 0 poistoa
  1. 1 0
      .gitignore
  2. 13 0
      README.md
  3. 5 0
      pyright.json
  4. 65 0
      src/config.py
  5. 13 0
      src/log.py
  6. 27 0
      src/shovel

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*__pycache__*

+ 13 - 0
README.md

@@ -0,0 +1,13 @@
+# shovel
+
+This is a program that helps me with 
+
+- maintaining 
+- forking
+- automated building
+- automated testing
+- automated packaging
+- automatically applying patches
+- repository syncing and management
+
+and possibly much more in the future. Shovel is heavily customized and tailored to my specific use case. There are seemingly millions of CI/CD Software Solutions already out there, but this will suit my needs just a little better, I think.

+ 5 - 0
pyright.json

@@ -0,0 +1,5 @@
+{
+  "include": [
+    "src"
+  ]
+}

+ 65 - 0
src/config.py

@@ -0,0 +1,65 @@
+import os
+import log
+
+def get_xdg_dir(env_var: str, fallback_dir: str, home: str):
+    xdg_dir = os.getenv(env_var)
+    if xdg_dir is None or xdg_dir == "":
+        xdg_dir = "{}/{}".format(home, fallback_dir)
+
+    return xdg_dir
+
+
+def get_configured_dirs():
+    log.msg("checking configured directories")
+    home = os.getenv("HOME")
+    if home is None or home == "":
+        log.error_exit("$HOME not accessible")
+
+    xdg_data_home = get_xdg_dir("XDG_DATA_HOME", ".local/share", home)
+    xdg_config_home = get_xdg_dir("XDG_CONFIG_HOME", ".config", home)
+    xdg_cache_home = get_xdg_dir("XDG_CACHE_HOME", ".cache", home)
+
+    return {"src_dir" : "{}/shovel/src".format(xdg_data_home),
+            "build_dir" : "{}/shovel/build".format(xdg_data_home),
+            "config_dir" : "{}/shovel".format(xdg_config_home),
+            "cache_dir" : "{}/shovel".format(xdg_cache_home),
+            "rule_dir" : "{}/shovel/rules".format(xdg_data_home)}
+
+
+def check_for_write_permissions(config_dirs: dict):
+    for path in config_dirs:
+        if not os.access(config_dirs[path], os.R_OK):
+            log.error_exit("cannot access directory '{}', please check".format(
+                           config_dirs[path]) + "your permissions")
+
+
+def get_rules(rule_dir):
+    log.msg("getting rules")
+    rules = []
+    for (_, subdirs, filenames) in os.walk(rule_dir):
+        if len(subdirs) > 0:
+            log.msg("warning: subdirectories in '{}' are ignored:".format(
+                    rule_dir), "warning")
+            for subdir in subdirs:
+                print(subdir)
+        rules.extend(filenames)
+        break
+
+    return rules
+
+
+class Config:
+    def __init__(self):
+        self.config_dirs = get_configured_dirs()
+        self.rules = get_rules(self.config_dirs.get("rule_dir"))
+        log.msg("configuration initialized", "success")
+
+    def parse(self):
+        #parse_config()
+        check_for_write_permissions(self.config_dirs)
+
+
+    def update(self):
+        self.config_dirs = get_configured_dirs()
+        self.rules = get_rules(self.config_dirs.get("rule_dir"))
+        log.msg("updated rules and directory configurations", "success")

+ 13 - 0
src/log.py

@@ -0,0 +1,13 @@
+import sys
+
+from termcolor import colored
+
+def error_exit(message: str, exit_code=1):
+    print(colored("[*] Error: {}".format(message), "red"))
+    sys.exit(exit_code)
+
+
+def msg(message: str, severity="normal"):
+    severity_colors = {"normal": "white", "success": "green", "error": "red",
+                       "action": "cyan", "warning": "yellow"}
+    print(colored("[*] {}".format(message), severity_colors.get(severity)))

+ 27 - 0
src/shovel

@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+from enum import Enum
+
+import typer
+
+import config
+import log
+
+main = typer.Typer(add_completion=False)
+
+class UserMode(str, Enum):
+    CURRENT = 'current',
+    ROOT = 'root',
+    SHOVEL = 'shovel'
+
+
+@main.command()
+def default_run(user_mode: UserMode = typer.Option('current', "--user", "-u",
+                help='change user mode')):
+    prgm_config = config.Config()
+    prgm_config.parse()
+
+    log.msg("nothing to do, exiting...")
+
+if __name__ == '__main__':
+    main()