From 6e5bb3ca8d95e6af189574522338ea48072b7dfa Mon Sep 17 00:00:00 2001 From: Stefan Sauer Date: Sun, 2 Apr 2017 21:58:16 +0200 Subject: [PATCH] common.py: start porting common.py from perl Add a first function with a test. --- Makefile.am | 2 ++ gtkdoc/common.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 2 +- tests/gtkdoc-common.py | 18 +++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 gtkdoc/common.py create mode 100755 tests/gtkdoc-common.py diff --git a/Makefile.am b/Makefile.am index baab3cb..f7f16fc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -38,6 +38,7 @@ pylibdatadir = $(datadir)/gtk-doc/python/gtkdoc pylibdata_DATA = \ gtkdoc/__init__.py \ gtkdoc/check.py \ + gtkdoc/common.py \ gtkdoc/config.py \ gtkdoc/mkhtml.py \ gtkdoc/mkman.py \ @@ -81,6 +82,7 @@ CLEANFILES = \ gtkdoc-rebasec \ gtkdoc/__init__.pyc \ gtkdoc/check.pyc \ + gtkdoc/common.pyc \ gtkdoc/config.pyc \ gtkdoc/mkhtml.pyc \ gtkdoc/mkman.pyc \ diff --git a/gtkdoc/common.py b/gtkdoc/common.py new file mode 100644 index 0000000..0bfb966 --- /dev/null +++ b/gtkdoc/common.py @@ -0,0 +1,59 @@ +#!/usr/bin/python -w +# -*- python -*- +# +# gtk-doc - GTK DocBook documentation generator. +# Copyright (C) 2001 Damon Chaplin +# 2007-2016 Stefan Sauer +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +import logging, os + + +def UpdateFileIfChanged(old_file, new_file, make_backup): + """Compares the old version of the file with the new version and if the + file has changed it moves the new version into the old versions place. This + is used so we only change files if needed, so we can do proper dependency + tracking. + + Args: + old_file (string): The pathname of the old file. + new_file (string): The pathname of the new version of the file. + make_backup (bool) True if a backup of the old file should be kept. + It will have the .bak suffix added to the file name. + + Returns: + bool: It returns False if the file hasn't changed, and True if it has. + """ + + logging.debug("Comparing %s with %s...", old_file, new_file); + + if os.path.exists(old_file): + old_contents = open(old_file, 'rb').read() + new_contents = open(new_file, 'rb').read() + if old_contents == new_contents: + return False + + if make_backup: + backupname = old_file + '.bak' + if os.path.exists(backupname): + os.unlink(backupname) + os.rename(old_file, backupname) + else: + os.unlink(old_file) + + os.rename(new_file, old_file) + return True diff --git a/tests/Makefile.am b/tests/Makefile.am index f5c41ef..1a7b303 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,7 +6,7 @@ if BUILD_TESTS TESTS = \ gtkdoc-common.t gtkdoc-fixxref.t gtkdoc-mkdb.t gtkdoc-scan.t \ - gtkdoc-check.py \ + gtkdoc-check.py gtkdoc-common.py \ tools.sh gobject.sh bugs.sh annotations.sh fail.sh empty.sh sanity.sh \ program.sh TESTS_ENVIRONMENT = \ diff --git a/tests/gtkdoc-common.py b/tests/gtkdoc-common.py new file mode 100755 index 0000000..722ae5a --- /dev/null +++ b/tests/gtkdoc-common.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +import mock, os, unittest + +from gtkdoc import common + +class TestCommon(unittest.TestCase): + + @mock.patch('os.path.exists') + @mock.patch('os.rename') + def test_UpdateFileIfChanged_NoOldFile(self, os_rename, os_path_exists): + os_path_exists.return_value = False + res = common.UpdateFileIfChanged('/foo', '/bar', False) + os_rename.assert_called_with('/bar', '/foo') + self.assertTrue(res) + +if __name__ == '__main__': + unittest.main() -- 2.11.4.GIT