Skip to content

Commit

Permalink
Make buckifier python3 compatible (facebook#5922)
Browse files Browse the repository at this point in the history
Summary:
Make buckifier/buckify_rocksdb.py run on both Python 3 and 2
Pull Request resolved: facebook#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
  • Loading branch information
riversand963 authored and facebook-github-bot committed Oct 23, 2019
1 parent 0933360 commit b4ebda7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ tp2/
fbcode/
fbcode
buckifier/*.pyc
buckifier/__pycache__
18 changes: 8 additions & 10 deletions buckifier/buckify_rocksdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion buckifier/targets_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
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):
Expand All @@ -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')
Expand Down
15 changes: 13 additions & 2 deletions buckifier/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
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'
Expand All @@ -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):
Expand Down

0 comments on commit b4ebda7

Please sign in to comment.