|
104 | 104 | debug(traceback.format_exc()) |
105 | 105 | error("rosdistro was not detected, please install it.", exit=True) |
106 | 106 |
|
| 107 | +try: |
| 108 | + import em |
| 109 | +except ImportError: |
| 110 | + debug(traceback.format_exc()) |
| 111 | + error("empy was not detected, please install it.", exit=True) |
| 112 | + |
| 113 | +# Fix unicode bug in empy |
| 114 | +# This should be removed once upstream empy is fixed |
| 115 | +# See: https://github.com/ros-infrastructure/bloom/issues/196 |
| 116 | +try: |
| 117 | + em.str = unicode |
| 118 | + em.Stream.write_old = em.Stream.write |
| 119 | + em.Stream.write = lambda self, data: em.Stream.write_old(self, data.encode('utf8')) |
| 120 | +except NameError: |
| 121 | + pass |
| 122 | +# End fix |
| 123 | + |
107 | 124 | # Drop the first log prefix for this command |
108 | 125 | enable_drop_first_log_prefix(True) |
109 | 126 |
|
@@ -136,7 +153,10 @@ def __place_template_folder(group, src, dst, gbp=False): |
136 | 153 | debug("Not overwriting existing file '{0}'".format(template_dst)) |
137 | 154 | else: |
138 | 155 | with io.open(template_dst, 'w', encoding='utf-8') as f: |
139 | | - if isinstance(template, bytes): |
| 156 | + if not isinstance(template, str): |
| 157 | + template = template.decode('utf-8') |
| 158 | + # Python 2 API needs a `unicode` not a utf-8 string. |
| 159 | + elif sys.version_info.major == 2: |
140 | 160 | template = template.decode('utf-8') |
141 | 161 | f.write(template) |
142 | 162 | shutil.copystat(template_abs_path, template_dst) |
@@ -465,11 +485,17 @@ def generate_substitutions_from_package( |
465 | 485 | data['Licenses'] = licenses |
466 | 486 |
|
467 | 487 | def convertToUnicode(obj): |
468 | | - if isinstance(obj, bytes): |
469 | | - return str(obj.decode('utf8')) |
470 | | - elif isinstance(obj, str): |
471 | | - return obj |
472 | | - elif isinstance(obj, list): |
| 488 | + if sys.version_info.major == 2: |
| 489 | + if isinstance(obj, str): |
| 490 | + return unicode(obj.decode('utf8')) |
| 491 | + elif isinstance(obj, unicode): |
| 492 | + return obj |
| 493 | + else: |
| 494 | + if isinstance(obj, bytes): |
| 495 | + return str(obj.decode('utf8')) |
| 496 | + elif isinstance(obj, str): |
| 497 | + return obj |
| 498 | + if isinstance(obj, list): |
473 | 499 | for i, val in enumerate(obj): |
474 | 500 | obj[i] = convertToUnicode(val) |
475 | 501 | return obj |
@@ -518,6 +544,8 @@ def __process_template_folder(path, subs): |
518 | 544 | continue |
519 | 545 | # Write the result |
520 | 546 | with io.open(template_path, 'w', encoding='utf-8') as f: |
| 547 | + if sys.version_info.major == 2: |
| 548 | + result = result.decode('utf-8') |
521 | 549 | f.write(result) |
522 | 550 | # Copy the permissions |
523 | 551 | shutil.copymode(item, template_path) |
|
0 commit comments