From b7e626f15fd3059686b8925b4e854568519fd92c Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Thu, 15 May 2014 11:26:03 +0200 Subject: Import helper functions that have been removed from python-fastimport. --- branch_updater.py | 2 +- cache_manager.py | 13 ++++++++++- cmds.py | 6 +++-- exporter.py | 2 +- helpers.py | 50 +++++++++++++++++++++++++++++++++++++++++ processors/generic_processor.py | 2 +- processors/info_processor.py | 8 +++---- 7 files changed, 73 insertions(+), 10 deletions(-) diff --git a/branch_updater.py b/branch_updater.py index e9a8aa5..0a4d5e1 100644 --- a/branch_updater.py +++ b/branch_updater.py @@ -22,6 +22,7 @@ from bzrlib.trace import show_error, note from bzrlib.plugins.fastimport.helpers import ( best_format_for_objects_in_a_repository, + single_plural, ) @@ -149,7 +150,6 @@ class BranchUpdater(object): :return: whether the branch was changed or not """ - from fastimport.helpers import single_plural last_rev_id = self.cache_mgr.lookup_committish(last_mark) self.repo.lock_read() try: diff --git a/cache_manager.py b/cache_manager.py index e0f7f90..ed88aa9 100644 --- a/cache_manager.py +++ b/cache_manager.py @@ -28,7 +28,7 @@ from bzrlib.plugins.fastimport import ( from bzrlib.plugins.fastimport.reftracker import ( RefTracker, ) -from fastimport.helpers import ( +from bzrlib.plugins.fastimport.helpers import ( single_plural, ) @@ -275,3 +275,14 @@ class CacheManager(object): return content +def invert_dictset(d): + """Invert a dictionary with keys matching a set of values, turned into lists.""" + # Based on recipe from ASPN + result = {} + for k, c in d.iteritems(): + for v in c: + keys = result.setdefault(v, []) + keys.append(k) + return result + + diff --git a/cmds.py b/cmds.py index 1c143f8..c4a5a8a 100644 --- a/cmds.py +++ b/cmds.py @@ -19,7 +19,10 @@ from bzrlib import bzrdir from bzrlib.commands import Command from bzrlib.option import Option, ListOption, RegistryOption -from bzrlib.plugins.fastimport import load_fastimport +from bzrlib.plugins.fastimport import ( + helpers, + load_fastimport, + ) def _run(source, processor_factory, verbose=False, user_map=None, **kwargs): @@ -47,7 +50,6 @@ def _run(source, processor_factory, verbose=False, user_map=None, **kwargs): def _get_source_stream(source): if source == '-' or source is None: import sys - from fastimport import helpers stream = helpers.binary_stream(sys.stdin) elif source.endswith('.gz'): import gzip diff --git a/exporter.py b/exporter.py index f75399d..f2006c1 100644 --- a/exporter.py +++ b/exporter.py @@ -64,7 +64,7 @@ from bzrlib.plugins.fastimport import ( ) from fastimport import commands -from fastimport.helpers import ( +from bzrlib.plugins.fastimport.helpers import ( binary_stream, single_plural, ) diff --git a/helpers.py b/helpers.py index fed4d68..6bba6fd 100644 --- a/helpers.py +++ b/helpers.py @@ -146,3 +146,53 @@ def mode_to_kind(mode): return 'tree-reference', False else: raise AssertionError("invalid mode %o" % mode) + + +def binary_stream(stream): + """Ensure a stream is binary on Windows. + + :return: the stream + """ + try: + import os + if os.name == 'nt': + fileno = getattr(stream, 'fileno', None) + if fileno: + no = fileno() + if no >= 0: # -1 means we're working as subprocess + import msvcrt + msvcrt.setmode(no, os.O_BINARY) + except ImportError: + pass + return stream + + +def single_plural(n, single, plural): + """Return a single or plural form of a noun based on number.""" + if n == 1: + return single + else: + return plural + + +def invert_dictset(d): + """Invert a dictionary with keys matching a set of values, turned into lists.""" + # Based on recipe from ASPN + result = {} + for k, c in d.iteritems(): + for v in c: + keys = result.setdefault(v, []) + keys.append(k) + return result + + +def invert_dict(d): + """Invert a dictionary with keys matching each value turned into a list.""" + # Based on recipe from ASPN + result = {} + for k, v in d.iteritems(): + keys = result.setdefault(v, []) + keys.append(k) + return result + + diff --git a/processors/generic_processor.py b/processors/generic_processor.py index 80b0c5d..ae63ad8 100644 --- a/processors/generic_processor.py +++ b/processors/generic_processor.py @@ -40,6 +40,7 @@ except ImportError: from bzrlib.plugins.fastimport import ( branch_updater, cache_manager, + helpers, idmapfile, marks_file, revision_store, @@ -47,7 +48,6 @@ from bzrlib.plugins.fastimport import ( from fastimport import ( commands, errors as plugin_errors, - helpers, processor, ) diff --git a/processors/info_processor.py b/processors/info_processor.py index bb162e7..af80557 100644 --- a/processors/info_processor.py +++ b/processors/info_processor.py @@ -18,14 +18,14 @@ from bzrlib.plugins.fastimport import ( reftracker, ) +from bzrlib.plugins.fastimport.helpers import ( + invert_dict, + invert_dictset, + ) from fastimport import ( commands, processor, ) -from fastimport.helpers import ( - invert_dict, - invert_dictset, - ) import stat -- cgit v1.2.1