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

Adds support for 'log_file' in hook config #530

Merged
merged 1 commit into from May 8, 2017
Merged

Adds support for 'log_file' in hook config #530

merged 1 commit into from May 8, 2017

Conversation

alex-hutton
Copy link

Specify a filename on a per hook basis and
pre-commit will write the STDOUT and STDERR
of that hook into the file. Useful for CI.

Resolves #499.

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

@@ -121,7 +121,8 @@ def _run_single_hook(hook, repo, args, skips, cols):
for out in (stdout, stderr):
assert type(out) is bytes, type(out)
if out.strip():
output.write_line(out.strip())
output.write_line(out.strip(),
logfile_name=hook.get('log_file'))
Copy link
Member

Choose a reason for hiding this comment

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


with ExitStack() as stack:
try:
logfile = stack.enter_context(open(logfile_name, 'wb'))
Copy link
Member

Choose a reason for hiding this comment

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

this is going to clobber the contents of the file on each write (probably not what you want?)

setup.py Outdated
@@ -41,6 +41,7 @@
install_requires=[
'aspy.yaml',
'cached-property',
'contextlib2',
Copy link
Member

Choose a reason for hiding this comment

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

I actually run a youtube channel and my next episode in "porting to python 3" is about proper use of backports. Haven't written up the slides yet or I'd link them but for backported modules you only want to conditionally install them on the versions that need them (otherwise we're installing contextlib2 in python3 where it's entirely unnecessary).

Here's the basic gist though: https://github.com/pre-commit/pre-commit/blob/v0.8.2/setup.py#L50-L52

try:
from contextlib import ExitStack
except ImportError:
from contextlib2 import ExitStack
Copy link
Member

Choose a reason for hiding this comment

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

when possible, prefer putting conditional imports after all the other imports (the import reorderer stops when it sees non-import code)

raise TypeError()
return mock.DEFAULT

with mock.patch('pre_commit.output.open', mock.mock_open()) as mocked_open:
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather see a real integration test which the file is actually written -- especially since the code as written doesn't properly write the file -- There's fixtures in testing/fixtures for setting up a git repository (and fixtures in testing/conftest.py as well)

@alex-hutton
Copy link
Author

Thanks for the helpful feedback. Please take a look at the changes.

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

Looks good! Address my question and fix the two nitpicks and I'll merge. Thanks for an awesome PR :D

Oh! I just thought of this, you should add the new field to the schemas -- these are located in pre_commit/clientlib.py

stream.write(b'\n')
stream.flush()
def write_line(s=None, stream=stdout_byte_stream, logfile_name=None):
output_streams = [stream, ]
Copy link
Member

Choose a reason for hiding this comment

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

just write [stream]

setup.py Outdated
@@ -46,6 +46,9 @@
'six',
'virtualenv',
],
extras_require={
':python_version<"3.0"': ['contextlib2', ],
Copy link
Member

Choose a reason for hiding this comment

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

just write ['contextlib2']

logfile = stack.enter_context(open(logfile_name, 'ab'))
output_streams.append(logfile)
except (TypeError, IOError):
pass
Copy link
Member

Choose a reason for hiding this comment

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

did you try the noop ctx idea from the first review? what did you think?

Copy link
Member

Choose a reason for hiding this comment

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

Oh wut, my original comment never posted! lol

Here's an idea that avoids needing contextlib2 (we use this pattern elsewhere in pre-commit too!)

if log_file:
    ctx = open(...)
else:
    ctx = noop_ctx()
with ctx:
    ...

Specify a filename on a per hook basis and
pre-commit will write the STDOUT and STDERR
of that hook into the file. Useful for CI.

Resolves #499.
@alex-hutton
Copy link
Author

Here's a way of doing it without contextlib2.

@asottile asottile merged commit 71e500a into pre-commit:master May 8, 2017
@asottile
Copy link
Member

asottile commented May 8, 2017

Thanks! I'll touch this up and put it into the next release :)

@alex-hutton
Copy link
Author

Thanks Anthony. And thanks for the awesome software!

I received a github comment as an email but I don't see it in github itself:

One thought: I think it only makes sense to set this in the consuming repository (and not the remote hook definition) -- would you be ok with me moving the configuration setting into the pre-commit config instead?

My answer to that is, sure, go ahead 👍 . I'm not sure what you are referring to, though. Did I put the schema definition in the wrong place?

@asottile
Copy link
Member

asottile commented May 9, 2017

Ah yeah I deleted the comment as I'll handle it better at a later date. Sorry for the email spam!

droctothorpe pushed a commit to droctothorpe/pre-commit that referenced this pull request Mar 23, 2022
Add --additional-domain to check-vcs-permalinks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants