From a0766b1d70727156cdc0fcdf53082e46f6f2ceca Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 12 Oct 2017 15:55:55 +0300 Subject: [PATCH] Don't redefine variables from outer scope --- src/bin/image-minimizer | 8 ++++---- src/pylorax/creator.py | 4 ++-- src/pylorax/imgutils.py | 24 ++++++++++++------------ src/pylorax/ltmpl.py | 22 +++++++++++----------- src/pylorax/sysutils.py | 4 ++-- src/pylorax/treebuilder.py | 2 +- src/sbin/livemedia-creator | 9 ++++++--- src/sbin/mkefiboot | 5 ++++- utils/test-parse-template | 5 ++++- 9 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/bin/image-minimizer b/src/bin/image-minimizer index 297d4e52..27c3b52a 100755 --- a/src/bin/image-minimizer +++ b/src/bin/image-minimizer @@ -32,8 +32,8 @@ class ImageMinimizer: drops_rpm = set() ts = None - def __init__(self, filename, root, dryrun, verbose): - self.filename = filename + def __init__(self, _filename, root, dryrun, verbose): + self.filename = _filename self.prefix = root self.dryrun = dryrun self.verbose = verbose @@ -173,12 +173,12 @@ def parse_options(): parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Display every action as it is performed.") - (options, args) = parser.parse_args() + (_options, _args) = parser.parse_args() if len(args) == 0: parser.print_help() sys.exit(1) - return (options, args) + return (_options, _args) if __name__ == "__main__": diff --git a/src/pylorax/creator.py b/src/pylorax/creator.py index 5b2b3f40..d46294fe 100644 --- a/src/pylorax/creator.py +++ b/src/pylorax/creator.py @@ -51,8 +51,8 @@ def is_image_mounted(disk_img): Return True if the disk_img is mounted """ with open("/proc/mounts") as mounts: - for mount in mounts: - fields = mount.split() + for _mount in mounts: + fields = _mount.split() if len(fields) > 2 and fields[1] == disk_img: return True return False diff --git a/src/pylorax/imgutils.py b/src/pylorax/imgutils.py index 12f17ce2..fc38d5f3 100644 --- a/src/pylorax/imgutils.py +++ b/src/pylorax/imgutils.py @@ -233,24 +233,24 @@ def mount(dev, opts="", mnt=None): if mnt is None: mnt = tempfile.mkdtemp(prefix="lorax.imgutils.") logger.debug("make tmp mountdir %s", mnt) - mount = ["mount"] + cmd = ["mount"] if opts: - mount += ["-o", opts] - mount += [dev, mnt] - runcmd(mount) + cmd += ["-o", opts] + cmd += [dev, mnt] + runcmd(cmd) return mnt def umount(mnt, lazy=False, maxretry=3, retrysleep=1.0): '''Unmount the given mountpoint. If lazy is True, do a lazy umount (-l). If the mount was a temporary dir created by mount, it will be deleted. raises CalledProcessError if umount fails.''' - umount = ["umount"] - if lazy: umount += ["-l"] - umount += [mnt] + cmd = ["umount"] + if lazy: cmd += ["-l"] + cmd += [mnt] count = 0 while maxretry > 0: try: - rv = runcmd(umount) + rv = runcmd(cmd) except CalledProcessError: count += 1 if count == maxretry: @@ -335,7 +335,7 @@ class LoopDev(object): def __enter__(self): self.loopdev = loop_attach(self.filename) return self.loopdev - def __exit__(self, exc_type, exc_value, traceback): + def __exit__(self, exc_type, exc_value, exc_tb): loop_detach(self.loopdev) class DMDev(object): @@ -345,7 +345,7 @@ class DMDev(object): def __enter__(self): self.mapperdev = dm_attach(self.dev, self.size, self.name) return self.mapperdev - def __exit__(self, exc_type, exc_value, traceback): + def __exit__(self, exc_type, exc_value, exc_tb): dm_detach(self.mapperdev) class Mount(object): @@ -354,7 +354,7 @@ class Mount(object): def __enter__(self): self.mnt = mount(self.dev, self.opts, self.mnt) return self.mnt - def __exit__(self, exc_type, exc_value, traceback): + def __exit__(self, exc_type, exc_value, exc_tb): umount(self.mnt) class PartitionMount(object): @@ -412,7 +412,7 @@ class PartitionMount(object): os.rmdir(mount_dir) return self - def __exit__(self, exc_type, exc_value, traceback): + def __exit__(self, exc_type, exc_value, exc_tb): if self.mount_dir: umount( self.mount_dir ) os.rmdir(self.mount_dir) diff --git a/src/pylorax/ltmpl.py b/src/pylorax/ltmpl.py index f3b3617e..af15bbc3 100644 --- a/src/pylorax/ltmpl.py +++ b/src/pylorax/ltmpl.py @@ -147,11 +147,11 @@ class LoraxTemplateRunner(object): * Commands should raise exceptions for errors - don't use sys.exit() ''' - def __init__(self, inroot, outroot, yum=None, fatalerrors=True, + def __init__(self, inroot, outroot, yum_obj=None, fatalerrors=True, templatedir=None, defaults={}): self.inroot = inroot self.outroot = outroot - self.yum = yum + self.yum = yum_obj self.fatalerrors = fatalerrors self.templatedir = templatedir or "/usr/share/lorax" self.templatefile = None @@ -455,9 +455,9 @@ class LoraxTemplateRunner(object): cmd = cmd[1:] try: - output = runcmd_output(cmd, cwd=cwd) - if output: - logger.debug('command output:\n%s', output) + _output = runcmd_output(cmd, cwd=cwd) + if _output: + logger.debug('command output:\n%s', _output) logger.debug("command finished successfully") except CalledProcessError as e: if e.output: @@ -555,15 +555,15 @@ class LoraxTemplateRunner(object): logger.debug("removefrom %s %s: no files matched!", pkg, g) # are we removing the matches, or keeping only the matches? if keepmatches: - remove = filelist.difference(matches) + files_to_remove = filelist.difference(matches) else: - remove = matches + files_to_remove = matches # remove the files - if remove: + if files_to_remove: logger.debug("%s: removed %i/%i files, %ikb/%ikb", cmd, - len(remove), len(filelist), - self._getsize(*remove)/1024, self._getsize(*filelist)/1024) - self.remove(*remove) + len(files_to_remove), len(filelist), + self._getsize(*files_to_remove)/1024, self._getsize(*filelist)/1024) + self.remove(*files_to_remove) else: logger.debug("removefrom %s: no files to remove!", cmd) diff --git a/src/pylorax/sysutils.py b/src/pylorax/sysutils.py index 601f9ba6..6f3f86ca 100644 --- a/src/pylorax/sysutils.py +++ b/src/pylorax/sysutils.py @@ -47,12 +47,12 @@ def touch(fname): pass -def replace(fname, find, replace): +def replace(fname, find, substitute): fin = fileinput.input(fname, inplace=1) pattern = re.compile(find) for line in fin: - line = pattern.sub(replace, line) + line = pattern.sub(substitute, line) sys.stdout.write(line) fin.close() diff --git a/src/pylorax/treebuilder.py b/src/pylorax/treebuilder.py index 633fb986..d34fcf1e 100644 --- a/src/pylorax/treebuilder.py +++ b/src/pylorax/treebuilder.py @@ -79,7 +79,7 @@ class RuntimeBuilder(object): basearch=arch.basearch, libdir=arch.libdir) self.yum = yum self._runner = LoraxTemplateRunner(inroot=root, outroot=root, - _yum=yum, templatedir=templatedir) + yum_obj=yum, templatedir=templatedir) self.add_templates = add_templates or [] self.add_template_vars = add_template_vars or {} self._installpkgs = installpkgs or [] diff --git a/src/sbin/livemedia-creator b/src/sbin/livemedia-creator index 7e49d64a..fb23d33f 100755 --- a/src/sbin/livemedia-creator +++ b/src/sbin/livemedia-creator @@ -239,7 +239,7 @@ def default_image_name(compression, basename): return basename + SUFFIXES.get(compression, ".xz") -if __name__ == '__main__': +def main(): # parse the arguments parser = lorax_parser() opts = parser.parse_args() @@ -390,8 +390,8 @@ if __name__ == '__main__': "multiple partitions. swap is allowed but not used.") if errors: - for e in errors: - log.error(e) + for _e in errors: + log.error(_e) sys.exit(1) # Make the image. Output of this is either a partitioned disk image or a fsimage @@ -480,3 +480,6 @@ if __name__ == '__main__': log.info("Results are in %s", opts.result_dir or result_dir) sys.exit( 0 ) + +if __name__ == '__main__': + main() diff --git a/src/sbin/mkefiboot b/src/sbin/mkefiboot index 38d85e5a..6b3793d6 100755 --- a/src/sbin/mkefiboot +++ b/src/sbin/mkefiboot @@ -113,7 +113,7 @@ def mkefidisk(efiboot, outfile): outfile.write(infile.read()) dm_detach(dmdev+"p1") -if __name__ == '__main__': +def main(): parser = argparse.ArgumentParser(description="Make an EFI boot image from the given directory.") parser.add_argument("--debug", action="store_const", const=logging.DEBUG, dest="loglevel", default=log.getEffectiveLevel(), @@ -157,3 +157,6 @@ if __name__ == '__main__': efiboot = tempfile.NamedTemporaryFile(prefix="mkefiboot.").name shutil.move(opt.outfile, efiboot) mkefidisk(efiboot, opt.outfile) + +if __name__ == '__main__': + main() diff --git a/utils/test-parse-template b/utils/test-parse-template index a94c2779..970f37ee 100755 --- a/utils/test-parse-template +++ b/utils/test-parse-template @@ -43,7 +43,7 @@ def readtemplate(templatefile): lookup = TemplateLookup(directories=parser.directories) return lookup.get_template(templatefile) -if __name__ == '__main__': +def main(): # check args if len(sys.argv) == 1: print "usage: %s TEMPLATE [arg1=value1] [arg2=value2] ..." @@ -78,3 +78,6 @@ if __name__ == '__main__': except Exception: print text_error_template().render() raise SystemExit(1) + +if __name__ == '__main__': + main()