def parser(): """Return the command line argument parser for test_cgal""" parser = CustomArgumentParser( description='''This script launches docker containers which run the CGAL testsuite.''', fromfile_prefix_chars='@') # Testing related arguments parser.add_argument('--images', nargs='*', help='List of images to launch, defaults to all prefixed with cgal-testsuite') parser.add_argument('--testsuite', metavar='/path/to/testsuite', help='Absolute path where the release is going to be stored.', default=path.abspath('./testsuite')) parser.add_argument('--testresults', metavar='/path/to/testresults', help='Absolute path where the testresults are going to be stored.', default=path.abspath('./testresults')) parser.add_argument('--packages', nargs='*', help='List of package base names to run the tests for. Will always include Installation.' 'e.g. AABB_tree will run AABB_tree, AABB_tree_Examples, and AABB_tree_Demo.') # Docker related arguments parser.add_argument('--docker-url', metavar='protocol://hostname/to/docker.sock[:PORT]', default='unix://var/run/docker.sock', help='The protocol+hostname+port where the Docker server is hosted.') parser.add_argument('--force-rm', action='store_true', help='If a container with the same name already exists, force it to quit') parser.add_argument('--max-cpus', metavar='N', default=cpu_count(), type=int, help='The maximum number of CPUs the testsuite is allowed to use at a single time. Defaults to all available cpus.') parser.add_argument('--container-cpus', metavar='N', default=1, type=int, help='The number of CPUs a single container should have. Defaults to one.') parser.add_argument('--jobs', metavar='N', default=None, type=int, help='The number of jobs a single container is going to launch. Defaults to --container-cpus.') parser.add_argument('--use-fedora-selinux-policy', action='store_true', help='Mount volumes with z option to accomodate SELinux on Fedora.') # Download related arguments parser.add_argument('--use-local', action='store_true', help='Use a local extracted CGAL release. --testsuite must be set to that release.') # TODO make internal releases and latest public? parser.add_argument('--user', help='Username for CGAL Members') parser.add_argument('--passwd', help='Password for CGAL Members') # Upload related arguments parser.add_argument('--upload-results', action='store_true', help='Actually upload the test results.') parser.add_argument('--tester', help='The tester', default=getuser()) parser.add_argument('--tester-name', help='The name of the tester', default=gethostname()) parser.add_argument('--tester-address', help='The mail address of the tester') if load_first_config('CGAL'): default_arg_file = path.join(load_first_config('CGAL'), 'test_cgal_rc') else: default_arg_file = path.join(xdg_config_home, 'test_cgal_rc') if path.isfile(default_arg_file): logging.info('Using default arguments from: {}'.format(default_arg_file)) sys.argv.insert(1, '@' + default_arg_file) return parser
def main(): loglevel = logging.WARN if len(sys.argv) == 2 and sys.argv[1] == '-d': loglevel = logging.INFO logging.basicConfig( level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt='%F %T' ) save_config_path('udevedu', 'hooks') hooks_dir = load_first_config('udevedu', 'hooks') hooks = load_hooks(hooks_dir) try: context = pyudev.Context() monitor = pyudev.Monitor.from_netlink(context) while True: try: for args in monitor: # args is (action, device) for h in hooks: spawn_partial(process_hook, h, args) except IOError as e: if e.errno == errno.EINTR: continue logging.warn('Recovering from IOError, continue polling') logging.exception(e) except KeyboardInterrupt: logging.warn('User interrupt, exiting')
def __init__(self, *args, **kwargs): ToolBase.CommandLineInterface.__init__(self, args, kwargs) self.cp = ConfigParser() d = load_first_config('opensuse-release-tools') if d: self.cp.read(os.path.join(d, 'requestfinder.conf'))
def main(): loglevel = logging.WARN if len(sys.argv) == 2 and sys.argv[1] == '-d': loglevel = logging.INFO logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt='%F %T') save_config_path('udevedu', 'hooks') hooks_dir = load_first_config('udevedu', 'hooks') hooks = load_hooks(hooks_dir) try: context = pyudev.Context() monitor = pyudev.Monitor.from_netlink(context) while True: try: for args in monitor: # args is (action, device) for h in hooks: spawn_partial(process_hook, h, args) except IOError as e: if e.errno == errno.EINTR: continue logging.warn('Recovering from IOError, continue polling') logging.exception(e) except KeyboardInterrupt: logging.warn('User interrupt, exiting')
def __init_logging(self): log_cnf = load_first_config(self.xdg_resource,"logging.ini") if log_cnf is not None: from logging.config import fileConfig fileConfig(log_cnf) getLogger("%s_%s" % (__name__, self.__class__.__name__,)).debug("logging init")
def __init__(self): """Load the config""" self._config = {} old_config_file = load_first_config(settings.OLD_CONFIG_FILENAME) config_file = load_first_config(settings.CONFIG_FILENAME) if old_config_file: if not config_file: config_file = old_config_file.replace(settings.OLD_CONFIG_FILENAME, settings.CONFIG_FILENAME) os.rename(old_config_file, config_file) logger.debug("Opening {}".format(config_file)) try: with open(config_file) as f: self._config = yaml.load(f) except (TypeError, FileNotFoundError): logger.info("No configuration file found") except (yaml.scanner.ScannerError, yaml.parser.ParserError) as e: logger.error("Invalid configuration file found: {}".format(e))
def get_auth_info(): config_dir = load_first_config('rogers-usage') with open(os.path.join(config_dir, 'auth.json'), 'rb') as fh: info = json.loads(fh.read()) if info: return AuthInfo(info['username'], info['password']) else: # Maybe throw an exception? return AuthInfo(None, None)
def __init__(self, path, default_filename): '''Initialize config object''' assert(default_filename is not None) self.path = path if path is None: self.path = load_first_config("abs", default_filename) if not isinstance(self.path, str) or not os.path.exists(self.path): logging.debug("No such config file: %s" % self.path) raise MissingConfigFile() self.load()
def conf(self): if self._cnf is None: cnf = load_first_config(self.xdg_resource,"config.ini") if cnf is None: import cpk, sys msg = "missing config.ini in $XDG_CONFIG_HOME\nyou " \ "can find example files in %s/data/configs" % dirname(cpk.__file__) print(msg, file=sys.stderr) exit(1) self._cnf = self.conf_parser(cnf) return self._cnf
def load_base_colour_scheme(): kdeglobals = ConfigParser() kdeglobals.read(load_first_config("kdeglobals")) base_colour_scheme = kdeglobals["General"]["ColorScheme"] for color_schemes_dir in load_data_paths("color-schemes"): for filename in os.listdir(color_schemes_dir): if filename == "{}.colors".format(base_colour_scheme): colour_scheme = ConfigParser() colour_scheme.read(os.path.join(color_schemes_dir, filename)) return colour_scheme raise EnvironmentError(("Could not find colour scheme {}" ).format(base_colour_scheme))
def main(): loglevel = logging.WARN if len(sys.argv) == 2 and sys.argv[1] == "-d": loglevel = logging.INFO logging.basicConfig(level=loglevel, format="%(asctime)s %(levelname)s %(message)s", datefmt="%F %T") save_config_path("udevedu", "hooks") hooks_dir = load_first_config("udevedu", "hooks") hooks = [] # Load all hooks for fname in sorted(glob.glob(os.path.join(hooks_dir, "*.py"))): if not os.path.isfile(fname): continue logging.info("Loading source %s", fname) mod = os.path.splitext(os.path.basename(fname))[0] try: hooks.append(imp.load_source("udevedu.hooks.%s" % mod, fname)) except Exception as e: logging.error("Loading of %s failed", fname) logging.exception(e) for h in hooks: if hasattr(h, "init"): try: h.init() except Exception as e: logging.error("Initialization of %s failed", h.__file__) logging.exception(e) try: context = pyudev.Context() monitor = pyudev.Monitor.from_netlink(context) while True: try: for args in monitor: # args is (action, device) for h in hooks: spawn_partial(process_hook, h, args) except IOError as e: if e.errno == errno.EINTR: continue logging.warn("Recovering from IOError, continue polling") logging.exception(e) except KeyboardInterrupt: logging.warn("User interrupt, exiting")
def load_config(): """Read and return configuration from disk.""" filename = load_first_config('snapcraft', 'snapcraft.cfg') or '' parser = ConfigParser() if os.path.exists(filename): parser.read(filename) api_endpoint = os.environ.get( 'UBUNTU_SSO_API_ROOT_URL', UBUNTU_SSO_API_ROOT_URL) location = urlparse(api_endpoint).netloc config = {} if parser.has_section(location): config.update(dict(parser.items(location))) return config
def load_config(config_file=None): global logger # Nothing specified, try to load the default configuration if config_file is None: config_file = load_first_config(APP_NAME, "default.json") # That fails too if config_file is None: logger.error( f"Couldn't find configuration file. Config file must be at: $XDG_CONFIG_HOME/{APP_NAME}/default.json" ) logger.error("Current value of $XDG_CONFIG_HOME is {}".format( os.getenv("XDG_CONFIG_HOME"))) return None return json.load(open(config_file, "r"))
def get_options(): config_dir = load_first_config('hnss') data_dir = save_data_path('hnss') auth_file_default = os.path.join(config_dir, 'auth.json') data_file_default = os.path.join(data_dir, 'data.json') if os.path.exists('auth.json'): with open('auth.json', 'rb') as fh: auth = json.loads(fh.read()) parser = argparse.ArgumentParser(description=""" Download saved stories from HackerNews and dump the resultant data into a .json file. Subsequent runs using a previous data file will only scrape the newest saved stories. (Note: There is a 30 second delay between requests.) """) parser.add_argument('-u', '--username', default=None, help="HackerNews username") parser.add_argument('-p', '--password', default=None, help="HackerNews password") parser.add_argument('-a', '--auth-file', default=None, help="Auth file (JSON format). (default: %s)" % auth_file_default) parser.add_argument('-f', '--file', help="File to download to. '-' can be used to redirect output to stdout. (default: %s)" % data_file_default, default=data_file_default) parser.add_argument('-m', '--max-pages', type=int, default=1, help="The maximum number of pages to go into the past. 0 goes back all the way to the beginning of time. (default: 1)") parser.add_argument('-d', '--debug', action='store_true', help="Debug mode.") parser.add_argument('-v', '--verbose', action='store_true', help="Verbose output.") options = parser.parse_args() if options.auth_file: with open(options.auth_file, 'rb') as fh: auth_info = json.loads(fh.read()) options.username = options.username or auth_info['username'] options.password = options.password or auth_info['password'] else: if os.path.exists(auth_file_default): with open(auth_file_default, 'rb') as fh: auth_info = json.loads(fh.read()) options.username = options.username or auth_info['username'] options.password = options.password or auth_info['password'] if not options.username: sys.exit("Error: No username given.") if not options.password: sys.exit("Error: No password given.") return options
def daynight_gtk3(dark, preferences): gtk_config_path = join(load_first_config(GTK_VERSION), GTK_SETTINGS_FILE); gtkconfig = ConfigParser(); gtkconfig.read(gtk_config_path); use_gtk_prefer_dark = preferences.getboolean(PREF_KEY_USE_GTK_PREFER_DARK); if use_gtk_prefer_dark: gtkconfig[GTK_SETTINGS][GTK_APP_PREFER_DARK] = str(int(dark)); theme = preferences[PREF_KEY_THEME_DEFAULT]; else: if dark: theme = preferences[PREF_KEY_THEME_DARK]; else: theme = preferences[PREF_KEY_THEME_LIGHT]; gtkconfig[GTK_SETTINGS][GTK_PREF_THEME_NAME] = theme; with open(gtk_config_path, FILE_OPEN_WRITE) as file_open: gtkconfig.write(file_open);
def daynight_gtk3(dark, preferences): gtk_config_path = join(load_first_config(GTK_VERSION), GTK_SETTINGS_FILE) gtkconfig = ConfigParser() gtkconfig.read(gtk_config_path) use_gtk_prefer_dark = preferences.getboolean(PREF_KEY_USE_GTK_PREFER_DARK) if use_gtk_prefer_dark: gtkconfig[GTK_SETTINGS][GTK_APP_PREFER_DARK] = str(int(dark)) theme = preferences[PREF_KEY_THEME_DEFAULT] else: if dark: theme = preferences[PREF_KEY_THEME_DARK] else: theme = preferences[PREF_KEY_THEME_LIGHT] gtkconfig[GTK_SETTINGS][GTK_PREF_THEME_NAME] = theme with open(gtk_config_path, FILE_OPEN_WRITE) as file_open: gtkconfig.write(file_open)
def initialize(cfgfile=None): cfname = 'ciflastic/ciflastic.yml' if cfgfile is None: cfgfile = load_first_config(cfname) if cfgfile is None: cf = pathjoin(xdg_config_dirs[0], cfname) tf = ciflastic.datapath('config/ciflastic.yml.template') wmsg = ("Cannot find configuration file {}.\n" "Falling back to {}.").format(cf, tf) warn(wmsg) return cfg = loadConfig(cfgfile) global CODDIR global PDFSTORAGE CODDIR = abspath(expanduser(cfg['coddir'])) PDFSTORAGE = abspath(expanduser(cfg.get('pdfstorage', ''))) PDFCALCULATOR.clear() PDFCALCULATOR.update(cfg.get('pdfcalculator', {})) return
#! /usr/bin/env python3 from __future__ import print_function from builtins import (bytes, str) import github, sys, os, configparser, argparse, textwrap from github import Github, Label, Issue, PullRequest from xdg.BaseDirectory import load_first_config, xdg_config_home from dulwich.repo import Repo from dulwich.walk import Walker config = configparser.ConfigParser() if load_first_config('CGAL'): config_file = os.path.join(load_first_config('CGAL'), 'list_pull_requests.ini') else: config_file = os.path.join(xdg_config_home, 'list_pull_requests.ini') config.read(config_file) parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description='''\ Print the URLs or CGAL pull-request with a given label.''',\ epilog=textwrap.dedent('''\ The file {} must contain, in a `[main]` section, two keys: - user, - token to define Github credentials. For example: [main] user=lrineau
def config_basedir(resource=XDG_RESOURCE): return load_first_config(resource)
def _get_config(app=None, file=None): return json.load(open(load_first_config('oauth2token', app, file), 'r'))
DAYNIGHT = "daynight"; DAYNIGHT_CONFIG_DIR = "daynight"; DAYNIGHT_CONFIG_FILE = "daynight"; PREF_LIST_SEPARATOR = ","; MODULE_SEPARATOR = "-"; FUNCTION_SEPARATOR = "_"; PREF_KEY_COMPONENTS = "components"; COMPONENT_GTK = "gtk"; COMPONENT_VIM = "vim"; COMPONENT_XFCE4TERM = "xfce4-terminal"; COMPONENT_MUTT = "mutt"; daynight_config_path = load_first_config(DAYNIGHT_CONFIG_DIR); if not daynight_config_path: raise FileNotFoundError("Config folder missing"); config = ConfigParser(); if not config.read(join(daynight_config_path, DAYNIGHT_CONFIG_FILE)): raise FileNotFoundError("Config file missing"); pref_daynight = config[DAYNIGHT]; components = pref_daynight[PREF_KEY_COMPONENTS].split(PREF_LIST_SEPARATOR); if len(components) != len(set(components)): warn("Possible duplicates in config, you probably don't want this."); for component in components: component = component.strip(); # remove spaces after comma "foo, bar"
from configparser import ConfigParser from xdg.BaseDirectory import xdg_config_home, load_first_config from os import system from os import environ from os.path import join, expanduser DEBUG = 0 GTK_VERSION = "gtk-3.0" GTK_SETTINGS = "Settings" GTK_SETTINGS_FILE = "settings.ini" GTK_APPLICATION_PREFER_DARK_THEME = "gtk-application-prefer-dark-theme" FILE_OPEN_WRITE = "w" gtk_config_path = join(load_first_config(GTK_VERSION), GTK_SETTINGS_FILE) gtkconfig = ConfigParser() gtkconfig.read(gtk_config_path) # we receive a string ('0' or '1'), so cast it to int (0 or 1) and then bool prefer_dark_theme = not bool( int(gtkconfig[GTK_SETTINGS][GTK_APPLICATION_PREFER_DARK_THEME])) # other way around, we have bool (True or False), cast it to int (0 or 1) # but ConfigParser wants strings, so cast it to string ('0' or '1') gtkconfig[GTK_SETTINGS] \ [GTK_APPLICATION_PREFER_DARK_THEME] = str(int(prefer_dark_theme)) with open(gtk_config_path, FILE_OPEN_WRITE) as file_open: gtkconfig.write(file_open)
#! /bin/env python3 from __future__ import print_function from builtins import (bytes, str) import github, sys, os, configparser, argparse, textwrap from github import Github, Label, Issue, PullRequest from xdg.BaseDirectory import load_first_config, xdg_config_home from dulwich.repo import Repo from dulwich.walk import Walker config = configparser.ConfigParser() if load_first_config('CGAL'): config_file = os.path.join(load_first_config('CGAL'), 'list_pull_requests.ini') else: config_file = os.path.join(xdg_config_home, 'list_pull_requests.ini') config.read(config_file) parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description='''\ Print the URLs or CGAL pull-request with a given label.''',\ epilog=textwrap.dedent('''\ The file {} must contain, in a `[main]` section, two keys: - user, - token to define Github credentials. For example: [main] user=lrineau
def parser(): """Return the command line argument parser for test_cgal""" parser = CustomArgumentParser( description= '''This script launches docker containers which run the CGAL testsuite.''', fromfile_prefix_chars='@') # Testing related arguments parser.add_argument( '--images', nargs='*', help= 'List of images to launch, defaults to all prefixed with cgal-testsuite' ) parser.add_argument( '--testsuite', metavar='/path/to/testsuite', help='Absolute path where the release is going to be stored.', default=path.abspath('./testsuite')) parser.add_argument( '--testresults', metavar='/path/to/testresults', help='Absolute path where the testresults are going to be stored.', default=path.abspath('./testresults')) parser.add_argument( '--packages', nargs='*', help= 'List of package base names to run the tests for. Will always include Installation.' 'e.g. AABB_tree will run AABB_tree, AABB_tree_Examples, and AABB_tree_Demo.' ) # Docker related arguments parser.add_argument( '--docker-url', metavar='protocol://hostname/to/docker.sock[:PORT]', default='unix://var/run/docker.sock', help='The protocol+hostname+port where the Docker server is hosted.') parser.add_argument( '--force-rm', action='store_true', help= 'If a container with the same name already exists, force it to quit') parser.add_argument( '--max-cpus', metavar='N', default=cpu_count(), type=int, help= 'The maximum number of CPUs the testsuite is allowed to use at a single time. Defaults to all available cpus.' ) parser.add_argument( '--container-cpus', metavar='N', default=1, type=int, help= 'The number of CPUs a single container should have. Defaults to one.') parser.add_argument( '--jobs', metavar='N', default=None, type=int, help= 'The number of jobs a single container is going to launch. Defaults to --container-cpus.' ) parser.add_argument( '--use-fedora-selinux-policy', action='store_true', help='Mount volumes with z option to accomodate SELinux on Fedora.') # Download related arguments parser.add_argument( '--use-local', action='store_true', help= 'Use a local extracted CGAL release. --testsuite must be set to that release.' ) # TODO make internal releases and latest public? parser.add_argument('--user', help='Username for CGAL Members') parser.add_argument('--passwd', help='Password for CGAL Members') # Upload related arguments parser.add_argument('--upload-results', action='store_true', help='Actually upload the test results.') parser.add_argument('--tester', help='The tester', default=getuser()) parser.add_argument('--tester-name', help='The name of the tester', default=gethostname()) parser.add_argument('--tester-address', help='The mail address of the tester') if load_first_config('CGAL'): default_arg_file = path.join(load_first_config('CGAL'), 'test_cgal_rc') else: default_arg_file = path.join(xdg_config_home, 'test_cgal_rc') if path.isfile(default_arg_file): logging.info( 'Using default arguments from: {}'.format(default_arg_file)) sys.argv.insert(1, '@' + default_arg_file) return parser
from configparser import ConfigParser; from xdg.BaseDirectory import xdg_config_home, load_first_config; from os import system; from os import environ; from os.path import join, expanduser; DEBUG = 0; GTK_VERSION = "gtk-3.0"; GTK_SETTINGS = "Settings"; GTK_SETTINGS_FILE = "settings.ini"; GTK_APPLICATION_PREFER_DARK_THEME = "gtk-application-prefer-dark-theme"; FILE_OPEN_WRITE = "w"; gtk_config_path = join(load_first_config(GTK_VERSION), GTK_SETTINGS_FILE); gtkconfig = ConfigParser(); gtkconfig.read(gtk_config_path); # we receive a string ('0' or '1'), so cast it to int (0 or 1) and then bool prefer_dark_theme = not bool(int(gtkconfig[GTK_SETTINGS] [GTK_APPLICATION_PREFER_DARK_THEME])); # other way around, we have bool (True or False), cast it to int (0 or 1) # but ConfigParser wants strings, so cast it to string ('0' or '1') gtkconfig[GTK_SETTINGS] \ [GTK_APPLICATION_PREFER_DARK_THEME] = str(int(prefer_dark_theme)); with open(gtk_config_path, FILE_OPEN_WRITE) as file_open: gtkconfig.write(file_open);