Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SeqeraPlatform): seqerakit can write JSON to stdout for detailed records #146

Merged
merged 7 commits into from
Oct 23, 2024

Conversation

adamrtalbot
Copy link
Collaborator

@adamrtalbot adamrtalbot commented May 1, 2024

We needed a method of storing run info from the seqerakit CLI. After this change, you can use the '--json' flag to write the JSON return from the CLI to STDOUT. This can be captured and used in a different process or piped. This is enabled by a new command line flag, --json but should be possible when instantiating the SeqeraPlatform class in Python. The to_json kwarg is removed and replaced with this method so this might break some uses. The JSON is an attribute of the class and can be referenced whenever you use the SeqeraPlatform class.

BREAKING CHANGE: This could break anyone using the Python implementation with to_json=True. This is mainly me.

To use, try running seqerakit with the flag -j and piping:

seqerakit -j launch/*.yml | jq --slurp > launched-pipelines.json

(update): Because the function is overlapping with to_json, we just use either will create JSON output. If to_json is used it will print to stderr, if -j is used it will write to stdout suitable for piping.

… logs

We needed a method of storing run info from the seqerakit CLI. After this change, you can use the '--json' flag to write the JSON return from the CLI to STDOUT. This can be captured and used in a different process or piped. This is enabled by a new command line flag, --json but should be possible when instantiating the SeqeraPlatform class in Python. The to_json kwarg is removed and replaced with this method so this might break some uses. The JSON is an attribute of the class and can be referenced whenever you use the SeqeraPlatform class.

BREAKING CHANGE:
@adamrtalbot adamrtalbot marked this pull request as draft May 1, 2024 17:14
@adamrtalbot
Copy link
Collaborator Author

Appears to double up the -o json part sometimes:

ERROR:root:Resource creation failed: 'option '--output' (<output>) should be specified only once`

@ejseqera
Copy link
Member

@adamrtalbot I like this implementation, it might break things but it's a feature worth adding. What's left to do?

@adamrtalbot
Copy link
Collaborator Author

@adamrtalbot I like this implementation, it might break things but it's a feature worth adding. What's left to do?

Fix the doubling of --output json twice is the main issue.

Ideally, we might cache the output and write a valid JSON as a list although piping to jq seems acceptable for a first draft.

@adamrtalbot adamrtalbot marked this pull request as ready for review August 29, 2024 08:21
@adamrtalbot
Copy link
Collaborator Author

Trying to finish this off and I'm confused about how I should handle the to_json flag in sp._execute_command():

# Executes a 'tw' command in a subprocess and returns the output.
def _execute_command(self, full_cmd, to_json=False, print_stdout=True):
logging.info(f" Running command: {full_cmd}")
process = subprocess.Popen(
full_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True
)
stdout, _ = process.communicate()
stdout = stdout.decode("utf-8").strip()
should_print = (
print_stdout if print_stdout is not None else self.print_stdout
) and not self._suppress_output
if should_print:
logging.info(f" Command output: {stdout}")
if "ERROR: " in stdout or process.returncode != 0:
self._handle_command_errors(stdout)
if self.json:
out = json.loads(stdout)
print(json.dumps(out))
else:
out = stdout
print(stdout)

It seems like these have overlapping purposes so I've added an or statement which fixes the tests: 1131da8, but not sure if this is the right thing to do.

@adamrtalbot adamrtalbot requested a review from ejseqera August 29, 2024 08:52
@adamrtalbot
Copy link
Collaborator Author

Yo @ejseqera this one is good to go and I think it would be useful with our current projects. Let me know what else it needs.

Copy link
Member

@ejseqera ejseqera left a comment

Choose a reason for hiding this comment

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

Thanks Adam 🙏

@ejseqera ejseqera merged commit 427132f into dev Oct 23, 2024
2 checks passed
@ejseqera ejseqera deleted the json_output_to_stdout branch October 23, 2024 02:02
@adamrtalbot
Copy link
Collaborator Author

Ah heck, this still doubles up if you use overwrite: true:

INFO:root: Running command: tw -o json -o json pipelines list -w $ORGANIZATION_NAME/$WORKSPACE_NAME
ERROR:root:Resource creation failed: 'option '--output' (<output>) should be specified only once

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants