diff --git a/ognibuild/debian/fix_build.py b/ognibuild/debian/fix_build.py index 907a69e..4abf1c5 100644 --- a/ognibuild/debian/fix_build.py +++ b/ognibuild/debian/fix_build.py @@ -19,10 +19,11 @@ __all__ = [ "build_incrementally", ] +from datetime import datetime import logging import os import sys -from typing import List, Set, Optional, Type +from typing import List, Set, Optional, Type, Tuple from debian.deb822 import ( Deb822, @@ -32,6 +33,7 @@ from debian.deb822 import ( from breezy.commit import PointlessCommit from breezy.mutabletree import MutableTree from breezy.tree import Tree +from debmutate.changelog import ChangelogEditor from debmutate.control import ( ensure_relation, ControlEditor, @@ -49,12 +51,40 @@ from debmutate.reformatting import ( try: from breezy.workspace import reset_tree -except ImportError: - from lintian_brush import reset_tree +except ImportError: # breezy < 3.2 + def delete_items(deletables, dry_run=False): + """Delete files in the deletables iterable""" + import errno + import shutil + def onerror(function, path, excinfo): + """Show warning for errors seen by rmtree. + """ + # Handle only permission error while removing files. + # Other errors are re-raised. + if function is not os.remove or excinfo[1].errno != errno.EACCES: + raise + logging.warning('unable to remove %s' % path) + for path, subp in deletables: + if os.path.isdir(path): + shutil.rmtree(path, onerror=onerror) + else: + try: + os.unlink(path) + except OSError as e: + # We handle only permission error here + if e.errno != errno.EACCES: + raise e + logging.warning('unable to remove "%s": %s.', path, e.strerror) + + def reset_tree(local_tree, subpath=''): + from breezy.transform import revert + from breezy.clean_tree import iter_deletables + revert(local_tree, local_tree.branch.basis_tree(), + [subpath] if subpath not in ('.', '') else None) + deletables = list(iter_deletables( + local_tree, unknown=True, ignored=False, detritus=False)) + delete_items(deletables) -from lintian_brush.changelog import ( - add_changelog_entry, -) from debmutate._rules import ( dh_invoke_add_with, @@ -283,9 +313,9 @@ def commit_debian_changes( with tree.lock_write(): try: if update_changelog: - add_changelog_entry( - tree, os.path.join(subpath, "debian/changelog"), [summary] - ) + cl_path = tree.abspath(os.path.join(subpath, "debian/changelog")) + with ChangelogEditor(cl_path) as editor: + editor.add_entry([summary]) debcommit(tree, committer=committer, subpath=subpath) else: tree.commit(