-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
fix file preambles for Python scripts #357
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com> | ||
# | ||
# This code is distributed under the terms and conditions | ||
# from the MIT License (MIT). | ||
# | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line instead of blank comment, to separate the blocks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to keep the entire preamble as a single comment. If you use blank lines to separate comment blanks, identifying preambles in files (for scripting, etc.) becomes more difficult. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not separate comment blocks – it separates preamble from actual code (imports). |
||
import sys | ||
import time | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com> | ||
# | ||
# This code is distributed under the terms and conditions | ||
# from the MIT License (MIT). | ||
# | ||
|
||
"""Checks preambles of Python script files. | ||
|
||
We want to ensure they all contain the appropriate license and copyright. | ||
|
||
For the purposes of this script, the *preamble* is defined as the first | ||
lines of the file starting with a hash (#). Any line that does not start | ||
with a hash ends the preamble. | ||
|
||
Usage:: | ||
|
||
python check_preamble.py --replace /path/to/template.py script.py | ||
|
||
The above command reads the preamble from ``template.py``, and then copies | ||
that preamble into ``script.py``. If ``script.py`` already contains a | ||
preamble, then the existing preamble will be replaced **entirely**. | ||
|
||
Processing entire subdirectories with one command:: | ||
|
||
find subdir1 subdir2 -iname "*.py" | xargs -n 1 python check_preamble.py --replace template.py | ||
|
||
""" | ||
import argparse | ||
import logging | ||
import os | ||
import sys | ||
|
||
|
||
def extract_preamble(fin): | ||
end_preamble = False | ||
preamble, body = [], [] | ||
|
||
for line in fin: | ||
if end_preamble: | ||
body.append(line) | ||
elif line.startswith('#'): | ||
preamble.append(line) | ||
else: | ||
end_preamble = True | ||
body.append(line) | ||
|
||
return preamble, body | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('path', help='the path of the file to check') | ||
parser.add_argument('--replace', help='replace the preamble with the one from this file') | ||
parser.add_argument('--loglevel', default=logging.INFO) | ||
args = parser.parse_args() | ||
|
||
logging.basicConfig(level=args.loglevel) | ||
|
||
with open(args.path) as fin: | ||
preamble, body = extract_preamble(fin) | ||
|
||
for line in preamble: | ||
logging.info('%s: %s', args.path, line.rstrip()) | ||
|
||
if not args.replace: | ||
sys.exit(0) | ||
|
||
with open(args.replace) as fin: | ||
preamble, _ = extract_preamble(fin) | ||
|
||
if os.access(args.path, os.X_OK): | ||
preamble.insert(0, '#!/usr/bin/env python\n') | ||
|
||
with open(args.path, 'w') as fout: | ||
for line in preamble + body: | ||
fout.write(line) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com> | ||
# | ||
# This code is distributed under the terms and conditions | ||
# from the MIT License (MIT). | ||
# |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The leading blank comment on top looks a bit weird, but I looked it up and
coding
can indeed be on the first or second line, so I guess this is fine: PEP263I'd prefer to include the Python shebang always (any harm?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can get rid of the leading blank line. It's not necessary.
Personally, I avoid the shebang unless the script is executable, simply because it is unnecessary otherwise.
Well, there's no harm (other than it being an eyesore). But consider this analogy: there's no harm in making all of our modules executable (chmod u+x), but we don't do it for the same reason (lack of necessity).