Преглед изворни кода

Removed git logic from the code, replaced git ls-files with python function. Included possibility to exclude certain files from indexing during read_dir.

db.py - removed git logic, fixed minor formating issues

build_image_cache.py - removed git logic

read_settings.py - removed git logic

parser.py - removed git logic, added function to return all the files with data

git.py - removed as obsolete
slow_tiger пре 1 година
родитељ
комит
b16f22391f
5 измењених фајлова са 39 додато и 187 уклоњено
  1. 11 25
      app/db.py
  2. 0 136
      app/git.py
  3. 23 9
      app/parser.py
  4. 1 11
      app/read_settings.py
  5. 4 6
      build_image_cache.py

+ 11 - 25
app/db.py

@@ -8,7 +8,6 @@ import yaml
 from openpyxl import load_workbook
 from openpyxl import load_workbook
 from pydantic import ValidationError
 from pydantic import ValidationError
 
 
-from app.git import git_add_commit
 from app.parser import read_dir, read_file, parse_file
 from app.parser import read_dir, read_file, parse_file
 from app.schema import (
 from app.schema import (
     CartOrderInfo,
     CartOrderInfo,
@@ -106,10 +105,6 @@ def update_product_inventory(
         with open(p, "w") as f:
         with open(p, "w") as f:
             f.write(txt_doc)
             f.write(txt_doc)
 
 
-        # update git repo
-        commit_msg = "Update product inventory"
-        git_add_commit(settings.git_repo, filepath, commit_msg)
-
 
 
 def create_variation_id(product: DocumentProduct):
 def create_variation_id(product: DocumentProduct):
     """
     """
@@ -164,6 +159,16 @@ def write_product_id_db(product:DocumentProduct, settings, products_data = {}):
             yaml.dump(products_data, f, default_flow_style = False)
             yaml.dump(products_data, f, default_flow_style = False)
 
 
 
 
+def get_variation_id(product:DocumentProduct, size: str | None, style: str | None) -> str | None:
+    """Returns variation ID for product that has size or style"""
+    if size or style:
+        for variant in product.meta.inventory:
+            if variant.size == size and variant.style == style:
+                return variant.product_variation_id
+
+    return None
+
+
 def write_variation_id(product:DocumentProduct, settings):
 def write_variation_id(product:DocumentProduct, settings):
     """ Generate and write IDs for variation of the product into file"""
     """ Generate and write IDs for variation of the product into file"""
 
 
@@ -189,16 +194,6 @@ def write_variation_id(product:DocumentProduct, settings):
     #    git_add_commit(settings.git_repo, filepath, commit_msg)
     #    git_add_commit(settings.git_repo, filepath, commit_msg)
 
 
 
 
-def get_variation_id(product:DocumentProduct, size: str | None, style: str | None) -> str | None:
-    """Returns variation ID for product that has size or style"""
-    if size or style:
-        for variant in product.meta.inventory:
-            if variant.size == size and variant.style == style:
-                return variant.product_variation_id
-
-    return None
-
-
 def get_products_by_category(settings):
 def get_products_by_category(settings):
     """Helper function to product a list of products organized by their categories."""
     """Helper function to product a list of products organized by their categories."""
     products = read_dir(
     products = read_dir(
@@ -236,6 +231,7 @@ def get_product_by_product_id(
     else:
     else:
         return None
         return None
 
 
+
 def get_docs_by_category(category: str, settings):
 def get_docs_by_category(category: str, settings):
     """Read a directory of directories and return a sublist with only the
     """Read a directory of directories and return a sublist with only the
     documents matching the given category.
     documents matching the given category.
@@ -380,8 +376,6 @@ def save_shipping_info(order: dict[str], git_repo: str, filepath: str) -> NoRetu
     with open(p, "a") as f:
     with open(p, "a") as f:
         f.write(order["data"])
         f.write(order["data"])
 
 
-    git_add_commit(git_repo, filepath, order["commit_msg"])
-
 
 
 def update_order_info(
 def update_order_info(
     order_id: str, order_status: str, git_repo: str, filepath: str
     order_id: str, order_status: str, git_repo: str, filepath: str
@@ -468,10 +462,6 @@ def update_status_order(
         client_reference_id, order_status, git_repo, local_db_filepath
         client_reference_id, order_status, git_repo, local_db_filepath
     )
     )
 
 
-    if status_res:
-        commit_msg = "Update order status"
-        git_add_commit(git_repo, local_db_filepath, commit_msg)
-
     return status_res
     return status_res
 
 
 
 
@@ -652,7 +642,3 @@ def export_order_to_xlsx(
     timestamp = arrow.get(order.date).format("YYYY-MM-DD-HHmmss")
     timestamp = arrow.get(order.date).format("YYYY-MM-DD-HHmmss")
     filepath = f"{git_repo}/orders/upload/{timestamp}--{order.order_id}.xlsx"
     filepath = f"{git_repo}/orders/upload/{timestamp}--{order.order_id}.xlsx"
     workbook.save(filename=filepath)
     workbook.save(filename=filepath)
-
-    # -- git add commit newly created file
-    commit_msg = f"Export order {order.order_id} to .xlsx format"
-    git_add_commit(git_repo, filepath, commit_msg)

+ 0 - 136
app/git.py

@@ -1,136 +0,0 @@
-import logging
-import subprocess
-import sys
-from pathlib import Path
-from typing import NoReturn
-
-logger = logging.getLogger(__name__)
-
-GIT_PATH = None
-
-try:
-    GIT_PATH = subprocess.check_output(["/usr/bin/which", "git"], text=True).strip()
-
-except subprocess.CalledProcessError:
-    # no git found in the system.
-    # stop everything and exit. print a user-facing
-    # message asking to install git.
-    logger.error(
-        "search_file_content error =>  git is not installed",
-    )
-    sys.exit(1)
-
-
-def is_git_repo(repo: str) -> bool:
-    """Spawn a shell and use git binary to test if current path is under git."""
-
-    def check_if_valid_repo(repo: str) -> bool:
-        is_repo = subprocess.check_output(
-            [GIT_PATH, "rev-parse", "--is-inside-work-tree"], text=True, cwd=repo
-        )
-
-        is_repo = is_repo.strip()
-
-        if is_repo == "true":
-            return True
-        elif is_repo == "false":
-            return False
-        else:
-            logger.error(f"is-git-repo error => {is_repo} not a valid path")
-            return False
-
-    repo = Path(repo)
-    if repo.exists():
-        if repo.is_dir():
-            return check_if_valid_repo(repo)
-
-        else:
-            logger.error(f"is-git-repo error => {repo} is not a directory")
-            repo = repo.parent
-
-            return check_if_valid_repo(repo)
-    else:
-        logger.error(f"is-git-repo error => {repo} is an invalid path")
-        return False
-
-
-def git_ls_files(
-    repo: str, pattern_matching: list[str], fullpath: bool = True
-) -> list[Path]:
-    """Return list of git-tracked filepaths from given repository path."""
-    filepaths = subprocess.check_output(
-        [GIT_PATH, "ls-files", "--", *pattern_matching], text=True, cwd=repo
-    )
-
-    filepaths = filepaths.strip().splitlines()
-
-    if fullpath:
-        filepaths = [Path(repo / filepath) for filepath in filepaths]
-
-    return filepaths
-
-
-def git_is_file_tracked(repo: str, filepath: str) -> bool:
-    """Check if given filepath is found by git ls-files -- <filepath> command."""
-    matched = subprocess.check_output(
-        [GIT_PATH, "ls-files", "--", filepath], text=True, cwd=repo
-    )
-
-    is_tracked = True if matched.strip() == filepath else False
-
-    return is_tracked
-
-
-def git_add_commit(repo: str, filepath: str, msg: str) -> NoReturn:
-    """Spawn a shell and use git binary to perform `git add` and `git commit`
-    on given <filepath>, and with specificed commit <msg>.
-    """
-    logger.info(f"git-add-commit... {repo, filepath, msg}")
-
-    r = subprocess.run(
-        f"{GIT_PATH} add '{filepath}' && {GIT_PATH} commit '{filepath}' -m '{msg}'",
-        shell=True,
-        cwd=repo,
-    )
-
-    logger.info(f"git-add-commit result => {r.returncode, r.stdout, r.stderr}")
-
-
-def git_log_last_commit(repo: str):
-    """Spawn a shell and run git binary to get the last commit from the
-    git log of the given <repo>.
-    """
-    filepaths = subprocess.check_output(
-        [GIT_PATH, "log", "-1", "--pretty=format:%cd"], text=True, cwd=repo
-    )
-
-    filepaths = filepaths.strip().splitlines()
-    filepaths = [Path(repo / filepath) for filepath in filepaths]
-
-    return filepaths
-
-
-def git_get_list_of_changed_filepaths_in_last_commit(
-    repo: str, oldrev: str, newrev: str, pattern: list[str]
-) -> tuple[str, str]:
-    """Return list of git modified filepaths, filtered by image type.
-
-    git diff --name-only <oldrev>..<newrev>
-    """
-    filepaths = subprocess.check_output(
-        [
-            GIT_PATH,
-            "diff",
-            "--name-only",
-            oldrev,
-            newrev,
-        ],
-        text=True,
-        cwd=repo,
-    )
-
-    filepaths = filepaths.strip().splitlines()
-
-    filepaths = [filepath for filepath in filepaths if Path(filepath).suffix in pattern]
-
-    return filepaths

+ 23 - 9
app/parser.py

@@ -9,7 +9,6 @@ from markdown_it import MarkdownIt
 from mdit_py_plugins.anchors import anchors_plugin as section_header
 from mdit_py_plugins.anchors import anchors_plugin as section_header
 from pydantic import ValidationError
 from pydantic import ValidationError
 
 
-from app.git import git_is_file_tracked, git_ls_files
 from app.schema import (
 from app.schema import (
     DocumentCheckout,
     DocumentCheckout,
     DocumentEmail,
     DocumentEmail,
@@ -35,6 +34,21 @@ def md(text: str) -> str:
     return md.render(text)
     return md.render(text)
 
 
 
 
+def get_files_by_extension(folder: Path, extensions: list[str], ignore_list: list = [], relative_path: bool = False) -> list[Path]:
+    """Getting list of files specified by extensions"""
+
+    file_list = []
+
+    for extension in extensions:
+        file_list.extend([f for f in list(folder.rglob(f'{extension}')) if f not in ignore_list])
+
+    if relative_path:
+        for i in range(len(file_list)):
+            file_list[i] = file_list[i].relative_to(folder)
+
+    return file_list
+
+
 def set_default_product_meta_fields(block) -> dict[str, str]:
 def set_default_product_meta_fields(block) -> dict[str, str]:
     """
     """
     Helper function to make sure all necessary fields expected by the
     Helper function to make sure all necessary fields expected by the
@@ -277,16 +291,17 @@ def read_dir(
     exclude_doc: str | None = None,
     exclude_doc: str | None = None,
     tree: str | None = None,
     tree: str | None = None,
 ):
 ):
-    """Run git ls-files with appropriate pattern matching to return list
-    of desired git-tracked filepaths.
-    """
+    """Run get_files_by_extension with appropriate pattern matching to return list"""
     d = Path(repo_path).expanduser()
     d = Path(repo_path).expanduser()
 
 
     pattern_matching = []
     pattern_matching = []
     [pattern_matching.append(f"*{ext}") for ext in document_match]
     [pattern_matching.append(f"*{ext}") for ext in document_match]
-    [pattern_matching.append(f":!:{ext}") for ext in document_exclude]
 
 
-    paths = git_ls_files(d, pattern_matching)
+    ignore_list = []
+    for file in document_exclude:
+        ignore_list.append(Path(f"{repo_path}/{file}"))
+
+    paths = get_files_by_extension(d, pattern_matching, ignore_list)
     paths = [p for p in paths if tree in str(p)]
     paths = [p for p in paths if tree in str(p)]
 
 
     docs = []
     docs = []
@@ -333,9 +348,8 @@ def read_file(
         # get file path relative to the content repo
         # get file path relative to the content repo
         # eg <path/to/repo/dir/file> => <dir>/<file>
         # eg <path/to/repo/dir/file> => <dir>/<file>
         filename = str(Path(filepath).relative_to(repo_path))
         filename = str(Path(filepath).relative_to(repo_path))
-        if git_is_file_tracked(repo_path, filename):
-            doc = parse_file(repo_path, Path(filepath), block_types)
-            return doc
+        doc = parse_file(repo_path, Path(filepath), block_types)
+        return doc
 
 
     else:
     else:
         raise HTTPException(status_code=404, detail="Nothing was found here.")
         raise HTTPException(status_code=404, detail="Nothing was found here.")

+ 1 - 11
app/read_settings.py

@@ -1,29 +1,19 @@
 import logging
 import logging
 import sys
 import sys
-from pathlib import Path
 from typing import NoReturn
 from typing import NoReturn
 
 
 import tomli
 import tomli
 
 
-from app.git import is_git_repo
 from app.schema import Settings
 from app.schema import Settings
 
 
 logger = logging.getLogger(__name__)
 logger = logging.getLogger(__name__)
 
 
 
 
-def read_settings(filepath: str, check_valid_repo: bool = True) -> Settings | NoReturn:
+def read_settings(filepath: str) -> Settings | NoReturn:
     """Read settings.toml from the root of the project folder"""
     """Read settings.toml from the root of the project folder"""
     try:
     try:
         with open(filepath, mode="rb") as f:
         with open(filepath, mode="rb") as f:
             config = tomli.load(f)
             config = tomli.load(f)
-
-            if check_valid_repo:
-                git_repo = str(Path(config["git_repo"]).expanduser())
-                if not is_git_repo(str(git_repo)):
-                    raise FileNotFoundError("git-repo has not been found")
-
-                config["git_repo"] = git_repo
-
             settings = Settings(**config)
             settings = Settings(**config)
             return settings
             return settings
 
 

+ 4 - 6
build_image_cache.py

@@ -10,7 +10,7 @@ from pathlib import Path
 import cv2
 import cv2
 import typer
 import typer
 
 
-from app.git import git_get_list_of_changed_filepaths_in_last_commit, git_ls_files
+from app.parser import get_files_by_extension
 from app.read_settings import read_settings
 from app.read_settings import read_settings
 
 
 logger = logging.getLogger(__name__)
 logger = logging.getLogger(__name__)
@@ -24,7 +24,7 @@ app = typer.Typer()
 # we're currently in there, though empirical testing shows
 # we're currently in there, though empirical testing shows
 # doign Path.cwd() returns git repo path
 # doign Path.cwd() returns git repo path
 cwd = Path(__file__).parent
 cwd = Path(__file__).parent
-settings = read_settings(f"{cwd}/settings.toml", False)
+settings = read_settings(f"{cwd}/settings.toml")
 git_repo = Path(settings.git_repo).expanduser()
 git_repo = Path(settings.git_repo).expanduser()
 
 
 IMAGE_CACHE_PATH = f"{cwd}/static/images/cache"
 IMAGE_CACHE_PATH = f"{cwd}/static/images/cache"
@@ -141,7 +141,7 @@ def build_all_images(repo: str):
     repo = Path(repo).expanduser()
     repo = Path(repo).expanduser()
 
 
     pattern_matching = ["*.png", "*.jpg"]
     pattern_matching = ["*.png", "*.jpg"]
-    filepaths = git_ls_files(repo, pattern_matching, False)
+    filepaths = get_files_by_extension(repo, pattern_matching, True)
 
 
     images = [
     images = [
         (str(Path(filepath).parent).split("/")[0], filepath) for filepath in filepaths
         (str(Path(filepath).parent).split("/")[0], filepath) for filepath in filepaths
@@ -167,9 +167,7 @@ def build_images(repo: str, oldrev: str, newrev: str):
     repo = Path(repo).expanduser()
     repo = Path(repo).expanduser()
 
 
     pattern_matching = [".png", ".jpg"]
     pattern_matching = [".png", ".jpg"]
-    filepaths = git_get_list_of_changed_filepaths_in_last_commit(
-        repo, oldrev, newrev, pattern_matching
-    )
+    filepaths = get_files_by_extension(repo, pattern_matching, True)
 
 
     images = [
     images = [
         (str(Path(filepath).parent).split("/")[0], filepath) for filepath in filepaths
         (str(Path(filepath).parent).split("/")[0], filepath) for filepath in filepaths