Skip to content
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
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
known_third_party = ad_ci_tools,coreapi,docker,git,jinja2,pytest,pytz,requests,setuptools,urllib3,version_utils,yaml
50 changes: 50 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
---
repos:
- repo: https://github.com/asottile/seed-isort-config
rev: v2.2.0
hooks:
- id: seed-isort-config
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.7.0
hooks:
- id: isort
- repo: https://github.com/python/black.git
rev: 20.8b1
hooks:
- id: black
language_version: python3
- repo: https://github.com/pre-commit/pre-commit-hooks.git
rev: v3.4.0
hooks:
- id: requirements-txt-fixer
- id: check-added-large-files
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-byte-order-marker
- id: check-executables-have-shebangs
- id: mixed-line-ending
- id: check-merge-conflict
- id: debug-statements
- repo: https://gitlab.com/pycqa/flake8.git
rev: 3.8.4
hooks:
- id: flake8
args: ['--config=.flake8']
types: [python]
additional_dependencies:
- flake8>=3.7.8
- flake8-black
language_version: python3
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
- repo: https://github.com/markdownlint/markdownlint
rev: v0.11.0
hooks:
- id: markdownlint
42 changes: 29 additions & 13 deletions adcm_client/packer/add_to_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,57 @@ def __init__(self, message, errors=None):


def compare_helper(version, except_list):
if version not in (None, "1.0"):
raise NotValidSpecVersion("Not valid spec version").with_traceback(
sys.exc_info()[2]
)

if version is None:
except_list.extend([
'spec.yaml', 'pylintrc', '.[0-9a-zA-Z]*', '*pycache*',
'README.md', '*requirements*', '*.gz', '*.md'])
except_list.extend(
[
"spec.yaml",
"pylintrc",
".[0-9a-zA-Z]*",
"*pycache*",
"README.md",
"*requirements*",
"*.gz",
"*.md",
]
)

def v_None(sub: DirEntry):
for n in except_list:
if fnmatch(normpath(sub.path), n) or fnmatch(sub.name, n):
def v_none(sub: DirEntry):
for pattern in except_list:
if fnmatch(normpath(sub.path), pattern) or fnmatch(sub.name, pattern):
return True
return False
return v_None

func = v_none
elif version == "1.0":
prog = [re.compile(i) for i in except_list]

def v_1_0(sub: DirEntry):
for n in prog:
if n.match(normpath(sub.path)):
for pattern in prog:
if pattern.match(normpath(sub.path)):
return True
return False
return v_1_0
else:
raise NotValidSpecVersion('Not valid spec version').with_traceback(sys.exc_info()[2])

func = v_1_0
return func


def add_to_tar(version, directory, except_list, tar):
comparator = compare_helper(version, except_list)
cwd = getcwd()
chdir(directory)

def _add_to_tar(path='./'):
def _add_to_tar(path="./"):
for sub in scandir(path):
if not comparator(sub=sub):
if sub.is_dir():
_add_to_tar(path=normpath(sub.path))
else:
tar.add(normpath(sub.path))

_add_to_tar()
chdir(cwd)
79 changes: 44 additions & 35 deletions adcm_client/packer/bundle_build.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,49 @@
from tempfile import mkdtemp
from time import gmtime, strftime

from .add_to_tar import add_to_tar
from .naming_rules import add_build_id
from .spec import SpecFile, spec_processing
from adcm_client.packer.add_to_tar import add_to_tar
from adcm_client.packer.naming_rules import add_build_id
from adcm_client.packer.spec import SpecFile


def _prepare_ws(reponame, workspace, src_path, spec: SpecFile):
edition_dirs = {}
tmpdir = mkdtemp(prefix=reponame + '_', dir=workspace)
for edition in spec.data['editions']:
edition_dirs.update({edition['name']: os.path.join(tmpdir, str(edition['name']))})
copy_tree(src_path, edition_dirs[edition['name']], preserve_symlinks=True)
tmpdir = mkdtemp(prefix=reponame + "_", dir=workspace)
for edition in spec.data["editions"]:
edition_dirs.update(
{edition["name"]: os.path.join(tmpdir, str(edition["name"]))}
)
copy_tree(src_path, edition_dirs[edition["name"]], preserve_symlinks=True)
return tmpdir, edition_dirs


def _prepare_result_dir(workspace, tarball_path):
if tarball_path:
os.makedirs(tarball_path, exist_ok=True)
return tarball_path
else:
return workspace
return workspace


def _pack(reponame, repopaths, tarpaths, spec: SpecFile, **kwargs):
pack_timestamp = strftime("%Y%m%d%H%M%S", gmtime())
for edition in spec.data['editions']:
name = edition.get('name')
for edition in spec.data["editions"]:
name = edition.get("name")
tarpath = tarpaths[name] if isinstance(tarpaths, dict) else tarpaths
repopath = repopaths[name]
tar_except = edition.get('exclude', [])
tar_except = edition.get("exclude", [])

# naming rules
tarname = add_build_id(
repopath,
reponame,
name,
kwargs['master_branches'],
pack_timestamp
repopath, reponame, name, kwargs["master_branches"], pack_timestamp
)

stream = BytesIO()
tar = tarfile.open(fileobj=stream, mode='w|gz')
add_to_tar(spec.data['version'], repopath, tar_except, tar)
tar = tarfile.open(fileobj=stream, mode="w|gz")
add_to_tar(spec.data["version"], repopath, tar_except, tar)
logging.info("\n#######\n Edition %s \n#######", name)
logging.info("\n#######\n Packed files list:\n%s\n#######", "\n".join(tar.getnames()))
logging.info(
"\n#######\n Packed files list:\n%s\n#######", "\n".join(tar.getnames())
)
tar.close()
stream.seek(0)
# saving tarball
Expand All @@ -76,10 +75,18 @@ def _clean_ws(path):
remove_tree(path)


def build(reponame=None, repopath=None, workspace='/tmp', # pylint: disable=R0913
tarball_path=None, loglevel='ERROR',
clean_ws=True, master_branches=None,
release_version=False, edition=None, **args):
def build( # pylint: disable=R0913
reponame: str = None,
repopath: str = None,
workspace: str = "/tmp",
tarball_path: str = None,
loglevel: str = "ERROR",
clean_ws: bool = True,
master_branches: list = None,
release_version: bool = False,
edition: str = None,
prepared_image: bool = False,
):
"""Moves sources to workspace inside of temporary directory. \
Some operations over sources cant be proceed concurent(for exemple in pytest with xdist \
plugin) that why each thread need is own tmp dir with sources. \
Expand Down Expand Up @@ -107,30 +114,32 @@ def build(reponame=None, repopath=None, workspace='/tmp', # pylint: disable=R09
:rtype: dict
"""
if not master_branches:
master_branches = ['master']
master_branches = ["master"]
if not repopath:
raise ValueError('path to source should be defined')
raise ValueError("path to source should be defined")
if not reponame:
reponame = os.path.basename(os.path.realpath(repopath))
workspace = os.path.abspath(workspace)

logging.basicConfig(stream=sys.stdout, level=getattr(logging, loglevel))
spec = SpecFile(os.path.join(repopath, 'spec.yaml'))
spec = SpecFile(
os.path.join(repopath, "spec.yaml"), release_version, prepared_image
)
spec.normalize_spec()
spec.pop_edition(edition)

# Edition parameter processing.
# To build only one edition we remove every one else editions.
if edition:
spec.pop_edition(edition)

ws_tepm_dir, work_dir_paths = _prepare_ws(reponame, workspace, repopath, spec)

spec.spec_processing(work_dir_paths, workspace)
tarpath = _prepare_result_dir(workspace, tarball_path)
spec_processing(spec, work_dir_paths, workspace, release_version)

out = dict(
_pack(
reponame,
work_dir_paths,
tarpath,
spec,
master_branches=master_branches))
_pack(reponame, work_dir_paths, tarpath, spec, master_branches=master_branches)
)

if clean_ws:
_clean_ws(ws_tepm_dir)
Expand Down
Loading