def add_user_env(env, dotfile, print=print): df = os.path.expanduser(dotfile) try: with open(df, 'r') as f: dotvars = read_env_file(f.read()) except IOError: if os.path.exists(df): warn("Dotfile %s exists but can't be read." % dotfile, print) dotvars = {} bad_names = [] for name, value in dotvars.items(): if name in env: if is_string(env[name]): env[name] = value else: env[name] = shlex.split(value) else: bad_names.append(name) if bad_names: error = _BAD_VARS_ERROR % (dotfile, '\n '.join(bad_names)) warn(error, print) for name, default in env.items(): env[name] = os.environ.get(name, default)
def get_tags(arguments, print=print): result = {} bad_tags = set(arguments) - _TAGS if bad_tags: warn("don't understand tags " + ' '.join(bad_tags), print=print) debug = result.get('debug', True) optimize = result.get('optimize', not debug) return _to_tag('debug', debug), _to_tag('optimize', optimize)
def remove_quotes(line, quote='"', print=print): if not line.startswith(quote): return line if line.endswith(quote): return line[1:-1] warn('line started with %s but didn\'t end with one:' % quote, print) print(line) return line[1:]
def read_env_file(data, print=print): try: return json.loads(data) except ValueError: pass bad_lines = [] results = {} for number, raw_line in enumerate(data.splitlines()): line = String.remove_comment(raw_line).strip() if line: match = ENV_LINE_MATCH.match(line) if match: name, value = match.groups() results[name.strip()] = String.remove_quotes(value.strip()) else: bad_lines.append([number, raw_line]) if bad_lines: warn("Didn't understand the following environment file lines:", print) for number, line in bad_lines: print("%d. >>> %s" % (number + 1, line)) return results
def read_env_file(data, print=print): try: return json.loads(data) except ValueError: pass bad_lines = [] results = {} for number, raw_line in enumerate(data.splitlines()): line = String.remove_comment(raw_line).strip() if line: match = ENV_LINE_MATCH.match(line) if match: name, value = match.groups() results[name.strip()] = String.remove_quotes(value.strip()) else: bad_lines.append([number, raw_line]) if bad_lines: warn("Didn't understand the following environment file lines:", print) for number, line in bad_lines: print('%d. >>> %s' % (number + 1, line)) return results