2013-08-06 13:38:00 +00:00
|
|
|
"""
|
|
|
|
mbed SDK
|
|
|
|
Copyright (c) 2011-2013 ARM Limited
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
"""
|
2013-02-18 15:32:11 +00:00
|
|
|
import sys
|
2015-05-07 14:06:57 +00:00
|
|
|
import inspect
|
|
|
|
import os
|
2016-06-24 22:15:01 +00:00
|
|
|
import argparse
|
|
|
|
import math
|
2013-02-18 15:32:11 +00:00
|
|
|
from os import listdir, remove, makedirs
|
|
|
|
from shutil import copyfile
|
2016-07-27 16:30:47 +00:00
|
|
|
from os.path import isdir, join, exists, split, relpath, splitext, abspath
|
|
|
|
from os.path import commonprefix, normpath
|
2014-10-08 14:15:11 +00:00
|
|
|
from subprocess import Popen, PIPE, STDOUT, call
|
2016-06-09 21:05:35 +00:00
|
|
|
import json
|
|
|
|
from collections import OrderedDict
|
2016-07-19 10:14:42 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
def compile_worker(job):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""Standard task runner used for compiling
|
|
|
|
|
|
|
|
Positional argumets:
|
|
|
|
job - a dict containing a list of commands and the remaining arguments
|
|
|
|
to run_cmd
|
|
|
|
"""
|
2016-07-19 10:14:42 +00:00
|
|
|
results = []
|
|
|
|
for command in job['commands']:
|
|
|
|
try:
|
2016-07-27 16:30:47 +00:00
|
|
|
_, _stderr, _rc = run_cmd(command, work_dir=job['work_dir'],
|
|
|
|
chroot=job['chroot'])
|
|
|
|
except KeyboardInterrupt:
|
2016-07-19 10:14:42 +00:00
|
|
|
raise ToolException
|
|
|
|
|
|
|
|
results.append({
|
|
|
|
'code': _rc,
|
|
|
|
'output': _stderr,
|
|
|
|
'command': command
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
'source': job['source'],
|
|
|
|
'object': job['object'],
|
|
|
|
'commands': job['commands'],
|
|
|
|
'results': results
|
|
|
|
}
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
def cmd(command, check=True, verbose=False, shell=False, cwd=None):
|
|
|
|
"""A wrapper to run a command as a blocking job"""
|
|
|
|
text = command if shell else ' '.join(command)
|
2014-06-11 13:47:54 +00:00
|
|
|
if verbose:
|
|
|
|
print text
|
2016-07-27 16:30:47 +00:00
|
|
|
return_code = call(command, shell=shell, cwd=cwd)
|
|
|
|
if check and return_code != 0:
|
|
|
|
raise Exception('ERROR %d: "%s"' % (return_code, text))
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
|
2016-07-19 10:14:42 +00:00
|
|
|
def run_cmd(command, work_dir=None, chroot=None, redirect=False):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""Run a command in the forground
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
command - the command to run
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
work_dir - the working directory to run the command in
|
|
|
|
chroot - the chroot to run the command in
|
|
|
|
redirect - redirect the stderr to a pipe to be used later
|
|
|
|
"""
|
2016-07-19 10:14:42 +00:00
|
|
|
if chroot:
|
|
|
|
# Conventions managed by the web team for the mbed.org build system
|
|
|
|
chroot_cmd = [
|
|
|
|
'/usr/sbin/chroot', '--userspec=33:33', chroot
|
|
|
|
]
|
2016-07-27 16:30:47 +00:00
|
|
|
for element in command:
|
|
|
|
chroot_cmd += [element.replace(chroot, '')]
|
2016-07-19 10:14:42 +00:00
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
logging.debug("Running command %s", ' '.join(chroot_cmd))
|
2016-07-19 10:14:42 +00:00
|
|
|
command = chroot_cmd
|
|
|
|
work_dir = None
|
|
|
|
|
2016-06-09 21:05:35 +00:00
|
|
|
try:
|
2016-07-27 16:30:47 +00:00
|
|
|
process = Popen(command, stdout=PIPE,
|
|
|
|
stderr=STDOUT if redirect else PIPE, cwd=work_dir)
|
|
|
|
_stdout, _stderr = process.communicate()
|
|
|
|
except OSError:
|
2016-06-09 21:05:35 +00:00
|
|
|
print "[OS ERROR] Command: "+(' '.join(command))
|
|
|
|
raise
|
2016-06-15 00:08:35 +00:00
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
return _stdout, _stderr, process.returncode
|
2014-06-11 14:51:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_cmd_ext(command):
|
2016-07-27 16:30:47 +00:00
|
|
|
""" A version of run command that checks if the command exists befor running
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
command - the command line you are trying to invoke
|
|
|
|
"""
|
2015-05-07 14:06:57 +00:00
|
|
|
assert is_cmd_valid(command[0])
|
2016-07-27 16:30:47 +00:00
|
|
|
process = Popen(command, stdout=PIPE, stderr=PIPE)
|
|
|
|
_stdout, _stderr = process.communicate()
|
|
|
|
return _stdout, _stderr, process.returncode
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
def is_cmd_valid(command):
|
|
|
|
""" Verify that a command exists and is executable
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
Positional arguments:
|
|
|
|
command - the command to check
|
|
|
|
"""
|
2015-05-07 14:06:57 +00:00
|
|
|
caller = get_caller_name()
|
2016-07-27 16:30:47 +00:00
|
|
|
cmd_path = find_cmd_abspath(command)
|
|
|
|
if not cmd_path:
|
|
|
|
error("%s: Command '%s' can't be found" % (caller, command))
|
|
|
|
if not is_exec(cmd_path):
|
|
|
|
error("%s: Command '%s' resolves to file '%s' which is not executable"
|
|
|
|
% (caller, command, cmd_path))
|
2015-05-07 14:06:57 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def is_exec(path):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""A simple check to verify that a path to an executable exists
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
path - the executable
|
|
|
|
"""
|
2015-05-16 10:18:30 +00:00
|
|
|
return os.access(path, os.X_OK) or os.access(path+'.exe', os.X_OK)
|
2015-05-07 14:06:57 +00:00
|
|
|
|
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
def find_cmd_abspath(command):
|
2015-05-07 14:06:57 +00:00
|
|
|
""" Returns the absolute path to a command.
|
|
|
|
None is returned if no absolute path was found.
|
2016-07-27 16:30:47 +00:00
|
|
|
|
|
|
|
Positional arguhments:
|
|
|
|
command - the command to find the path of
|
2015-05-07 14:06:57 +00:00
|
|
|
"""
|
2016-07-27 16:30:47 +00:00
|
|
|
if exists(command) or exists(command + '.exe'):
|
|
|
|
return os.path.abspath(command)
|
2015-05-07 14:06:57 +00:00
|
|
|
if not 'PATH' in os.environ:
|
2016-07-27 16:30:47 +00:00
|
|
|
raise Exception("Can't find command path for current platform ('%s')"
|
|
|
|
% sys.platform)
|
|
|
|
path_env = os.environ['PATH']
|
|
|
|
for path in path_env.split(os.pathsep):
|
|
|
|
cmd_path = '%s/%s' % (path, command)
|
|
|
|
if exists(cmd_path) or exists(cmd_path + '.exe'):
|
|
|
|
return cmd_path
|
2015-05-07 14:06:57 +00:00
|
|
|
|
|
|
|
|
2013-06-10 14:44:08 +00:00
|
|
|
def mkdir(path):
|
2016-07-27 16:30:47 +00:00
|
|
|
""" a wrapped makedirs that only tries to create a directory if it does not
|
|
|
|
exist already
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
path - the path to maybe create
|
|
|
|
"""
|
2013-06-10 14:44:08 +00:00
|
|
|
if not exists(path):
|
|
|
|
makedirs(path)
|
|
|
|
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
def copy_file(src, dst):
|
2014-09-25 10:03:37 +00:00
|
|
|
""" Implement the behaviour of "shutil.copy(src, dst)" without copying the
|
2016-07-27 16:30:47 +00:00
|
|
|
permissions (this was causing errors with directories mounted with samba)
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
src - the source of the copy operation
|
|
|
|
dst - the destination of the copy operation
|
2013-02-18 15:32:11 +00:00
|
|
|
"""
|
|
|
|
if isdir(dst):
|
2016-07-27 16:30:47 +00:00
|
|
|
_, base = split(src)
|
|
|
|
dst = join(dst, base)
|
2013-02-18 15:32:11 +00:00
|
|
|
copyfile(src, dst)
|
|
|
|
|
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
def delete_dir_files(directory):
|
|
|
|
""" A function that does rm -rf
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
directory - the directory to remove
|
|
|
|
"""
|
|
|
|
if not exists(directory):
|
2013-02-18 15:32:11 +00:00
|
|
|
return
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
for element in listdir(directory):
|
|
|
|
to_remove = join(directory, element)
|
|
|
|
if not isdir(to_remove):
|
2013-02-18 15:32:11 +00:00
|
|
|
remove(file)
|
|
|
|
|
|
|
|
|
2015-05-07 14:06:57 +00:00
|
|
|
def get_caller_name(steps=2):
|
|
|
|
"""
|
|
|
|
When called inside a function, it returns the name
|
|
|
|
of the caller of that function.
|
2016-07-27 16:30:47 +00:00
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
steps - the number of steps up the stack the calling function is
|
2015-05-07 14:06:57 +00:00
|
|
|
"""
|
|
|
|
return inspect.stack()[steps][3]
|
|
|
|
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
def error(msg):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""Fatal error, abort hard
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
msg - the message to print before crashing
|
|
|
|
"""
|
2015-05-07 14:36:48 +00:00
|
|
|
print("ERROR: %s" % msg)
|
2013-02-18 15:32:11 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def rel_path(path, base, dot=False):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""Relative path calculation that optionaly always starts with a dot
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
path - the path to make relative
|
|
|
|
base - what to make the path relative to
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
dot - if True, the path will always start with a './'
|
|
|
|
"""
|
|
|
|
final_path = relpath(path, base)
|
|
|
|
if dot and not final_path.startswith('.'):
|
|
|
|
final_path = './' + final_path
|
|
|
|
return final_path
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ToolException(Exception):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""A class representing an exception throw by the tools"""
|
2013-02-18 15:32:11 +00:00
|
|
|
pass
|
|
|
|
|
2016-02-25 22:29:26 +00:00
|
|
|
class NotSupportedException(Exception):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""A class a toolchain not supporting a particular target"""
|
2016-02-25 22:29:26 +00:00
|
|
|
pass
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2016-07-26 15:22:02 +00:00
|
|
|
class InvalidReleaseTargetException(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
def split_path(path):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""spilt a file name into it's directory name, base name, and extension
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
path - the file name to split
|
|
|
|
"""
|
|
|
|
base, has_ext = split(path)
|
|
|
|
name, ext = splitext(has_ext)
|
2013-02-18 15:32:11 +00:00
|
|
|
return base, name, ext
|
2013-08-16 15:39:30 +00:00
|
|
|
|
|
|
|
|
2016-07-25 18:15:34 +00:00
|
|
|
def get_path_depth(path):
|
|
|
|
""" Given a path, return the number of directory levels present.
|
|
|
|
This roughly translates to the number of path separators (os.sep) + 1.
|
|
|
|
Ex. Given "path/to/dir", this would return 3
|
|
|
|
Special cases: "." and "/" return 0
|
2016-07-27 16:30:47 +00:00
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
path - the path to calculate the depth of
|
2016-07-25 18:15:34 +00:00
|
|
|
"""
|
|
|
|
normalized_path = normpath(path)
|
|
|
|
path_depth = 0
|
|
|
|
head, tail = split(normalized_path)
|
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
while tail and tail != '.':
|
2016-07-25 18:15:34 +00:00
|
|
|
path_depth += 1
|
|
|
|
head, tail = split(head)
|
|
|
|
|
|
|
|
return path_depth
|
|
|
|
|
|
|
|
|
2013-08-16 15:39:30 +00:00
|
|
|
def args_error(parser, message):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""Abort with an error that was generated by the arguments to a CLI program
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
parser - the ArgumentParser object that parsed the command line
|
|
|
|
message - what went wrong
|
|
|
|
"""
|
2016-08-04 19:48:18 +00:00
|
|
|
parser.error(message)
|
|
|
|
sys.exit(2)
|
2014-07-30 10:03:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def construct_enum(**enums):
|
2016-07-27 16:30:47 +00:00
|
|
|
""" Create your own pseudo-enums
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
* - a member of the Enum you are creating and it's value
|
|
|
|
"""
|
2014-07-30 10:03:32 +00:00
|
|
|
return type('Enum', (), enums)
|
2014-10-08 14:15:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_required_modules(required_modules, verbose=True):
|
2016-07-27 16:30:47 +00:00
|
|
|
""" Function checks for Python modules which should be "importable"
|
2014-10-08 14:15:11 +00:00
|
|
|
before test suite can be used.
|
|
|
|
@return returns True if all modules are installed already
|
|
|
|
"""
|
|
|
|
import imp
|
|
|
|
not_installed_modules = []
|
|
|
|
for module_name in required_modules:
|
|
|
|
try:
|
|
|
|
imp.find_module(module_name)
|
2016-07-27 16:30:47 +00:00
|
|
|
except ImportError:
|
2015-05-06 15:53:17 +00:00
|
|
|
# We also test against a rare case: module is an egg file
|
|
|
|
try:
|
|
|
|
__import__(module_name)
|
2016-07-27 16:30:47 +00:00
|
|
|
except ImportError as exc:
|
2015-05-06 15:53:17 +00:00
|
|
|
not_installed_modules.append(module_name)
|
|
|
|
if verbose:
|
2016-07-27 16:30:47 +00:00
|
|
|
print "Error: %s" % exc
|
2015-05-06 15:53:17 +00:00
|
|
|
|
2014-10-08 14:15:11 +00:00
|
|
|
if verbose:
|
2015-05-06 15:53:17 +00:00
|
|
|
if not_installed_modules:
|
2016-07-27 16:30:47 +00:00
|
|
|
print ("Warning: Module(s) %s not installed. Please install " + \
|
|
|
|
"required module(s) before using this script.")\
|
|
|
|
% (', '.join(not_installed_modules))
|
2015-05-06 15:53:17 +00:00
|
|
|
|
|
|
|
if not_installed_modules:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2016-06-09 21:05:35 +00:00
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
def dict_to_ascii(dictionary):
|
|
|
|
""" Utility function: traverse a dictionary and change all the strings in
|
|
|
|
the dictionary to ASCII from Unicode. Useful when reading ASCII JSON data,
|
|
|
|
because the JSON decoder always returns Unicode string. Based on
|
|
|
|
http://stackoverflow.com/a/13105359
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
dictionary - The dict that contains some Unicode that should be ASCII
|
|
|
|
"""
|
|
|
|
if isinstance(dictionary, dict):
|
|
|
|
return OrderedDict([(dict_to_ascii(key), dict_to_ascii(value))
|
|
|
|
for key, value in dictionary.iteritems()])
|
|
|
|
elif isinstance(dictionary, list):
|
|
|
|
return [dict_to_ascii(element) for element in dictionary]
|
|
|
|
elif isinstance(dictionary, unicode):
|
|
|
|
return dictionary.encode('ascii')
|
2016-06-09 21:05:35 +00:00
|
|
|
else:
|
2016-07-27 16:30:47 +00:00
|
|
|
return dictionary
|
2016-06-09 21:05:35 +00:00
|
|
|
|
|
|
|
def json_file_to_dict(fname):
|
2016-07-27 16:30:47 +00:00
|
|
|
""" Read a JSON file and return its Python representation, transforming all
|
|
|
|
the strings from Unicode to ASCII. The order of keys in the JSON file is
|
|
|
|
preserved.
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
fname - the name of the file to parse
|
|
|
|
"""
|
2016-07-12 14:14:59 +00:00
|
|
|
try:
|
2016-07-27 16:30:47 +00:00
|
|
|
with open(fname, "r") as file_obj:
|
|
|
|
return dict_to_ascii(json.load(file_obj,
|
|
|
|
object_pairs_hook=OrderedDict))
|
2016-07-12 14:14:59 +00:00
|
|
|
except (ValueError, IOError):
|
|
|
|
sys.stderr.write("Error parsing '%s':\n" % fname)
|
|
|
|
raise
|
2016-06-24 22:15:01 +00:00
|
|
|
|
|
|
|
# Wowza, double closure
|
2016-07-27 16:30:47 +00:00
|
|
|
def argparse_type(casedness, prefer_hyphen=False):
|
|
|
|
def middle(lst, type_name):
|
2016-06-24 22:15:01 +00:00
|
|
|
def parse_type(string):
|
2016-07-27 16:30:47 +00:00
|
|
|
""" validate that an argument passed in (as string) is a member of
|
|
|
|
the list of possible arguments. Offer a suggestion if the case of
|
|
|
|
the string, or the hyphens/underscores do not match the expected
|
|
|
|
style of the argument.
|
|
|
|
"""
|
|
|
|
if prefer_hyphen:
|
|
|
|
newstring = casedness(string).replace("_", "-")
|
|
|
|
else:
|
|
|
|
newstring = casedness(string).replace("-", "_")
|
|
|
|
if string in lst:
|
2016-06-24 22:15:01 +00:00
|
|
|
return string
|
2016-07-27 16:30:47 +00:00
|
|
|
elif string not in lst and newstring in lst:
|
|
|
|
raise argparse.ArgumentTypeError(
|
|
|
|
"{0} is not a supported {1}. Did you mean {2}?".format(
|
|
|
|
string, type_name, newstring))
|
2016-06-24 22:15:01 +00:00
|
|
|
else:
|
2016-07-27 16:30:47 +00:00
|
|
|
raise argparse.ArgumentTypeError(
|
|
|
|
"{0} is not a supported {1}. Supported {1}s are:\n{2}".
|
|
|
|
format(string, type_name, columnate(lst)))
|
2016-06-24 22:15:01 +00:00
|
|
|
return parse_type
|
|
|
|
return middle
|
|
|
|
|
2016-06-29 00:31:06 +00:00
|
|
|
# short cuts for the argparse_type versions
|
2016-06-24 22:15:01 +00:00
|
|
|
argparse_uppercase_type = argparse_type(str.upper, False)
|
|
|
|
argparse_lowercase_type = argparse_type(str.lower, False)
|
|
|
|
argparse_uppercase_hyphen_type = argparse_type(str.upper, True)
|
|
|
|
argparse_lowercase_hyphen_type = argparse_type(str.lower, True)
|
|
|
|
|
2016-06-29 00:46:22 +00:00
|
|
|
def argparse_force_type(case):
|
2016-07-27 16:30:47 +00:00
|
|
|
""" validate that an argument passed in (as string) is a member of the list
|
|
|
|
of possible arguments after converting it's case.
|
|
|
|
"""
|
|
|
|
def middle(lst, type_name):
|
|
|
|
""" The parser type generator"""
|
2016-06-29 00:46:22 +00:00
|
|
|
def parse_type(string):
|
2016-07-27 16:30:47 +00:00
|
|
|
""" The parser type"""
|
|
|
|
for option in lst:
|
2016-06-29 15:48:09 +00:00
|
|
|
if case(string) == case(option):
|
|
|
|
return option
|
2016-07-27 16:30:47 +00:00
|
|
|
raise argparse.ArgumentTypeError(
|
|
|
|
"{0} is not a supported {1}. Supported {1}s are:\n{2}".
|
|
|
|
format(string, type_name, columnate(lst)))
|
2016-06-29 00:46:22 +00:00
|
|
|
return parse_type
|
|
|
|
return middle
|
|
|
|
|
|
|
|
# these two types convert the case of their arguments _before_ validation
|
|
|
|
argparse_force_uppercase_type = argparse_force_type(str.upper)
|
|
|
|
argparse_force_lowercase_type = argparse_force_type(str.lower)
|
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
def argparse_many(func):
|
|
|
|
""" An argument parser combinator that takes in an argument parser and
|
|
|
|
creates a new parser that accepts a comma separated list of the same thing.
|
|
|
|
"""
|
2016-06-24 22:15:01 +00:00
|
|
|
def wrap(string):
|
2016-07-27 16:30:47 +00:00
|
|
|
""" The actual parser"""
|
|
|
|
return [func(s) for s in string.split(",")]
|
2016-06-24 22:15:01 +00:00
|
|
|
return wrap
|
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
def argparse_filestring_type(string):
|
|
|
|
""" An argument parser that verifies that a string passed in corresponds
|
|
|
|
to a file"""
|
|
|
|
if exists(string):
|
2016-06-24 22:15:01 +00:00
|
|
|
return string
|
2016-07-27 16:30:47 +00:00
|
|
|
else:
|
|
|
|
raise argparse.ArgumentTypeError(
|
|
|
|
"{0}"" does not exist in the filesystem.".format(string))
|
|
|
|
|
|
|
|
def columnate(strings, separator=", ", chars=80):
|
|
|
|
""" render a list of strings as a in a bunch of columns
|
2016-06-24 22:15:01 +00:00
|
|
|
|
2016-07-27 16:30:47 +00:00
|
|
|
Positional arguments:
|
|
|
|
strings - the strings to columnate
|
|
|
|
|
|
|
|
Keyword arguments;
|
|
|
|
separator - the separation between the columns
|
|
|
|
chars - the maximum with of a row
|
|
|
|
"""
|
2016-06-24 22:15:01 +00:00
|
|
|
col_width = max(len(s) for s in strings)
|
2016-07-27 16:30:47 +00:00
|
|
|
total_width = col_width + len(separator)
|
2016-06-24 22:15:01 +00:00
|
|
|
columns = math.floor(chars / total_width)
|
|
|
|
output = ""
|
2016-07-27 16:30:47 +00:00
|
|
|
for i, string in zip(range(len(strings)), strings):
|
|
|
|
append = string
|
2016-06-24 22:15:01 +00:00
|
|
|
if i != len(strings) - 1:
|
2016-07-27 16:30:47 +00:00
|
|
|
append += separator
|
2016-06-24 22:15:01 +00:00
|
|
|
if i % columns == columns - 1:
|
|
|
|
append += "\n"
|
|
|
|
else:
|
|
|
|
append = append.ljust(total_width)
|
|
|
|
output += append
|
|
|
|
return output
|
2016-07-11 16:08:11 +00:00
|
|
|
|
|
|
|
def argparse_dir_not_parent(other):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""fail if argument provided is a parent of the specified directory"""
|
2016-07-11 16:08:11 +00:00
|
|
|
def parse_type(not_parent):
|
2016-07-27 16:30:47 +00:00
|
|
|
"""The parser type"""
|
2016-07-11 16:08:11 +00:00
|
|
|
abs_other = abspath(other)
|
|
|
|
abs_not_parent = abspath(not_parent)
|
|
|
|
if abs_not_parent == commonprefix([abs_not_parent, abs_other]):
|
2016-07-27 16:30:47 +00:00
|
|
|
raise argparse.ArgumentTypeError(
|
|
|
|
"{0} may not be a parent directory of {1}".format(
|
|
|
|
not_parent, other))
|
2016-07-11 16:08:11 +00:00
|
|
|
else:
|
|
|
|
return not_parent
|
|
|
|
return parse_type
|