diff --git a/.gitignore b/.gitignore index 199458901e..9a281d6767 100644 --- a/.gitignore +++ b/.gitignore @@ -79,3 +79,4 @@ tp2/ fbcode/ fbcode buckifier/*.pyc +buckifier/__pycache__ diff --git a/buckifier/buckify_rocksdb.py b/buckifier/buckify_rocksdb.py index 3b814f3271..d2bba5940c 100644 --- a/buckifier/buckify_rocksdb.py +++ b/buckifier/buckify_rocksdb.py @@ -3,6 +3,10 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals +try: + from builtins import str +except ImportError: + from __builtin__ import str from targets_builder import TARGETSBuilder import json import os @@ -108,9 +112,9 @@ def get_tests(repo_path): # Parse extra dependencies passed by user from command line def get_dependencies(): deps_map = { - ''.encode('ascii'): { - 'extra_deps'.encode('ascii'): [], - 'extra_compiler_flags'.encode('ascii'): [] + '': { + 'extra_deps': [], + 'extra_compiler_flags': [] } } if len(sys.argv) < 2: @@ -119,13 +123,7 @@ def get_dependencies(): def encode_dict(data): rv = {} for k, v in data.items(): - if isinstance(k, unicode): - k = k.encode('ascii') - if isinstance(v, unicode): - v = v.encode('ascii') - elif isinstance(v, list): - v = [x.encode('ascii') for x in v] - elif isinstance(v, dict): + if isinstance(v, dict): v = encode_dict(v) rv[k] = v return rv diff --git a/buckifier/targets_builder.py b/buckifier/targets_builder.py index 78db6a169b..ba90bc612d 100644 --- a/buckifier/targets_builder.py +++ b/buckifier/targets_builder.py @@ -3,6 +3,12 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals +try: + from builtins import object + from builtins import str +except ImportError: + from __builtin__ import object + from __builtin__ import str import targets_cfg def pretty_list(lst, indent=8): @@ -18,7 +24,7 @@ def pretty_list(lst, indent=8): return res -class TARGETSBuilder: +class TARGETSBuilder(object): def __init__(self, path): self.path = path self.targets_file = open(path, 'w') diff --git a/buckifier/util.py b/buckifier/util.py index 2eda69f107..f04929a277 100644 --- a/buckifier/util.py +++ b/buckifier/util.py @@ -6,11 +6,16 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals +try: + from builtins import object +except ImportError: + from __builtin__ import object import subprocess +import sys import os import time -class ColorString: +class ColorString(object): """ Generate colorful strings on terminal """ HEADER = '\033[95m' BLUE = '\033[94m' @@ -21,7 +26,13 @@ class ColorString: @staticmethod def _make_color_str(text, color): - return "".join([color, text.encode('utf-8'), ColorString.ENDC]) + # In Python2, default encoding for unicode string is ASCII + if sys.version_info.major <= 2: + return "".join( + [color, text.encode('utf-8'), ColorString.ENDC]) + # From Python3, default encoding for unicode string is UTF-8 + return "".join( + [color, text, ColorString.ENDC]) @staticmethod def ok(text):