Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
parser = setup_parser()
# parse cmd args
args = parser.parse_args(args)
# run the function associated with the selected command
if args.common_debug:
# So we could see/stop clearly at the point of failure
setup_exceptionhook()
args.func(args)
else:
# Otherwise - guard and only log the summary. Postmortem is not
# as convenient if being caught in this ultimate except
try:
args.func(args)
except Exception as exc:
lgr.error('%s (%s)' % (str(exc), exc.__class__.__name__))
sys.exit(1)
try:
with open(progname) as fp:
code = compile(fp.read(), progname, 'exec')
# try to emulate __main__ namespace as much as possible
globs = {
'__file__': progname,
'__name__': '__main__',
'__package__': None,
'__cached__': None,
}
# Since used explicitly -- activate the beast
due.activate(True)
runctx(code, globs, globs)
# TODO: see if we could hide our presence from the final tracebacks if execution fails
except IOError as err:
lgr.error("Cannot run file %r because: %s" % (sys.argv[0], err))
sys.exit(1)
except SystemExit:
pass
# Clean up current bindings first
for k in filter(is_public_or_special, dir(self)):
delattr(self, k)
new_due = self.__collectors[activate]
for k in filter(is_public_or_special, dir(new_due)):
setattr(self, k, getattr(new_due, k))
self.__active = activate
# 2nd -- if activating, we might still need to have activations done
if activate and not self.__activations_done:
try:
self.__prepare_exit_and_injections()
except Exception as e:
lgr.error("Failed to prepare injections etc: %s" % str(e))
finally:
self.__activations_done = True
def deactivate(self):
if not self._orig_import:
lgr.warning("_orig_import is not yet known, so we haven't decorated default importer yet."
" Nothing TODO")
return
if not self._active: # pragma: no cover
lgr.error("Must have not happened, but we will survive!")
lgr.debug("Assigning original importer")
__builtin__.__import__ = self._orig_import
self._orig_import = None
self._active = False
bib_source = cpBibTeX(fname)
except decode_exceptions as e:
# So .bib must be having UTF-8 characters. With
# a recent (not yet released past v0.3.0-68-g9800dad
# we should be able to provide encoding argument
bib_source = cpBibTeX(fname, encoding='utf-8')
except Exception as e:
msg = "Failed to process BibTeX file %s: %s." % (fname, e)
citeproc_version = external_versions['citeproc']
if 'unexpected keyword argument' in str(e) and \
citeproc_version and citeproc_version < '0.4':
err = "need a newer citeproc-py >= 0.4.0"
msg += " You might just " + err
else:
err = str(e)
lgr.error(msg)
return "ERRORED: %s" % err
finally:
# return warnings back
warnings.filters = old_filters
bib_style = cp.CitationStylesStyle(style, validate=False)
# TODO: specify which tags of formatter we want
bibliography = cp.CitationStylesBibliography(bib_style, bib_source,
cp.formatter.plain)
citation = cp.Citation([cp.CitationItem(key)])
bibliography.register(citation)
finally:
if not os.environ.get("DUECREDIT_KEEPTEMP"):
exceptions = (OSError, WindowsError) if on_windows else OSError
for i in range(50):
try:
os.unlink(fname)
def _mitigate_None_orig_import(self, name, *args, **kwargs):
lgr.error("For some reason self._orig_import is None"
". Importing using stock importer to mitigate and adjusting _orig_import")
self._orig_import = _very_orig_import
return _very_orig_import(name, *args, **kwargs)