Skip to content

Commit

Permalink
Merge de355de into d174eb4
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Jul 23, 2019
2 parents d174eb4 + de355de commit 7d82295
Show file tree
Hide file tree
Showing 10 changed files with 444 additions and 335 deletions.
64 changes: 41 additions & 23 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,36 +1,54 @@
# Install pre-commit hooks via
# pre-commit install

# modernizer: make sure our code-base is Python 3 ready
#- repo: https://github.com/python-modernize/python-modernize.git
# sha: a234ce4e185cf77a55632888f1811d83b4ad9ef2
# hooks:
# - id: python-modernize
# exclude: ^docs/
# args:
# - --write
# - --nobackups
- repo: git://github.com/pre-commit/pre-commit-hooks
sha: v2.2.3
hooks:
- id: check-json
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: double-quote-string-fixer
- id: flake8

- repo: local
hooks:
# yapf = yet another python formatter

- id: yapf
name: yapf
name: Yet Another Python Formatter
entry: yapf
language: system
exclude: '^(aiida_crystal17/tests/)'

# prospector: collection of linters
- id: prospector
language: system
types: [file, python]
name: prospector
description: "This hook runs Prospector: https://github.com/landscapeio/prospector"
entry: prospector
exclude: '^(aiida_crystal17/tests/)|(examples/)'
types: [python]
args: ["-i", "-vv"]

- id: version-number
name: Check version numbers
entry: python ./.travis-data/check_version.py
entry: python ./.travis-data/check_version.py version
language: system
files: '^(setup.json)|(aiida_crystal17/__init__.py)'
files: >-
(?x)^(
setup.json|
.travis-data/check_version.py|
aiida_crystal17/__init__.py
)$
pass_filenames: false

- id: conda
name: Create environment.yml
entry: python ./.travis-data/check_version.py conda
language: system
files: >-
(?x)^(
setup.json|
setup.py|
.travis-data/check_version.py|
environment.yml|
)$
pass_filenames: false

- id: travis-linter
name: Travis Lint
entry: travis lint
files: .travis.yml
language: ruby
additional_dependencies: ['travis']
131 changes: 102 additions & 29 deletions .travis-data/check_version.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,107 @@
"""Check that version numbers match.
"""Validate consistency of versions and dependencies.
Check version number in setup.json and aiida_crystal17/__init__.py and make sure
they match.
Validates consistency of setup.json and
* environment.yml
* version in aiida/__init__.py
"""
import os
import json
import os
import sys

this_path = os.path.split(os.path.realpath(__file__))[0]

# Get content of setup.json
setup_fname = 'setup.json'
setup_path = os.path.join(this_path, os.pardir, setup_fname)
with open(setup_path) as f:
setup_content = json.load(f)

# Get version from python package
sys.path.insert(0, os.path.join(this_path, os.pardir))
import aiida_crystal17 # noqa
version = aiida_crystal17.__version__

if version != setup_content['version']:
print("Version number mismatch detected:")
print("Version number in '{}': {}".format(setup_fname,
setup_content['version']))
print("Version number in '{}/__init__.py': {}".format(
'aiida_crystal17', version))
sys.exit(1)

# Overwrite version in setup.json
# setup_content['version'] = version
# with open(setup_path, 'w') as f:
# json.dump(setup_content, f, indent=4, sort_keys=True)
import click

FILENAME_SETUP_JSON = 'setup.json'
SCRIPT_PATH = os.path.split(os.path.realpath(__file__))[0]
ROOT_DIR = os.path.join(SCRIPT_PATH, os.pardir)
FILEPATH_SETUP_JSON = os.path.join(ROOT_DIR, FILENAME_SETUP_JSON)


def get_setup_json():
"""Return the `setup.json` as a python dictionary """
with open(FILEPATH_SETUP_JSON, 'r') as handle:
setup_json = json.load(handle) # , object_pairs_hook=OrderedDict)

return setup_json


@click.group()
def cli():
pass


@cli.command('version')
def validate_version():
"""Check that version numbers match.
Check version number in setup.json and aiida_crystal17/__init__.py and make sure
they match.
"""
# Get version from python package
sys.path.insert(0, ROOT_DIR)
import aiida_crystal17 # pylint: disable=wrong-import-position
version = aiida_crystal17.__version__

setup_content = get_setup_json()
if version != setup_content['version']:
click.echo('Version number mismatch detected:')
click.echo("Version number in '{}': {}".format(FILENAME_SETUP_JSON, setup_content['version']))
click.echo("Version number in '{}/__init__.py': {}".format('aiida_crystal17', version))
click.echo("Updating version in '{}' to: {}".format(FILENAME_SETUP_JSON, version))

setup_content['version'] = version
with open(FILEPATH_SETUP_JSON, 'w') as fil:
# Write with indentation of two spaces and explicitly define separators to not have spaces at end of lines
json.dump(setup_content, fil, indent=2, separators=(',', ': '))

sys.exit(1)


@cli.command('conda')
def update_environment_yml():
"""
Updates environment.yml file for conda.
"""
import re
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from ruamel.yaml import YAML

cmap = CommentedMap()
cmap.yaml_set_start_comment('Usage: conda env create -n myenvname -f environment.yml python=3.6')
cmap['name'] = 'aiida_crystal17'
cmap['channels'] = CommentedSeq(['conda-forge', 'cjs'])
cmap['channels'].yaml_add_eol_comment('for sqlalchemy-diff and pgtest', 1)
cmap['dependencies'] = dmap = CommentedSeq()

# fix incompatibilities between conda and pypi
replacements = {}
setup_json = get_setup_json()

for base, key in [(None, 'install_requires'), ('extras_require', 'testing'), ('extras_require', 'code_style'),
('extras_require', 'docs')]:
requirements = setup_json.get(base, setup_json)[key]
count = 0
for req in sorted(requirements, key=lambda x: x.lower()):
# skip packages required for specific python versions < 3
if re.findall("python_version\\s*\\<\\s*\\'?3", req):
continue
req = req.split(';')[0]
for (regex, replacement) in iter(replacements.items()):
req = re.sub(regex, replacement, req)
count += 1
dmap.append(req.lower())

dmap.yaml_set_comment_before_after_key(len(dmap) - count, before=key)

yaml = YAML(typ='rt')
yaml.default_flow_style = False
yaml.encoding = 'utf-8'
yaml.allow_unicode = True
environment_filename = 'environment.yml'
file_path = os.path.join(ROOT_DIR, environment_filename)
with open(file_path, 'w') as env_file:
yaml.dump(cmap, env_file)


if __name__ == '__main__':
cli() # pylint: disable=no-value-for-parameter
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ matrix:
install:
# Upgrade pip setuptools and wheel
- pip install -U "pip<19" wheel setuptools "reentry>=1.3"
- |
if [[ "$TEST_TYPE" == "version" ]]; then
pip install click
fi
- |
if [[ "$TEST_TYPE" == "code-style" ]]; then
pip install "flake8<3.8.0,>=3.7.0"
Expand Down Expand Up @@ -85,7 +89,7 @@ script:
fi
- |
if [[ "$TEST_TYPE" == "version" ]]; then
python ./.travis-data/check_version.py
python ./.travis-data/check_version.py version
fi
after_success:
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"**/.eggs": true
},

"python.pythonPath": "/anaconda/envs/aiida-workshop/bin/python",
"python.pythonPath": "/anaconda/envs/aiida_crystal17/bin/python",
"python.formatting.provider": "yapf",
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
Expand Down

0 comments on commit 7d82295

Please sign in to comment.