Make buckifier python3 compatible (#5922)

Summary:
Make buckifier/buckify_rocksdb.py run on both Python 3 and 2
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5922

Test Plan:
```
$python3 buckifier/buckify_rocksdb.py
$python3 buckifier/buckify_rocksdb.py '{"fake": {"extra_deps": [":test_dep", "//fakes/module:mock1"], "extra_compiler_flags": ["-DROCKSDB_LITE", "-Os"]}}'
$python2 buckifier/buckify_rocksdb.py
$python2 buckifier/buckify_rocksdb.py '{"fake": {"extra_deps": [":test_dep", "//fakes/module:mock1"], "extra_compiler_flags": ["-DROCKSDB_LITE", "-Os"]}}'
```

Differential Revision: D17920611

Pulled By: riversand963

fbshipit-source-id: cc6e2f36013a88a710d96098f6ca18cbe85e3f62
This commit is contained in:
Yanqin Jin 2019-10-23 13:51:03 -07:00 committed by Facebook Github Bot
parent 0933360644
commit b4ebda7a39
4 changed files with 29 additions and 13 deletions

1
.gitignore vendored
View File

@ -79,3 +79,4 @@ tp2/
fbcode/ fbcode/
fbcode fbcode
buckifier/*.pyc buckifier/*.pyc
buckifier/__pycache__

View File

@ -3,6 +3,10 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
try:
from builtins import str
except ImportError:
from __builtin__ import str
from targets_builder import TARGETSBuilder from targets_builder import TARGETSBuilder
import json import json
import os import os
@ -108,9 +112,9 @@ def get_tests(repo_path):
# Parse extra dependencies passed by user from command line # Parse extra dependencies passed by user from command line
def get_dependencies(): def get_dependencies():
deps_map = { deps_map = {
''.encode('ascii'): { '': {
'extra_deps'.encode('ascii'): [], 'extra_deps': [],
'extra_compiler_flags'.encode('ascii'): [] 'extra_compiler_flags': []
} }
} }
if len(sys.argv) < 2: if len(sys.argv) < 2:
@ -119,13 +123,7 @@ def get_dependencies():
def encode_dict(data): def encode_dict(data):
rv = {} rv = {}
for k, v in data.items(): for k, v in data.items():
if isinstance(k, unicode): if isinstance(v, dict):
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):
v = encode_dict(v) v = encode_dict(v)
rv[k] = v rv[k] = v
return rv return rv

View File

@ -3,6 +3,12 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals 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 import targets_cfg
def pretty_list(lst, indent=8): def pretty_list(lst, indent=8):
@ -18,7 +24,7 @@ def pretty_list(lst, indent=8):
return res return res
class TARGETSBuilder: class TARGETSBuilder(object):
def __init__(self, path): def __init__(self, path):
self.path = path self.path = path
self.targets_file = open(path, 'w') self.targets_file = open(path, 'w')

View File

@ -6,11 +6,16 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
try:
from builtins import object
except ImportError:
from __builtin__ import object
import subprocess import subprocess
import sys
import os import os
import time import time
class ColorString: class ColorString(object):
""" Generate colorful strings on terminal """ """ Generate colorful strings on terminal """
HEADER = '\033[95m' HEADER = '\033[95m'
BLUE = '\033[94m' BLUE = '\033[94m'
@ -21,7 +26,13 @@ class ColorString:
@staticmethod @staticmethod
def _make_color_str(text, color): 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 @staticmethod
def ok(text): def ok(text):