From ada63e45f48fe343e496c814efa357986acabec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= Date: Fri, 11 May 2012 15:54:55 +0200 Subject: [PATCH] string interpolation must be done in unicode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we have to format (aka interpolatation) we need to be sure this have to be done in unicode and not in ascii. This avoid unicode error with ascii string encoded in utf-8. Example of failure between a string and an object b = Exception(u"é") Exception("error: %s" % b) => Exception("error: %s" % str(b) => UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128) --- bin/is | 26 ++--- installsystems/config.py | 18 +-- installsystems/database.py | 6 +- installsystems/image.py | 214 +++++++++++++++++------------------ installsystems/printer.py | 20 ++-- installsystems/repository.py | 135 +++++++++++----------- installsystems/template.py | 4 +- installsystems/tools.py | 42 +++---- 8 files changed, 233 insertions(+), 232 deletions(-) diff --git a/bin/is b/bin/is index 57a4fda..ff19d36 100755 --- a/bin/is +++ b/bin/is @@ -71,11 +71,11 @@ def get_images(patterns, repoman, local=True, min=None, max=None): ans += sorted(repoman.select_images([pattern]).items()) # check selected images cound if min is not None and len(ans) < min: - raise Exception("%s images found. Should be at least %s" % ( + raise Exception(u"%s images found. Should be at least %s" % ( len(ans), min)) # check max selected images if max is not None and len(ans) > max: - raise Exception("Too many selected images: %s. Max is %s" % ( + raise Exception(u"Too many selected images: %s. Max is %s" % ( ", ".join([n[0] for n in ans]), max)) for item in ans: if item[1] is None: @@ -112,7 +112,7 @@ def c_build(args): # compute building time t1 = time.time() dt = int(t1 - t0) - arrow("Build time: %s" % datetime.timedelta(seconds=dt)) + arrow(u"Build time: %s" % datetime.timedelta(seconds=dt)) def c_cat(args): ''' @@ -164,8 +164,8 @@ def c_copy(args): if not args.force: out("You will copy the following images:") for img, repo in todo: - out(" %s/%s:%s" % (repo.config.name, img.name, img.version)) - out("Inside repository: #l##b#%s#R#" % dstrepo.config.name) + out(u" %s/%s:%s" % (repo.config.name, img.name, img.version)) + out(u"Inside repository: #l##b#%s#R#" % dstrepo.config.name) if not confirm(): raise Exception("Aborted!") # copy it for real @@ -192,7 +192,7 @@ def c_del(args): if not args.force: out("You will remove the following images:") for img, repo in todo: - out(" %s/%s:%s" % (repo.config.name, img.name, img.version)) + out(u" %s/%s:%s" % (repo.config.name, img.name, img.version)) if not confirm(): raise Exception("Aborted!") # delete it for real @@ -272,7 +272,7 @@ def c_install(args): repoman = load_repositories(args) image, repo = next(get_images([args.pattern], repoman, min=1, max=1)) # Print setup information - arrow("Installing %s v%s" % (image.name, image.version)) + arrow(u"Installing %s v%s" % (image.name, image.version)) # install start time t0 = time.time() # run parser scripts with parser parser argument @@ -286,7 +286,7 @@ def c_install(args): # compute building time t1 = time.time() dt = int(t1 - t0) - arrow("Install time: %s" % datetime.timedelta(seconds=dt)) + arrow(u"Install time: %s" % datetime.timedelta(seconds=dt)) def c_list(args): ''' @@ -318,8 +318,8 @@ def c_move(args): if not args.force: out("You will copy and remove the following images:") for img, repo in todo: - out(" %s/%s:%s" % (repo.config.name, img.name, img.version)) - out("Inside repository: #l##b#%s#R#" % dstrepo.config.name) + out(u" %s/%s:%s" % (repo.config.name, img.name, img.version)) + out(u"Inside repository: #l##b#%s#R#" % dstrepo.config.name) if not confirm(): raise Exception("Aborted!") # move it for real @@ -679,7 +679,7 @@ def main(): proc.nice = options.nice debug("Setting nice to %d" % options.nice) except Exception: - warn("Unable to nice process to %s" % options.nice) + warn(u"Unable to nice process to %s" % options.nice) if options.ionice_class is not None: try: ioclassmap = { @@ -688,10 +688,10 @@ def main(): "be": psutil.IOPRIO_CLASS_BE, "idle": psutil.IOPRIO_CLASS_IDLE} proc.set_ionice(ioclassmap[options.ionice_class], options.ionice_level) - debug("Setting ionice to class %s, level %s" % + debug(u"Setting ionice to class %s, level %s" % (options.ionice_class, options.ionice_level)) except Exception: - warn("Unable to ionice process to %s" % options.ionice_class) + warn(u"Unable to ionice process to %s" % options.ionice_class) # set timeout option if options.timeout is not None: socket.setdefaulttimeout(options.timeout) diff --git a/installsystems/config.py b/installsystems/config.py index 877161b..0bfe8e6 100644 --- a/installsystems/config.py +++ b/installsystems/config.py @@ -41,8 +41,8 @@ class ConfigFile(object): ''' Return path of the best config file ''' - for cf in [ os.path.join(os.path.expanduser("~/.config/installsystems/%s.conf" % name)), - "/etc/installsystems/%s.conf" % name ]: + for cf in [ os.path.join(os.path.expanduser(u"~/.config/installsystems/%s.conf" % name)), + u"/etc/installsystems/%s.conf" % name ]: if (os.path.isfile(cf) and os.access(cf, os.R_OK)): return cf return None @@ -82,7 +82,7 @@ class MainConfigFile(ConfigFile): if self.path is None: debug("No main config file to load") return - debug("Loading main config file: %s" % self.path) + debug(u"Loading main config file: %s" % self.path) try: cp = RawConfigParser() cp.read(self.path) @@ -90,7 +90,7 @@ class MainConfigFile(ConfigFile): if cp.has_section(self.prefix): self._config.update(cp.items(self.prefix)) except Exception as e: - raise Exception("Unable load main config file %s: %s" % (self.path, e)) + raise Exception(u"Unable load main config file %s: %s" % (self.path, e)) def parse(self, namespace=None): ''' @@ -101,11 +101,11 @@ class MainConfigFile(ConfigFile): for option, value in self._config.items(): # check option is valid if option not in self.valid_options.keys(): - warn("Invalid option %s in %s, skipped" % (option, self.path)) + warn(u"Invalid option %s in %s, skipped" % (option, self.path)) continue # we expect a string like if not isinstance(option, basestring): - raise TypeError("Invalid config parser option %s type" % option) + raise TypeError(u"Invalid config parser option %s type" % option) # smartly cast option's value if self.valid_options[option] is bool: value = value.strip().lower() not in ("false", "no", "0", "") @@ -170,7 +170,7 @@ class MainConfigFile(ConfigFile): os.mkdir(di) break except Exception as e: - debug("Unable to create %s: %s" % (di, e)) + debug(u"Unable to create %s: %s" % (di, e)) return self._cache_path() @@ -190,7 +190,7 @@ class RepoConfigFile(ConfigFile): if self.path is None: return # loading config file if exists - debug("Loading repository config file: %s" % self.path) + debug(u"Loading repository config file: %s" % self.path) try: cp = RawConfigParser() cp.readfp(codecs.open(self.path, "r", "utf8")) @@ -202,7 +202,7 @@ class RepoConfigFile(ConfigFile): # get all options in repo self._repos.append(RepositoryConfig(rep, **dict(cp.items(rep)))) except Exception as e: - raise Exception("Unable to load repository file %s: %s" % (self.path, e)) + raise Exception(u"Unable to load repository file %s: %s" % (self.path, e)) @property def repos(self): diff --git a/installsystems/database.py b/installsystems/database.py index f9dc86e..df8df8b 100644 --- a/installsystems/database.py +++ b/installsystems/database.py @@ -35,7 +35,7 @@ class Database(object): conn.commit() conn.close() except Exception as e: - raise Exception("Create database failed: %s" % e) + raise Exception(u"Create database failed: %s" % e) return cls(path) def __init__(self, path): @@ -57,13 +57,13 @@ class Database(object): self.version = 1.0 # we only support database v1 if self.version >= 2.0: - debug("Invalid database format: %s" % self.version) + debug(u"Invalid database format: %s" % self.version) raise Exception("Invalid database format") # we make a query to be sure format is valid try: self.ask("SELECT * FROM image") except: - debug("Invalid database format: %s" % self.version) + debug(u"Invalid database format: %s" % self.version) raise Exception("Invalid database format") def begin(self): diff --git a/installsystems/image.py b/installsystems/image.py index d2131ed..c086b23 100644 --- a/installsystems/image.py +++ b/installsystems/image.py @@ -43,7 +43,7 @@ class Image(object): Check if @buf is a valid image name ''' if re.match("^[-_\w]+$", buf) is None: - raise Exception("Invalid image name %s" % buf) + raise Exception(u"Invalid image name %s" % buf) @staticmethod def check_image_version(buf): @@ -51,7 +51,7 @@ class Image(object): Check if @buf is a valid image version ''' if re.match("^\d+$", buf) is None: - raise Exception("Invalid image version %s" % buf) + raise Exception(u"Invalid image version %s" % buf) @staticmethod def compare_versions(v1, v2): @@ -86,7 +86,7 @@ class SourceImage(Image): if not os.path.exists(d) or not os.path.isdir(d): os.mkdir(d) except Exception as e: - raise Exception("Unable to create directory: %s: %s" % (d, e)) + raise Exception(u"Unable to create directory: %s: %s" % (d, e)) # create example files arrow("Creating examples") arrowlevel(1) @@ -110,14 +110,14 @@ class SourceImage(Image): examples["setup"] = {"path": "setup/01-setup.py", "content": istemplate.setup} for name in examples: try: - arrow("Creating %s example" % name) + arrow(u"Creating %s example" % name) expath = os.path.join(path, examples[name]["path"]) if not force and os.path.exists(expath): - warn("%s already exists. Skipping!" % expath) + warn(u"%s already exists. Skipping!" % expath) continue open(expath, "w").write(examples[name]["content"]) except Exception as e: - raise Exception("Unable to create example file: %s" % e) + raise Exception(u"Unable to create example file: %s" % e) try: # setting executable rights on files in setup and parser arrow("Setting executable rights on scripts") @@ -127,7 +127,7 @@ class SourceImage(Image): for f in os.listdir(dpath): istools.chrights(os.path.join(dpath, f), mode=0777 & ~umask) except Exception as e: - raise Exception("Unable to set rights on %s: %s" % (pf, e)) + raise Exception(u"Unable to set rights on %s: %s" % (pf, e)) arrowlevel(-1) def __init__(self, path): @@ -137,14 +137,14 @@ class SourceImage(Image): Image.__init__(self) self.base_path = os.path.abspath(path) for pathtype in ("build", "parser", "setup", "payload"): - setattr(self, "%s_path" % pathtype, os.path.join(self.base_path, pathtype)) + setattr(self, u"%s_path" % pathtype, os.path.join(self.base_path, pathtype)) self.check_source_image() self.description = self.parse_description() self.changelog = self.parse_changelog() # script tarball path - self.image_name = "%s-%s%s" % (self.description["name"], - self.description["version"], - self.extension) + self.image_name = u"%s-%s%s" % (self.description["name"], + self.description["version"], + self.extension) def check_source_image(self): ''' @@ -153,11 +153,11 @@ class SourceImage(Image): for d in (self.base_path, self.build_path, self.parser_path, self.setup_path, self.payload_path): if not os.path.exists(d): - raise Exception("Invalid source image: directory %s is missing" % d) + raise Exception(u"Invalid source image: directory %s is missing" % d) if not os.path.isdir(d): - raise Exception("Invalid source image: %s is not a directory" % d) + raise Exception(u"Invalid source image: %s is not a directory" % d) if not os.access(d, os.R_OK|os.X_OK): - raise Exception("Invalid source image: unable to access to %s" % d) + raise Exception(u"Invalid source image: unable to access to %s" % d) if not os.path.exists(os.path.join(self.base_path, "description")): raise Exception("Invalid source image: no description file") @@ -196,12 +196,12 @@ class SourceImage(Image): # create tarball arrow("Creating image tarball") arrowlevel(1) - arrow("Name %s" % self.image_name) + arrow(u"Name %s" % self.image_name) try: try: tarball = Tarball.open(self.image_name, mode="w:gz", dereference=True) except Exception as e: - raise Exception("Unable to create tarball %s: %s" % (self.image_name, e)) + raise Exception(u"Unable to create tarball %s: %s" % (self.image_name, e)) # add description.json arrow("Add description.json") tarball.add_str("description.json", jdescription, tarfile.REGTYPE, 0644) @@ -231,13 +231,13 @@ class SourceImage(Image): ''' ans = {} ans["source_path"] = os.path.join(self.payload_path, name) - ans["dest_path"] = "%s-%s%s" % (self.description["name"], - name, - Payload.extension) - ans["link_path"] = "%s-%s-%s%s" % (self.description["name"], - self.description["version"], - name, - Payload.extension) + ans["dest_path"] = u"%s-%s%s" % (self.description["name"], + name, + Payload.extension) + ans["link_path"] = u"%s-%s-%s%s" % (self.description["name"], + self.description["version"], + name, + Payload.extension) source_stat = os.stat(ans["source_path"]) ans["isdir"] = stat.S_ISDIR(source_stat.st_mode) ans["uid"] = source_stat.st_uid @@ -292,7 +292,7 @@ class SourceImage(Image): os.unlink(paydesc["link_path"]) os.symlink(paydesc["dest_path"], paydesc["link_path"]) except Exception as e: - raise Exception("Unable to create payload %s: %s" % (payload_name, e)) + raise Exception(u"Unable to create payload %s: %s" % (payload_name, e)) def create_payload_tarball(self, tar_path, data_path): ''' @@ -322,7 +322,7 @@ class SourceImage(Image): raise Exception("Tar return is not zero") # check compressor return 0 if p_comp.wait() != 0: - raise Exception("Compressor %s return is not zero" % a_comp[0]) + raise Exception(u"Compressor %s return is not zero" % a_comp[0]) except (SystemExit, KeyboardInterrupt): if os.path.exists(tar_path): os.unlink(tar_path) @@ -351,7 +351,7 @@ class SourceImage(Image): f_dst.close() # check compressor return 0 if p_comp.wait() != 0: - raise Exception("Compressor %s return is not zero" % a_comp[0]) + raise Exception(u"Compressor %s return is not zero" % a_comp[0]) except (SystemExit, KeyboardInterrupt): if os.path.exists(dest): os.unlink(dest) @@ -362,7 +362,7 @@ class SourceImage(Image): Add scripts inside a directory into a tarball ''' basedirectory = os.path.basename(directory) - arrow("Add %s scripts" % basedirectory) + arrow(u"Add %s scripts" % basedirectory) arrowlevel(1) # adding base directory ti = tarball.gettarinfo(directory, arcname=basedirectory) @@ -377,7 +377,7 @@ class SourceImage(Image): ti.uid = ti.gid = 0 ti.uname = ti.gname = "root" tarball.addfile(ti, open(fp, "rb")) - arrow("%s added" % fn) + arrow(u"%s added" % fn) arrowlevel(-1) def check_scripts(self, directory): @@ -385,7 +385,7 @@ class SourceImage(Image): Check if scripts inside a directory can be compiled ''' basedirectory = os.path.basename(directory) - arrow("Checking %s scripts" % basedirectory) + arrow(u"Checking %s scripts" % basedirectory) arrowlevel(1) # checking each file for fp, fn in self.select_scripts(directory): @@ -400,7 +400,7 @@ class SourceImage(Image): Execute script inside a directory Return a list of payload to force rebuild ''' - arrow("Run %s scripts" % os.path.basename(script_directory)) + arrow(u"Run %s scripts" % os.path.basename(script_directory)) rebuild_list = [] cwd = os.getcwd() arrowlevel(1) @@ -412,7 +412,7 @@ class SourceImage(Image): try: o_scripts = compile(open(fp, "r").read(), fn, "exec") except Exception as e: - raise Exception("Unable to compile %s fail: %s" % + raise Exception(u"Unable to compile %s fail: %s" % (fn, e)) # define execution context gl = {"rebuild": rebuild_list, @@ -421,7 +421,7 @@ class SourceImage(Image): try: exec o_scripts in gl except Exception as e: - raise Exception("Execution script %s fail: %s" % + raise Exception(u"Execution script %s fail: %s" % (fn, e)) arrowlevel(level=old_level) os.chdir(cwd) @@ -510,7 +510,7 @@ class SourceImage(Image): if self.compare_versions(installsystems.version, d["is_min_version"]) < 0: raise Exception("Minimum Installsystems version not satisfied") except Exception as e: - raise Exception("Bad description: %s" % e) + raise Exception(u"Bad description: %s" % e) return d def parse_changelog(self): @@ -528,7 +528,7 @@ class SourceImage(Image): try: cl = Changelog(fo.read()) except Exception as e: - raise Exception("Bad changelog: %s" % e) + raise Exception(u"Bad changelog: %s" % e) return cl @property @@ -550,10 +550,10 @@ class PackageImage(Image): ''' Diff two packaged images ''' - arrow("Difference from images #y#%s v%s#R# to #r#%s v%s#R#:" % (pkg1.name, - pkg1.version, - pkg2.name, - pkg2.version)) + arrow(u"Difference from images #y#%s v%s#R# to #r#%s v%s#R#:" % (pkg1.name, + pkg1.version, + pkg2.name, + pkg2.version)) # Extract images for diff scripts files fromfiles = set(pkg1._tarball.getnames(re_pattern="(parser|setup)/.*")) tofiles = set(pkg2._tarball.getnames(re_pattern="(parser|setup)/.*")) @@ -577,11 +577,11 @@ class PackageImage(Image): fromfile=fromfile, tofile=tofile): # coloring diff if line.startswith("+"): - out("#g#%s#R#" % line, endl="") + out(u"#g#%s#R#" % line, endl="") elif line.startswith("-"): - out("#r#%s#R#" % line, endl="") + out(u"#r#%s#R#" % line, endl="") elif line.startswith("@@"): - out("#c#%s#R#" % line, endl="") + out(u"#c#%s#R#" % line, endl="") else: out(line, endl="") @@ -611,17 +611,17 @@ class PackageImage(Image): memfile.seek(0) self._tarball = Tarball.open(fileobj=memfile, mode='r:gz') except Exception as e: - raise Exception("Unable to open image %s: %s" % (path, e)) + raise Exception(u"Unable to open image %s: %s" % (path, e)) self._metadata = self.read_metadata() # print info - arrow("Image %s v%s loaded" % (self.name, self.version)) - arrow("Author: %s" % self.author, 1) - arrow("Date: %s" % istools.time_rfc2822(self.date), 1) + arrow(u"Image %s v%s loaded" % (self.name, self.version)) + arrow(u"Author: %s" % self.author, 1) + arrow(u"Date: %s" % istools.time_rfc2822(self.date), 1) # build payloads info self.payload = {} for pname, pval in self._metadata["payload"].items(): - pfilename = "%s-%s%s" % (self.filename[:-len(Image.extension)], - pname, Payload.extension) + pfilename = u"%s-%s%s" % (self.filename[:-len(Image.extension)], + pname, Payload.extension) if self.md5name: ppath = os.path.join(self.base_path, self._metadata["payload"][pname]["md5"]) @@ -642,7 +642,7 @@ class PackageImage(Image): ''' Return image filename ''' - return "%s-%s%s" % (self.name, self.version, self.extension) + return u"%s-%s%s" % (self.name, self.version, self.extension) def read_metadata(self): ''' @@ -655,7 +655,7 @@ class PackageImage(Image): if float(img_format) >= math.floor(float(self.format)) + 1.0: raise Exception() except: - raise Exception("Invalid image format %s" % img_format) + raise Exception(u"Invalid image format %s" % img_format) desc["format"] = img_format # check description try: @@ -670,7 +670,7 @@ class PackageImage(Image): if self.compare_versions(installsystems.version, desc["is_min_version"]) < 0: raise Exception("Minimum Installsystems version not satisfied") except Exception as e: - raise Exception("Invalid description: %s" % e) + raise Exception(u"Invalid description: %s" % e) # try to load changelog try: img_changelog = self._tarball.get_str("changelog") @@ -678,7 +678,7 @@ class PackageImage(Image): except KeyError: desc["changelog"] = Changelog("") except Exception as e: - warn("Invalid changelog: %s" % e) + warn(u"Invalid changelog: %s" % e) return desc def show(self, o_verbose=False, o_changelog=False, o_json=False): @@ -688,27 +688,27 @@ class PackageImage(Image): if o_json: out(json.dumps(self._metadata)) else: - out('#light##yellow#Name:#reset# %s' % self.name) - out('#light##yellow#Version:#reset# %s' % self.version) - out('#yellow#Date:#reset# %s' % istools.time_rfc2822(self.date)) - out('#yellow#Description:#reset# %s' % self.description) - out('#yellow#Author:#reset# %s' % self.author) + out(u'#light##yellow#Name:#reset# %s' % self.name) + out(u'#light##yellow#Version:#reset# %s' % self.version) + out(u'#yellow#Date:#reset# %s' % istools.time_rfc2822(self.date)) + out(u'#yellow#Description:#reset# %s' % self.description) + out(u'#yellow#Author:#reset# %s' % self.author) if o_verbose: # field is_build_version is new in version 5. I can be absent. - try: out('#yellow#IS build version:#reset# %s' % self.is_build_version) + try: out(u'#yellow#IS build version:#reset# %s' % self.is_build_version) except AttributeError: pass # field is_min_version is new in version 5. I can be absent. - try: out('#yellow#IS minimum version:#reset# %s' % self.is_min_version) + try: out(u'#yellow#IS minimum version:#reset# %s' % self.is_min_version) except AttributeError: pass - out('#yellow#MD5:#reset# %s' % self.md5) + out(u'#yellow#MD5:#reset# %s' % self.md5) if o_verbose: payloads = self.payload for payload_name in payloads: payload = payloads[payload_name] - out('#light##yellow#Payload:#reset# %s' % payload_name) - out(' #yellow#Date:#reset# %s' % istools.time_rfc2822(payload.mtime)) - out(' #yellow#Size:#reset# %s' % (istools.human_size(payload.size))) - out(' #yellow#MD5:#reset# %s' % payload.md5) + out(u'#light##yellow#Payload:#reset# %s' % payload_name) + out(u' #yellow#Date:#reset# %s' % istools.time_rfc2822(payload.mtime)) + out(u' #yellow#Size:#reset# %s' % (istools.human_size(payload.size))) + out(u' #yellow#MD5:#reset# %s' % payload.md5) # display image content out('#light##yellow#Content:#reset#') self._tarball.list(o_verbose) @@ -728,9 +728,9 @@ class PackageImage(Image): fo.consume() fo.close() if self.size != fo.read_size: - raise Exception("Invalid size of image %s" % self.name) + raise Exception(u"Invalid size of image %s" % self.name) if self.md5 != fo.md5: - raise Exception("Invalid MD5 of image %s" % self.name) + raise Exception(u"Invalid MD5 of image %s" % self.name) # check payloads for pay_name, pay_obj in self.payload.items(): arrow(pay_name) @@ -743,7 +743,7 @@ class PackageImage(Image): ''' filelist = self._tarball.getnames(glob_pattern=filename, dir=False) if len(filelist) == 0: - warn("No file matching %s" % filename) + warn(u"No file matching %s" % filename) for filename in filelist: arrow(filename) out(self._tarball.get_str(filename)) @@ -759,27 +759,27 @@ class PackageImage(Image): if image: dest = os.path.join(directory, self.filename) if not force and os.path.exists(dest): - raise Exception("Image destination already exists: %s" % dest) + raise Exception(u"Image destination already exists: %s" % dest) # some display - arrow("Downloading image in %s" % directory) - debug("Downloading %s from %s" % (self.filename, self.path)) + arrow(u"Downloading image in %s" % directory) + debug(u"Downloading %s from %s" % (self.filename, self.path)) # open source fs = PipeFile(self.path, progressbar=True) # check if announced file size is good if fs.size is not None and self.size != fs.size: - raise Exception("Downloading image %s failed: Invalid announced size" % self.name) + raise Exception(u"Downloading image %s failed: Invalid announced size" % self.name) # open destination fd = open(self.filename, "wb") fs.consume(fd) fs.close() fd.close() if self.size != fs.consumed_size: - raise Exception("Download image %s failed: Invalid size" % self.name) + raise Exception(u"Download image %s failed: Invalid size" % self.name) if self.md5 != fs.md5: - raise Exception("Download image %s failed: Invalid MD5" % self.name) + raise Exception(u"Download image %s failed: Invalid MD5" % self.name) if payload: for payname in self.payload: - arrow("Downloading payload %s in %s" % (payname, directory)) + arrow(u"Downloading payload %s in %s" % (payname, directory)) self.payload[payname].info self.payload[payname].download(directory, force=force) @@ -790,17 +790,17 @@ class PackageImage(Image): # check validity of dest if os.path.exists(directory): if not os.path.isdir(directory): - raise Exception("Destination %s is not a directory" % directory) + raise Exception(u"Destination %s is not a directory" % directory) if not force and len(os.listdir(directory)) > 0: - raise Exception("Directory %s is not empty (need force)" % directory) + raise Exception(u"Directory %s is not empty (need force)" % directory) else: istools.mkdir(directory) # extract content - arrow("Extracting image in %s" % directory) + arrow(u"Extracting image in %s" % directory) self._tarball.extractall(directory) # generate description file from description.json if gendescription: - arrow("Generating description file in %s" % directory) + arrow(u"Generating description file in %s" % directory) with open(os.path.join(directory, "description"), "w") as f: f.write((istemplate.description % self._metadata).encode('utf-8')) # launch payload extraction @@ -809,7 +809,7 @@ class PackageImage(Image): # here we need to decode payname which is in unicode to escape # tarfile to encode filename of file inside tarball inside unicode dest = os.path.join(directory, "payload", payname.encode("utf-8")) - arrow("Extracting payload %s in %s" % (payname, dest)) + arrow(u"Extracting payload %s in %s" % (payname, dest)) self.payload[payname].extract(dest, force=force) def run_parser(self, **kwargs): @@ -828,7 +828,7 @@ class PackageImage(Image): ''' Run scripts in a tarball directory ''' - arrow("Run %s scripts" % directory) + arrow(u"Run %s scripts" % directory) arrowlevel(1) # get list of parser scripts l_scripts = self._tarball.getnames(re_pattern="%s/.*\.py" % directory) @@ -842,13 +842,13 @@ class PackageImage(Image): try: s_scripts = self._tarball.get_str(n_scripts) except Exception as e: - raise Exception("Extracting script %s fail: %s" % + raise Exception(u"Extracting script %s fail: %s" % (n_scripts, e)) # compile source code try: o_scripts = compile(s_scripts, n_scripts, "exec") except Exception as e: - raise Exception("Unable to compile %s fail: %s" % + raise Exception(u"Unable to compile %s fail: %s" % (n_scripts, e)) # define execution context gl = {} @@ -859,7 +859,7 @@ class PackageImage(Image): try: exec o_scripts in gl except Exception as e: - raise Exception("Execution script %s fail: %s" % + raise Exception(u"Execution script %s fail: %s" % (n_scripts, e)) arrowlevel(level=old_level) arrowlevel(-1) @@ -887,14 +887,14 @@ class Payload(object): def __getattr__(self, name): # get all value with an understance as if there is no underscore - if hasattr(self, "_%s" % name): - return getattr(self, "_%s" % name) + if hasattr(self, u"_%s" % name): + return getattr(self, u"_%s" % name) raise AttributeError def __setattr__(self, name, value): # set all value which exists have no underscore, but where underscore exists if name in self.legit_attr: - object.__setattr__(self, "_%s" % name, value) + object.__setattr__(self, u"_%s" % name, value) else: object.__setattr__(self, name, value) @@ -993,9 +993,9 @@ class Payload(object): fileobj.consume() fileobj.close() if self._size != fileobj.read_size: - raise Exception("Invalid size of payload %s" % self.name) + raise Exception(u"Invalid size of payload %s" % self.name) if self._md5 != fileobj.md5: - raise Exception("Invalid MD5 of payload %s" % self._md5) + raise Exception(u"Invalid MD5 of payload %s" % self._md5) def download(self, dest, force=False): ''' @@ -1010,15 +1010,15 @@ class Payload(object): # check validity of dest if os.path.exists(dest): if os.path.isdir(dest): - raise Exception("Destination %s is a directory" % dest) + raise Exception(u"Destination %s is a directory" % dest) if not force: - raise Exception("File %s already exists" % dest) + raise Exception(u"File %s already exists" % dest) # Open remote file - debug("Downloading payload %s from %s" % (self.filename, self.path)) + debug(u"Downloading payload %s from %s" % (self.filename, self.path)) fs = PipeFile(self.path, progressbar=True) # check if announced file size is good if fs.size is not None and self.size != fs.size: - raise Exception("Downloading payload %s failed: Invalid announced size" % + raise Exception(u"Downloading payload %s failed: Invalid announced size" % self.name) fd = open(dest, "wb") fs.consume(fd) @@ -1027,9 +1027,9 @@ class Payload(object): fd.close() # checking download size if self.size != fs.read_size: - raise Exception("Downloading payload %s failed: Invalid size" % self.name) + raise Exception(u"Downloading payload %s failed: Invalid size" % self.name) if self.md5 != fs.md5: - raise Exception("Downloading payload %s failed: Invalid MD5" % self.name) + raise Exception(u"Downloading payload %s failed: Invalid MD5" % self.name) def extract(self, dest, force=False, filelist=None): ''' @@ -1043,7 +1043,7 @@ class Payload(object): else: self.extract_file(dest, force=force) except Exception as e: - raise Exception("Extracting payload %s failed: %s" % (self.name, e)) + raise Exception(u"Extracting payload %s failed: %s" % (self.name, e)) def extract_tar(self, dest, force=False, filelist=None): ''' @@ -1053,19 +1053,19 @@ class Payload(object): # check validity of dest if os.path.exists(dest): if not os.path.isdir(dest): - raise Exception("Destination %s is not a directory" % dest) + raise Exception(u"Destination %s is not a directory" % dest) if not force and len(os.listdir(dest)) > 0: - raise Exception("Directory %s is not empty (need force)" % dest) + raise Exception(u"Directory %s is not empty (need force)" % dest) else: istools.mkdir(dest) # try to open payload file try: fo = PipeFile(self.path, progressbar=True) except Exception as e: - raise Exception("Unable to open %s" % self.path) + raise Exception(u"Unable to open %s" % self.path) # check if announced file size is good if fo.size is not None and self.size != fo.size: - raise Exception("Invalid announced size on %s" % self.path) + raise Exception(u"Invalid announced size on %s" % self.path) # get compressor argv (first to escape file creation if not found) a_comp = istools.get_compressor_path(self.compressor, compress=False) a_tar = ["tar", "--extract", "--numeric-owner", "--ignore-zeros", @@ -1093,7 +1093,7 @@ class Payload(object): p_comp.stdin.close() # check compressor return 0 if p_comp.wait() != 0: - raise Exception("Compressor %s return is not zero" % a_comp[0]) + raise Exception(u"Compressor %s return is not zero" % a_comp[0]) # check tar return 0 if p_tar.wait() != 0: raise Exception("Tar return is not zero") @@ -1112,24 +1112,24 @@ class Payload(object): # check validity of dest if os.path.exists(dest): if os.path.isdir(dest): - raise Exception("Destination %s is a directory" % dest) + raise Exception(u"Destination %s is a directory" % dest) if not force: - raise Exception("File %s already exists" % dest) + raise Exception(u"File %s already exists" % dest) # get compressor argv (first to escape file creation if not found) a_comp = istools.get_compressor_path(self.compressor, compress=False) # try to open payload file (source) try: f_src = PipeFile(self.path, "r", progressbar=True) except Exception as e: - raise Exception("Unable to open payload file %s: %s" % (self.path, e)) + raise Exception(u"Unable to open payload file %s: %s" % (self.path, e)) # check if announced file size is good if f_src.size is not None and self.size != f_src.size: - raise Exception("Invalid announced size on %s" % self.path) + raise Exception(u"Invalid announced size on %s" % self.path) # opening destination try: f_dst = open(dest, "wb") except Exception as e: - raise Exception("Unable to open destination file %s: %s" % (dest, e)) + raise Exception(u"Unable to open destination file %s: %s" % (dest, e)) # run compressor process p_comp = subprocess.Popen(a_comp, shell=False, close_fds=True, stdin=subprocess.PIPE, stdout=f_dst) @@ -1149,7 +1149,7 @@ class Payload(object): p_comp.stdin.close() # check compressor return 0 if p_comp.wait() != 0: - raise Exception("Compressor %s return is not zero" % a_comp[0]) + raise Exception(u"Compressor %s return is not zero" % a_comp[0]) # settings file orginal rights istools.chrights(dest, self.uid, self.gid, self.mode, self.mtime) @@ -1209,6 +1209,6 @@ class Changelog(dict): ''' Display a version content ''' - out(' #yellow#Version:#reset# %s' % version) + out(u' #yellow#Version:#reset# %s' % version) for line in self[version]: - out(" %s" % line) + out(u" %s" % line) diff --git a/installsystems/printer.py b/installsystems/printer.py index cb3a674..925d824 100644 --- a/installsystems/printer.py +++ b/installsystems/printer.py @@ -71,29 +71,29 @@ def err(message, fd=sys.stderr, endl=os.linesep): out(message, fd, endl) def fatal(message, quit=True, fd=sys.stderr, endl=os.linesep): - out("#light##red#Fatal:#reset# #red#%s#reset#" % message, fd, endl) + out(u"#light##red#Fatal:#reset# #red#%s#reset#" % message, fd, endl) if sys.exc_info()[0] is not None and installsystems.verbosity > 1: raise if quit: os._exit(21) def error(message, quit=True, fd=sys.stderr, endl=os.linesep): - out("#light##red#Error:#reset# #red#%s#reset#" % message, fd, endl) + out(u"#light##red#Error:#reset# #red#%s#reset#" % message, fd, endl) if sys.exc_info()[0] is not None and installsystems.verbosity > 1: raise if quit: exit(42) def warn(message, fd=sys.stderr, endl=os.linesep): - out("#light##yellow#Warning:#reset# #yellow#%s#reset#" % message, fd, endl) + out(u"#light##yellow#Warning:#reset# #yellow#%s#reset#" % message, fd, endl) def info(message, fd=sys.stderr, endl=os.linesep): if installsystems.verbosity > 0: - out("#light#Info:#reset# %s" % message, fd, endl) + out(u"#light#Info:#reset# %s" % message, fd, endl) def debug(message, fd=sys.stderr, endl=os.linesep): if installsystems.verbosity > 1: - out("#light##black#%s#reset#" % message, fd, endl) + out(u"#light##black#%s#reset#" % message, fd, endl) def arrowlevel(inc=None, level=None): global _arrow_level @@ -110,13 +110,13 @@ def arrow(message, inclevel=None, level=None, fd=sys.stdout, endl=os.linesep): # define new level old_level = arrowlevel(inc=inclevel, level=level) if _arrow_level == 1: - out("#light##red#=>#reset# %s" % message, fd=fd, endl=endl) + out(u"#light##red#=>#reset# %s" % message, fd=fd, endl=endl) elif _arrow_level == 2: - out(" #light##yellow#=>#reset# %s" % message, fd=fd, endl=endl) + out(u" #light##yellow#=>#reset# %s" % message, fd=fd, endl=endl) elif _arrow_level == 3: - out(" #light##blue#=>#reset# %s" % message, fd=fd, endl=endl) + out(u" #light##blue#=>#reset# %s" % message, fd=fd, endl=endl) elif _arrow_level == 4: - out(" #light##green#=>#reset# %s" % message, fd=fd, endl=endl) + out(u" #light##green#=>#reset# %s" % message, fd=fd, endl=endl) # restore old on one shot level arrowlevel(level = old_level) @@ -134,5 +134,5 @@ def confirm(message=None, ans=None, fd=sys.stdout, endl=""): if ans is None: ans = "yes" if message is None: - message = "#u##l##w#Are you sure?#R# (%s) " % ans + message = u"#u##l##w#Are you sure?#R# (%s) " % ans return ask(message, fd, endl) == ans diff --git a/installsystems/repository.py b/installsystems/repository.py index 2c534b3..3019484 100644 --- a/installsystems/repository.py +++ b/installsystems/repository.py @@ -39,7 +39,7 @@ class Repository(object): Raise exception is repository name is invalid ''' if not Repository.is_repository_name(name): - raise Exception("Invalid repository name %s" % name) + raise Exception(u"Invalid repository name %s" % name) return name @staticmethod @@ -50,7 +50,7 @@ class Repository(object): ''' x = re.match(u"^(?:([^/:]+)/)?([^/:]+)?(?::v?([^/:]+)?)?$", path) if x is None: - raise Exception("invalid image path: %s" % path) + raise Exception(u"invalid image path: %s" % path) return x.group(1, 2, 3) @staticmethod @@ -67,8 +67,8 @@ class Repository(object): ''' Comptue a diff between two repositories ''' - arrow("Diff between repositories #y#%s#R# and #g#%s#R#" % (repo1.config.name, - repo2.config.name)) + arrow(u"Diff between repositories #y#%s#R# and #g#%s#R#" % (repo1.config.name, + repo2.config.name)) # Get info from databases i_dict1 = dict((b[0], b[1:]) for b in repo1.db.ask( "SELECT md5, name, version FROM image").fetchall()) @@ -105,10 +105,10 @@ class Repository(object): try: self.db = Database(config.dbpath) except: - debug("Unable to load database %s" % config.dbpath) + debug(u"Unable to load database %s" % config.dbpath) self.config.offline = True if self.config.offline: - debug("Repository %s is offline" % config.name) + debug(u"Repository %s is offline" % config.name) def __getattribute__(self, name): ''' @@ -122,7 +122,7 @@ class Repository(object): return object.__getattribute__(self, name) # if no db (not init or not accessible) raise error if config.offline: - raise Exception("Repository %s is offline" % config.name) + raise Exception(u"Repository %s is offline" % config.name) return object.__getattribute__(self, name) @property @@ -139,19 +139,19 @@ class Repository(object): config = self.config # check local repository if not self.local: - raise Exception("Repository creation must be local") + raise Exception(u"Repository creation must be local") # create base directories arrow("Creating base directories") arrowlevel(1) # creating local directory try: if os.path.exists(config.path): - arrow("%s already exists" % config.path) + arrow(u"%s already exists" % config.path) else: istools.mkdir(config.path, config.uid, config.gid, config.dmod) - arrow("%s directory created" % config.path) + arrow(u"%s directory created" % config.path) except Exception as e: - raise Exception("Unable to create directory %s: %s" % (config.path, e)) + raise Exception(u"Unable to create directory %s: %s" % (config.path, e)) arrowlevel(-1) # create database d = Database.create(config.dbpath) @@ -170,14 +170,14 @@ class Repository(object): ''' # check local repository if not self.local: - raise Exception("Repository addition must be local") + raise Exception(u"Repository addition must be local") try: arrow("Updating last file") last_path = os.path.join(self.config.path, self.config.lastname) open(last_path, "w").write("%s\n" % int(time.time())) istools.chrights(last_path, self.config.uid, self.config.gid, self.config.fmod) except Exception as e: - raise Exception("Update last file failed: %s" % e) + raise Exception(u"Update last file failed: %s" % e) def last(self, name): ''' @@ -197,19 +197,19 @@ class Repository(object): ''' # check local repository if not self.local: - raise Exception("Repository addition must be local") + raise Exception(u"Repository addition must be local") # cannot add already existant image if self.has(image.name, image.version): - raise Exception("Image already in database, delete first!") + raise Exception(u"Image already in database, delete first!") # adding file to repository arrow("Copying images and payload") for obj in [ image ] + image.payload.values(): dest = os.path.join(self.config.path, obj.md5) basesrc = os.path.basename(obj.path) if os.path.exists(dest): - arrow("Skipping %s: already exists" % basesrc, 1) + arrow(u"Skipping %s: already exists" % basesrc, 1) else: - arrow("Adding %s (%s)" % (basesrc, obj.md5), 1) + arrow(u"Adding %s (%s)" % (basesrc, obj.md5), 1) dfo = open(dest, "wb") sfo = PipeFile(obj.path, "r", progressbar=True) sfo.consume(dfo) @@ -272,7 +272,7 @@ class Repository(object): ''' # Check if the repo is local if not self.local: - raise Exception("Repository must be local") + raise Exception(u"Repository must be local") local_files = set(os.listdir(self.config.path)) local_files.remove(self.config.dbname) local_files.remove(self.config.lastname) @@ -302,7 +302,7 @@ class Repository(object): ''' # Check if the repo is local if not self.local: - raise Exception("Repository must be local") + raise Exception(u"Repository must be local") allmd5 = set(self.getallmd5()) repofiles = set(os.listdir(self.config.path)) - set([self.config.dbname, self.config.lastname]) dirtyfiles = repofiles - allmd5 @@ -313,19 +313,19 @@ class Repository(object): arrow(f, 1) # ask confirmation if not force and not confirm("Remove dirty files? (yes) "): - raise Exception("Aborted!") + raise Exception(u"Aborted!") # start cleaning arrow("Cleaning") for f in dirtyfiles: p = os.path.join(self.config.path, f) - arrow("Removing %s" % p, 1) + arrow(u"Removing %s" % p, 1) try: if os.path.isdir(p): os.rmdir(p) else: os.unlink(p) except: - warn("Removing %s failed" % p) + warn(u"Removing %s failed" % p) else: arrow("Nothing to clean") @@ -335,7 +335,7 @@ class Repository(object): ''' # check local repository if not self.local: - raise Exception("Repository deletion must be local") + raise Exception(u"Repository deletion must be local") # get md5 of files related to images (exception is raised if not exists md5s = self.getmd5(name, version) # cleaning db (must be done before cleaning) @@ -388,7 +388,7 @@ class Repository(object): if md5 not in res: res[md5] = {"size": payload[1], "isdir": payload[2], "images": {}} # add image to list - imgpath = "%s/%s:%s" % (self.config.name, payload[3], payload[4]) + imgpath = u"%s/%s:%s" % (self.config.name, payload[3], payload[4]) res[md5]["images"][imgpath] = {"repo": self.config.name, "imgname": payload[3], "imgver": payload[4], @@ -404,12 +404,12 @@ class Repository(object): WHERE name LIKE ? OR\ description LIKE ? OR\ author LIKE ?", - tuple( ["%%%s%%" % pattern ] * 3) + tuple( [u"%%%s%%" % pattern ] * 3) ).fetchall() for name, version, author, description in images: - arrow("%s v%s" % (name, version), 1) - out(" #yellow#Author:#reset# %s" % author) - out(" #yellow#Description:#reset# %s" % description) + arrow(u"%s v%s" % (name, version), 1) + out(u" #yellow#Author:#reset# %s" % author) + out(u" #yellow#Description:#reset# %s" % description) def _remove_file(self, filename): ''' @@ -418,14 +418,14 @@ class Repository(object): # check existance in table image have = False for table in ("image", "payload"): - have = have or self.db.ask("SELECT md5 FROM %s WHERE md5 = ? LIMIT 1" % table, + have = have or self.db.ask(u"SELECT md5 FROM %s WHERE md5 = ? LIMIT 1" % table, (filename,)).fetchone() is not None # if no reference, delete! if not have: - arrow("%s, deleted" % filename) + arrow(u"%s, deleted" % filename) os.unlink(os.path.join(self.config.path, filename)) else: - arrow("%s, skipped" % filename) + arrow(u"%s, skipped" % filename) def has(self, name, version): ''' @@ -441,30 +441,30 @@ class Repository(object): if version is None: version = self.last(name) if version < 0: - raise Exception("Unable to find image %s in %s" % (name, - self.config.name)) + raise Exception(u"Unable to find image %s in %s" % (name, + self.config.name)) # get file md5 from db r = self.db.ask("select md5 from image where name = ? and version = ? limit 1", (name, version)).fetchone() if r is None: - raise Exception("Unable to find image %s v%s in %s" % (name, version, - self.config.name)) + raise Exception(u"Unable to find image %s v%s in %s" % (name, version, + self.config.name)) path = os.path.join(self.config.path, r[0]) # getting the file - arrow("Loading image %s v%s from repository %s" % (name, - version, - self.config.name)) + arrow(u"Loading image %s v%s from repository %s" % (name, + version, + self.config.name)) memfile = cStringIO.StringIO() try: fo = PipeFile(path, "r") fo.consume(memfile) fo.close() except Exception as e: - raise Exception("Loading image %s v%s failed: %s" % (name, version, e)) + raise Exception(u"Loading image %s v%s failed: %s" % (name, version, e)) memfile.seek(0) pkg = PackageImage(path, fileobj=memfile, md5name=True) if pkg.md5 != r[0]: - raise Exception("Image MD5 verification failure") + raise Exception(u"Image MD5 verification failure") return pkg def getmd5(self, name, version): @@ -476,7 +476,7 @@ class Repository(object): a = self.db.ask("SELECT md5 FROM image WHERE name = ? AND version = ? LIMIT 1", (name,version)).fetchone() if a is None: - raise Exception("No such image %s version %s" % (name, version)) + raise Exception(u"No such image %s version %s" % (name, version)) b = self.db.ask("SELECT md5 FROM payload WHERE image_md5 = ?", (a[0],)).fetchall() return [ a[0] ] + [ x[0] for x in b ] @@ -496,7 +496,7 @@ class RepositoryManager(object): self.filter = [] if filter is None else filter self.search = [] if search is None else search self.timeout = timeout or 3 - debug("Repostiory timeout setted to %ds" % self.timeout) + debug(u"Repostiory timeout setted to %ds" % self.timeout) if cache_path is None: self.cache_path = None debug("No repository cache") @@ -510,14 +510,14 @@ class RepositoryManager(object): os.mkdir(self.cache_path) # ensure directories are avaiblable if not os.access(self.cache_path, os.W_OK | os.X_OK): - raise Exception("%s is not writable or executable" % self.cache_path) - debug("Repository cache is in %s" % self.cache_path) + raise Exception(u"%s is not writable or executable" % self.cache_path) + debug(u"Repository cache is in %s" % self.cache_path) def __del__(self): # delete temporary files (used by db) for f in self.tempfiles: try: - debug("Removing temporary db file %s" % f) + debug(u"Removing temporary db file %s" % f) os.unlink(f) except OSError: pass @@ -532,15 +532,15 @@ class RepositoryManager(object): ''' Return a repostiory by its position in list ''' - if type(key) == int: + if isinstance(key, int): return self.repos[key] - elif type(key) == str: + elif isinstance(key, basestring): for repo in self.repos: if repo.config.name == key: return repo - raise IndexError("No repository named: %s" % key) + raise IndexError(u"No repository named: %s" % key) else: - raise TypeError + raise TypeError(u"Invalid type %s for %" % (type(key), key)) def __contains__(self, key): ''' @@ -561,21 +561,21 @@ class RepositoryManager(object): # check filter on name if len(self.filter) > 0: if config.name not in self.filter: - debug("Filtering repository %s" % config.name) + debug(u"Filtering repository %s" % config.name) return # repository is offline if config.offline or offline: - debug("Registering offline repository %s (%s)" % (config.path, config.name)) + debug(u"Registering offline repository %s (%s)" % (config.path, config.name)) # we must force offline in cast of argument offline config.offline = True self.repos.append(Repository(config)) # if path is local, no needs to create a cache elif istools.isfile(config.path): - debug("Registering direct repository %s (%s)" % (config.path, config.name)) + debug(u"Registering direct repository %s (%s)" % (config.path, config.name)) self.repos.append(Repository(config)) # path is remote, we need to create a cache else: - debug("Registering cached repository %s (%s)" % (config.path, config.name)) + debug(u"Registering cached repository %s (%s)" % (config.path, config.name)) self.repos.append(self._cachify(config, temp, nosync)) def _cachify(self, config, temp=False, nosync=False): @@ -620,7 +620,7 @@ class RepositoryManager(object): # if repo is out of date, download it if rlast != llast: try: - arrow("Downloading %s" % original_dbpath) + arrow(u"Downloading %s" % original_dbpath) rdb.progressbar = True ldb = open(config.dbpath, "wb") rdb.consume(ldb) @@ -637,7 +637,7 @@ class RepositoryManager(object): raise except IOError as e: # if something append bad during caching, we mark repo as offline - debug("Unable to cache repository %s: %s" % (config.name, e)) + debug(u"Unable to cache repository %s: %s" % (config.name, e)) config.offline = True return Repository(config) @@ -667,13 +667,13 @@ class RepositoryManager(object): Return a list of available images ''' if len(self.onlines) == 0: - raise Exception("No online repository") + raise Exception(u"No online repository") ans = {} for pattern in patterns: path, image, version = Repository.split_image_path(pattern) # no image name, skip it if image is None: - warn("No image name in pattern %s, skipped" % pattern) + warn(u"No image name in pattern %s, skipped" % pattern) continue # building image list images = {} @@ -698,9 +698,9 @@ class RepositoryManager(object): last = reduce(f, versions) versions.remove(last) for rmv in versions: - del images["%s/%s:%s" % (repo, img, rmv)] + del images[u"%s/%s:%s" % (repo, img, rmv)] # filter with pattern on path - filter_pattern = "%s/%s:%s" % (path, image, version) + filter_pattern = u"%s/%s:%s" % (path, image, version) for k in images.keys(): if not fnmatch.fnmatch(k, filter_pattern): del images[k] @@ -753,7 +753,7 @@ class RepositoryManager(object): Return a list of available payloads ''' if len(self.onlines) == 0: - raise Exception("No online repository") + raise Exception(u"No online repository") # building payload list paylist = {} for reponame in self.onlines: @@ -811,7 +811,7 @@ class RepositoryManager(object): Remove local cached repository files ''' for reponame in self.select_repositories(patterns): - arrow("Purging cache of repository %s" % reponame) + arrow(u"Purging cache of repository %s" % reponame) db = os.path.join(self.cache_path, reponame) if os.path.lexists(db): try: @@ -858,11 +858,11 @@ class RepositoryManager(object): sl = "#l##y#Local#R# " if repo["local"] else "#l##c#Remote#R# " rc = "#l##r#" if repo["offline"] else "#l##g#" if o_state: - ln += "%s%s " % (so, sl) + ln += u"%s%s " % (so, sl) rc = "#l##b#" - ln += "%s%s#R#"% (rc, name) + ln += u"%s%s#R#"% (rc, name) if o_url: - ln += " (%s)" % repo["path"] + ln += u" (%s)" % repo["path"] l.append(ln) s = os.linesep.join(l) out(s) @@ -895,7 +895,7 @@ class RepositoryConfig(object): def __str__(self): l = [] for k, v in self.items(): - l.append("%s: %s" % (k, v)) + l.append(u"%s: %s" % (k, v)) return os.linesep.join(l) def __eq__(self, other): @@ -1049,6 +1049,7 @@ class RepositoryConfig(object): try: setattr(self, k, kwargs[k]) except Exception as e: - warn("Unable to set config parameter %s in repository %s: %s" % (k, self.name, e)) + warn(u"Unable to set config parameter %s in repository %s: %s" % + (k, self.name, e)) else: - debug("No such repository parameter: %s" % k) + debug(u"No such repository parameter: %s" % k) diff --git a/installsystems/template.py b/installsystems/template.py index 56e8025..06ce72d 100644 --- a/installsystems/template.py +++ b/installsystems/template.py @@ -39,7 +39,7 @@ from installsystems.printer import arrow class TargetAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): if not os.path.isdir(values): - raise Exception("Invalid target directory %s" % values) + raise Exception(u"Invalid target directory %s" % values) namespace.target = values parser.add_argument("-n", "--hostname", dest="hostname", type=str, required=True) @@ -57,7 +57,7 @@ setup = u"""# -*- python -*- from installsystems.printer import arrow -arrow("hostname: %s" % namespace.hostname) +arrow(u"hostname: %s" % namespace.hostname) # uncomment to extract payload named root in namespace.target directory #image.payload["rootfs"].extract(namespace.target) diff --git a/installsystems/tools.py b/installsystems/tools.py index c83aa84..8898b00 100644 --- a/installsystems/tools.py +++ b/installsystems/tools.py @@ -189,8 +189,8 @@ class PipeFile(object): def close(self): if self.progressbar: self._progressbar.finish() - debug("MD5: %s" % self.md5) - debug("Size: %s" % self.consumed_size) + debug(u"MD5: %s" % self.md5) + debug(u"Size: %s" % self.consumed_size) self.fo.close() def read(self, size=None): @@ -381,7 +381,7 @@ def human_size(num, unit='B'): if power >= len(prefixes): power = len(prefixes) - 1 scaled = num / float(1024 ** power) - return "%3.1f%s%s" % (scaled, prefixes[power], unit) + return u"%3.1f%s%s" % (scaled, prefixes[power], unit) def time_rfc2822(timestamp): ''' @@ -409,16 +409,16 @@ def prepare_chroot(path, mount=True): mps = ("proc", "sys", "dev", "dev/pts", "dev/shm") arrow("Mouting filesystems") for mp in mps: - origin = "/%s" % mp + origin = u"/%s" % mp target = os.path.join(path, mp) if os.path.ismount(target): - warn("%s is already a mountpoint, skipped" % target) + warn(u"%s is already a mountpoint, skipped" % target) elif os.path.ismount(origin) and os.path.isdir(target): - arrow("%s -> %s" % (origin, target), 1) + arrow(u"%s -> %s" % (origin, target), 1) try: check_call(["mount", "--bind", origin, target], close_fds=True) except CalledProcessError as e: - warn("Mount failed: %s.\n" % e) + warn(u"Mount failed: %s.\n" % e) arrow("Tricks") exists = os.path.exists join = os.path.join @@ -440,7 +440,7 @@ def prepare_chroot(path, mount=True): open(resolv_trick_path, "wb") shutil.copy("/etc/resolv.conf", resolv_path) except Exception as e: - warn("resolv.conf tricks fail: %s" % e) + warn(u"resolv.conf tricks fail: %s" % e) # trick mtab try: mtab_path = join(path, "etc", "mtab") @@ -452,7 +452,7 @@ def prepare_chroot(path, mount=True): os.rename(mtab_path, mtab_backup_path) os.symlink("/proc/self/mounts", mtab_path) except Exception as e: - warn("mtab tricks fail: %s" % e) + warn(u"mtab tricks fail: %s" % e) # try to guest distro distro = guess_distro(path) # in case of debian disable policy @@ -490,14 +490,14 @@ def unprepare_chroot(path, mount=True): try: os.unlink(mtab_trick_path) except OSError: - warn("Unable to remove %s" % mtab_trick_path) + warn(u"Unable to remove %s" % mtab_trick_path) if exists(mtab_backup_path): try: os.unlink(mtab_path) except OSError: pass try: os.rename(mtab_backup_path, mtab_path) except OSError: - warn("Unable to restore %s" % mtab_backup_path) + warn(u"Unable to restore %s" % mtab_backup_path) # untrick resolv.conf resolv_path = join(path, "etc", "resolv.conf") @@ -512,14 +512,14 @@ def unprepare_chroot(path, mount=True): try: os.unlink(resolv_trick_path) except OSError: - warn("Unable to remove %s" % resolv_trick_path) + warn(u"Unable to remove %s" % resolv_trick_path) if exists(resolv_backup_path): try: os.unlink(resolv_path) except OSError: pass try: os.rename(resolv_backup_path, resolv_path) except OSError: - warn("Unable to restore %s" % resolv_backup_path) + warn(u"Unable to restore %s" % resolv_backup_path) # try to guest distro distro = guess_distro(path) # cleaning debian stuff @@ -546,7 +546,7 @@ def chroot(path, shell="/bin/bash", mount=True): # prepare to chroot prepare_chroot(path, mount) # chrooting - arrow("Chrooting inside %s and running %s" % (path, shell)) + arrow(u"Chrooting inside %s and running %s" % (path, shell)) call(["chroot", path, shell], close_fds=True) # revert preparation of chroot unprepare_chroot(path, mount) @@ -556,7 +556,7 @@ def is_version(version): Check if version is valid ''' if re.match("^(\d+)(?:([-~+]).*)?$", version) is None: - raise TypeError("Invalid version format %s" % buf) + raise TypeError(u"Invalid version format %s" % buf) def compare_versions(v1, v2): ''' @@ -574,7 +574,7 @@ def compare_versions(v1, v2): elif isinstance(version, basestring): iv = re.match("^(\d+)(?:([-~+]).*)?$", version) if iv is None: - raise TypeError('Invalid version format: %s' % version) + raise TypeError(u"Invalid version format: %s" % version) rv = float(iv.group(1)) if iv.group(2) == "~": rv -= 0.1 @@ -582,7 +582,7 @@ def compare_versions(v1, v2): rv += 0.1 return rv else: - raise TypeError('Invalid version format: %s' % version) + raise TypeError(u"Invalid version format: %s" % version) fv1 = get_ver(v1) fv2 = get_ver(v2) @@ -607,7 +607,7 @@ def get_compressor_path(name, compress=True, level=None): allcompressors = compressors if compress else decompressors # check compressor exists if name not in allcompressors.keys(): - raise Exception("Invalid compressor name: %s" % name) + raise Exception(u"Invalid compressor name: %s" % name) # get valid compressors for compressor in allcompressors[name]: path = pathsearch(compressor[0]) @@ -616,7 +616,7 @@ def get_compressor_path(name, compress=True, level=None): if level is not None: compressor.append("-%d" % level) return compressor - raise Exception("No external decompressor for %s" % name) + raise Exception(u"No external decompressor for %s" % name) def render_templates(target, context, tpl_ext=".istpl", force=False, keep=False): ''' @@ -631,7 +631,7 @@ def render_templates(target, context, tpl_ext=".istpl", force=False, keep=False) file_path = os.path.join(path[0], name) arrow(tpl_path) if os.path.exists(file_path) and not force: - raise Exception("%s will be overwritten, cancel template " + raise Exception(u"%s will be overwritten, cancel template " "generation (set force=True if you know " "what you do)" % file_path) try: @@ -640,7 +640,7 @@ def render_templates(target, context, tpl_ext=".istpl", force=False, keep=False) with open(file_path, "w") as rendered_file: rendered_file.write(template.render(context)) except Exception as e: - raise Exception("Render template fail: %s" % e) + raise Exception(u"Render template fail: %s" % e) st = os.stat(tpl_path) os.chown(file_path, st.st_uid, st.st_gid) os.chmod(file_path, st.st_mode) -- GitLab