Skip to content

Commit

Permalink
fix the codefactor issues, add tarball_dir as tarball download destin…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
definite committed Sep 5, 2018
1 parent 968c1a2 commit 177632b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
20 changes: 13 additions & 7 deletions ZanataArgParser.py
Expand Up @@ -65,16 +65,22 @@ def __init__(self, patern):

@staticmethod
def _color(colors, content):
if os.getenv('LOGGING_NO_COLOR', ''):
return
return "\033[%d;%dm%s\033[0m" % (colors[0], colors[1], content)

def format(self, record):
colors = ColoredFormatter.COLOR_MAPPING.get(
record.levelname, ColoredFormatter.COLOR_MAPPING['DEBUG'])
record.levelname = ColoredFormatter._color(colors, record.levelname)
record.message = ColoredFormatter._color(colors, record.getMessage())
if self.usesTime():
record.asctime = ColoredFormatter._color(
colors, self.formatTime(record, self.datefmt))
if not os.getenv('LOGGING_NO_COLOR', ''):
# Turn of color with env LOGGING_NO_COLOR=1
colors = ColoredFormatter.COLOR_MAPPING.get(
record.levelname, ColoredFormatter.COLOR_MAPPING['DEBUG'])
record.levelname = ColoredFormatter._color(
colors, record.levelname)
record.message = ColoredFormatter._color(
colors, record.getMessage())
if self.usesTime():
record.asctime = ColoredFormatter._color(
colors, self.formatTime(record, self.datefmt))
try:
s = self._fmt % record.__dict__
except UnicodeDecodeError as e:
Expand Down
10 changes: 4 additions & 6 deletions ZanataFunctions.py
Expand Up @@ -77,7 +77,7 @@ def exec_call(cmd_list, **kwargs):
int: exit status of command.
"""
logging.debug("Running command: %s", " ".join(cmd_list))
return subprocess.call(cmd_list, **kwargs)
return subprocess.call(cmd_list, **kwargs) # nosec


def exec_check_call(cmd_list, **kwargs):
Expand All @@ -102,7 +102,7 @@ def exec_check_call(cmd_list, **kwargs):
"""
logging.debug("Running command: %s", " ".join(cmd_list))
try:
return subprocess.check_call(cmd_list, **kwargs)
return subprocess.check_call(cmd_list, **kwargs) # nosec
except subprocess.CalledProcessError as e:
raise e

Expand Down Expand Up @@ -130,7 +130,7 @@ def exec_check_output(cmd_list, **kwargs):
"""
logging.debug("Running command: %s", " ".join(cmd_list))
try:
return subprocess.check_output(cmd_list, **kwargs).rstrip()
return subprocess.check_output(cmd_list, **kwargs).rstrip() # nosec
except subprocess.CalledProcessError as e:
raise e

Expand Down Expand Up @@ -416,9 +416,7 @@ def rsync(self, src, dest, options=None):

if options:
cmd_prefix += options
full_cmd = cmd_prefix + [src, dest]
logging.debug(" ".join(full_cmd))
subprocess.check_call(full_cmd)
exec_check_call(cmd_prefix + [src, dest])


class UrlHelper(object):
Expand Down
50 changes: 34 additions & 16 deletions ZanataRpmRepo.py
Expand Up @@ -23,12 +23,12 @@

import logging
import os
import subprocess
import sys

from ZanataArgParser import ZanataArgParser # pylint: disable=E0401
from ZanataFunctions import GitHelper, SshHost, WORK_ROOT
from ZanataFunctions import mkdir_p, working_directory
from ZanataFunctions import exec_check_call, exec_check_output

try:
# We need to import 'List' and 'Any' for mypy to work
Expand Down Expand Up @@ -71,11 +71,16 @@ def pull(self):
self.rsync(src_dir, self.local_dir, ['--delete'])

def update_epel_repos(
self, spec_file, version='auto', dist_versions=None):
self, spec_file, version='auto',
tarball_dir=None, dist_versions=None):
"""Update all EPEL repositories
Args:
spec_file (str): RPM spec file
tarball_dir (str): Default to None.
Tarballs are downloaded to this directory.
If not specified, it downloads inside container,
which you cannot reused.
dist_versions (List[str]): Defaults to ["7", "6"].
List of distrion versions to update.
"""
Expand All @@ -84,7 +89,7 @@ def update_epel_repos(
for dist in dist_versions:
logging.info("Update EL%s repo", dist)
elrepo = ElRepo(dist, self.local_dir)
elrepo.build_and_update(spec_file, version)
elrepo.build_and_update(spec_file, version, tarball_dir)

def push(self):
# type (str) -> None
Expand Down Expand Up @@ -128,28 +133,42 @@ def __init__(self, dist_ver, local_dir=LOCAL_DIR):
self.dist_ver = dist_ver
self.local_dir = local_dir

def build_and_update(self, spec_file, version=None):
# type () -> None
"""Build RPM and update dnf/yum repository
def build_and_update(self, spec_file, version=None, tarball_dir=None):
# type (str, str, str) -> None
"""build RPM and update yum repo
This program uses docker container,
docker.io/zanata/centos-repo-builder,
to update the repository.
Args:
spec_file (str): RPM spec file
spec_file (str): RPM spec file.
This should be related to local_dir.
version (str, optional): Defaults to None.
tarball_dir ([type], optional): Defaults to None.
tarballs are downloaded to this directory.
"""
docker_cmd = '/usr/bin/docker'
with working_directory(self.local_dir):
volume_name = "zanata-el-%s-repo" % self.dist_ver
vols = subprocess.check_output([
'docker', 'volume', 'ls', '-q']).rstrip().split('\n')
vols = exec_check_output([
docker_cmd, 'volume', 'ls', '-q']).split('\n')
if volume_name not in vols:
subprocess.check_call([
'docker', 'volume', 'create', '--name', volume_name])
exec_check_call([
docker_cmd, 'volume', 'create', '--name', volume_name])

docker_run_cmd = [
"docker", "run", "--rm", "--name",
docker_cmd, "run", "--rm", "--name",
"zanata-el-{}-builder".format(self.dist_ver),
"-v", "{}:/repo:Z".format(volume_name),
"-v", "/tmp:/rpmbuild/SOURCES:Z",
"-v", "{}:/repo_host_dir:Z".format(self.local_dir),
"-v", "{}:/output_dir:Z".format(self.local_dir),
"-v", "{}:/output_dir:Z".format(self.local_dir)]

if tarball_dir:
docker_run_cmd += [
"-v", "%s:/rpmbuild/SOURCES:Z" % tarball_dir]

docker_run_cmd += [
"docker.io/zanata/centos-repo-builder:{}".format(
self.dist_ver),
"-S", "/repo_host_dir/",
Expand All @@ -166,8 +185,7 @@ def build_and_update(self, spec_file, version=None):
docker_run_cmd += ['-u', version]

docker_run_cmd.append(spec_file)
logging.info("Run command: %s", " ".join(docker_run_cmd))
subprocess.check_call(docker_run_cmd)
exec_check_call(docker_run_cmd)


def main(argv=None):
Expand Down

0 comments on commit 177632b

Please sign in to comment.