Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge multiple if statements #1120

Closed
wants to merge 4 commits into from
Closed
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
128 changes: 83 additions & 45 deletions mock/py/mockbuild/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,53 +98,91 @@ def scrub(self, scrub_opts):
"""clean out chroot and/or cache dirs with extreme prejudice :)"""
statestr = "scrub %s" % scrub_opts
self.state.start(statestr)

try:
try:
self.plugins.call_hooks('clean')
self.plugins.call_hooks('clean')
if self.bootstrap_buildroot is not None:
self.bootstrap_buildroot.plugins.call_hooks('clean')

for scrub in scrub_opts:
self.plugins.call_hooks('scrub', scrub)
if self.bootstrap_buildroot is not None:
self.bootstrap_buildroot.plugins.call_hooks('clean')

for scrub in scrub_opts:
self.plugins.call_hooks('scrub', scrub)
if self.bootstrap_buildroot is not None:
self.bootstrap_buildroot.plugins.call_hooks('scrub', scrub)

if scrub == 'all':
self.buildroot.root_log.info("scrubbing everything for %s", self.config_name)
self.buildroot.delete()
file_util.rmtree(self.buildroot.cachedir, selinux=self.buildroot.selinux)
if self.bootstrap_buildroot is not None:
self.bootstrap_buildroot.delete()
file_util.rmtree(self.bootstrap_buildroot.cachedir,
selinux=self.bootstrap_buildroot.selinux)
elif scrub == 'chroot':
self.buildroot.root_log.info("scrubbing chroot for %s", self.config_name)
self.buildroot.delete()
elif scrub == 'cache':
self.buildroot.root_log.info("scrubbing cache for %s", self.config_name)
file_util.rmtree(self.buildroot.cachedir, selinux=self.buildroot.selinux)
elif scrub == 'c-cache':
self.buildroot.root_log.info("scrubbing c-cache for %s", self.config_name)
file_util.rmtree(os.path.join(self.buildroot.cachedir, 'ccache'),
selinux=self.buildroot.selinux)
elif scrub == 'root-cache':
self.buildroot.root_log.info("scrubbing root-cache for %s", self.config_name)
file_util.rmtree(os.path.join(self.buildroot.cachedir, 'root_cache'),
selinux=self.buildroot.selinux)
elif scrub in ['yum-cache', 'dnf-cache']:
self.buildroot.root_log.info("scrubbing yum-cache and dnf-cache for %s", self.config_name)
file_util.rmtree(os.path.join(self.buildroot.cachedir, 'yum_cache'),
selinux=self.buildroot.selinux)
file_util.rmtree(os.path.join(self.buildroot.cachedir, 'dnf_cache'),
selinux=self.buildroot.selinux)
elif scrub == 'bootstrap' and self.bootstrap_buildroot is not None:
self.buildroot.root_log.info("scrubbing bootstrap for %s", self.config_name)
self.bootstrap_buildroot.delete()
file_util.rmtree(self.bootstrap_buildroot.cachedir, selinux=self.bootstrap_buildroot.selinux)

except IOError as e:
getLog().warning("parts of chroot do not exist: %s", e)
raise
self.bootstrap_buildroot.plugins.call_hooks('scrub', scrub)

scrub_actions = {
'all': {
'msg': f"scrubbing everything for {self.config_name}",
'actions': [
self.buildroot.delete,
lambda: file_util.rmtree(self.buildroot.cachedir,
selinux=self.buildroot.selinux),
self.bootstrap_buildroot.delete,
(lambda: file_util.rmtree(self.bootstrap_buildroot.cachedir,
selinux=self.bootstrap_buildroot.selinux)
if self.bootstrap_buildroot is not None else None),
]
},
'chroot': {
'msg': f"scrubbing chroot for {self.config_name}",
'actions': [self.buildroot.delete]
},
'cache': {
'msg': f"scrubbing cache for {self.config_name}",
'actions': [lambda: file_util.rmtree(self.buildroot.cachedir,
selinux=self.buildroot.selinux)]
},
'c-cache': {
'msg': f"scrubbing c-cache for {self.config_name}",
'actions': [lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'ccache'),
selinux=self.buildroot.selinux)]
},
'root-cache': {
'msg': f"scrubbing root-cache for {self.config_name}",
'actions': [lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'root_cache'),
selinux=self.buildroot.selinux)]
},
'yum-cache': {
'msg': f"scrubbing yum-cache and dnf-cache for {self.config_name}",
'actions': [
lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'yum_cache'),
selinux=self.buildroot.selinux),
lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'dnf_cache'),
selinux=self.buildroot.selinux)
]
},
'dnf-cache': {
'msg': f"scrubbing yum-cache and dnf-cache for {self.config_name}",
'actions': [
lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'yum_cache'),
selinux=self.buildroot.selinux),
lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'dnf_cache'),
selinux=self.buildroot.selinux)
]
},
'bootstrap': {
'msg': f"scrubbing bootstrap for {self.config_name}",
'actions': [
self.bootstrap_buildroot.delete,
(lambda: file_util.rmtree(self.bootstrap_buildroot.cachedir,
selinux=self.bootstrap_buildroot.selinux)
if self.bootstrap_buildroot is not None else None)
]
}
}

try:
scrub_action = scrub_actions[scrub]
except KeyError as exc:
raise RuntimeError(f"Unknown scrub option '{scrub}'") from exc

self.buildroot.root_log.info(f"scrubbing {scrub_action['msg']} for {self.config_name}")
for action in scrub_action['actions']:
if action is not None:
action()

except IOError as e:
getLog().warning("parts of chroot do not exist: %s", e)
raise
finally:
self.state.finish(statestr)

Expand Down
55 changes: 31 additions & 24 deletions mock/py/mockbuild/file_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _initialize(cls):
cls.tmpdir = tempfile.mkdtemp()

@classmethod
def get(cls, pkg_url_or_local_file):
def get(cls, pkg_url_or_local_file, max_retry=3, timeout=10):
"""
If the pkg_url_or_local_file looks like a link, try to download it and
store it to a temporary directory - and return path to the local
Expand All @@ -32,7 +32,7 @@ def get(cls, pkg_url_or_local_file):
If the pkg_url_or_local_file is not a link, do nothing - just return
the pkg_url_or_local_file argument.
"""
pkg = pkg_url_or_local_file
pkg = pkg_url_or_local_file # 添加这一行
log = getLog()

url_prefixes = ['http://', 'https://', 'ftp://']
Expand All @@ -41,30 +41,37 @@ def get(cls, pkg_url_or_local_file):
return pkg

cls._initialize()
try:
url = pkg
log.info('Fetching remote file %s', url)
req = requests.get(url)
req.raise_for_status()
if req.status_code != requests.codes.ok:
log.error("Can't download {}".format(url))
return False
try_count = 0
while try_count < max_retry:
try:
url = pkg
log.info('Fetching remote file %s', url)
req = requests.get(url, timeout=timeout)
req.raise_for_status()

filename = urlsplit(req.url).path.rsplit('/', 1)[1]
if 'content-disposition' in req.headers:
_, params = cgi.parse_header(req.headers['content-disposition'])
if 'filename' in params and params['filename']:
filename = params['filename']
pkg = cls.tmpdir + '/' + filename
with open(pkg, 'wb') as filed:
for chunk in req.iter_content(4096):
filed.write(chunk)
cls.backmap[pkg] = url
return pkg
except Exception as err: # pylint: disable=broad-except
log.error('Downloading error %s: %s', url, str(err))
return None
filename = urlsplit(req.url).path.rsplit('/', 1)[1]
if 'content-disposition' in req.headers:
_, params = cgi.parse_header(req.headers['content-disposition'])
if 'filename' in params and params['filename']:
filename = params['filename']

pkg = cls.tmpdir + '/' + filename
with open(pkg, 'wb') as filed:
for chunk in req.iter_content(4096):
filed.write(chunk)

cls.backmap[pkg] = url
return pkg
except requests.exceptions.RequestException as err:
try_count += 1
log.error('Downloading error %s: %s (retrying...)', url, str(err))
time.sleep(2 ** try_count)

Check warning

Code scanning / vcs-diff-lint

FileDownloader.get: Undefined variable 'time'

FileDownloader.get: Undefined variable 'time'
except Exception as err:

Check warning

Code scanning / vcs-diff-lint

FileDownloader.get: Catching too general exception Exception

FileDownloader.get: Catching too general exception Exception
log.error('Error downloading %s: %s', url, str(err))
break

return None

Check warning

Code scanning / vcs-diff-lint

Trailing whitespace

Trailing whitespace

Check warning

Code scanning / vcs-diff-lint

Trailing whitespace

Trailing whitespace
@classmethod
def original_name(cls, localname):
""" Get the URL from the local name """
Expand Down