From: Catalin Marinas Date: Fri, 8 Jun 2007 22:18:04 +0000 (+0100) Subject: Factor out common functionality into the utils.py file X-Git-Tag: v0.13~47 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/stgit/commitdiff_plain/17364282a3b40e2c68f4d15ec878f22d73487fb9?hp=ca8b854cbf353ed87fd9284d50f69229bf40e22d Factor out common functionality into the utils.py file Sequences for writing or reading lists to/from files are moved to the stgit.utils file. Signed-off-by: Catalin Marinas --- diff --git a/stgit/stack.py b/stgit/stack.py index 9338f2b..a64aff6 100644 --- a/stgit/stack.py +++ b/stgit/stack.py @@ -423,26 +423,17 @@ class Series(StgitObject): def get_applied(self): if not os.path.isfile(self.__applied_file): raise StackException, 'Branch "%s" not initialised' % self.__name - f = file(self.__applied_file) - names = [line.strip() for line in f.readlines()] - f.close() - return names + return read_strings(self.__applied_file) def get_unapplied(self): if not os.path.isfile(self.__unapplied_file): raise StackException, 'Branch "%s" not initialised' % self.__name - f = file(self.__unapplied_file) - names = [line.strip() for line in f.readlines()] - f.close() - return names + return read_strings(self.__unapplied_file) def get_hidden(self): if not os.path.isfile(self.__hidden_file): return [] - f = file(self.__hidden_file) - names = [line.strip() for line in f.readlines()] - f.close() - return names + return read_strings(self.__hidden_file) def get_base(self): # Return the parent of the bottommost patch, if there is one. @@ -865,10 +856,7 @@ class Series(StgitObject): self.log_patch(patch, 'new') patches = [patch.get_name()] + self.get_unapplied() - - f = file(self.__unapplied_file, 'w+') - f.writelines([line + '\n' for line in patches]) - f.close() + write_strings(self.__unapplied_file, patches) elif before_existing: self.log_patch(patch, 'new') @@ -901,9 +889,7 @@ class Series(StgitObject): unapplied = self.get_unapplied() unapplied.remove(name) - f = file(self.__unapplied_file, 'w+') - f.writelines([line + '\n' for line in unapplied]) - f.close() + write_strings(self.__unapplied_file, unapplied) def forward_patches(self, names): """Try to fast-forward an array of patches. @@ -975,10 +961,7 @@ class Series(StgitObject): git.switch(top) append_strings(self.__applied_file, names[0:forwarded]) - - f = file(self.__unapplied_file, 'w+') - f.writelines([line + '\n' for line in unapplied]) - f.close() + write_strings(self.__unapplied_file, unapplied) return forwarded @@ -1056,9 +1039,7 @@ class Series(StgitObject): append_string(self.__applied_file, name) unapplied.remove(name) - f = file(self.__unapplied_file, 'w+') - f.writelines([line + '\n' for line in unapplied]) - f.close() + write_strings(self.__unapplied_file, unapplied) # head == bottom case doesn't need to refresh the patch if empty or head != bottom: @@ -1126,17 +1107,11 @@ class Series(StgitObject): popped = applied[:idx] popped.reverse() unapplied = popped + self.get_unapplied() - - f = file(self.__unapplied_file, 'w+') - f.writelines([line + '\n' for line in unapplied]) - f.close() + write_strings(self.__unapplied_file, unapplied) del applied[:idx] applied.reverse() - - f = file(self.__applied_file, 'w+') - f.writelines([line + '\n' for line in applied]) - f.close() + write_strings(self.__applied_file, applied) def empty_patch(self, name): """Returns True if the patch is empty @@ -1169,18 +1144,12 @@ class Series(StgitObject): if oldname in unapplied: Patch(oldname, self.__patch_dir, self.__refs_dir).rename(newname) unapplied[unapplied.index(oldname)] = newname - - f = file(self.__unapplied_file, 'w+') - f.writelines([line + '\n' for line in unapplied]) - f.close() + write_strings(self.__unapplied_file, unapplied) elif oldname in applied: Patch(oldname, self.__patch_dir, self.__refs_dir).rename(newname) applied[applied.index(oldname)] = newname - - f = file(self.__applied_file, 'w+') - f.writelines([line + '\n' for line in applied]) - f.close() + write_strings(self.__applied_file, applied) else: raise StackException, 'Unknown patch "%s"' % oldname @@ -1221,9 +1190,7 @@ class Series(StgitObject): append_string(self.__hidden_file, name) unapplied.remove(name) - f = file(self.__unapplied_file, 'w+') - f.writelines([line + '\n' for line in unapplied]) - f.close() + write_strings(self.__unapplied_file, unapplied) def unhide_patch(self, name): """Remove the patch from the hidden list. @@ -1236,9 +1203,7 @@ class Series(StgitObject): raise StackException, 'Unknown patch "%s"' % name hidden.remove(name) - f = file(self.__hidden_file, 'w+') - f.writelines([line + '\n' for line in hidden]) - f.close() + write_strings(self.__hidden_file, hidden) if not self.patch_applied(name) and not self.patch_unapplied(name): # check needed for backward compatibility with the old diff --git a/stgit/utils.py b/stgit/utils.py index ad9b1f1..039b433 100644 --- a/stgit/utils.py +++ b/stgit/utils.py @@ -107,6 +107,14 @@ def mkdir_file(filename, mode): create_dirs(os.path.dirname(filename)) return file(filename, mode) +def read_strings(filename): + """Reads the lines from a file + """ + f = file(filename, 'r') + lines = [line.strip() for line in f.readlines()] + f.close() + return lines + def read_string(filename, multiline = False): """Reads the first line from a file """ @@ -118,6 +126,13 @@ def read_string(filename, multiline = False): f.close() return result +def write_strings(filename, lines): + """Write 'lines' sequence to file + """ + f = file(filename, 'w+') + f.writelines([line + '\n' for line in lines]) + f.close() + def write_string(filename, line, multiline = False): """Writes 'line' to file and truncates it """