Skip to content

Commit

Permalink
Fix/ignore flake8 and pylint warnings
Browse files Browse the repository at this point in the history
The pylint warnings "missing-class-docstring",
"missing-module-docstring", and "fixme" have been disabled for now.

Signed-off-by: Simon Gerber <simon.gerber@vshn.ch>
  • Loading branch information
simu committed Feb 14, 2020
1 parent 8149237 commit ad36ee1
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 59 deletions.
7 changes: 5 additions & 2 deletions commodore/catalog.py
@@ -1,11 +1,12 @@
import click
from pathlib import Path as P

import click

from . import git
from .helpers import rm_tree_contents


def fetch_customer_catalog(cfg, target_name, repoinfo):
def fetch_customer_catalog(repoinfo):
click.secho('Updating customer catalog...', bold=True)
return git.clone_repository(repoinfo['url'], 'catalog')

Expand All @@ -24,6 +25,7 @@ def _pretty_print_config_commit(name, repo):


def _render_catalog_commit_msg(cfg):
# pylint: disable=import-outside-toplevel
import datetime
now = datetime.datetime.now().isoformat(timespec='milliseconds')

Expand All @@ -49,6 +51,7 @@ def _render_catalog_commit_msg(cfg):

def update_catalog(cfg, target_name, repo):
click.secho('Updating catalog repository...', bold=True)
# pylint: disable=import-outside-toplevel
from distutils import dir_util
import textwrap
catalogdir = P(repo.working_tree_dir, 'manifests')
Expand Down
6 changes: 4 additions & 2 deletions commodore/cli.py
Expand Up @@ -21,6 +21,7 @@
@verbosity
@click.version_option('0.0.1', prog_name='commodore')
@click.pass_context
# pylint: disable=too-many-arguments
def commodore(ctx, api_url, api_token, global_git_base, customer_git_base, verbose):
ctx.obj = Config(api_url, api_token, global_git_base, customer_git_base, verbose)

Expand All @@ -36,12 +37,13 @@ def clean(config, verbose):
@commodore.command(short_help='Compile inventory and catalog')
@click.argument('cluster')
@click.option('--local', is_flag=True, default=False,
help='Run in local mode, Local mode does not try to connect to ' +
'SYNventory or fetch/push Git repositories.')
help=('Run in local mode, Local mode does not try to connect to ' +
'Lieutenant API or fetch/push Git repositories.'))
@click.option('--push', is_flag=True, default=False,
help='Push catalog to remote repository. Defaults to False')
@verbosity
@pass_config
# pylint: disable=redefined-builtin
def compile(config, cluster, local, push, verbose):
config.update_verbosity(verbose)
config.local = local
Expand Down
7 changes: 4 additions & 3 deletions commodore/cluster.py
@@ -1,14 +1,15 @@
import click
import os

from pathlib import Path as P

import click

from .helpers import (
lieutenant_query,
yaml_dump,
yaml_load,
)

from pathlib import Path as P


def fetch_cluster(cfg, clusterid):
cluster = lieutenant_query(cfg.api_url, cfg.api_token, 'clusters', clusterid)
Expand Down
14 changes: 10 additions & 4 deletions commodore/compile.py
@@ -1,6 +1,7 @@
from pathlib import Path as P

import click
from kapitan.resources import inventory_reclass
from pathlib import Path as P

from . import git
from .catalog import (
Expand Down Expand Up @@ -70,8 +71,7 @@ def _regular_setup(config, cluster_id):

# Fetch catalog
try:
catalog_repo = fetch_customer_catalog(config, target_name,
cluster['gitRepo'])
catalog_repo = fetch_customer_catalog(cluster['gitRepo'])
except Exception as e:
raise click.ClickException(f"While cloning git repositories: {e}") from e

Expand All @@ -90,6 +90,11 @@ def _local_setup(config, cluster_id):

click.echo(f" > Reconstructing Cluster API data from target")
cluster = reconstruct_api_response(target_yml)
if cluster['id'] != cluster_id:
error = f"[Local mode] Cluster ID mismatch: local state targets " + \
f"{cluster['id']}, compilation was requested for {cluster_id}"
raise click.ClickException(error)

customer_id = cluster['tenant']

click.secho('Registering config...', bold=True)
Expand All @@ -114,6 +119,7 @@ def _local_setup(config, cluster_id):
return cluster, target_name, catalog_repo


# pylint: disable=redefined-builtin
def compile(config, cluster_id):
if config.local:
cluster, target_name, catalog_repo = _local_setup(config, cluster_id)
Expand All @@ -133,7 +139,7 @@ def compile(config, cluster_id):
jsonnet_libs = kapitan_inventory['parameters'].get(
'commodore', {}).get('jsonnet_libs', None)
if jsonnet_libs and not config.local:
fetch_jsonnet_libs(config, jsonnet_libs)
fetch_jsonnet_libs(jsonnet_libs)

# Generate Kapitan secret references from refs found in inventory
# parameters
Expand Down
3 changes: 2 additions & 1 deletion commodore/component_template.py
@@ -1,7 +1,8 @@
from pathlib import Path as P

import click

from cookiecutter.main import cookiecutter
from pathlib import Path as P

from . import git
from .dependency_mgmt import create_component_symlinks
Expand Down
3 changes: 2 additions & 1 deletion commodore/config.py
Expand Up @@ -3,7 +3,8 @@
Component = namedtuple('Component', ['name', 'repo', 'version'])


class Config(object):
class Config:
# pylint: disable=too-many-arguments
def __init__(self, api_url, api_token, global_git, customer_git, verbose):
self.api_url = api_url
self.api_token = api_token
Expand Down
5 changes: 3 additions & 2 deletions commodore/dependency_mgmt.py
@@ -1,8 +1,9 @@
import click
import errno
import os
from pathlib import Path as P

import click

from . import git
from .helpers import yaml_load

Expand Down Expand Up @@ -115,7 +116,7 @@ def set_component_versions(cfg, versions):
_set_component_version(cfg, cn, c['version'])


def fetch_jsonnet_libs(cfg, libs):
def fetch_jsonnet_libs(libs):
"""
Download all libraries specified in list `libs`.
Each entry in `libs` is assumed to be a dict with keys
Expand Down
17 changes: 10 additions & 7 deletions commodore/git.py
@@ -1,17 +1,20 @@
import click
import difflib
import hashlib

import click

from git import Repo, Actor
from git.exc import GitCommandError, BadName


class RefError(ValueError):
def __init__(self, message):
super().__init__()
self.message = message


def _normalize_git_ssh(url):
# pylint: disable=import-outside-toplevel
from url_normalize.url_normalize import normalize_userinfo, normalize_host, \
normalize_path, provide_url_scheme
from url_normalize.tools import deconstruct_url, reconstruct_url
Expand Down Expand Up @@ -39,15 +42,15 @@ def checkout_version(repo, ref):
checkout that commit. Always checkout as detached HEAD as that massively
simplifies the implementation.
"""
commit = None
rev = None
try:
commit = repo.commit(f"{ref}")
rev = repo.commit(f"{ref}")
except BadName:
pass
try:
if not commit:
commit = repo.commit(f"remotes/origin/{ref}")
repo.head.reference = commit
if not rev:
rev = repo.commit(f"remotes/origin/{ref}")
repo.head.reference = rev
repo.head.reset(index=True, working_tree=True)
except GitCommandError as e:
raise RefError(f"Failed to checkout revision '{ref}'") from e
Expand Down Expand Up @@ -166,4 +169,4 @@ def commit(repo, commit_message):


def add_remote(repo, name, url):
return repo.create_remote('origin', _normalize_git_ssh(url))
return repo.create_remote(name, _normalize_git_ssh(url))
27 changes: 17 additions & 10 deletions commodore/helpers.py
@@ -1,11 +1,14 @@
import click
import json
import requests
import shutil
from pathlib import Path as P

import click
import requests
import yaml

# pylint: disable=redefined-builtin
from requests.exceptions import ConnectionError, HTTPError
from url_normalize import url_normalize
from pathlib import Path as P


def yaml_load(file):
Expand Down Expand Up @@ -42,6 +45,7 @@ def yaml_dump_all(obj, file):

class ApiError(Exception):
def __init__(self, message):
super().__init__()
self.message = message


Expand All @@ -53,7 +57,7 @@ def lieutenant_query(api_url, api_token, api_endpoint, api_id):
raise ApiError(f"Unable to connect to Lieutenant at {api_url}") from e
try:
resp = json.loads(r.text)
except BaseException:
except json.JSONDecodeError:
resp = {'message': 'Client error: Unable to parse JSON'}
try:
r.raise_for_status()
Expand Down Expand Up @@ -85,23 +89,26 @@ def clean(cfg):

def kapitan_compile():
# TODO: maybe use kapitan.targets.compile_targets directly?
# pylint: disable=import-outside-toplevel
import shlex
import subprocess # nosec
click.secho('Compiling catalog...', bold=True)
return subprocess.run( # nosec
shlex.split('kapitan compile --fetch -J . dependencies --refs-path ./catalog/refs'))
shlex.split('kapitan compile --fetch -J . dependencies --refs-path ./catalog/refs'),
check=True)


def rm_tree_contents(dir):
def rm_tree_contents(basedir):
"""
Delete all files in directory `dir`, but do not delete the directory
Delete all files in directory `basedir`, but do not delete the directory
itself.
"""
# pylint: disable=import-outside-toplevel
import os
dir = P(dir)
if not dir.is_dir():
basedir = P(basedir)
if not basedir.is_dir():
raise ValueError('Expected directory as argument')
for f in dir.glob('*'):
for f in basedir.glob('*'):
if f.name.startswith('.'):
# pathlib's glob doesn't filter hidden files, skip them here
continue
Expand Down
4 changes: 2 additions & 2 deletions commodore/postprocess/__init__.py
@@ -1,7 +1,7 @@
import click

from pathlib import Path as P

import click

from commodore.helpers import yaml_load

from .jsonnet import run_jsonnet_filter
Expand Down
6 changes: 4 additions & 2 deletions commodore/postprocess/builtin_filters.py
@@ -1,10 +1,11 @@
import _jsonnet
import click
import json
import re

from pathlib import Path as P

import _jsonnet
import click

from .jsonnet import jsonnet_runner


Expand All @@ -17,6 +18,7 @@ def _builtin_filter_helm_namespace(inv, component, target, path, **kwargs):
exclude_objects = '|'.join([json.dumps(e) for e in exclude_objects])
output_dir = P('compiled', target, path)

# pylint: disable=c-extension-no-member
jsonnet_runner(inv, component, target, path,
_jsonnet.evaluate_file, P('filters', 'helm_namespace.jsonnet'),
namespace=kwargs['namespace'],
Expand Down
30 changes: 17 additions & 13 deletions commodore/postprocess/jsonnet.py
@@ -1,22 +1,23 @@
import _jsonnet
import click
import json
import os

from pathlib import Path as P

import _jsonnet
import click

from commodore.helpers import yaml_load, yaml_load_all, yaml_dump, yaml_dump_all

# Returns content if worked, None if file not found, or throws an exception


def _try_path(dir, rel):
def _try_path(basedir, rel):
if not rel:
raise RuntimeError('Got invalid filename (empty string).')
if rel[0] == '/':
full_path = P(rel)
else:
full_path = P(dir) / rel
full_path = P(basedir) / rel
if full_path.is_dir():
raise RuntimeError('Attempted to import a directory')

Expand All @@ -26,8 +27,8 @@ def _try_path(dir, rel):
return full_path.name, f.read()


def _import_callback_with_searchpath(search, dir, rel):
full_path, content = _try_path(dir, rel)
def _import_callback_with_searchpath(search, basedir, rel):
full_path, content = _try_path(basedir, rel)
if content:
return full_path, content
for p in search:
Expand All @@ -37,22 +38,23 @@ def _import_callback_with_searchpath(search, dir, rel):
raise RuntimeError('File not found')


def _import_cb(dir, rel):
def _import_cb(basedir, rel):
# Add current working dir to search path for Jsonnet import callback
search_path = [P('.').resolve(), P('./dependencies').resolve()]
return _import_callback_with_searchpath(search_path, dir, rel)
return _import_callback_with_searchpath(search_path, basedir, rel)


def _list_dir(dir, basename):
def _list_dir(basedir, basename):
"""
Non-recursively list files in directory `dir`. If `basename` is set to
Non-recursively list files in directory `basedir`. If `basename` is set to
True, only return the file name itself and not the full path.
"""
files = [x for x in P(dir).iterdir() if x.is_file()]
files = [x for x in P(basedir).iterdir() if x.is_file()]

if basename:
return [f.parts[-1] for f in files]
else:
return files

return files


_native_callbacks = {
Expand All @@ -62,6 +64,7 @@ def _list_dir(dir, basename):
}


# pylint: disable=too-many-arguments
def jsonnet_runner(inv, component, target, output_path, jsonnet_func,
jsonnet_input, **kwargs):
def _inventory():
Expand Down Expand Up @@ -99,5 +102,6 @@ def run_jsonnet_filter(inv, component, target, filterdir, f):
raise click.ClickException(f"Only type 'jsonnet' is supported, got {f['type']}")
filterpath = filterdir / f['filter']
output_path = f['output_path']
# pylint: disable=c-extension-no-member
jsonnet_runner(inv, component, target, output_path,
_jsonnet.evaluate_file, filterpath)

0 comments on commit ad36ee1

Please sign in to comment.