diff options
author | Chulwoo Shin <cw1.shin@samsung.com> | 2017-05-17 18:27:17 +0900 |
---|---|---|
committer | Chulwoo Shin <cw1.shin@samsung.com> | 2017-05-17 18:27:17 +0900 |
commit | 71efb9511db8ba5ad849f5ba841a2a86f433f885 (patch) | |
tree | a83a402b916d031c3a34721257c24f5eb71d28e0 | |
parent | d79ac30dfe99fee5248a32a8200bb5ee50db637a (diff) | |
download | tic-core-71efb9511db8ba5ad849f5ba841a2a86f433f885.tar.gz tic-core-71efb9511db8ba5ad849f5ba841a2a86f433f885.tar.bz2 tic-core-71efb9511db8ba5ad849f5ba841a2a86f433f885.zip |
[TIC-CORE] Improve code quality
Change-Id: I69684bfafe2a817435290fb154946ec8656a5f3f
Signed-off-by: Chulwoo Shin <cw1.shin@samsung.com>
-rw-r--r-- | test/test_dependency.py | 6 | ||||
-rw-r--r-- | tic/dependency.py | 29 | ||||
-rw-r--r-- | tic/parser/recipe_parser.py | 55 | ||||
-rw-r--r-- | tic/parser/repo_parser.py | 3 | ||||
-rw-r--r-- | tic/parser/view_parser.py | 4 | ||||
-rw-r--r-- | tic/repo.py | 7 | ||||
-rw-r--r-- | tic/server/tic_server.py | 11 | ||||
-rw-r--r-- | tic/utils/file.py | 6 | ||||
-rw-r--r-- | tic/utils/log.py | 12 | ||||
-rw-r--r-- | tic/utils/process.py | 10 | ||||
-rw-r--r-- | tic/utils/rpmmisc.py | 10 |
11 files changed, 73 insertions, 80 deletions
diff --git a/test/test_dependency.py b/test/test_dependency.py index b09100f..d7ffcb6 100644 --- a/test/test_dependency.py +++ b/test/test_dependency.py @@ -480,7 +480,7 @@ class Zypp(): localpkgs = self.localpkgs.keys() for pkg in dlpkgs: # if pkg.name() in localpkgs: -# hdr = rpmmisc.readRpmHeader(self.ts, self.localpkgs[pkg.name()]) +# hdr = rpmmisc.read_rpm_header(self.ts, self.localpkgs[pkg.name()]) # install_pkgs[pkg.name()] = {'name': pkg.name(), # 'arch': pkg.arch(), # 'version': pkg.edition().version(), @@ -639,7 +639,7 @@ class Zypp(): else: print('Can not get %s solv data.' % pkg) - hdr = rpmmisc.readRpmHeader(self.ts, pkg) + hdr = rpmmisc.read_rpm_header(self.ts, pkg) arch = zypp.Arch(hdr['arch']) sysarch = zypp.Arch(self.target_arch) @@ -714,7 +714,7 @@ class Zypp(): if not os.path.exists(rpmpath): raise RpmError("Error: %s doesn't exist" % rpmpath) - h = rpmmisc.readRpmHeader(self.ts, rpmpath) + h = rpmmisc.read_rpm_header(self.ts, rpmpath) if pkgname in self.pre_pkgs: print("pre-install package added: %s" % pkgname) diff --git a/tic/dependency.py b/tic/dependency.py index af98678..0c71555 100644 --- a/tic/dependency.py +++ b/tic/dependency.py @@ -19,7 +19,7 @@ import logging from lxml import etree from tic.utils.error import TICError -from tic.utils.rpmmisc import meetRequireVersion, compare_ver +from tic.utils.rpmmisc import meet_require_version, compare_ver from tic.utils.rpmmisc import Dependency from tic.config import configmgr @@ -41,22 +41,22 @@ def get_installed_packages(recipe, repoinfo, pkg_group): selected = pkg_dict.get(dep_rpm['name']) return selected - def _select_rpm_from_files(fileList, require): - if not fileList or not require: + def _select_rpm_from_files(file_list, require): + if not file_list or not require: return None # 1. sort - fileList.sort() + file_list.sort() # 2-1. Choose the default rpm or the selected rpm - for fname in fileList: + for fname in file_list: file_info = pkg_dict.get(fname) if fname in required_inst_rpms or selected[file_info['id']] >= 1: return file_info # 2-2. Choose the rpm (it does not conflitcs) - for fname in fileList: + for fname in file_list: file_info = pkg_dict.get(fname) if not _check_conflicts(file_info): return file_info - return pkg_dict.get(fileList[0]) + return pkg_dict.get(file_list[0]) def _select_rpm_from_provides(capability, require, recommends=None): provide_list = [] @@ -67,7 +67,7 @@ def get_installed_packages(recipe, repoinfo, pkg_group): # If there is no capability version, use version of package if not ver_data.get('ver'): ver_data = pkg_dict.get(provide['name']).get('version') - if meetRequireVersion(require, ver_data): + if meet_require_version(require, ver_data): provide_list.append(provide) else: provide_list = capability @@ -169,7 +169,7 @@ def get_installed_packages(recipe, repoinfo, pkg_group): for pro in pkg_info['provides']: if pro['name'] in conflicts: for con in conflicts[pro['name']]: - if not con['data'].get('ver') or meetRequireVersion(con['data'], pro): + if not con['data'].get('ver') or meet_require_version(con['data'], pro): #package conflict #logger.info('Conflict Case-1) %s is conflict with %s' % (pkg_info['name'], con['name'])) return pkg_dict.get(con['name']) @@ -181,13 +181,13 @@ def get_installed_packages(recipe, repoinfo, pkg_group): for pro in provides[con['name']]: pkg = pkg_dict[pro['name']] if selected[pkg['id']] != 0: - if not con.get('ver') or meetRequireVersion(con, pro['data']): + if not con.get('ver') or meet_require_version(con, pro['data']): #logger.info('Conflict Case-2) %s is conflict with %s' % (pkg['name'], pkg_info['name'])) return pkg elif con['name'] in pkg_dict: pkg = pkg_dict[con['name']] if selected[pkg['id']] != 0: - if not con.get('ver') or meetRequireVersion(con, pkg['version']): + if not con.get('ver') or meet_require_version(con, pkg['version']): #logger.info('Conflict Case-2) %s is conflict with %s' % (pkg['name'], pkg_info['name'])) return pkg return None @@ -298,7 +298,7 @@ def get_installed_packages(recipe, repoinfo, pkg_group): pkg['backward'] = None group_visited[g_id][pkg['name']] = -1 return True - + def _delete_conflictdata(node): if node.get('conflicts'): for con in node.get('conflicts'): @@ -357,7 +357,7 @@ def get_installed_packages(recipe, repoinfo, pkg_group): pro = provides[pkg_name][0] return pkg_dict[pro['name']] return None - + def is_compatible_pkg (origin_pkg, new_pkg): if origin_pkg['name'] in require_refer: for r_name in require_refer[origin_pkg['name']]: @@ -375,7 +375,7 @@ def get_installed_packages(recipe, repoinfo, pkg_group): if not matched: return False return True - + def _is_selfchecked(pkg_info): if pkg_info.get('selfChecked'): return True @@ -583,7 +583,6 @@ def get_installed_packages(recipe, repoinfo, pkg_group): for idx, value in enumerate([select_rpms, select_week_rpms]): for pkg_name in value: progress['status'] = True - #selected_pkg = _get_pkg_info(pkg_name) selected_pkg = _select_rpm_by_name(pkg_name) if selected_pkg: rpm_stack = [] diff --git a/tic/parser/recipe_parser.py b/tic/parser/recipe_parser.py index 5165c64..4ab310d 100644 --- a/tic/parser/recipe_parser.py +++ b/tic/parser/recipe_parser.py @@ -179,17 +179,17 @@ class RecipeParser(object): recipe_name = recipe_info['Recipe'].get('Name') if recipe_info['Recipe'].get('Repos'): for repo_name in recipe_info['Recipe'].get('Repos'): - isExist = False + is_exist = False if recipe_info.get('Repositories'): for repo_info in recipe_info.get('Repositories'): if repo_info.get('Name') == repo_name: recipe_repos.append(dict(name=repo_name, url=repo_info.get('Url'), options=repo_info.get('Options'))) - isExist = True + is_exist = True break # repository does not exist - if not isExist: + if not is_exist: raise TICError(configmgr.message['recipe_repo_not_exist'] % repo_name) if not recipe_name: recipe_name = 'recipe_%s' % name_count @@ -202,10 +202,11 @@ class RecipeParser(object): repos.append(data) return repos - def _rename_repository(self, repo_dict, repo_name): + @classmethod + def _rename_repository(cls, repo_dict, repo_name): number = repo_dict.get(repo_name) new_name = ''.join([repo_name, '_', str(number)]) - while(new_name in repo_dict): + while new_name in repo_dict: number += 1 new_name = ''.join([repo_name, '_', str(number)]) repo_dict[repo_name] = number + 1 @@ -217,7 +218,7 @@ class RecipeParser(object): repo_name = {} # 'name': count repo_url = {} # 'url': exist for target in repositories: - if target.get('type') == 'recipe': + if target['type'] == 'recipe': if target.get('repos'): for repo in target.get('repos'): # if repo's url is duplicated, remove it. @@ -233,7 +234,7 @@ class RecipeParser(object): else: # recipe does not have repository information pass - elif(target.get('type') == 'repository'): + elif target['type'] == 'repository': # if repo's url is duplicated, remove it. if target.get('url') in repo_url: continue @@ -249,7 +250,7 @@ class RecipeParser(object): if self._recipe: return self._recipe - mergedInfo = default_recipe.get_system_config() + merged_info = default_recipe.get_system_config() # merge recipe info for i in xrange(len(self.inputs), 0, -1): if self.inputs[i-1].get('type') == 'recipe': @@ -262,28 +263,28 @@ class RecipeParser(object): if k == 'Repos': continue for j in xrange(len(v), 0, -1): - mergedInfo['Recipe'][k].append(v[j-1]) + merged_info['Recipe'][k].append(v[j-1]) else: - mergedInfo['Recipe'][k] = v - for fieldName in RECIPE_EXTEND_FIELD: - if recipe.get(fieldName): - if fieldName == 'Repositories': + merged_info['Recipe'][k] = v + for field_name in RECIPE_EXTEND_FIELD: + if recipe.get(field_name): + if field_name == 'Repositories': continue - for data in recipe.get(fieldName): - mergedInfo[fieldName].append(data) + for data in recipe.get(field_name): + merged_info[field_name].append(data) # reverse order - for extName in RECIPE_EXTEND_FIELD: - if mergedInfo['Recipe'].get(extName): - mergedInfo['Recipe'][extName].reverse() - if mergedInfo.get(extName): - mergedInfo[extName].reverse() + for ext_name in RECIPE_EXTEND_FIELD: + if merged_info['Recipe'].get(ext_name): + merged_info['Recipe'][ext_name].reverse() + if merged_info.get(ext_name): + merged_info[ext_name].reverse() # set repositories - mergedInfo['Repositories'] = self.get_merged_repositories() - if mergedInfo.get('Repositories'): - for repo in mergedInfo['Repositories']: - mergedInfo['Recipe']['Repos'].append(repo['name']) - return mergedInfo + merged_info['Repositories'] = self.get_merged_repositories() + if merged_info.get('Repositories'): + for repo in merged_info['Repositories']: + merged_info['Recipe']['Repos'].append(repo['name']) + return merged_info def export_recipe(self, packages, outdir, filename): logger = logging.getLogger(__name__) @@ -388,9 +389,7 @@ def load_yaml(path): logger.info(err) raise TICError(configmgr.message['recipe_parse_error'] % os.path.basename(path)) -YamlType = collections.namedtuple('YamlInfo', 'cachedir, configs, repos') -def YamlInfo(cachedir, configs, repos): - return YamlType(cachedir, configs, repos) +YamlInfo = collections.namedtuple('YamlInfo', 'cachedir, configs, repos') if __name__ == '__main__': inputs = [{'url': DEFAULT_RECIPE_NAME, 'type': 'recipe'}, {'url': 'http://localhost/repo/recipe/recipe1.yaml', 'type': 'recipe'}] diff --git a/tic/parser/repo_parser.py b/tic/parser/repo_parser.py index f787879..5ae728d 100644 --- a/tic/parser/repo_parser.py +++ b/tic/parser/repo_parser.py @@ -131,7 +131,7 @@ class RepodataParser(object): pkg_info['installed'] = size_tag.attrib['installed'] format_tag = pkg.find(tag_dic['format']) - if(format_tag is not None): + if format_tag is not None: requires_tag = format_tag.find(tag_dic['requires']) if requires_tag is not None: dep_list = [] @@ -229,7 +229,6 @@ class RepodataParser(object): def _get_tagname(self, root): tags = {} - # xmlns = re.sub('metadata$', '', root.tag) tags['metadata'] = '{%s}metadata' % root.nsmap[None] tags['package'] = '{%s}package' % root.nsmap[None] tags['name'] = '{%s}name' % root.nsmap[None] diff --git a/tic/parser/view_parser.py b/tic/parser/view_parser.py index bb6f06d..9bfd7c4 100644 --- a/tic/parser/view_parser.py +++ b/tic/parser/view_parser.py @@ -20,7 +20,7 @@ import logging import requests import re from operator import itemgetter -from tic.utils.rpmmisc import meetRequireVersion +from tic.utils.rpmmisc import meet_require_version def make_view_data(pkg_group): @@ -33,7 +33,7 @@ def make_view_data(pkg_group): ver_data = provide['data'] if not ver_data.get('ver'): ver_data = pkg_dict.get(provide['name']).get('version') - if meetRequireVersion(require, ver_data): + if meet_require_version(require, ver_data): provide_list.append(provide) else: provide_list = capability diff --git a/tic/repo.py b/tic/repo.py index a7c09d4..96c0146 100644 --- a/tic/repo.py +++ b/tic/repo.py @@ -151,9 +151,4 @@ def get_repodata_from_repos(repos, cachedir): "proxies":None, "patterns":fpath_info['patterns'], "comps":fpath_info['comps']}) - return repodata - - -RepoType = collections.namedtuple('Repo', 'name, url') -def Repo(name, baseurl): - return RepoType(name, baseurl)
\ No newline at end of file + return repodata
\ No newline at end of file diff --git a/tic/server/tic_server.py b/tic/server/tic_server.py index 76aa2fd..d74eac0 100644 --- a/tic/server/tic_server.py +++ b/tic/server/tic_server.py @@ -44,7 +44,6 @@ def analysis(): logger.info('%s - %s %s : data=%s' % (request.remote_addr, request.method, request.path, request.data)) target_info = json.loads(request.data) view_data = command.analyze(target_info.get('recipes')) - #view_data = command.analyze(None) resp = makeresponse(view_data, None) except error.TICError as err: logger.error(err) @@ -78,9 +77,9 @@ def exports(): try: logger = logging.getLogger(__name__) logger.info('%s - %s %s : data=%s' % (request.remote_addr, request.method, request.path, request.data)) - exportInfo = json.loads(request.data) + export_info = json.loads(request.data) export_type = request.args.get('format') - output = command.exports(export_type, exportInfo.get('recipes'), exportInfo.get('packages'), exportInfo.get('outdir'), exportInfo.get('filename')) + output = command.exports(export_type, export_info.get('recipes'), export_info.get('packages'), export_info.get('outdir'), export_info.get('filename')) resp = makeresponse(output, None) except error.TICError as err: logger.error(err) @@ -113,9 +112,9 @@ def makeresponse(data, err): elif isinstance(err, Exception): status = 500 # Internal Server Error data='Internal Server Error' - res_body = json.dumps(ResultInfo('false', None, data)._asdict()) + res_body = json.dumps(make_result_info('false', None, data)._asdict()) else: - res_body = json.dumps(ResultInfo('true', data, None)._asdict()) + res_body = json.dumps(make_result_info('true', data, None)._asdict()) resp = Response(status=status, mimetype='application/json', @@ -123,7 +122,7 @@ def makeresponse(data, err): return resp ResultInfoType = collections.namedtuple('ResultInfo', 'result, data, message') -def ResultInfo(result='false', data=None, message=None): +def make_result_info(result='false', data=None, message=None): return ResultInfoType(result, data, message) if __name__ == '__main__': diff --git a/tic/utils/file.py b/tic/utils/file.py index 08952f0..1cf49cb 100644 --- a/tic/utils/file.py +++ b/tic/utils/file.py @@ -59,14 +59,14 @@ def make_dirs(path): def write(path, data): # make directory make_dirs(os.path.dirname(path)) - with(open(path, 'w')) as f: + with open(path, 'w') as f: f.write(data) def write_json_flock(path, data): try: make_dirs(os.path.dirname(path)) with FileLock(path): - with(open(path, 'w')) as f: + with open(path, 'w') as f: f.write(json.dumps(data)) except OSError as e: if e.errno != errno.EEXIST: @@ -84,7 +84,7 @@ def read_json(path): return ret def decompress_gzip(intput_path, output_path): - with(gzip.open(intput_path, 'rb')) as fobj: + with gzip.open(intput_path, 'rb') as fobj: f = open(output_path, 'wb') f.write(fobj.read()) f.close() diff --git a/tic/utils/log.py b/tic/utils/log.py index 084fff6..bfbb728 100644 --- a/tic/utils/log.py +++ b/tic/utils/log.py @@ -31,14 +31,14 @@ def setup(root): formatter.datefmt = '%Y-%m-%d %H:%M:%S' mkdir_p(TIC_LOG_DIR) - fileHandler = logging.handlers.RotatingFileHandler(os.path.join(TIC_LOG_DIR, 'tic-core.log'), maxBytes=LOG_FILE_MAX_BYTES, backupCount=10) - streamHandler = logging.StreamHandler() + filehandler = logging.handlers.RotatingFileHandler(os.path.join(TIC_LOG_DIR, 'tic-core.log'), maxBytes=LOG_FILE_MAX_BYTES, backupCount=10) + streamhandler = logging.StreamHandler() - fileHandler.setFormatter(formatter) - streamHandler.setFormatter(formatter) + filehandler.setFormatter(formatter) + streamhandler.setFormatter(formatter) - logger.addHandler(fileHandler) - logger.addHandler(streamHandler) + logger.addHandler(filehandler) + logger.addHandler(streamhandler) def mkdir_p(path): try: diff --git a/tic/utils/process.py b/tic/utils/process.py index 1ae1db0..b154204 100644 --- a/tic/utils/process.py +++ b/tic/utils/process.py @@ -26,11 +26,11 @@ def run(cmdln, catch=3): logger = logging.getLogger(__name__) if isinstance(cmdln, list): cmd = cmdln[0] - shellType = False + shell_type = False else: import shlex cmd = shlex.split(cmdln)[0] - shellType = True + shell_type = True if catch == 0: @@ -52,7 +52,7 @@ def run(cmdln, catch=3): p = subprocess.Popen(cmdln, stdout=sout, stderr=serr, - shell=shellType) + shell=shell_type) (sout, serr) = p.communicate() out = ''.join(filter(None, [sout, serr])) @@ -65,5 +65,5 @@ def run(cmdln, catch=3): finally: if catch == 0: os.close(dev_null) - - return (p.returncode, out)
\ No newline at end of file + result = (p.returncode, out) + return result
\ No newline at end of file diff --git a/tic/utils/rpmmisc.py b/tic/utils/rpmmisc.py index 70c390c..6c8a46c 100644 --- a/tic/utils/rpmmisc.py +++ b/tic/utils/rpmmisc.py @@ -53,11 +53,13 @@ def compare_req_cap_ver(req, cap): epoch = cap.get('epoch') ver = cap.get('ver') rel = cap.get('rel') - if not req.get('epoch'): epoch = None - if not req.get('rel'): rel = None + if not req.get('epoch'): + epoch = None + if not req.get('rel'): + rel = None return rpm.labelCompare((req.get('epoch'), req.get('ver'), req.get('rel')), (epoch, ver, rel)) -def meetRequireVersion(req_ver, cmp_ver): +def meet_require_version(req_ver, cmp_ver): cmp_ret = compare_req_cap_ver(req_ver, cmp_ver) if cmp_ret == 0 and (req_ver['flags'] in ['EQ', 'GE', 'LE']): return True @@ -67,7 +69,7 @@ def meetRequireVersion(req_ver, cmp_ver): return True return False -def readRpmHeader(ts, filename): +def read_rpm_header(ts, filename): """ Read an rpm header. """ fd = os.open(filename, os.O_RDONLY) h = ts.hdrFromFdno(fd) |