Skip to content

Commit

Permalink
Add support for stdout output using Click.File
Browse files Browse the repository at this point in the history
- This approach relies on Click==7.0, which brings a fix to the
Click.File annotation (previously file modes "w" and "a" were assuming
<stdin> as the default value, which is fixed to <stdout> now)
- Using the "a+" to read and write a file in the same stream, making the
code cleaner
- There is still a problem with the stdout check related to tests, but
it can be ignored for now
- Update requirements to match the new Click version

Signed-off-by: Agnaldo Junior <[email protected]>
  • Loading branch information
agnjunio committed Feb 2, 2019
1 parent d79cbb1 commit 1c3cccf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
51 changes: 29 additions & 22 deletions compiledb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,59 @@
import json
import os
import sys

from compiledb.parser import parse_build_log, Error


def __is_stdout(compdb_path):
return not compdb_path or compdb_path == '-'
def __is_stdout(pfile):
try:
return pfile.name == sys.stdout.name
except:
return pfile == sys.stdout


def basename(stream):
if __is_stdout(stream):
return "<stdout>"
else:
return os.path.basename(stream.name)


def generate_json_compdb(instream=None, proj_dir=os.getcwd(), verbose=False, exclude_files=[]):
if not os.path.isdir(proj_dir):
raise Error("Project dir '{}' does not exists!".format(proj_dir))

print("## Processing build commands from {}".format(instream.name))
print("## Processing build commands from {}".format(basename(instream)))
result = parse_build_log(instream, proj_dir, exclude_files, verbose)
return result


def write_json_compdb(compdb, compdb_path='compile_commands.json', verbose=False,
def write_json_compdb(compdb, outstream, verbose=False,
force=False, pretty_output=True):
if not __is_stdout(compdb_path):
print("## Writing compilation database with {} entries to {}".format(
len(compdb), compdb_path))
print("## Writing compilation database with {} entries to {}".format(
len(compdb), basename(outstream)))

with open(compdb_path, 'w') as outstream:
json.dump(compdb, outstream, indent=pretty_output)
outstream.write(os.linesep)
else:
print("## Writing compilation database with {} entries to <stdout>".format(
len(compdb)))
print(json.dumps(compdb, indent=pretty_output))
# We could truncate after reading, but here is easier to understand
if not __is_stdout(outstream):
outstream.seek(0)
outstream.truncate()
json.dump(compdb, outstream, indent=pretty_output)
outstream.write(os.linesep)


def load_json_compdb(compdb_path='compile_commands.json', verbose=False):
def load_json_compdb(outstream, verbose=False):
try:
if __is_stdout(compdb_path):
if __is_stdout(outstream):
return []

with open(compdb_path, 'r') as instream:
compdb = json.load(instream)

# Read from beggining of file
outstream.seek(0)
compdb = json.load(outstream)
print("## Loaded compilation database with {} entries from {}".format(
len(compdb), compdb_path))
len(compdb), basename(outstream)))
return compdb
except Exception as e:
if verbose:
print("## Failed to read previous {}: {}".format(compdb_path, e))
print("## Failed to read previous {}: {}".format(basename(outstream), e))
return []


Expand Down
2 changes: 1 addition & 1 deletion compiledb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, infile, outfile, build_dir, exclude_files, no_build,
@click.option('-p', '--parse', 'infile', type=click.File('r'),
help='Build log file to parse compilation commands from.' +
'(Default: stdin)', required=False, default=sys.stdin)
@click.option('-o', '--output', 'outfile', type=click.Path(),
@click.option('-o', '--output', 'outfile', type=click.File('a+'),
help="Output file path (Default: compile_commands.json). " +
'If -f/--overwrite is not specified, this file is updated ' +
'with the new contents. Use \'-\' to output to stdout',
Expand Down
2 changes: 1 addition & 1 deletion requirements-devel.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
attrs==18.1.0
bashlex==0.12
click==6.7
click==7.0
enum34==1.1.6
more-itertools==4.1.0
pluggy==0.6.0
Expand Down

0 comments on commit 1c3cccf

Please sign in to comment.