Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
script_path = sys.argv[1]
# Find the module and function names that correspond to this
# assuming it is actually a console script.
name = os.path.basename(script_path)
entry_points = [ep for ep in importlib_entry_points()["console_scripts"] if ep.name == name]
if not entry_points:
raise ArgcompleteMarkerNotFound('no entry point found matching script')
entry_point = entry_points[0]
module_name, function_name = entry_point.value.split(":", 1)
# Check this looks like the script we really expected.
with open(script_path) as f:
script = f.read()
if 'from {} import {}'.format(module_name, function_name) not in script:
raise ArgcompleteMarkerNotFound('does not appear to be a console script')
if 'sys.exit({}())'.format(function_name) not in script:
raise ArgcompleteMarkerNotFound('does not appear to be a console script')
# Look for the argcomplete marker in the script it imports.
with open(find(module_name, return_package=True)) as f:
head = f.read(1024)
if 'PYTHON_ARGCOMPLETE_OK' not in head:
raise ArgcompleteMarkerNotFound('marker not found')
def main():
# Argument is the full path to the console script.
script_path = sys.argv[1]
# Find the module and function names that correspond to this
# assuming it is actually a console script.
name = os.path.basename(script_path)
entry_points = [ep for ep in importlib_entry_points()["console_scripts"] if ep.name == name]
if not entry_points:
raise ArgcompleteMarkerNotFound('no entry point found matching script')
entry_point = entry_points[0]
module_name, function_name = entry_point.value.split(":", 1)
# Check this looks like the script we really expected.
with open(script_path) as f:
script = f.read()
if 'from {} import {}'.format(module_name, function_name) not in script:
raise ArgcompleteMarkerNotFound('does not appear to be a console script')
if 'sys.exit({}())'.format(function_name) not in script:
raise ArgcompleteMarkerNotFound('does not appear to be a console script')
# Look for the argcomplete marker in the script it imports.
with open(find(module_name, return_package=True)) as f:
head = f.read(1024)
if 'PYTHON_ARGCOMPLETE_OK' not in head:
raise ArgcompleteMarkerNotFound('marker not found')
entry_point = entry_points[0]
module_name, function_name = entry_point.value.split(":", 1)
# Check this looks like the script we really expected.
with open(script_path) as f:
script = f.read()
if 'from {} import {}'.format(module_name, function_name) not in script:
raise ArgcompleteMarkerNotFound('does not appear to be a console script')
if 'sys.exit({}())'.format(function_name) not in script:
raise ArgcompleteMarkerNotFound('does not appear to be a console script')
# Look for the argcomplete marker in the script it imports.
with open(find(module_name, return_package=True)) as f:
head = f.read(1024)
if 'PYTHON_ARGCOMPLETE_OK' not in head:
raise ArgcompleteMarkerNotFound('marker not found')
if 'from {} import {}'.format(module_name, function_name) not in script:
raise ArgcompleteMarkerNotFound('does not appear to be a console script')
if 'sys.exit({}())'.format(function_name) not in script:
raise ArgcompleteMarkerNotFound('does not appear to be a console script')
# Look for the argcomplete marker in the script it imports.
with open(find(module_name, return_package=True)) as f:
head = f.read(1024)
if 'PYTHON_ARGCOMPLETE_OK' not in head:
raise ArgcompleteMarkerNotFound('marker not found')
if __name__ == '__main__':
try:
main()
except ArgcompleteMarkerNotFound as e:
sys.exit(e)
def find(name, return_package=False):
names = name.split('.')
spec = find_spec(names[0])
if spec is None:
raise ArgcompleteMarkerNotFound(
'no module named "{}"'.format(names[0]))
if not spec.has_location:
raise ArgcompleteMarkerNotFound('cannot locate file')
if spec.submodule_search_locations is None:
if len(names) != 1:
raise ArgcompleteMarkerNotFound(
'{} is not a package'.format(names[0]))
return spec.origin
if len(spec.submodule_search_locations) != 1:
raise ArgcompleteMarkerNotFound('expecting one search location')
path = os.path.join(spec.submodule_search_locations[0], *names[1:])
if os.path.isdir(path):
filename = '__main__.py'
if return_package:
filename = '__init__.py'
return os.path.join(path, filename)
else:
return path + '.py'
def find(name, return_package=False):
names = name.split('.')
spec = find_spec(names[0])
if spec is None:
raise ArgcompleteMarkerNotFound(
'no module named "{}"'.format(names[0]))
if not spec.has_location:
raise ArgcompleteMarkerNotFound('cannot locate file')
if spec.submodule_search_locations is None:
if len(names) != 1:
raise ArgcompleteMarkerNotFound(
'{} is not a package'.format(names[0]))
return spec.origin
if len(spec.submodule_search_locations) != 1:
raise ArgcompleteMarkerNotFound('expecting one search location')
path = os.path.join(spec.submodule_search_locations[0], *names[1:])
if os.path.isdir(path):
filename = '__main__.py'
if return_package:
filename = '__init__.py'
return os.path.join(path, filename)
def find(name, return_package=False):
names = name.split('.')
spec = find_spec(names[0])
if spec is None:
raise ArgcompleteMarkerNotFound(
'no module named "{}"'.format(names[0]))
if not spec.has_location:
raise ArgcompleteMarkerNotFound('cannot locate file')
if spec.submodule_search_locations is None:
if len(names) != 1:
raise ArgcompleteMarkerNotFound(
'{} is not a package'.format(names[0]))
return spec.origin
if len(spec.submodule_search_locations) != 1:
raise ArgcompleteMarkerNotFound('expecting one search location')
path = os.path.join(spec.submodule_search_locations[0], *names[1:])
if os.path.isdir(path):
filename = '__main__.py'
if return_package:
filename = '__init__.py'
return os.path.join(path, filename)
else:
return path + '.py'
filename = find(name)
if hasattr(tokenize, 'open'):
open_func = tokenize.open
else:
open_func = open
try:
fp = open_func(filename)
except OSError:
raise ArgcompleteMarkerNotFound('cannot open file')
with fp:
head = fp.read(1024)
if 'PYTHON_ARGCOMPLETE_OK' not in head:
raise ArgcompleteMarkerNotFound('marker not found')