Skip to content

Commit

Permalink
Merge pull request #156 from kellyjonbrazil/dev
Browse files Browse the repository at this point in the history
Dev v1.16.1
  • Loading branch information
kellyjonbrazil authored Aug 16, 2021
2 parents 2689697 + 473f706 commit 1ce5336
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
jc changelog

20210813 v1.16.1
- Note to Package Maintainers: please see note at 20210720 v1.16.0
- Fix issue with process substitution with the magic syntax
- Fix issue with globs not including filenames with spaces with magic syntax
- Fix stat parser to properly handle filenames with spaces on macOS/BSD

20210720 v1.16.0
- Note to Package Maintainers:
TL;DR: `/man/jc.1.gz` and `/jc/man/jc.1.gz` are deprecated and only `/man/jc.1` should be used.
Expand Down
2 changes: 1 addition & 1 deletion docs/parsers/stat.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,4 @@ Returns:
## Parser Information
Compatibility: linux, darwin, freebsd

Version 1.8 by Kelly Brazil ([email protected])
Version 1.9 by Kelly Brazil ([email protected])
2 changes: 1 addition & 1 deletion jc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@
"""

name = 'jc'
__version__ = '1.16.0'
__version__ = '1.16.1'
32 changes: 22 additions & 10 deletions jc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import os
import os.path
import re
import shlex
import importlib
import textwrap
import signal
import shlex
import subprocess
import json
import jc
Expand Down Expand Up @@ -360,11 +360,10 @@ def help_doc(options):
# load parser module just in time so we don't need to load all modules
parser = parser_module(arg)
compatible = ', '.join(parser.info.compatible)
doc_text = f'''{parser.__doc__}
Compatibility: {compatible}
Version {parser.info.version} by {parser.info.author} ({parser.info.author_email})
'''
doc_text = \
f'{parser.__doc__}\n'\
f'Compatibility: {compatible}\n\n'\
f'Version {parser.info.version} by {parser.info.author} ({parser.info.author_email})\n'

return doc_text

Expand Down Expand Up @@ -415,8 +414,7 @@ def magic_parser(args):
if len(args) <= 1 or args[1].startswith('--'):
return False, None, None, []

# correctly parse escape characters and spaces with shlex
args_given = ' '.join(map(shlex.quote, args[1:])).split()
args_given = args[1:]
options = []

# find the options
Expand Down Expand Up @@ -466,7 +464,11 @@ def magic_parser(args):

def run_user_command(command):
"""Use subprocess to run the user's command. Returns the STDOUT, STDERR, and the Exit Code as a tuple."""
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
proc = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=False, # Allows inheriting file descriptors. Useful for process substitution
universal_newlines=True)
stdout, stderr = proc.communicate()

return (
Expand Down Expand Up @@ -545,7 +547,10 @@ def main():
# if magic syntax used, try to run the command and error if it's not found, etc.
magic_stdout, magic_stderr, magic_exit_code = None, None, 0
if run_command:
run_command_str = ' '.join(run_command)
try:
run_command_str = shlex.join(run_command) # python 3.8+
except AttributeError:
run_command_str = ' '.join(run_command) # older python versions

if valid_command:
try:
Expand All @@ -560,6 +565,13 @@ def main():
jc.utils.error_message(f'"{run_command_str}" command could not be found. For details use the -d or -dd option.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))

except OSError:
if debug:
raise
else:
jc.utils.error_message(f'"{run_command_str}" command could not be run due to too many open files. For details use the -d or -dd option.')
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))

except Exception:
if debug:
raise
Expand Down
Binary file modified jc/man/jc.1.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions jc/parsers/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@

class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.8'
version = '1.9'
description = '`stat` command parser'
author = 'Kelly Brazil'
author_email = '[email protected]'
Expand Down Expand Up @@ -322,7 +322,7 @@ def parse(data, raw=False, quiet=False):
for line in cleandata:
value = shlex.split(line)
output_line = {
'file': value[15],
'file': ' '.join(value[15:]),
'unix_device': value[0],
'inode': value[1],
'flags': value[2],
Expand Down
4 changes: 4 additions & 0 deletions man/jc.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<<<<<<< HEAD
.TH jc 1 2021-08-13 1.16.1 "JSON CLI output utility"
=======
.TH jc 1 2021-07-22 1.16.0 "JSON CLI output utility"
>>>>>>> master
.SH NAME
jc \- JSONifies the output of many CLI tools and file-types
.SH SYNOPSIS
Expand Down
Binary file modified man/jc.1.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='jc',
version='1.16.0',
version='1.16.1',
author='Kelly Brazil',
author_email='[email protected]',
description='Converts the output of popular command-line tools and file-types to JSON.',
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/osx-10.14.6/stat-filename-with-spaces.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"file":"file name with spaces.txt","unix_device":16777220,"inode":161929661,"flags":"-rw-r--r--","links":1,"user":"kbrazil","group":"staff","rdev":0,"size":0,"access_time":"Aug 13 15:03:52 2021","modify_time":"Aug 13 14:37:03 2021","change_time":"Aug 13 14:37:03 2021","birth_time":"Aug 13 14:37:03 2021","block_size":4096,"blocks":0,"unix_flags":"0","access_time_epoch":1628892232,"access_time_epoch_utc":null,"modify_time_epoch":1628890623,"modify_time_epoch_utc":null,"change_time_epoch":1628890623,"change_time_epoch_utc":null,"birth_time_epoch":1628890623,"birth_time_epoch_utc":null}]
2 changes: 2 additions & 0 deletions tests/fixtures/osx-10.14.6/stat-filename-with-spaces.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
16777220 161929661 -rw-r--r-- 1 kbrazil staff 0 0 "Aug 13 15:03:52 2021" "Aug 13 14:37:03 2021" "Aug 13 14:37:03 2021" "Aug 13 14:37:03 2021" 4096 0 0 file name with spaces.txt

12 changes: 12 additions & 0 deletions tests/test_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def setUp(self):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/stat.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_stat = f.read()

with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/stat-filename-with-spaces.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_stat_filename_with_spaces = f.read()

with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/stat.out'), 'r', encoding='utf-8') as f:
self.freebsd12_stat = f.read()

Expand All @@ -39,6 +42,9 @@ def setUp(self):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/stat.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_stat_json = json.loads(f.read())

with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/stat-filename-with-spaces.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_stat_filename_with_spaces_json = json.loads(f.read())

with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/stat.json'), 'r', encoding='utf-8') as f:
self.freebsd12_stat_json = json.loads(f.read())

Expand Down Expand Up @@ -66,6 +72,12 @@ def test_stat_osx_10_14_6(self):
"""
self.assertEqual(jc.parsers.stat.parse(self.osx_10_14_6_stat, quiet=True), self.osx_10_14_6_stat_json)

def test_stat_filename_with_spaces_osx_10_14_6(self):
"""
Test 'stat' filename with spaces on OSX 10.14.6
"""
self.assertEqual(jc.parsers.stat.parse(self.osx_10_14_6_stat_filename_with_spaces, quiet=True), self.osx_10_14_6_stat_filename_with_spaces_json)

def test_stat_freebsd12(self):
"""
Test 'stat /foo/*' on FreeBSD12
Expand Down

0 comments on commit 1ce5336

Please sign in to comment.