Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/hermes/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ def init_common_parser(self, parser: argparse.ArgumentParser) -> None:
type=pathlib.Path,
help="Configuration file in TOML format",
)
# Add a new argument to accept a URL for harvesting (in harvest command)
parser.add_argument(
"--url",
type=str,
help="URL from which to extract metadata (GitHub or GitLab))"
)
# Add a new argument to accept a token (from GitHub or GitLab) for harvesting (in harvest command)
parser.add_argument(
"--token",
type=str,
required=False,
help="Access token for GitHub/GitLab (optional, only needed for private repos or GitHub/GitLab API plugin)"
)

plugin_args = parser.add_argument_group("Extra options")
plugin_args.add_argument(
Expand Down
2 changes: 2 additions & 0 deletions src/hermes/commands/clean/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import argparse
import shutil
import logging
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aidajafarbigloo Is it necessary for your changes to include logging? If not, please remove it everywhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sferenz Thank you for the comments.
When using hermes clean command on Windows, I face this error:
Run subcommand clean
Removing HERMES caches...
An error occurred during execution of clean (Find details in './hermes.log')

Error in the "hermes.log":
Original exception was: [WinError 32] The process cannot access the file because it is being used by another process: '.hermes\\audit.log'.

This happens because Windows does not allow deletion of a file that is still open by the current process. The audit.log file inside .hermes is held open by a logging file handler. When shutil.rmtree() attempts to remove the directory, it fails due to the open file handle. I'm using logging.shutdown() to ensure that all logging handlers are closed before the directory is deleted, it does not introduce new logging behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was fixed before but somehow the fix got lost... Weird 🤔

#226

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, not only once. But it seems more important to not have a log file in the working directory than having a properly cleaned .hermes cache.

you should be able to configure the path to the logfile, though.


from pydantic import BaseModel

Expand All @@ -25,6 +26,7 @@ class HermesCleanCommand(HermesCommand):

def __call__(self, args: argparse.Namespace) -> None:
self.log.info("Removing HERMES caches...")
logging.shutdown()

# Naive implementation for now... check errors, validate directory, don't construct the path ourselves, etc.
shutil.rmtree(args.path / '.hermes')
Expand Down
30 changes: 29 additions & 1 deletion src/hermes/commands/harvest/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import argparse
import typing as t
from datetime import datetime
import tempfile
import pathlib
from hermes import logger

from pydantic import BaseModel

from hermes.commands.base import HermesCommand, HermesPlugin
from hermes.model.context import HermesContext, HermesHarvestContext
from hermes.model.errors import HermesValidationError, MergeError

from hermes.commands.harvest.util.token import update_token_to_toml, remove_token_from_toml
from hermes.commands.harvest.util.clone import clone_repository

class HermesHarvestPlugin(HermesPlugin):
"""Base plugin that does harvesting.
Expand Down Expand Up @@ -44,6 +48,30 @@ def __call__(self, args: argparse.Namespace) -> None:
# Initialize the harvest cache directory here to indicate the step ran
ctx.init_cache("harvest")

logger.init_logging()
log = logger.getLogger("hermes.cli")

if args.url:
with tempfile.TemporaryDirectory(dir=".") as temp_dir:
temp_path = pathlib.Path(temp_dir)
log.info(f"Cloning repository {args.url} into {temp_path}")

try:
clone_repository(args.url, temp_path, recursive=True, depth=1, filter_blobs=True, sparse=False, verbose=True)
except Exception as exc:
print("ERROR:", exc)
args.path = temp_path # Overwrite args.path to temp directory

if args.token:
update_token_to_toml(args.token)
self._harvest(ctx)
if args.token:
remove_token_from_toml('hermes.toml')
else:
self._harvest(ctx)

def _harvest(self, ctx: HermesContext) -> None:
"""Harvest metadata from configured sources using plugins."""
for plugin_name in self.settings.sources:
try:
plugin_func = self.plugins[plugin_name]()
Expand Down
Loading