From fe70b5d608ebb438cf2ec56ae030c9ece9d483cf Mon Sep 17 00:00:00 2001 From: ng0 Date: Sun, 27 Oct 2019 15:00:27 +0000 Subject: [PATCH] initial, not yet working gnunet_pytools. --- contrib/scripts/.gitignore | 1 + contrib/scripts/Makefile.am | 4 +- contrib/scripts/gnunet_pytools.py.in | 78 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 contrib/scripts/gnunet_pytools.py.in diff --git a/contrib/scripts/.gitignore b/contrib/scripts/.gitignore index 547c89185..b1ddb9699 100644 --- a/contrib/scripts/.gitignore +++ b/contrib/scripts/.gitignore @@ -1,2 +1,3 @@ gnunet-chk.py removetrailingwhitespace.py +gnunet_pytools.py diff --git a/contrib/scripts/Makefile.am b/contrib/scripts/Makefile.am index 5dc94439b..525031761 100644 --- a/contrib/scripts/Makefile.am +++ b/contrib/scripts/Makefile.am @@ -9,7 +9,8 @@ noinst_SCRIPTS = \ removetrailingwhitespace.py \ gnunet_pyexpect.py \ gnunet_janitor.py \ - gnunet-chk.py + gnunet-chk.py \ + gnunet_pytools.py bin_SCRIPTS = \ gnunet-bugreport @@ -20,6 +21,7 @@ EXTRA_DIST = \ gnunet_pyexpect.py.in \ gnunet_janitor.py.in \ gnunet-chk.py.in \ + gnunet_pytools.py.in \ $(SCRIPTS) \ removetrailingwhitespace.py.in \ pydiffer.py.in diff --git a/contrib/scripts/gnunet_pytools.py.in b/contrib/scripts/gnunet_pytools.py.in new file mode 100644 index 000000000..cc02d1330 --- /dev/null +++ b/contrib/scripts/gnunet_pytools.py.in @@ -0,0 +1,78 @@ +#!@PYTHON@ +# This file is part of GNUnet +# (C) 2019 GNUnet e.V. +# +# Authors: +# Author: ng0 +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE +# LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES +# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF +# THIS SOFTWARE. +# +# SPDX-License-Identifier: 0BSD + +import os +from distutils.spawn import find_executable + + +def existence(name): + return find_executable(name) is not None + + +# GNUNET_TMP which we suggest to use in place of the absolute definition of +# /tmp. So instead of /tmp/foo you would write $GNUNET_TMP/foo. The usage +# of $GNUNET_TMP/foo, will result in $TMPDIR/gnunet/foo, or $TMP/gnunet/foo +# and finally, if TMPDIR is undefined, /tmp/gnunet/foo. +# 'TEMP' is not (yet) covered by GNUNET_TMP. +# TODO: merge these two functions? +def gnunet_tmp_path(): + if os.environ.get('GNUNET_TMP'): + return os.environ.get('GNUNET_TMP') + elif os.environ.get('TMPDIR'): + return os.environ.get('TMPDIR') + '/gnunet' + elif os.environ.get('TMP'): + return os.environ.get('TMP') + '/gnunet' + elif os.environ.get('TEMP'): + return os.environ.get('TEMP') + '/gnunet' + else: + return '/tmp/gnunet' + + +def tmp_path(): + if os.environ.get('TMPDIR'): + return os.environ.get('TMPDIR') + elif os.environ.get('TMP'): + return os.environ.get('TMP') + elif os.environ.get('TEMP'): + return os.environ.get('TEMP') + else: + return '/tmp' + + +def get_path(name): + dpath = { + 'exec_prefix': '@exec_prefix@', + 'libexecdir': '@libexecdir@', + 'gnunet_prefix': os.environ.get('GNUNET_PREFIX', None), + 'tmp': tmp_path(), + 'gnunet_tmp': gnunet_tmp_path() + } + try: + return dpath[name] + except KeyError: + return [] + + +def suffix_executable(name): + if os.name == 'posix': + return string(name) + if os.name == 'nt': + return string(name + '.exe') -- 2.25.1