Skip to content

Commit 9bfce28

Browse files
authored
Merge pull request #16 from arenadata/ADCM-1133
Task ADCM-1133
2 parents d3cdb63 + 78c4bed commit 9bfce28

9 files changed

Lines changed: 87 additions & 52 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,6 @@ dmypy.json
110110

111111
# vscode
112112
.vscode
113+
114+
# venv
115+
/env/

adcm_client/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def strip_none_keys(args):
3737
return {k: v for k, v in args.items() if v is not None}
3838

3939

40+
class TooManyArguments(Exception):
41+
pass
42+
43+
4044
class NoSuchEndpoint(Exception):
4145
pass
4246

adcm_client/objects.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from version_utils import rpm
1919

2020
from .base import (ADCMApiError, BaseAPIListObject, BaseAPIObject,
21-
ObjectNotFound, strip_none_keys)
21+
ObjectNotFound, TooManyArguments, strip_none_keys)
2222

2323
# If we are running the client from tests with Allure we expected that code
2424
# to trace steps in Allure UI.
@@ -937,8 +937,22 @@ def _upload(self, bundle_stream) -> Bundle:
937937
return self.bundle(bundle_id=data['id'])
938938

939939
@allure_step('Upload bundle from "{1}"')
940-
def upload_from_fs(self, dirname) -> Bundle:
941-
return self._upload(stream.file(dirname))
940+
def upload_from_fs(self, dirname, **args) -> Bundle:
941+
streams = stream.file(dirname, **args)
942+
if len(streams) > 1:
943+
raise TooManyArguments('upload_bundle_from_fs is not capable with building multiple \
944+
bundle editions from one dir. Use upload_from_fs_all instead.')
945+
return self._upload(streams[0])
946+
947+
@allure_step('Upload bundles from "{1}"')
948+
def upload_from_fs_all(self, dirname, **args) -> BundleList:
949+
streams = stream.file(dirname, **args)
950+
# Create empty bundle list by filtering on wittingly nonexisting field
951+
# and value.
952+
result = BundleList(self._api, empty_bundlelist='not_existing_bundle')
953+
for st in streams:
954+
result.append(self._upload(st))
955+
return result
942956

943957
@allure_step('Upload bundle from "{1}"')
944958
def upload_from_url(self, url) -> Bundle:

adcm_client/packer/add_to_tar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def compare_helper(version, except_list):
2626
if version is None:
2727
except_list.extend([
2828
'spec.yaml', 'pylintrc', '.[0-9a-zA-Z]*', '*pycache*',
29-
'README.md', '*test*', '*requirements*', '*.gz', '*.md'])
29+
'README.md', '*requirements*', '*.gz', '*.md'])
3030

3131
def v_None(sub: DirEntry):
3232
for n in except_list:

adcm_client/packer/bundle_build.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def _prepare_result_dir(workspace, tarball_path):
4040

4141

4242
def _pack(reponame, repopaths, tarpaths, spec: SpecFile, **kwargs):
43-
tarballs = {}
4443
for edition in spec.data['editions']:
4544
name = edition.get('name')
4645
tarpath = tarpaths[name] if isinstance(tarpaths, dict) else tarpaths
@@ -56,12 +55,9 @@ def _pack(reponame, repopaths, tarpaths, spec: SpecFile, **kwargs):
5655
logging.info("#######\n Edition %s \n#######", name)
5756
logging.info("#######\n Packed files list:\n%s\n#######", "\n".join(tar.getnames()))
5857
tar.close()
58+
stream.seek(0)
5959
# saving tarball
60-
tarballs.update({name: os.path.join(tarpath, tarname)})
61-
with open(tarballs[name], 'wb') as file:
62-
stream.seek(0)
63-
file.write(stream.read())
64-
return {'tarballs': tarballs}
60+
yield os.path.join(tarpath, tarname), stream
6561

6662

6763
def _clean_ws(path):
@@ -72,44 +68,59 @@ def _clean_ws(path):
7268
remove_tree(path)
7369

7470

75-
def build(reponame, repopath, # pylint: disable=W0102, R0913
76-
workspace='/tmp', tarball_path=None, loglevel='ERROR',
77-
clean_ws=True, master_branches=['master'], autotest=False):
71+
def build(reponame=None, repopath=None, workspace='/tmp', # pylint: disable=R0913
72+
tarball_path=None, loglevel='ERROR',
73+
clean_ws=True, master_branches=None, **args):
7874
"""Moves sources to workspace inside of temporary directory. \
7975
Some operations over sources cant be proceed concurent(for exemple in pytest with xdist \
8076
plugin) that why each thread need is own tmp dir with sources. \
8177
Also when there is complex docker containers launching to process some information there is \
8278
necessery to share same workspace with every used container.
8379
Proceed spec file.
8480
Writes build number to bundle config file.
85-
Recursively add files to tarball.
81+
Recursively add files to bytes stream.
82+
Cleanup tmp dirs.
8683
87-
:param reponame: arenadata repository name. Used for naming aftifact and tmp dir.
88-
:type reponame: str
8984
:param repopath: Where bundle sources are
9085
:type repopath: str
86+
:param reponame: arenadata repository name. Used for naming aftifact and tmp dir.
87+
:type reponame: str
9188
:param workspace: where build operations will be performed, defaults to /tmp.
9289
:type workspace: str, optional
9390
:param tarball_path: where to copy builded bundle, defaults to None.
9491
None means that tarball will be left in temporary directory inside of workspace.
9592
:type tarball_path: str, optional
9693
:param loglevel: lower or equal to INFO will be stdout
9794
:type loglevel: str, optional
98-
:return: return a dict. Keys:
99-
tarball - path to aftifact
95+
:return: return a dict.
96+
Keys - path and name of tarball to save.
97+
Value - stream of bytes.
10098
:rtype: dict
10199
"""
100+
if not master_branches:
101+
master_branches = ['master']
102+
if not repopath:
103+
raise ValueError('path to source should be defined')
104+
if not reponame:
105+
reponame = os.path.basename(os.path.realpath(repopath))
106+
102107
logging.basicConfig(stream=sys.stdout, level=getattr(logging, loglevel))
103108
spec = SpecFile(os.path.join(repopath, 'spec.yaml'))
104109
spec.normalize_spec()
110+
105111
ws_tepm_dir, work_dir_paths = _prepare_ws(reponame, workspace, repopath, spec)
106-
if autotest:
107-
tarpath = _prepare_result_dir(work_dir_paths, tarball_path)
108-
else:
109-
tarpath = _prepare_result_dir(workspace, tarball_path)
112+
113+
tarpath = _prepare_result_dir(workspace, tarball_path)
110114
spec_processing(spec, work_dir_paths, workspace)
111115

112-
out = _pack(reponame, work_dir_paths, tarpath, spec, master_branches=master_branches)
116+
out = dict(
117+
_pack(
118+
reponame,
119+
work_dir_paths,
120+
tarpath,
121+
spec,
122+
master_branches=master_branches))
123+
113124
if clean_ws:
114125
_clean_ws(ws_tepm_dir)
115126

adcm_client/packer/naming_rules.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def write_version(file, old_version, new_version):
3939
config.truncate()
4040
config.write(data)
4141

42+
edition = "community" if edition is None or edition == "None" else edition
43+
bundle = ConfigData(catalog=path)
44+
version = bundle.get_data('version', 'catalog', explict_raw=True)
45+
46+
if version is None:
47+
raise NoVersionFound('No version detected').with_traceback(sys.exc_info()[2])
48+
4249
try:
4350
git = Repo(path).git
4451
if git.describe('--all').split('/')[0] == 'tags':
@@ -58,18 +65,12 @@ def write_version(file, old_version, new_version):
5865
branch = '-' + branch.replace("-", "_")
5966
except InvalidGitRepositoryError:
6067
branch = ''
61-
62-
bundle = ConfigData(catalog=path, branch='origin/master')
63-
version = bundle.get_data('version', 'catalog', explict_raw=True)
64-
65-
if version is None:
66-
raise NoVersionFound('No version detected').with_traceback(sys.exc_info()[2])
67-
if '-' in version:
68-
raise RestrictedSymbol('Version contains restricted symbol \
69-
"-" in position %s' % version.index('-')).with_traceback(sys.exc_info()[2])
70-
71-
if edition is None or edition == "None":
72-
edition = "ce"
73-
if branch:
68+
else:
69+
if not isinstance(version, str):
70+
raise TypeError('Bundle version must be string').with_traceback(sys.exc_info()[2])
71+
if '-' in version:
72+
raise RestrictedSymbol('Version contains restricted symbol \
73+
"-" in position %s' % version.index('-')).with_traceback(sys.exc_info()[2])
7474
write_version(bundle.file, version, version + branch)
75+
7576
return str(reponame) + '_v' + str(version) + branch + '_' + edition + '.tgz'

adcm_client/util/stream.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,21 @@
1212
import gzip
1313
import io
1414
import os
15-
import tarfile
1615

1716
import requests
17+
from adcm_client.packer.bundle_build import build
1818

1919

20-
def file(path):
21-
stream = None
20+
def file(path, **args):
2221
if os.path.isdir(path):
23-
stream = io.BytesIO()
24-
tar = tarfile.TarFile(fileobj=stream, mode="w")
25-
for sub in os.listdir(path):
26-
tar.add(os.path.join(path, sub), arcname=sub)
27-
tar.close()
22+
return list(build(repopath=path, **args).values())
2823
else:
2924
stream = io.BytesIO()
3025
try:
3126
stream.write(gzip.open(path, 'rb').read())
3227
except OSError:
3328
stream.write(io.open(path, 'rb').read())
34-
stream.seek(0)
35-
return stream
29+
return [stream.seek(0)]
3630

3731

3832
def web(url):

bin/adcm_sdk_pack

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from adcm_client.packer.bundle_build import build
1717

1818
if __name__ == "__main__":
1919
parser = argparse.ArgumentParser(description='Builds bundles from sources.')
20-
parser.add_argument('-n', '--name', type=str, help='bundle name', required=True)
20+
parser.add_argument('-n', '--name', type=str, help='bundle name', required=False)
2121
parser.add_argument('-p', '--path', type=str, help='path where to seek configs', required=True)
2222
parser.add_argument('-t', '--tarpath', type=str, help='path where to save', default=None)
2323
parser.add_argument('-w', '--workspace', type=str, help='workspace where to build',
@@ -26,8 +26,16 @@ if __name__ == "__main__":
2626
action="store_false")
2727
parser.add_argument('-m', '--master-branches', nargs='+',
2828
help='non default master branches', default=['master'])
29+
parser.add_argument('-v', '--verbose', action="store_true", help='show info', required=False)
2930
args = parser.parse_args()
30-
build(args.name, args.path,
31-
workspace=args.workspace, tarball_path=args.tarpath,
32-
loglevel='INFO', clean_ws=args.dont_clean_ws,
33-
master_branches=args.master_branches)
31+
32+
loglevel = 'INFO' if args.verbose else 'ERROR'
33+
34+
tarballs = build(args.name, args.path,
35+
workspace=args.workspace, tarball_path=args.tarpath,
36+
loglevel=loglevel, clean_ws=args.dont_clean_ws,
37+
master_branches=args.master_branches)
38+
39+
for edition in tarballs:
40+
with open(edition, 'wb') as file:
41+
file.write(tarballs[edition].read())

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
setuptools.setup(
2222
name="adcm_client",
23-
version="2020.02.07.13",
23+
version="2020.02.11.12",
2424
author="Anton Chevychalov",
2525
author_email="cab@arenadata.io",
2626
description="ArenaData Cluster Manager Client",

0 commit comments

Comments
 (0)