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 template locations in site-packages #79

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/dist
/build
/licenseheaders.egg-info/
*__pycache__*
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include LICENSE.txt
include README.md

recursive-include licenseheaders *.*
6 changes: 3 additions & 3 deletions licenseheaders.py → licenseheaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def update_c_style_comments(extensions):
"blockCommentEndPattern": None,
"lineCommentStartPattern": re.compile(r'^\s*#'),
"lineCommentEndPattern": None,
"headerStartLine": None,
"headerEndLine": "\n",
"headerStartLine": "#\n",
"headerEndLine": "#\n",
"headerLinePrefix": "# ",
"headerLineSuffix": None
},
Expand Down Expand Up @@ -586,7 +586,7 @@ def read_file(file, args, type_settings):
settings = type_settings.get(ftype)
if not os.access(file, os.R_OK):
LOGGER.error("File %s is not readable.", file)
with open(file, 'r', encoding=args.encoding) as f:
with open(file, 'r', encoding=args.encoding, errors="replace") as f:
lines = f.readlines()
# now iterate throw the lines and try to determine the various indies
# first try to find the start of the header: skip over shebang or empty lines
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
48 changes: 48 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[build-system]
requires = [
"setuptools >= 61.0.0",
]
build-backend = "setuptools.build_meta"

[project]
name = "licenseheaders"
authors = [
{name = "Johann Petrak", email = "[email protected]"},
]
description = "Add or change license headers for all files in a directory"
readme = "README.md"
requires-python = ">=3.5"
license = {text = "BSD-3"}
classifiers = [
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: BSD License",
"Environment :: Console",
"Natural Language :: English",
"Programming Language :: Python :: 3",
"Topic :: Software Development",
"Topic :: Software Development :: Code Generators",
"Intended Audience :: Developers",
]
dependencies = [
"packaging",
"regex"
]
dynamic = ["version"]

[tool.setuptools.dynamic]
version = {attr = "licenseheaders.__version__"}

[tool.setuptools.packages.find]
include = [
"licenseheaders"
]

[project.optional-dependencies]
test = [
"pytest",
"pytest-cov",
"pytest-dependency"
]

[project.scripts]
licenseheaders = "licenseheaders:main"
55 changes: 0 additions & 55 deletions setup.py

This file was deleted.

93 changes: 55 additions & 38 deletions tests/driver.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import os
import stat
import shutil
import subprocess
import sys
import unittest
import licenseheaders
import tempfile
from pathlib import Path

def run_file(licenseheaders_path, file_path, extra_args=None):
TEST_FILE = Path(__file__)
TEST_DIR = TEST_FILE.parent


def run_file(file_path, extra_args=None):
if (extra_args is None):
extra_args = []
subprocess.check_call(
[sys.executable, licenseheaders_path,
"-t", "lgpl-v3",
"-y", "2012-2014",
"-o", "ThisNiceCompany",
"-n", "ProjectName",
"-u", "http://the.projectname.com",
"-f", file_path] + extra_args
)
# HACK to avoid refactoring elsewhere
orig_argv = sys.argv
sys.argv = [
"licenseheaders", "-t", "lgpl-v3", "-y",
"2012-2014", "-o", "ThisNiceCompany", "-n", "ProjectName", "-u",
"http://the.projectname.com", "-f", file_path
] + extra_args
licenseheaders.main()
sys.argv = orig_argv


def compare_files(file_name, result_file_path, expected_file_path):
with open(result_file_path) as f1, open(expected_file_path) as f2:
Expand All @@ -24,43 +32,52 @@ def compare_files(file_name, result_file_path, expected_file_path):
print(f"File {file_name} content is different from expected")
return 1
result_file_permissions = stat.S_IMODE(os.lstat(result_file_path).st_mode)
expected_file_permissions = stat.S_IMODE(os.lstat(expected_file_path).st_mode)
expected_file_permissions = stat.S_IMODE(
os.lstat(expected_file_path).st_mode)
if (result_file_permissions != expected_file_permissions):
return 1
return 0

def main():
input_dir = "input"
expected_dir = "expected"
result_dir = "result"
licenseheaders_path = "../licenseheaders.py"
differences = 0

if not os.path.exists(result_dir):
os.makedirs(result_dir)
class TestLicenseHeaders(unittest.TestCase):

file_names = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
for file_name in file_names:
input_file_path = os.path.join(input_dir, file_name)
expected_file_path = os.path.join(expected_dir, file_name)
result_file_path = os.path.join(result_dir, file_name)
@classmethod
def setUpClass(cls) -> None:
"""
Create a directory for results.
"""
cls.input_dir = TEST_DIR / "input"
cls.expected_dir = TEST_DIR / "expected"
cls.result_dir = TEST_DIR / "result"

shutil.copyfile(input_file_path, result_file_path)
def test_licenseheaders(self):
"""
Check that adding licenses to known inputs matches expectations.
"""
with tempfile.TemporaryDirectory() as tmpdir:
file_names = [
f for f in os.listdir(self.input_dir)
if os.path.isfile(os.path.join(self.input_dir, f))
]
for file_name in file_names:
input_file_path = os.path.join(self.input_dir, file_name)
expected_file_path = os.path.join(self.expected_dir, file_name)
result_file_path = os.path.join(tmpdir, file_name)

extra_args = []
if not os.access(result_file_path, os.W_OK):
extra_args += ['--force-overwrite']
shutil.copyfile(input_file_path, result_file_path)

# Run it twice for identifying of removed comments
run_file(licenseheaders_path, result_file_path, extra_args)
run_file(licenseheaders_path, result_file_path, extra_args)
extra_args = []
if not os.access(result_file_path, os.W_OK):
extra_args += ['--force-overwrite']

differences += compare_files(file_name, result_file_path, expected_file_path)
# Run it twice for identifying of removed comments
run_file(result_file_path, extra_args)
run_file(result_file_path, extra_args)

difference = compare_files(file_name, result_file_path,
expected_file_path)
self.assertEqual(difference, 0)

if differences:
return 1
else:
return 0

if __name__ == "__main__":
sys.exit(main())
unittest.main()