Skip to content

Commit

Permalink
Enhance carbonTracker CLI to support arbitrary commands
Browse files Browse the repository at this point in the history
  • Loading branch information
PedramBakh committed Sep 13, 2023
1 parent 6580e10 commit bc962f4
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions carbontracker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

def main():
parser = argparse.ArgumentParser(description="CarbonTracker CLI")
parser.add_argument("--script", type=str, required=True, help="Python file to execute")

# Accept a list of arguments
parser.add_argument("command", type=str, nargs='+',
help="Command and arguments to execute. E.g., 'python myscript.py arg1 arg2'")
parser.add_argument("--log_dir", type=str, help="Log directory", default="./logs")
parser.add_argument("--api_keys", type=str, help="API keys in a dictionary-like format, e.g., "
"'{\"electricitymaps\": \"YOUR_KEY\"}'", default=None)
Expand All @@ -18,11 +21,11 @@ def main():
tracker = CarbonTracker(epochs=1, log_dir=args.log_dir, epochs_before_pred=0, api_keys=api_keys)
tracker.epoch_start()

# Execute script with python
# Execute the provided command with its arguments
try:
subprocess.run(["python", args.script], check=True)
subprocess.run(args.command, check=True)
except subprocess.CalledProcessError:
print(f"Error executing script: {args.script}")
print(f"Error executing command: {' '.join(args.command)}")
# Handle errors or exceptions if needed

tracker.epoch_end()
Expand Down

0 comments on commit bc962f4

Please sign in to comment.