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

fix file preambles for Python scripts #357

Merged
merged 3 commits into from
Oct 14, 2019
Merged
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
7 changes: 7 additions & 0 deletions integration-tests/test_184.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# -*- coding: utf-8 -*-
Copy link
Owner

@piskvorky piskvorky Oct 1, 2019

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: PEP263

I'd prefer to include the Python shebang always (any harm?).

Copy link
Collaborator Author

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.

I'd prefer to include the Python shebang always (any harm?).

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).

#
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com>
#
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#
Copy link
Owner

Choose a reason for hiding this comment

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

Blank line instead of blank comment, to separate the blocks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Owner

@piskvorky piskvorky Oct 9, 2019

Choose a reason for hiding this comment

The 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

Expand Down
7 changes: 7 additions & 0 deletions integration-tests/test_207.py
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).
#
import os
import sys
import tempfile
Expand Down
8 changes: 8 additions & 0 deletions integration-tests/test_209.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# -*- 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).
#
import io
import json
import logging
import os
Expand Down
11 changes: 9 additions & 2 deletions integration-tests/test_hdfs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# -*- coding: utf-8 -*-
#
# Sample code for HDFS integration tests.
# Requires hadoop to be running on localhost, at the moment.
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com>
#
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#
"""
Sample code for HDFS integration tests.
Requires hadoop to be running on localhost, at the moment.
"""
import smart_open

with smart_open.smart_open("hdfs://user/root/input/core-site.xml") as fin:
Expand Down
6 changes: 5 additions & 1 deletion integration-tests/test_http.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env python
# -*- 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).
#
from __future__ import unicode_literals

import logging
Expand Down
7 changes: 7 additions & 0 deletions integration-tests/test_minio.py
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).
#
import logging
import boto3

Expand Down
6 changes: 6 additions & 0 deletions integration-tests/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,4 +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).
#

from __future__ import unicode_literals
import io
Expand Down
11 changes: 9 additions & 2 deletions integration-tests/test_webhdfs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# -*- coding: utf-8 -*-
#
# Sample code for WebHDFS integration tests.
# Requires hadoop to be running on localhost, at the moment.
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com>
#
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#
"""
Sample code for WebHDFS integration tests.
Requires hadoop to be running on localhost, at the moment.
"""
import smart_open

with smart_open.smart_open("webhdfs://localhost:50070/user/root/input/core-site.xml") as fin:
Expand Down
82 changes: 82 additions & 0 deletions release/check_preamble.py
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()
1 change: 0 additions & 1 deletion smart_open/__init__.py
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>
Expand Down
6 changes: 6 additions & 0 deletions smart_open/bytebuffer.py
Original file line number Diff line number Diff line change
@@ -1,4 +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).
#
"""Implements ByteBuffer class for amortizing network transfer overhead."""

import io
Expand Down
4 changes: 2 additions & 2 deletions smart_open/doctools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#
# -*- 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).
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#

"""Common functions for working with docstrings.
Expand Down
4 changes: 2 additions & 2 deletions smart_open/hdfs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python
# -*- 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).
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#

"""Implements reading and writing to/from HDFS.
Expand Down
6 changes: 6 additions & 0 deletions smart_open/http.py
Original file line number Diff line number Diff line change
@@ -1,4 +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).
#
"""Implements file-like objects for reading from http."""

import io
Expand Down
6 changes: 6 additions & 0 deletions smart_open/s3.py
Original file line number Diff line number Diff line change
@@ -1,4 +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).
#
"""Implements file-like objects for reading and writing from/to S3."""

import io
Expand Down
3 changes: 1 addition & 2 deletions smart_open/smart_open_lib.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Radim Rehurek <me@radimrehurek.com>
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com>
#
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
Expand Down
4 changes: 2 additions & 2 deletions smart_open/ssh.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python
# -*- 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).
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#

"""Implements I/O streams over SSH.
Expand Down
7 changes: 7 additions & 0 deletions smart_open/tests/__init__.py
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).
#
6 changes: 6 additions & 0 deletions smart_open/tests/test_bytebuffer.py
Original file line number Diff line number Diff line change
@@ -1,4 +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).
#
import random
import unittest

Expand Down
6 changes: 6 additions & 0 deletions smart_open/tests/test_hdfs.py
Original file line number Diff line number Diff line change
@@ -1,4 +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).
#
from __future__ import print_function
from __future__ import unicode_literals

Expand Down
7 changes: 7 additions & 0 deletions smart_open/tests/test_http.py
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).
#
import unittest

import responses
Expand Down
6 changes: 6 additions & 0 deletions smart_open/tests/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,4 +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).
#
import gzip
import io
import logging
Expand Down
4 changes: 2 additions & 2 deletions smart_open/tests/test_smart_open.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Radim Rehurek <me@radimrehurek.com>
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com>
#
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#

import bz2
import io
Expand Down
13 changes: 7 additions & 6 deletions smart_open/tests/test_smart_open_old.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Radim Rehurek <me@radimrehurek.com>
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com>
#
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#
# These are tests that test the deprecated smart_open.smart_open function.
# They mostly duplicate tests in test_smart_open.py and are here to guarantee
# backwards compatibility.
#
"""
These are tests that test the deprecated smart_open.smart_open function.
They mostly duplicate tests in test_smart_open.py and are here to guarantee
backwards compatibility.
"""


import io
import logging
Expand Down
4 changes: 2 additions & 2 deletions smart_open/webhdfs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python
# -*- 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).
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#

"""Implements reading and writing to/from WebHDFS.
Expand Down