Skip to content

Commit

Permalink
refactor: minor changes across the codebase (#656)
Browse files Browse the repository at this point in the history
* refactor: move getting add-on version to a separate function

* refactor: get rid of admin_match

* refactor: move GlobalConfigPostProcessor to its own file

* refactor: remove unused method from GlobalConfigPostProcessor

* refactor: rename global_config to global_config_builder_schema

* refactor: no neeed to pass static import_declare_test variable

* chore: get rid of isort
  • Loading branch information
artemrys authored Feb 17, 2023
1 parent 3083108 commit c667430
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 167 deletions.
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ repos:
rev: 22.12.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.11.1
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
hooks:
Expand Down
2 changes: 1 addition & 1 deletion example/globalConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
"meta": {
"name": "Splunk_TA_dummy_data",
"restRoot": "Splunk_TA_dummy_data",
"version": "5.18.0R05a4667a",
"version": "5.20.0Rc5bf18ae",
"displayName": "Splunk_TA_dummy_data",
"schemaVersion": "0.0.3"
}
Expand Down
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ importlib-metadata = {version="*", python="<3.8"}
[tool.poetry.scripts]
ucc-gen="splunk_add_on_ucc_framework.main:main"

[tool.isort]
profile = "black"

[build-system]
requires = ["poetry>=1.0.2"]
build-backend = "poetry.masonry.api"
54 changes: 30 additions & 24 deletions splunk_add_on_ucc_framework/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import os
import shutil
import sys
from typing import Optional

import yaml
from jinja2 import Environment, FileSystemLoader
Expand All @@ -34,11 +35,11 @@
meta_conf,
utils,
)
from splunk_add_on_ucc_framework.commands.rest_builder import global_config
from splunk_add_on_ucc_framework.commands.rest_builder.builder import RestBuilder
from splunk_add_on_ucc_framework.commands.rest_builder.global_config import (
GlobalConfigBuilderSchema,
from splunk_add_on_ucc_framework.commands.rest_builder import (
global_config_builder_schema,
global_config_post_processor,
)
from splunk_add_on_ucc_framework.commands.rest_builder.builder import RestBuilder
from splunk_add_on_ucc_framework.install_python_libraries import (
SplunktaucclibNotFound,
install_python_libraries,
Expand Down Expand Up @@ -132,21 +133,22 @@ def _replace_token(ta_name, outputdir):


def _generate_rest(
ta_name, scheme: GlobalConfigBuilderSchema, import_declare_name, outputdir
ta_name,
scheme: global_config_builder_schema.GlobalConfigBuilderSchema,
outputdir,
):
"""
Build REST for Add-on.
Args:
ta_name (str): Name of TA.
scheme (GlobalConfigBuilderSchema): REST schema.
import_declare_name (str): Name of import_declare_* file.
outputdir (str): output directory.
"""
builder_obj = RestBuilder(scheme, os.path.join(outputdir, ta_name))
builder_obj.build()
post_process = global_config.GlobalConfigPostProcessor()
post_process(builder_obj, scheme, import_declare_name=import_declare_name)
post_process = global_config_post_processor.GlobalConfigPostProcessor()
post_process(builder_obj, scheme)
return builder_obj


Expand Down Expand Up @@ -252,7 +254,7 @@ def _modify_and_replace_token_for_oauth_templates(
os.remove(redirect_js_src)


def _add_modular_input(ta_name, schema_content, import_declare_name, outputdir):
def _add_modular_input(ta_name, schema_content, outputdir):
"""
Generate Modular input for addon.
Expand All @@ -276,7 +278,7 @@ def _add_modular_input(ta_name, schema_content, import_declare_name, outputdir):

# filter fields in allow list
entity = [x for x in entity if x.get("field") not in field_allow_list]
import_declare = "import " + import_declare_name
import_declare = "import import_declare_test"

content = j2_env.get_template(template).render(
import_declare=import_declare,
Expand Down Expand Up @@ -423,25 +425,28 @@ def _get_os_path(path):
return path.strip(os.sep)


def generate(
source, config_path, addon_version, outputdir=None, python_binary_name="python3"
):
logger.info(f"ucc-gen version {__version__} is used")
logger.info(f"Python binary name to use: {python_binary_name}")
if outputdir is None:
outputdir = os.path.join(os.getcwd(), "output")
def _get_addon_version(addon_version: Optional[str]) -> str:
if not addon_version:
try:
addon_version = utils.get_version_from_git()
return utils.get_version_from_git()
except exceptions.CouldNotVersionFromGitException:
logger.error(
"Could not find the proper version from git tags. "
"Check out "
"https://github.com/splunk/addonfactory-ucc-generator/issues/404"
)
exit(1)
else:
addon_version = addon_version.strip()
return addon_version.strip()


def generate(
source, config_path, addon_version, outputdir=None, python_binary_name="python3"
):
logger.info(f"ucc-gen version {__version__} is used")
logger.info(f"Python binary name to use: {python_binary_name}")
if outputdir is None:
outputdir = os.path.join(os.getcwd(), "output")
addon_version = _get_addon_version(addon_version)

if not os.path.exists(source):
raise NotADirectoryError(f"{os.path.abspath(source)} not found.")
Expand Down Expand Up @@ -506,13 +511,14 @@ def generate(
config_path, is_global_config_yaml
)

scheme = global_config.GlobalConfigBuilderSchema(schema_content, j2_env)
scheme = global_config_builder_schema.GlobalConfigBuilderSchema(
schema_content, j2_env
)

addon_version = schema_content.get("meta").get("version")
logger.info("Addon Version : " + addon_version)
ta_tabs = schema_content.get("pages").get("configuration").get("tabs")
ta_namespace = schema_content.get("meta").get("restRoot")
import_declare_name = "import_declare_test"
is_inputs = "inputs" in schema_content.get("pages")

logger.info("Package ID is " + ta_name)
Expand Down Expand Up @@ -550,7 +556,7 @@ def generate(

_replace_token(ta_name, outputdir)

_generate_rest(ta_name, scheme, import_declare_name, outputdir)
_generate_rest(ta_name, scheme, outputdir)

_modify_and_replace_token_for_oauth_templates(
ta_name, ta_tabs, schema_content.get("meta").get("version"), outputdir
Expand All @@ -566,7 +572,7 @@ def generate(
"default_no_input.xml",
)
os.remove(default_no_input_xml_file)
_add_modular_input(ta_name, schema_content, import_declare_name, outputdir)
_add_modular_input(ta_name, schema_content, outputdir)
else:
_handle_no_inputs(ta_name, outputdir)

Expand Down
11 changes: 7 additions & 4 deletions splunk_add_on_ucc_framework/commands/rest_builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import os
import os.path as op

from splunk_add_on_ucc_framework.commands.rest_builder.global_config import (
GlobalConfigBuilderSchema,
from splunk_add_on_ucc_framework.commands.rest_builder import (
global_config_builder_schema,
)
from splunk_add_on_ucc_framework.rest_map_conf import RestmapConf
from splunk_add_on_ucc_framework.web_conf import WebConf
Expand Down Expand Up @@ -57,7 +57,11 @@ def save(self):

class RestBuilder:
def __init__(
self, schema: GlobalConfigBuilderSchema, output_path: str, *args, **kwargs
self,
schema: global_config_builder_schema.GlobalConfigBuilderSchema,
output_path: str,
*args,
**kwargs
):
"""
Expand Down Expand Up @@ -123,7 +127,6 @@ def build(self):
RestmapConf.build(
self._schema.endpoints,
self._schema.namespace,
self._schema.admin_match,
),
)
self.output.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@


import json
import os
import os.path as op
import shutil
from typing import Any, Dict, List, Type

from splunk_add_on_ucc_framework.commands.rest_builder.endpoint.base import (
Expand Down Expand Up @@ -77,10 +74,6 @@ def product(self) -> str:
def namespace(self) -> str:
return self._meta["restRoot"]

@property
def admin_match(self):
return ""

@property
def inputs(self):
return self._inputs
Expand Down Expand Up @@ -277,118 +270,3 @@ def _get_oauth_enitities(self, content):
content.remove(entity_element)
break
return content


class GlobalConfigPostProcessor:
"""
Post process for REST builder.
"""

output_local = "local"
_import_declare_template = """
import {import_declare_name}
"""

_import_declare_content = """
import os
import sys
import re
from os.path import dirname
ta_name = '{ta_name}'
pattern = re.compile(r'[\\\\/]etc[\\\\/]apps[\\\\/][^\\\\/]+[\\\\/]bin[\\\\/]?$')
new_paths = [path for path in sys.path if not pattern.search(path) or ta_name in path]
new_paths.append(os.path.join(dirname(dirname(__file__)), "lib"))
new_paths.insert(0, os.path.sep.join([os.path.dirname(__file__), ta_name]))
sys.path = new_paths
"""

def __init__(self):
self.builder = None
self.schema = None
self.import_declare_name = None

@property
def root_path(self):
return getattr(self.builder.output, "_path")

def third_path(self):
return self.schema.namespace

def default_to_local(self):
default_dir = op.join(
self.root_path,
self.builder.output.default,
)
local_dir = op.join(
self.root_path,
self.output_local,
)
if not op.isdir(local_dir):
os.makedirs(local_dir)
for i in os.listdir(default_dir):
child = op.join(default_dir, i)
if op.isdir(child):
shutil.copytree(child, local_dir)
else:
shutil.copy(child, op.join(local_dir, i))

# remove the default folder
shutil.rmtree(default_dir, ignore_errors=True)

def import_declare_py_name(self):
if self.import_declare_name:
return self.import_declare_name
return f"{self.schema.namespace}_import_declare"

def import_declare_py_content(self):
import_declare_file = op.join(
self.root_path,
self.builder.output.bin,
self.import_declare_py_name() + ".py",
)
content = self._import_declare_content.format(
ta_name=self.schema.product,
)
with open(import_declare_file, "w") as f:
f.write(content)

def import_declare(self, rh_file):
with open(rh_file) as f:
cont = f.readlines()
import_declare = self._import_declare_template.format(
import_declare_name=self.import_declare_py_name()
)
cont.insert(0, import_declare)
with open(rh_file, "w") as f:
f.write("".join(cont))

def __call__(self, builder, schema, import_declare_name=None):
"""
:param builder: REST builder
:param schema: Global Config Schema
:return:
"""
self.builder = builder
self.schema = schema
self.import_declare_name = import_declare_name

self.import_declare_py_content()
for endpoint in schema.endpoints:
rh_file = op.join(
getattr(builder.output, "_path"),
builder.output.bin,
endpoint.rh_name + ".py",
)
self.import_declare(rh_file)
# self.default_to_local()

# add executable permission to files under bin folder
def add_executable_attribute(file_path):
if op.isfile(file_path):
st = os.stat(file_path)
os.chmod(file_path, st.st_mode | 0o111)

bin_path = op.join(getattr(builder.output, "_path"), builder.output.bin)
items = os.listdir(bin_path)
list(map(add_executable_attribute, items))
Loading

0 comments on commit c667430

Please sign in to comment.