Exemple #1
0
def list_profiles( base_dir ):
    """ enumerates profile names and related filenames """

    # dicts are ordered since 3.7 (since 3.6 for CPython) ;
    # till then, we would sacrifice profile order for pretty-printing )
    result = {}

    inifile = os.path.join(base_dir, 'profiles.ini')
    if os.path.exists( inifile ):
        
        cp = SafeConfigParser()
        cp.read( inifile )
        
        for section in cp.sections():
            if cp.has_option(section, 'path'):
                if cp.has_option( section, 'name' ):
                   
                    p = cp.get(section, 'path', raw = True)
                    n = cp.get(section, 'name', raw = True)
                    
                    fullpath = os.path.join( base_dir, p )
                    
                    result[ fullpath ] = n
                    
    return result
def load_custom_commands(reload, script_location):
    global commands, parent_location

    if parent_location is None:
        parent_location = script_location

    if reload:
        commands = defaultdict(dict)

    config = SafeConfigParser()
    commands_input = common.text_file_read(
        parent_location + '/config/custom_discord_commands.cfg')
    config.read_string(commands_input)
    sections = config.sections()

    for section in sections:
        commands[section]['content'] = config.get(section, 'content')
        commands[section]['base_command'] = None
        commands[section]['aliases'] = None
        commands[section]['description'] = None

        if config.has_option(section, 'alias_commands'):
            alternates = config.get(section, 'alias_commands').split(',')
            commands[section]['aliases'] = alternates

            for alternate in alternates:
                alternate = alternate.strip()
                commands[alternate]['base_command'] = section

        if config.has_option(section, 'description'):
            commands[section]['description'] = config.get(
                section, 'description')
def fixDefaultPath(wspath):
    """Mimic most often case of hg fdefpath usage, walk across repositories
      and replace(or add) to hgrc file

      default = http://closedjdk.us.oracle.com/jdk7u/jdk7u-cpu/jdk/test/closed
      default-push = ssh://[email protected]/jdk7u/jdk7u-cpu-gate/jdk/test/closed

      as it doesn't use mercurital api to do it, it works much faster then defpath extension
    """
    config = SafeConfigParser()
    ini = os.path.join(wspath, '.hg/hgrc')

    config.read(ini)

    defaultPull = None
    defaultPush = None

    if config.has_section('paths'):
        if config.has_option('paths', 'default'):
            defaultPull = config.get('paths', 'default')
        if config.has_option('paths', 'default-push'):
            defaultPush = config.get('paths', 'default-push')

    if defaultPull == None:
        print("Can't build push path default path is invalid")
        return

    ojn = "" if G_ojdkname == None else G_ojdkname + '@'

    if defaultPull.startswith('http://'):
        p = defaultPull[7:]
    elif defaultPull.startswith('https://'):
        p = defaultPull[8:]
    elif defaultPull.startswith('ssh://'):
        p = defaultPull[6:]
    else:
        print("Can't build push path default path is invalid or local (%s)" %
              defaultPull)
        return

    ps = p.split('/')
    if G_gatedpush:
        ps[2] = ps[2] + '-gate'

    newDefaultPush = 'ssh://' + ojn + '/'.join(
        ps) if 'push' in G_defpath else defaultPush
    newDefaultPull = 'ssh://' + ojn + p if 'pull' in G_defpath else defaultPull

    if defaultPush == newDefaultPush and defaultPull == newDefaultPull:
        print("Defpath: %s (not changing)\n %s\n %s" %
              (ini, defaultPull, defaultPush))
    else:
        print("Defpath: %s\n %s\n %s" % (ini, newDefaultPull, newDefaultPush))
        shutil.move(ini, ini + '.old')
        config.set('paths', 'default', newDefaultPull)
        config.set('paths', 'default-push', newDefaultPush)
        fp = open(ini, 'w')
        config.write(fp)
        fp.close()
Exemple #4
0
    def __init__(self, options, gitlab_host):
        self.options = options
        self.config = find_config(self.options.config)
        # Get the host from upstream and remove the https://
        # the case for alpine linux, https://gitlab.alpinelinux.org
        # will be reduced to gitlab.alpinelinux.org
        #
        # Do note that it does not matter if we the origin or upstream
        # remote is used here since gitlab is not federated, so both will
        # call the same server
        self.section = gitlab_host.replace("https://", "")
        """
            Write the configuration passed to us via the CLI to the config
            file if it's not there already or the user wants to overwrite it
        """
        parser = SafeConfigParser()
        parser.read(self.config)

        if parser.has_section(self.section) is False:
            parser.add_section(self.section)
            with open(self.config, "w") as c:
                parser.write(c)

        # In case the 'url' options is not set in the section we are looking for
        # then just write it out.
        if parser.has_option(self.section, "url") is False:
            parser[self.section]["url"] = "https://" + self.section
            with open(self.config, "w") as c:
                parser.write(c)

        if parser.has_option(self.section, "private_token") is False or (
                self.options.overwrite is True):
            # If --token is not passed to us then drop out with a long useful
            # message, if it is passed to us write it out in the configuration
            # file
            if self.options.token is None:
                token_answer = dict()
                token_answer["token"] = ""
                print("Please visit https://" + self.section +
                      "/profile/personal_access_tokens to generate your token")
                while token_answer is not None and token_answer["token"] == "":
                    questions = [
                        inquirer.Text("token", message="personal access token")
                    ]
                    token_answer = inquirer.prompt(questions)
                if token_answer is None:
                    raise ValueError("personal access token not provided")
                else:
                    parser[
                        self.section]["private_token"] = token_answer["token"]
                    with open(self.config, "w") as c:
                        parser.write(c)
            else:
                parser[self.section]["private_token"] = self.options.token
                with open(self.config, "w") as c:
                    parser.write(c)
Exemple #5
0
def init():
	cont=False
	clear()

	credentials_from_file = False

	credentials = SafeConfigParser();
	credentials.read('settings.txt')
	if (credentials.has_option('main','email') 
		  and credentials.has_option('main','password')):
		credentials_from_file = True

	while(cont == False):
		driver.get('https://www.facebook.com/')
	
		if credentials_from_file:
			email = credentials.get('main', 'email')
			password = credentials.get('main', 'password')
		else:
			email=input('Email : ')
			password=getpass('Password : '******'input')
		inputs[1].send_keys(email)
		inputs[2].send_keys(password)
		driver.implicitly_wait(10)
		inputs[3].click()

		if str(driver.current_url).split('=')[0] == 'https://www.facebook.com/login.php?login_attempt':
			clear()
			print('Invalid Email/Password')
			if credentials_from_file:
				print('Switching to manual input')
				credentials_from_file = False
		else: 
			cont=True

	print('Loading...\n')
	profile=[x for x in driver.find_elements_by_tag_name('a') if x.get_attribute('title') == 'Profile'][0].get_attribute('href').split('/')[3]
	driver.get('https://www.facebook.com/messages/'+profile)
	
	global replyButton
	replyButton=[x for x in driver.find_elements_by_tag_name('input') if x.get_attribute('value') == 'Reply'][0]

	if not(replyButton.is_displayed()):
		driver.find_element_by_css_selector('._1s0').click()

	if os.path.isfile(os.getcwd()+'/commands.txt'):
		with open('commands.txt','r') as foo:
			for a in foo.read().split('\n'):
				ls=a.split(' ')
				if len(ls) >= 2:
					global customCommands
					customCommands[ls[0]]=' '.join(ls[1:])

	print('Ready!')
Exemple #6
0
    def __init__(self, config_path):

        # Read configuration file
        cfg = SafeConfigParser()
        cfg.read(config_path)

        # Case
        self.v_inf = cfg.getfloat('case', 'v_inf')
        self.rpm = cfg.getfloat('case', 'rpm')
        if cfg.has_option('case', 'twist'):
            self.twist = cfg.getfloat('case', 'twist')
        else:
            self.twist = 0.0
        if cfg.has_option('case', 'coaxial'):
            self.coaxial = cfg.getboolean('case', 'coaxial')
        else:
            self.coaxial = False

        # Rotor
        if cfg.has_section('turbine'):
            self.mode = 'turbine'
            self.rotor = Rotor(cfg, 'turbine', self.mode)
        else:
            self.mode = 'rotor'
            self.rotor = Rotor(cfg, 'rotor', self.mode)

        # Fluid
        self.fluid = Fluid(cfg)

        # Output
        self.T = 0  # Thrust
        self.Q = 0  # Torque
        self.P = 0  # Power

        # Coaxial
        if self.coaxial:
            self.rpm2 = cfg.getfloat('case', 'rpm2')
            if cfg.has_option('case', 'twist2'):
                self.twist2 = cfg.getfloat('case', 'twist2')
            else:
                self.twist2 = 0.0
            self.rotor2 = Rotor(cfg, 'rotor2', self.mode)
            self.zD = cfg.getfloat('case', 'dz') / self.rotor.diameter
            self.T2 = 0
            self.Q2 = 0
            self.P2 = 0

        # Solver
        self.solver = 'bisect'
        self.Cs = 0.625
        if cfg.has_section('solver'):
            self.solver = cfg.get('solver', 'solver')
            if cfg.has_option('solver', 'Cs'):
                self.Cs = cfg.getfloat('solver', 'Cs')
Exemple #7
0
def load_setup_connection_string(section):
    """
    Attempts to read the default connection string from the setup.cfg file.

    If the file does not exist or if it exists but does not contain the connection string, None is returned.  If the
    file exists but cannot be parsed, an exception is raised.
    """
    from os.path import exists, join, dirname, splitext, basename
    from configparser import SafeConfigParser

    FILENAME = 'setup.cfg'
    KEY      = 'connection-string'

    path = dirname(abspath(__file__))
    while True:
        fqn = join(path, 'tmp', FILENAME)
        if exists(fqn):
            break
        parent = dirname(path)
        print('{} --> {}'.format(path, parent))
        if parent == path:
            return None
        path = parent

    try:
        p = SafeConfigParser()
        p.read(fqn)
    except:
        raise SystemExit('Unable to parse %s: %s' % (path, sys.exc_info()[1]))

    if p.has_option(section, KEY):
        return p.get(section, KEY)
Exemple #8
0
def load_setup_connection_string(section):
    """
    Attempts to read the default connection string from the setup.cfg file.

    If the file does not exist or if it exists but does not contain the connection string, None is returned.  If the
    file exists but cannot be parsed, an exception is raised.
    """
    from os.path import exists, join, dirname, splitext, basename
    from configparser import SafeConfigParser

    FILENAME = 'odbc.cfg'
    KEY = 'connection-string'

    path = join(dirname(abspath(__file__)), FILENAME)

    if exists(path):
        try:
            p = SafeConfigParser()
            p.read(path)
        except:
            raise SystemExit('Unable to parse %s: %s' %
                             (path, sys.exc_info()[1]))
        if p.has_option(section, KEY):
            return p.get(section, KEY)

    return None
Exemple #9
0
def load_config(logger):
    """Loads config values from the settings.cfg file in the script dir"""
    config_path = get_script_dir() + 'settings.cfg'
    section_name = 'accounts'

    if not os.path.exists(config_path):
        logger.error("Error: No config file found. Copy settings-example.cfg "
                     "to settings.cfg and customize it.")
        exit()

    config = SafeConfigParser()
    config.read(config_path)

    # Do basic checks on the config file
    if not config.has_section(section_name):
        logger.error("Error: The config file doesn't have an accounts "
                     "section. Check the config file format.")
        exit()

    if not config.has_option(section_name, 'api_key'):
        logger.error("Error: No developer key found in the config file. "
                     "Check the config file values.")
        exit()

    config_values = {
        'api_key': config.get(section_name, 'api_key'),
    }

    return config_values
Exemple #10
0
def main(settings="settings.ini"):
    parser = SafeConfigParser()
    parser.read(settings)

    settings_dict = {}
    clients_dict = {}

    # Covert the settings.ini settings into a dictionary for later processing
    if parser.has_section("havocbot"):
        # Create a bundle of havocbot settings
        settings_dict["havocbot"] = parser.items("havocbot")

        if parser.has_option("havocbot", "clients_enabled"):
            clients_string = parser.get("havocbot", "clients_enabled")
            clients_list = clients_string.strip().split(",")

            # Create a bundle of settings to pass the client integration for processing
            # Bundle format is a list of tuples in the format [('integration name'), [('property1', 'value1'), ('property2', 'value12)], ...]
            for client in clients_list:
                if parser.has_section(client):
                    clients_dict[client] = parser.items(client)
    else:
        sys.exit("Could not find havocbot settings in settings.ini")

    # Configure logging
    configure_logging(settings_dict)

    # Get an instance of the bot if it does not exist
    havocbot = get_bot()

    # Pass a dictionary of settings to the bot
    havocbot.set_settings(havocbot_settings=settings_dict, clients_settings=clients_dict)

    # Start it. Off we go
    havocbot.start()
Exemple #11
0
    def _read_repos(self, repos):
        repodir = '/etc/zypp/repos.d'
        solvfile = '/var/cache/zypp/solv/%s/solv'
        onlyenabled = False

        parser = SafeConfigParser()

        if not repos:
            repos = [f for f in os.listdir(repodir) if fnmatch(f, '*.repo')]
            onlyenabled = True

        for r in repos:
            if '/' in r or r.endswith('.solv'):
                name = os.path.basename(os.path.splitext(r)[0])
                repo = self.pool.add_repo(name)
                repo.add_solv(r)
                logger.debug("add repo %s" % name)
            else:
                try:
                    if r.endswith('.repo'):
                        name = os.path.splitext(r)[0]
                    else:
                        name = r
                        r += '.repo'
                    parser.read('/'.join((repodir, r)))
                    if onlyenabled is False or parser.get(name,
                                                          'enabled') == '1':
                        repo = self.pool.add_repo(name)
                        repo.add_solv(solvfile % name)
                        if parser.has_option(name, 'priority'):
                            repo.priority = parser.getint(name, 'priority')
                        logger.debug("add repo %s" % name)
                except Exception as e:
                    logger.error(e)
Exemple #12
0
def setclientfortraining(clientname):
    print('====Inside exectraining====')
    try:
        parser_setclient = SafeConfigParser()
        parser_setclient.read(os.path.dirname(__file__) + '/training.ini')
        if parser_setclient.has_option('chat_trainer', 'client'):
            if clientname.strip() == "":
                print(
                    "=====Inside setclientfortraining blank client name==== " +
                    clientname)
                return False
            else:
                print("=====Inside setclientfortraining==== " + clientname)
                parser_setclient.set('chat_trainer', 'client', clientname)
                with open(os.path.dirname(__file__) + '/training.ini',
                          'w') as configfile:
                    parser_setclient.write(configfile)

                print(parser_setclient.get('chat_trainer', 'client'))
                return True
        else:
            return False
    except Exception as e:
        print('train_chatbot:: setclientfortraining Failed: ' + str(e))
        return False
    finally:
        parser_setclient.clear
Exemple #13
0
 def __init__(self, configpath):
     conf = SafeConfigParser()
     conf.readfp(open(configpath))
     self.conf = conf
     if not conf.has_option_with_value('server', 'module'):
         raise ServerConfigurationError(
             'The factory module is not defined in the configuration file.')
     try:
         mapfactorymodule = __import__(conf.get('server', 'module'))
     except ImportError:
         raise ServerConfigurationError(
             'The factory module could not be loaded.')
     if hasattr(mapfactorymodule, 'WMSFactory'):
         self.mapfactory = getattr(mapfactorymodule, 'WMSFactory')()
     else:
         raise ServerConfigurationError(
             'The factory module does not have a WMSFactory class.')
     if conf.has_option('server', 'debug'):
         self.debug = int(conf.get('server', 'debug'))
     else:
         self.debug = 0
     if self.conf.has_option_with_value('server', 'maxage'):
         self.max_age = 'max-age=%d' % self.conf.get('server', 'maxage')
     else:
         self.max_age = None
Exemple #14
0
def load_setup_connection_string(section):
    """
    Attempts to read the default connection string from the setup.cfg file.

    If the file does not exist or if it exists but does not contain the connection string, None is returned.  If the
    file exists but cannot be parsed, an exception is raised.
    """
    from os.path import exists, join, dirname, splitext, basename
    from configparser import SafeConfigParser

    FILENAME = "setup.cfg"
    KEY = "connection-string"

    path = join(dirname(dirname(abspath(__file__))), "tmp", FILENAME)

    if exists(path):
        try:
            p = SafeConfigParser()
            p.read(path)
        except:
            raise SystemExit("Unable to parse %s: %s" % (path, sys.exc_info()[1]))

        if p.has_option(section, KEY):
            return p.get(section, KEY)

    return None
Exemple #15
0
 async def react(self, ctx, arg):
     """
     Posts a reaction image or copypasta from a keyword specified.
     """
     config = SafeConfigParser()
     config.read('reactions.ini')
     if config.has_option('gifs', '{}'.format(arg)):
         gif = config.get('gifs', '{}'.format(arg))
         if gif.startswith('http'):
             embed = discord.Embed(title=None,
                                   description=None,
                                   color=0x00FF99)
             embed.set_image(url=gif)
         else:
             embed = discord.Embed(title=None,
                                   description=None,
                                   color=0x00FF99)
             embed.add_field(name=gif,
                             value='Requested by {}'.format(
                                 ctx.message.author),
                             inline=True)
         await self.bot.say(embed=embed)
     else:
         embed = discord.Embed(
             title='I could not find that reaction!',
             description='Please enter a valid reaction or try again.',
             color=0xFF0000)
         embed.set_thumbnail(url='https://i.imgur.com/z2xfrsH.png')
         await self.bot.say(embed=embed)
def read_config(filename):
    parser = SafeConfigParser()
    found = parser.read(filename)
    if not filename in found:
        print("The ini file " + filename + " was not found.", file=sys.stderr)
        sys.exit(-1)

    cfg = {}
    for section_definition in [ { 'name': 'mqtt', 'parameters': [ 'host', 'port', 'client_id', 'user', 'pw'] },\
                                { 'name': 'upload', 'parameters': ['discovery_topic', 'discovery_name', 'state_topic']},\
                                { 'name': 'download', 'parameters': ['discovery_topic', 'discovery_name', 'state_topic']},\
                                { 'name': 'ping', 'parameters': ['discovery_topic', 'discovery_name', 'state_topic']}]:

        section_name = section_definition['name']

        section = {}
        for parameter in section_definition['parameters']:
            if not parser.has_option(section_name, parameter):
                print("Parameter '" + parameter + "' is missing in section '" +
                      section_name + "'",
                      file=sys.stderr)
                sys.exit(-1)
            section[parameter] = parser.get(section_name, parameter)

        cfg[section_name] = section

    try:
        cfg['mqtt']['port'] = int(cfg['mqtt']['port'])
    except ValueError:
        print("The port " + cfg['mqtt']['port'] +
              " cannot be parsed as integer.",
              file=sys.stderr)
        sys.exit(-1)

    return cfg
Exemple #17
0
    def connect_from_web(klass, url):
        """
        Find a connection that matches this kojiweb URL.

        Check all koji.conf.d files' kojiweb URLs and load the profile that
        matches the url we pass in here.

        For example, if a user pastes a kojiweb URL into chat, we want to
        discover the corresponding Koji instance hub automatically.

        See also from_web().

        :param url: ``str``, for example
                    "http://cbs.centos.org/koji/buildinfo?buildID=21155"
        :returns: A "Connection" instance
        """
        # Treat any input with whitespace as invalid:
        if re.search(r'\s', url):
            return
        url = url.split(' ', 1)[0]
        for path in profiles():
            cfg = SafeConfigParser()
            cfg.read(path)
            for profile in cfg.sections():
                if not cfg.has_option(profile, 'weburl'):
                    continue
                weburl = cfg.get(profile, 'weburl')
                if url.startswith(weburl):
                    return klass(profile)
Exemple #18
0
class GitConfigParser():
    CORE = 'core'
    def __init__(self, branch):
        self.section = branch
        self.file = join(GIT_DIR, '.git', 'gitcc')
        self.parser = SafeConfigParser();
        self.parser.add_section(self.section)
    def set(self, name, value):
        self.parser.set(self.section, name, value)
    def read(self):
        self.parser.read(self.file)
    def write(self):
        self.parser.write(open(self.file, 'w'))
    def getCore(self, name, *args):
        return self._get(self.CORE, name, *args)
    def get(self, name, *args):
        return self._get(self.section, name, *args)
    def _get(self, section, name, default=None):
        if not self.parser.has_option(section, name):
            return default
        return self.parser.get(section, name)
    def getList(self, name, default=None):
        return self.get(name, default).split('|')
    def getInclude(self):
        return self.getCore('include', '.').split('|')
    def getBranches(self):
        return self.getList('branches', 'main')
    def getExtraBranches(self):
        return self.getList('_branches', 'main')
Exemple #19
0
class GitConfigParser():
    CORE = 'core'
    def __init__(self, branch):
        self.section = branch
        self.file = join(GIT_DIR, '.git', 'gitcc')
        self.parser = SafeConfigParser();
        self.parser.add_section(self.section)
    def set(self, name, value):
        self.parser.set(self.section, name, value)
    def read(self):
        self.parser.read(self.file)
    def write(self):
        self.parser.write(open(self.file, 'w'))
    def getCore(self, name, *args):
        return self._get(self.CORE, name, *args)
    def get(self, name, *args):
        return self._get(self.section, name, *args)
    def _get(self, section, name, default=None):
        if not self.parser.has_option(section, name):
            return default
        return self.parser.get(section, name)
    def getList(self, name, default=None):
        return self.get(name, default).split('|')
    def getInclude(self):
        return self.getCore('include', '.').split('|')
    def getExclude(self):
        return self.getCore('exclude', '.').split('|')
    def getBranches(self):
        return self.getList('branches', 'main')
    def getExtraBranches(self):
        return self.getList('_branches', 'main')
Exemple #20
0
    def __init__(self,
                 profile,
                 okta_profile,
                 account,
                 verbose,
                 logger,
                 region,
                 reset,
                 debug=False):
        home_dir = os.path.expanduser('~')
        self.creds_dir = os.path.join(home_dir, ".aws")
        self.creds_file = os.path.join(self.creds_dir, "credentials")
        self.profile = profile
        self.account = account
        self.verbose = verbose
        self.logger = logger
        self.role = ""
        self.region = region
        self.debug = debug

        okta_info = os.path.join(home_dir, '.okta-alias-info')
        if not os.path.isfile(okta_info):
            open(okta_info, 'a').close()

        okta_config = os.path.join(home_dir, '.okta-aws')
        parser = SafeConfigParser()
        parser.read(okta_config)

        if parser.has_option(okta_profile, 'role') and not reset:
            self.role = parser.get(okta_profile, 'role')
            self.logger.debug("Setting AWS role to %s" % self.role)
Exemple #21
0
def load_setup_connection_string(section):
    """
    Attempts to read the default connection string from the setup.cfg file.

    If the file does not exist or if it exists but does not contain the connection string, None is returned.  If the
    file exists but cannot be parsed, an exception is raised.
    """
    from os.path import exists, join, dirname, splitext, basename
    from configparser import SafeConfigParser

    FILENAME = 'setup.cfg'
    KEY = 'connection-string'

    path = dirname(abspath(__file__))
    while True:
        fqn = join(path, 'tmp', FILENAME)
        if exists(fqn):
            break
        parent = dirname(path)
        print('{} --> {}'.format(path, parent))
        if parent == path:
            return None
        path = parent

    try:
        p = SafeConfigParser()
        p.read(fqn)
    except:
        raise SystemExit('Unable to parse %s: %s' % (path, sys.exc_info()[1]))

    if p.has_option(section, KEY):
        return p.get(section, KEY)
Exemple #22
0
    def _load_schema(self, location, xml=None):
        """
        location -- location of schema, also used as a key
        xml -- optional string representation of schema
        """
        cachedir = self._cachedir
        # wsdl2py: deal with XML Schema
        if not os.path.isdir(cachedir): os.mkdir(cachedir)
    
        file = os.path.join(cachedir, '.cache')
        section = 'TYPES'
        cp = ConfigParser()
        try:
            cp.readfp(open(file, 'r'))
        except IOError:
            del cp;  cp = None
            
        option = location.replace(':', '-') # colons seem to screw up option
        if (cp is not None and cp.has_section(section) and 
            cp.has_option(section, option)):
            types = cp.get(section, option)
        else:
            # dont do anything to anames
            if not self._pyclass:
                containers.ContainerBase.func_aname = lambda instnc,n: str(n)
                
            from pysphere.ZSI.wstools import XMLSchema
            reader = XMLSchema.SchemaReader(base_url=location)
            if xml is not None and isinstance(xml, str):
                schema = reader.loadFromString(xml)
            elif xml is not None:
                raise RuntimeError('Unsupported: XML must be string')
            elif not os.path.isfile(location):
                schema = reader.loadFromURL(location)
            else:
                schema = reader.reader.loadFromFile(location)
                
            # TODO: change this to keyword list
            class options:
                output_dir = cachedir
                schema = True
                simple_naming = False
                address = False
                lazy = self._lazy
                complexType = self._pyclass

            schema.location = location
            files = commands._wsdl2py(options, schema)
            if cp is None: cp = ConfigParser()
            if not cp.has_section(section): cp.add_section(section)
            types = filter(lambda f: f.endswith('_types.py'), files)[0]
            cp.set(section, option, types)
            cp.write(open(file, 'w'))
            
        if os.path.abspath(cachedir) not in sys.path:
            sys.path.append(os.path.abspath(cachedir))
            
        mod = os.path.split(types)[-1].rstrip('.py')
        return __import__(mod)
Exemple #23
0
def _init(apiurl):
    """Initialize osc library.

    apiurl is the apiurl which should be used.

    """
    if hasattr(apiurl, 'extend') and len(set(apiurl)) > 1:
        msg = ("Different apiurls are not supported at the moment: "
               "%s (using %s)" % (', '.join(apiurl), apiurl[0]))
        logger().info(msg)
        apiurl = apiurl[0]
    conf_filename = os.environ.get('OSC_CONFIG', '~/.oscrc')
    conf_filename = os.path.expanduser(conf_filename)
    cp = SafeConfigParser({'plaintext_password': True, 'aliases': ''})
    cp.read(conf_filename)
    apiurl = apiurl.strip('/')
    if apiurl == 'api':
        apiurl = 'https://api.opensuse.org'
    for section in cp.sections():
        aliases = cp.get(section, 'aliases', raw=True)
        aliases = aliases.split(',')
        if section.strip('/') == apiurl or apiurl in aliases:
            user = cp.get(section, 'user', raw=True)
            password = None
            if cp.has_option(section, 'pass'):
                password = cp.get(section, 'pass', raw=True)
            if cp.has_option(section, 'passx'):
                password = cp.get(section, 'pass', raw=True)
                password = password.decode('base64').decode('bz2')
            if (cp.has_option(section, 'keyring')
                    and cp.getboolean(section, 'keyring')):
                try:
                    import keyring
                    host = urllib.parse.urlparse(apiurl).hostname
                    password = keyring.get_password(host, user)
                except ImportError:
                    msg = ("keyring module not available but '%s' "
                           "stores password there") % conf_filename
                    raise ValueError(msg)
            if password is None:
                msg = "No password provided for %s" % section
                raise ValueError(msg)
            if '://' not in section:
                section = 'https://{0}'.format(section)
            Osc.init(section, username=user, password=password)
            return section
Exemple #24
0
    def _load_schema(self, location, xml=None):
        """
        location -- location of schema, also used as a key
        xml -- optional string representation of schema
        """
        cachedir = self._cachedir
        # wsdl2py: deal with XML Schema
        if not os.path.isdir(cachedir): os.mkdir(cachedir)

        _file = os.path.join(cachedir, '.cache')
        section = 'TYPES'
        cp = ConfigParser()
        try:
            cp.readfp(open(_file, 'r'))
        except IOError:
            del cp;  cp = None

        option = location.replace(':', '-') # colons seem to screw up option
        if (cp is not None and cp.has_section(section) and
            cp.has_option(section, option)):
            types = cp.get(section, option)
        else:
            # dont do anything to anames
            if not self._pyclass:
                containers.ContainerBase.func_aname = lambda instnc,n: str(n)

            from pysphere.ZSI.wstools import XMLSchema
            reader = XMLSchema.SchemaReader(base_url=location)
            if xml is not None and isinstance(xml, str):
                schema = reader.loadFromString(xml)
            elif xml is not None:
                raise RuntimeError('Unsupported: XML must be string')
            elif not os.path.isfile(location):
                schema = reader.loadFromURL(location)
            else:
                schema = reader.reader.loadFromFile(location)

            # TODO: change this to keyword list
            class options:
                output_dir = cachedir
                schema = True
                simple_naming = False
                address = False
                lazy = self._lazy
                complexType = self._pyclass

            schema.location = location
            files = commands._wsdl2py(options, schema)
            if cp is None: cp = ConfigParser()
            if not cp.has_section(section): cp.add_section(section)
            types = [f for f in files if f.endswith('_types.py')][0]
            cp.set(section, option, types)
            cp.write(open(_file, 'w'))
        if os.path.abspath(cachedir) not in sys.path:
            sys.path.append(os.path.abspath(cachedir))

        mod = os.path.split(types)[-1].rstrip('.py')
        return __import__(mod)
Exemple #25
0
    def __init__(self, config: str):
        import os
        from configparser import SafeConfigParser
        CONFIG = SafeConfigParser()
        if not os.path.isfile(config):
            config = os.path.join(os.getcwd(), "config.ini")
            if not os.path.isfile(config):
                config_file = open(config, "w")
                config_file.write(self.CONFIG_DEFAULT)
        CONFIG.read(config)

        # Assign default settings if the file or settings do not exist
        if not CONFIG.has_option("DEFAULT", "meta"):
            CONFIG.set("DEFAULT", "meta",
                       "{assignment}_{user}_attempt_{date}.txt")
        if not CONFIG.has_option("DEFAULT", "upload"):
            CONFIG.set("DEFAULT", "upload",
                       "{assignment}_{user}_attempt_{date}_{filename}")
        if not CONFIG.has_option("DEFAULT", "delim"):
            CONFIG.set("DEFAULT", "delim", "_")
        if not CONFIG.has_option("DEFAULT", "encasing"):
            CONFIG.set("DEFAULT", "encasing", "{}")

        self.meta_format = str(CONFIG.get("DEFAULT", "meta"))
        self.upload_format = str(CONFIG.get("DEFAULT", "upload"))
        self.delim = str(CONFIG.get("DEFAULT", "delim"))
        self.encasing = str(CONFIG.get("DEFAULT", "encasing"))

        # Test if the necessary attributes have been defined in the format
        test_meta = self._mapMetaAttributes()
        test_upload = self._mapUploadAttributes()
        if not any("user" in attrib for attrib in test_meta) or not any(
                "user" in attrib for attrib in test_upload):
            raise self.NO_USER_FORMAT_ERROR
        if not any("filename" in attrib for attrib in test_upload):
            raise self.NO_FILENAME_FORMAT_ERROR
        if len(self.encasing) != 2:
            raise self.ATTRIB_ENCASING_ERROR
        test_meta = test_upload = None

        self.meta_regex = self._formatRegex(self.meta_format, self.delim,
                                            self.encasing)
        self.upload_regex = self._formatRegex(self.upload_format, self.delim,
                                              self.encasing)
Exemple #26
0
    def __init__(self, section="PeptideScan"):

        pkgdir = __file__
        while not os.path.isdir(pkgdir):
            pkgdir, dummy = os.path.split(pkgdir)
        # print >>sys.stderr, pkgdir

        platname = platform.uname()
        archdir = '-'.join([platname[0], platname[4]]).strip('-')
        iniName = '%s.ini' % archdir

        cfg = SafeConfigParser()
        # if resource_exists(__name__,iniName):
        #    assert(False)
        # cfg.readfp(resource_stream(__name__,iniName),iniName)
        # else:
        cfg.read(os.path.join(pkgdir, iniName))

        assert cfg.has_section(
            section), "Can't find section %s in config file: %s." % (section,
                                                                     iniName)
        exeBase = cfg.get(section, "exeBase")
        exeExtn = cfg.get(section, "exeExtn")
        exeName = "%s-%s%s" % (exeBase, archdir, exeExtn)

        if cfg.has_option(section, "exePath"):
            exePath = cfg.get(section, "exePath", vars=dict(pkgDir=pkgdir))
        elif cfg.has_option(section, "exeDir"):
            exePath = os.path.join(
                cfg.get(section, "exeDir", vars=dict(pkgDir=pkgdir)), exeName)
        else:
            assert (False)
            # exePath = resource_filename(__name__, exeName)

        # print >>sys.stderr, exePath
        assert (os.path.exists(exePath))

        if cfg.has_option(section, "exeOpt"):
            exeOpt = cfg.get(section, "exeOpt")
        else:
            exeOpt = ""
        self.exe = exePath
        self.opt = exeOpt
Exemple #27
0
 def from_cfg(self, filename):
     parser = SafeConfigParser()
     with open(filename) as fh:
         parser.readfp(fh)
     self.client_id = parser.get('credentials', 'client_id')
     self.client_secret = parser.get('credentials', 'client_secret')
     if parser.has_option('credentials', 'refresh_token'):
         token = parser.get('credentials', 'refresh_token').strip()
         if token:
             self.refresh_token = token
Exemple #28
0
 def from_cfg(self, filename):
     parser = SafeConfigParser()
     with open(filename) as fh:
         parser.readfp(fh)
     self.client_id = parser.get('credentials', 'client_id')
     self.client_secret = parser.get('credentials', 'client_secret')
     if parser.has_option('credentials', 'refresh_token'):
         token = parser.get('credentials', 'refresh_token').strip()
         if token:
             self.refresh_token = token
Exemple #29
0
def api_key_from_file(url):
    """ Check bugzillarc for an API key for this Bugzilla URL. """
    path = os.path.expanduser('~/.config/python-bugzilla/bugzillarc')
    cfg = SafeConfigParser()
    cfg.read(path)
    domain = urlparse(url)[1]
    if domain not in cfg.sections():
        return None
    if not cfg.has_option(domain, 'api_key'):
        return None
    return cfg.get(domain, 'api_key')
Exemple #30
0
    def load_information(self):
        """ Load the add-on's information from file. """
        parser = SafeConfigParser()
        with self.path.open(self.file) as file:
            parser.readfp(file, self.file)

        # Read the core information.
        if not parser.has_section('Core'):
            raise ValueError(
                "No Core section in the add-on information file for the "
                "add-on %r." % self.name
            )

        for key in self.CORE_VALUES + ('version',):
            if isinstance(key, (list, tuple)):
                key, key_type = key
                if isinstance(key_type, basestring):
                    key_type = eval(key_type)
            else:
                key_type = lambda thing: thing

            # If we don't have that key, and we have a default value, just
            # continue, otherwise raise a ValueError.
            if not parser.has_option('Core', key):
                if not hasattr(self, key):
                    raise ValueError(
                        "Core value %r not defined in the add-on "
                        "information file for the add-on %r." %
                            (key, self.name)
                    )
                continue

            # Load the value and set it as an attribute of self.
            setattr(self, key, key_type(parser.get('Core', key)))

        # Split the inheritance.
        if (hasattr(self, 'inherits') and self.inherits and
                isinstance(self.inherits, basestring)):
            self.inherits = [x.strip() for x in self.inherits.split(',')]

        # Now, read the requirements.
        if parser.has_section('Requires'):
            for key, value in parser.items('Requires'):
                name, match = VersionMatch.from_string(value)
                self.requires[name] = match

        # Finally, read the data section. This generally just contains a nice
        # description of the add-on.
        if parser.has_section('Data'):
            self.data.update(parser.items('Data'))

        if parser.has_section('Description'):
            self.data.update(parser.items('Description'))
Exemple #31
0
class BaseCloudProvider(CloudProvider):

    def __init__(self, config):
        self._config = BaseConfiguration(config)
        self._config_parser = SafeConfigParser()
        self._config_parser.read(CloudBridgeConfigLocations)

    @property
    def config(self):
        return self._config

    @property
    def name(self):
        return str(self.__class__.__name__)

    def has_service(self, service_type):
        """
        Checks whether this provider supports a given service.

        :type service_type: str or :class:``.CloudServiceType``
        :param service_type: Type of service to check support for.

        :rtype: bool
        :return: ``True`` if the service type is supported.
        """
        try:
            if getattr(self, service_type):
                return True
        except AttributeError:
            pass  # Undefined service type
        return False

    def _get_config_value(self, key, default_value):
        """
        A convenience method to extract a configuration value.

        :type key: str
        :param key: a field to look for in the ``self.config`` field

        :type default_value: anything
        :param default_value: the default value to return if a value for the
                              ``key`` is not available

        :return: a configuration value for the supplied ``key``
        """
        if isinstance(self.config, dict) and self.config.get(key):
            return self.config.get(key, default_value)
        elif hasattr(self.config, key) and getattr(self.config, key):
            return getattr(self.config, key)
        elif (self._config_parser.has_option(self.PROVIDER_ID, key) and
              self._config_parser.get(self.PROVIDER_ID, key)):
            return self._config_parser.get(self.PROVIDER_ID, key)
        return default_value
Exemple #32
0
def ReadINI():

    if(os.path.exists(Crawler_INI_Name) == False):
        pass

    try:
        parser = SafeConfigParser()
        parser.read(Crawler_INI_Name)
        
        if parser.has_option("Path", "HomePath"     ):  Global.SetHomePath      (parser.get("Path", "HomePath"     ))
        if parser.has_option("Path", "SystemPath"   ):  Global.SetSystemPath    (parser.get("Path", "SystemPath"   ))
        if parser.has_option("Path", "LogFilePath"  ):  Global.SetLogFilePath   (parser.get("Path", "LogFilePath"  ))
        if parser.has_option("Path", "XmlFilePath"  ):  Global.SetXmlFilePath   (parser.get("Path", "XmlFilePath"  ))
        if parser.has_option("Path", "INIFilePath"  ):  Global.SetINIFilePath   (parser.get("Path", "INIFilePath"  ))

        if parser.has_option("File", "XmlFileName"  ):  Global.SetXmlFileName   (parser.get("File", "XmlFileName"  ))

        if parser.has_option("Data", "RankListCount"):  Global.SetRankListCount (parser.get("Data", "RankListCount"))
        if parser.has_option("Data", "RelationCount"):  Global.SetRelationCount (parser.get("Data", "RelationCount"))
        if parser.has_option("Data", "RelationDepth"):  Global.SetRelationDepth (parser.get("Data", "RelationDepth"))

    except:
        return False
    else:
        return True
Exemple #33
0
 def setStarted(self, setBol):
     config = SafeConfigParser()
     config.read(self.configFile)
     if not config.has_section('main'):
         config.add_section('main')
     if setBol:
         pid = os.getpid()
         config.set('main', 'started', str(pid))
     else:
         if config.has_option('main', 'started'):
             config.remove_option('main', 'started')
     with open(self.configFile, 'w') as f:
         config.write(f)
Exemple #34
0
def _write_missing(path):
    cp = SafeConfigParser()
    cp.read(path)

    for section, values in _options.items():
        if not cp.has_section(section):
            cp.add_section(section)
        for name, value in values.items():
            if not cp.has_option(section, name):
                cp.set(section, name, str(value.default))

    with open(path, "w") as f:
        cp.write(f)
def generate_seed_files():
    theseed_parser = SafeConfigParser()
    theseed_parser.optionxform=str
    theseed_parser.read('TheCoreSeed.ini')
    
    theseed = open("TheCoreSeed.ini", 'r')
    outputs = ["","","",""]
    for line in theseed:
        line = line.strip()
        if len(line) == 0 or line[0] == "[":
            for i in range(4):
                outputs[i] += line + "\n"
            continue    

        pair = line.split("=")
        key = pair[0]
        values = pair[1].split("|")
        numvals = len(values)
        if numvals == 1:
            # it is a copy of another value
            if theseed_parser.has_option("Hotkeys", values[0]):
                values = theseed_parser.get("Hotkeys", values[0]).split("|")
            elif theseed_parser.has_option("Commands", values[0]):
                values = theseed_parser.get("Commands", values[0]).split("|")
            else:
                values = [values[0],values[0],values[0],values[0]]
            numvals = len(values)
        if numvals == 2:
            values = [values[0],values[0],values[0],values[0]] # all layouts are the same
        for i in range(4):
            outputs[i] += key + "=" + values[i] + "\n"
    i = 0
    for r in races:
        filename = prefix + " " + r + "LM " + suffix
        fileio = open(filename, 'w')
        fileio.write(outputs[i])
        fileio.close()
        i += 1
Exemple #36
0
def derive_setup_options():
    options = infer_setup_options()
    options['install_requires'] = read_requirements_file('requirements.txt')
    cfg = SafeConfigParser()
    cfg.read('setup.cfg')
    if cfg.has_option('autopy', 'console_scripts'):
        console_scripts = cfg.get('autopy', 'console_scripts').strip().split()
        options['entry_points'] = {
            'console_scripts': console_scripts,
        }
    string_metadata = [
        'name',
        'version',
        'author',
        'author_email',
        'url',
        'description',
        'license',
    ]
    for key in string_metadata:
        if cfg.has_option('autopy', key):
            options[key] = cfg.get('autopy', 'key')
    return options
Exemple #37
0
def getAlbumInfo(albumInfoPath, tags):
    """Reads the album info file and adds the content to the tag dictionary
        as string type.
        Posible album info options are:
        album, albumartist, date, genre, discnumber, totaldiscs
    """
    
    parser = SafeConfigParser()
    parser.read(albumInfoPath)
    for option in parser.options('albuminfo'):
        if parser.has_option('albuminfo', option):
                tags[option] = parser.get('albuminfo', option)
        
    return tags
Exemple #38
0
    def lookup(self, profile, setting):
        """ Check koji.conf.d files for this profile's setting.

        :param setting: ``str`` like "server" (for kojihub) or "weburl"
        :returns: ``str``, value for this setting
        """
        for path in profiles():
            cfg = SafeConfigParser()
            cfg.read(path)
            if profile not in cfg.sections():
                continue
            if not cfg.has_option(profile, setting):
                continue
            return cfg.get(profile, setting)
Exemple #39
0
class Config(Log):
  _title= 'cfg'
  _config = None
  _configdir = expanduser("~/.ads")
  _configfile = None
  _schema = None

  def __init__(self, filename, schema=None):
    Log.__init__(self, self._title)
    self._config = SafeConfigParser()
    if not isdir(self._configdir):
      self._logger.info(u"creating configuration directory %s" % self._configdir)
      makedirs(self._configdir)
    self._configfile = join(self._configdir, filename)
    self._schema = schema
    self.load()

  def configdir(self):
    return self._configdir

  def load(self):
    self._logger.info(u"loading configuration from %s" % self._configfile)
    self._config.read(self._configfile)

  def save(self):
    self._logger.info(u"saving configuration to %s" % self._configfile)
    self._config.write(open(self._configfile,'wb'))

  def set(self, section, option, value):
    if self._schema: _section = u"%s-%s" % (self._schema, section)
    else: _section = section
    if not self._config.has_section(_section):
      self._config.add_section(_section)
    self._config.set(_section, option, unicode(value))
    return value

  def get(self, section, option, default):
    if self._schema: _section = u"%s-%s" % (self._schema, section)
    else: _section = section
    if (not self._config.has_section(_section) or
        not self._config.has_option(_section, option)):
      return self.set(section, option, default)
    return self._config.get(_section, option)

  def getint(self, section, option, default):
    return int(self.get(section, option, default))

  def getfloat(self, section, option, default):
    return float(self.get(section, option, default))
Exemple #40
0
 async def animedesc(self, ctx, anime):
     config = SafeConfigParser()
     config.read('anime.ini')
     if config.has_option('anime', '{}'.format(anime)):
         desc = config.get('anime', '{}'.format(anime))
         embed = discord.Embed(title=anime,
                               description=desc,
                               color=0x00FF99)
         await self.bot.say(embed=embed)
     else:
         embed = discord.Embed(
             title='I could not find that anime in the list!',
             description='Please enter a valid anime or try again.',
             color=0xFF0000)
         embed.set_thumbnail(url='https://i.imgur.com/z2xfrsH.png')
         await self.bot.say(embed=embed)
Exemple #41
0
 def __init__(self):
     conf = SafeConfigParser()
     conf.readfp(open(self.configpath))
     self.conf = conf
     if not conf.has_option_with_value('server', 'module'):
         raise ServerConfigurationError('The factory module is not defined in the configuration file.')
     try:
         mapfactorymodule = __import__(conf.get('server', 'module'))
     except ImportError:
         raise ServerConfigurationError('The factory module could not be loaded.')
     if hasattr(mapfactorymodule, 'WMSFactory'):
         self.mapfactory = getattr(mapfactorymodule, 'WMSFactory')()
     else:
         raise ServerConfigurationError('The factory module does not have a WMSFactory class.')
     if conf.has_option('server', 'debug'):
         self.debug = int(conf.get('server', 'debug'))
     else:
         self.debug = 0
Exemple #42
0
class GitConfigParser():
    section = 'gitcc'
    def __init__(self):
        self.file = join(GIT_DIR, '.git', 'gitcc')
        self.parser = SafeConfigParser();
        self.parser.add_section(self.section)
    def set(self, name, value):
        self.parser.set(self.section, name, value)
    def read(self):
        self.parser.read(self.file)
    def write(self):
        self.parser.write(open(self.file, 'w'))
    def get(self, name, default=None):
        if not self.parser.has_option(self.section, name):
            return default
        return self.parser.get(self.section, name)
    def getList(self, name, default=None):
        return self.get(name, default).split('|')
Exemple #43
0
    def _load_config(self):
        "Load and parse config file, pass options to livestreamer"
        
        config = SafeConfigParser()
        config_file = os.path.join(self.config_path, 'settings.ini')
        config.read(config_file)

        for option, type in list(AVAILABLE_OPTIONS.items()):
            if config.has_option('DEFAULT', option):
                if type == 'int':
                    value = config.getint('DEFAULT', option)
                if type == 'float':
                    value = config.getfloat('DEFAULT', option)
                if type == 'bool':
                    value = config.getboolean('DEFAULT', option)
                if type == 'str':
                    value = config.get('DEFAULT', option)

                self.livestreamer.set_option(option, value)
Exemple #44
0
    def _get_user_default_db(self):
        """ Reads a user's notmuch config and returns his db location

        Throws a NotmuchError if it cannot find it"""
        try:
            # python3.x
            from configparser import SafeConfigParser
        except ImportError:
            # python2.x
            from ConfigParser import SafeConfigParser

        config = SafeConfigParser()
        conf_f = os.getenv('NOTMUCH_CONFIG',
                           os.path.expanduser('~/.notmuch-config'))
        config.read(conf_f)
        if not config.has_option('database', 'path'):
            raise NotmuchError(message="No DB path specified"
                               " and no user default found")
        return config.get('database', 'path').decode('utf-8')
Exemple #45
0
    def _load(self, location):
        """
        location -- URL or file location
        isxsd -- is this a xsd file?
        """
        cachedir = self._cachedir
        # wsdl2py: deal with XML Schema
        if not os.path.isdir(cachedir): os.mkdir(cachedir)

        _file = os.path.join(cachedir, '.cache')
        section = 'TYPES'
        cp = ConfigParser()
        try:
            cp.readfp(open(_file, 'r'))
        except IOError:
            del cp
            cp = None

        option = location.replace(':', '-')  # colons seem to screw up option
        if (not self._force and cp is not None and cp.has_section(section)
                and cp.has_option(section, option)):
            types = cp.get(section, option)
        else:
            # dont do anything to anames
            if not self._pyclass:
                containers.ContainerBase.func_aname = lambda instnc, n: str(n)

            args = ['-o', cachedir, location]
            if self._lazy: args.insert(0, '-l')
            if self._pyclass: args.insert(0, '-b')
            files = commands.wsdl2py(args)

            if cp is None: cp = ConfigParser()
            if not cp.has_section(section): cp.add_section(section)
            types = [f for f in files if f.endswith('_types.py')][0]
            cp.set(section, option, types)
            cp.write(open(_file, 'w'))

        if os.path.abspath(cachedir) not in sys.path:
            sys.path.append(os.path.abspath(cachedir))

        mod = os.path.split(types)[-1].rstrip('.py')
        return __import__(mod)
Exemple #46
0
    def _get_user_default_db(self):
        """ Reads a user's notmuch config and returns his db location

        Throws a NotmuchError if it cannot find it"""
        try:
            # python3.x
            from configparser import SafeConfigParser
        except ImportError:
            # python2.x
            from ConfigParser import SafeConfigParser

        config = SafeConfigParser()
        conf_f = os.getenv('NOTMUCH_CONFIG',
                           os.path.expanduser('~/.notmuch-config'))
        config.read(conf_f)
        if not config.has_option('database', 'path'):
            raise NotmuchError(message="No DB path specified"
                                       " and no user default found")
        return config.get('database', 'path').decode('utf-8')
Exemple #47
0
def _init(apiurl):
    conf_filename = os.environ.get('OSC_CONFIG', '~/.oscrc')
    conf_filename = os.path.expanduser(conf_filename)
    cp = SafeConfigParser({'plaintext_password': True, 'aliases': ''})
    cp.read(conf_filename)
    apiurl = apiurl.strip('/')
    if apiurl == 'api':
        apiurl = 'https://api.opensuse.org'
    for section in cp.sections():
        aliases = cp.get(section, 'aliases', raw=True)
        aliases = aliases.split(',')
        if section.strip('/') == apiurl or apiurl in aliases:
            user = cp.get(section, 'user', raw=True)
            password = cp.get(section, 'pass', raw=True)
            if cp.has_option(section, 'passx'):
                password = cp.get(section, 'pass', raw=True)
                password = password.decode('base64').decode('bz2')
            Osc.init(section, username=user, password=password)
            return section
Exemple #48
0
    def _load(self, location):
        """
        location -- URL or file location
        isxsd -- is this a xsd file?
        """
        cachedir = self._cachedir
        # wsdl2py: deal with XML Schema
        if not os.path.isdir(cachedir): os.mkdir(cachedir)

        _file = os.path.join(cachedir, '.cache')
        section = 'TYPES'
        cp = ConfigParser()
        try:
            cp.readfp(open(_file, 'r'))
        except IOError:
            del cp;  cp = None

        option = location.replace(':', '-') # colons seem to screw up option
        if (not self._force and cp is not None and cp.has_section(section) and
            cp.has_option(section, option)):
            types = cp.get(section, option)
        else:
            # dont do anything to anames
            if not self._pyclass:
                containers.ContainerBase.func_aname = lambda instnc,n: str(n)

            args = ['-o', cachedir, location]
            if self._lazy: args.insert(0, '-l')
            if self._pyclass: args.insert(0, '-b')
            files = commands.wsdl2py(args)

            if cp is None: cp = ConfigParser()
            if not cp.has_section(section): cp.add_section(section)
            types = [f for f in files if f.endswith('_types.py')][0]
            cp.set(section, option, types)
            cp.write(open(_file, 'w'))

        if os.path.abspath(cachedir) not in sys.path:
            sys.path.append(os.path.abspath(cachedir))

        mod = os.path.split(types)[-1].rstrip('.py')
        return __import__(mod)
Exemple #49
0
class _BugzillaTokenCache(object):
    """
    Cache for tokens, including, with apologies for the duplicative
    terminology, both Bugzilla Tokens and API Keys.
    """

    def __init__(self, uri, tokenfilename):
        self.tokenfilename = tokenfilename
        self.tokenfile = SafeConfigParser()
        self.domain = urlparse(uri)[1]

        if self.tokenfilename:
            self.tokenfile.read(self.tokenfilename)

        if self.domain not in self.tokenfile.sections():
            self.tokenfile.add_section(self.domain)

    @property
    def value(self):
        if self.tokenfile.has_option(self.domain, 'token'):
            return self.tokenfile.get(self.domain, 'token')
        else:
            return None

    @value.setter
    def value(self, value):
        if self.value == value:
            return

        if value is None:
            self.tokenfile.remove_option(self.domain, 'token')
        else:
            self.tokenfile.set(self.domain, 'token', value)

        if self.tokenfilename:
            with open(self.tokenfilename, 'w') as tokenfile:
                log.debug("Saving to tokenfile")
                self.tokenfile.write(tokenfile)

    def __repr__(self):
        return '<Bugzilla Token Cache :: %s>' % self.value
Exemple #50
0
    def __init__(self):
        super(MuninMySQLPlugin, self).__init__()
        self.default_name = "mobigate"
        self.dbname = ((sys.argv[0].rsplit('_', 1)[-1] if self.dbname_in_args else None)
                       or os.environ.get('DATABASE') or self.default_name)

        self.conninfo = dict(
            user="******",
            host="localhost",
            password="******",
            cursorclass=pymysql.cursors.DictCursor
        )

        cnfpath = ""

        m = re.findall(r"--defaults-file=([^\s]+)", os.environ.get("mysqlopts") or "")
        if m:
            cnfpath = m[0]

        if not cnfpath:
            m = re.findall(r"mysql_read_default_file=([^\s;:]+)", os.environ.get("mysqlconnection") or "")
            if m:
                cnfpath = m[0]

        if cnfpath:
            cnf = SafeConfigParser()
            cnf.read([cnfpath])
            for section in ["client", "munin"]:
                if not cnf.has_section(section):
                    continue
                for connkey, opt in [("user", "user"), ("passwd", "password"), ("host", "host"), ("port", "port")]:
                    if cnf.has_option(section, opt):
                        self.conninfo[connkey] = cnf.get(section, opt)

        for k in ('user', 'passwd', 'host', 'port'):
            # Use lowercase because that's what the existing mysql plugins do
            v = os.environ.get(k)
            if v:
                self.conninfo[k] = v
Exemple #51
0
class _BugzillaToken(object):
    def __init__(self, uri, tokenfilename):
        self.tokenfilename = tokenfilename
        self.tokenfile = SafeConfigParser()
        self.domain = urlparse(uri)[1]

        if self.tokenfilename:
            self.tokenfile.read(self.tokenfilename)

        if self.domain not in self.tokenfile.sections():
            self.tokenfile.add_section(self.domain)

    @property
    def value(self):
        if self.tokenfile.has_option(self.domain, 'token'):
            return self.tokenfile.get(self.domain, 'token')
        else:
            return None

    @value.setter
    def value(self, value):
        if self.value == value:
            return

        if value is None:
            self.tokenfile.remove_option(self.domain, 'token')
        else:
            self.tokenfile.set(self.domain, 'token', value)

        if self.tokenfilename:
            with open(self.tokenfilename, 'w') as tokenfile:
                log.debug("Saving to tokenfile")
                self.tokenfile.write(tokenfile)

    def __repr__(self):
        return '<Bugzilla Token :: %s>' % (self.value)
Exemple #52
0
class Configuration(BaseComponent):
    """
    This component provides a repository for configuration values.
    
    The component reads the initial configuration values from an
    ini-style configuration file when created. During application bootstrap,
    it intercepts the ``started`` event with a filter with  priority 999999.
    After receiving the ``started`` event, it fires all known configuration 
    values on the ``configuration`` channel as :class:`config_value` events.
    Then, it re-fires the intercepted started event.
     
    Components that depend on configuration values define handlers
    for ``config_value`` events on the ``configuration`` channel 
    and adapt themselves to the values propagated. Note that 
    due to the intercepted ``started`` event, the initial
    configuration events are received before the ``startup`` event, so
    components' configurations can be rearranged before they actually
    start doing something.
 
    Besides initially publishing the stored configuration values,
    :class:`Configuration` listens for :class:`config_value` events
    fired by other components, merges them with the already existing 
    configuration values and saves any changes to the configuration file.

    Other components that are capable of adjusting themselves to changed
    configuration values should, of course, continue to listen for
    :class:`config_value` events and adapt their behavior to the
    changed configuration if possible.
    
    If your application requires a different startup behavior, you 
    can also fire an :class:`emit_config` event or call
    method :meth:`emit_values`. This causes the 
    :class:`Configuration` to emit the configuration values 
    immediately. If this event is received before the ``started`` event, 
    the ``started`` event will not be intercepted.    
    """
    
    channel = "configuration"
    
    def __init__(self, filename, initial_config=None, 
                 defaults=None, channel=channel):
        """
        The constructor creates a new configuration using the given
        parameters.
        
        :param filename: the name of the file that is used to store the
                         configuration. If the file does not exist
                         it will be created.
        :type filename: string
        :param initial_config: a dict of name/section pairs, each section being
                               a dict of option/value pairs that is used
                               to initialize the configuration if no existing
                               configuration file is found
        :type initial_config: dict of dicts
        :param defaults: defaults passed to to the :class:`ConfigParser`
        :param channel: the channel to be used by this :class:`Configuration`
                        (defaults to "configuration")
        """
        super(Configuration, self).__init__(channel=channel)
        self._emit_done = False

        self._filename = filename
        self._config = SafeConfigParser(defaults=defaults)
        self._config.optionxform = str
        if os.path.exists(filename):
            self._config.read(filename)
        modified = False
        for section in initial_config:
            if not self._config.has_section(section):
                self._config.add_section(section)
                for option, value in initial_config[section].items():
                    if not self._config.has_option(section, option):
                        self._config.set(section, option, str(value))
                        modified = True
        if modified:
            with open(filename, "w") as f:
                self._config.write(f)

    def emit_values(self):
        """
        Fire all known configuration values as :class:`config_value`
        events.
        """
        for section in self._config.sections():
            for option in self._config.options(section):
                self.fire(config_value
                          (section, option, self._config.get(section, option)))
        self._emit_done = True

    @handler("emit_config")
    def _on_emit_config(self):
        self.emit_values()

    @handler("started", channel="*", filter=True, priority=999999)
    def _on_started(self, event, component):
        if not self._emit_done:
            self.emit_values()
            self.fire(event, *event.channels)
            return event.value

    @handler("config_value")
    def _on_config_value(self, section, option, value):
        if self._config.has_option(section, option):
            if self._config.get(section, option) == str(value):
                return
        if not self._config.has_section(section):
            self._config.add_section(section)
        self._config.set(section, option, str(value))
        with open(self._filename, "w") as f:
            self._config.write(f)

    def options(self, section):
        return self._config.options(section)

    def get(self, section, option, default=None):
        if self._config.has_option(section, option):
            return self._config.get(section, option)
        else:
            return default
def init():
    cont = False
    clear()

    credentials_from_file = False

    credentials = SafeConfigParser()

    if os.path.isfile("settings.txt") and os.name != "nt":
        os.system("chmod +r settings.txt")

    credentials.read("settings.txt")
    if credentials.has_option("main", "email") and credentials.has_option("main", "password"):
        credentials_from_file = True

    while not cont:
        driver.get("https://www.facebook.com/")

        if credentials_from_file:
            email = credentials.get("main", "email")
            password = credentials.get("main", "password")
        else:
            email = input("Email : ")
            password = getpass("Password : "******"input")
        inputs[1].send_keys(email)
        inputs[2].send_keys(password)
        driver.implicitly_wait(10)
        inputs[3].click()

        if str(driver.current_url).split("=")[0] == "https://www.facebook.com/login.php?login_attempt":
            clear()
            print("Invalid Email/Password")
            if credentials_from_file:
                print("Switching to manual input")
                credentials_from_file = False
        else:
            cont = True

    print("Loading...\n")

    if os.path.isfile("settings.txt") and os.name != "nt":
        os.system("chmod -r settings.txt")

    profile_url = [x for x in driver.find_elements_by_tag_name("a") if x.get_attribute("title") == "Profile"][
        0
    ].get_attribute("href")
    re_search = re.search(r"(\?id=\d+)$", profile_url)
    profile = ""

    if re_search:
        profile = re_search.group(0)
        profile = profile.replace("?id=", "")
    else:
        profile = profile_url[profile_url.rfind("/") + 1 :]

    driver.get("https://www.facebook.com/messages/" + profile)

    global replyButton
    replyButton = [x for x in driver.find_elements_by_tag_name("input") if x.get_attribute("value") == "Reply"][0]

    if not (replyButton.is_displayed()):
        driver.find_element_by_css_selector("._1s0").click()

    if os.path.isfile(os.getcwd() + "/commands.txt"):
        with open("commands.txt", "r") as foo:
            for a in foo.read().split("\n"):
                ls = a.split(" ")
                if len(ls) >= 2:
                    global customCommands
                    customCommands[ls[0]] = " ".join(ls[1:])

    print("\033[92mReady!\033[0m\n\n-------------- COMMANDS --------------")
Exemple #54
0
class GlobalSettings(GObject.Object):

    """
    Pitivi app settings.

    The settings object loads settings from different sources, currently:
    - the local configuration file,
    - environment variables.

    Modules declare which settings they wish to access by calling the
    addConfigOption() class method during initialization.

    @cvar options: A dictionnary of available settings.
    @cvar environment: A list of the controlled environment variables.
    """

    options = {}
    environment = set()
    defaults = {}

    __gsignals__ = {}

    def __init__(self, **unused_kwargs):
        GObject.Object.__init__(self)
        self._config = SafeConfigParser()
        self._readSettingsFromConfigurationFile()
        self._readSettingsFromEnvironmentVariables()

    def _readSettingsFromConfigurationFile(self):
        """
        Read the configuration from the user configuration file.
        """

        try:
            conf_file_path = os.path.join(xdg_config_home(), "pitivi.conf")
            self._config.read(conf_file_path)
        except UnicodeDecodeError:
            unicode_error_dialog()
            return
        except ParsingError:
            return

        for (section, attrname, typ, key, env, value) in self.iterAllOptions():
            if not self._config.has_section(section):
                continue
            if key and self._config.has_option(section, key):
                if typ == int or typ == int:
                    try:
                        value = self._config.getint(section, key)
                    except ValueError:
                        # In previous configurations we incorrectly stored
                        # ints using float values.
                        value = int(self._config.getfloat(section, key))
                elif typ == float:
                    value = self._config.getfloat(section, key)
                elif typ == bool:
                    value = self._config.getboolean(section, key)
                else:
                    value = self._config.get(section, key)
                setattr(self, attrname, value)

    @classmethod
    def readSettingSectionFromFile(self, cls, section):
        """
        Force reading a particular section of the settings file.

        Use this if you dynamically determine settings sections/keys at runtime
        (like in tabsmanager.py). Otherwise, the settings file would be read
        only once (at the initialization phase of your module) and your config
        sections would never be read, and thus values would be reset to defaults
        on every startup because GlobalSettings would think they don't exist.
        """
        if cls._config.has_section(section):
            for option in cls._config.options(section):
                # We don't know the value type in advance, just try them all.
                try:
                    value = cls._config.getfloat(section, option)
                except:
                    try:
                        value = cls._config.getint(section, option)
                    except:
                        try:
                            value = cls._config.getboolean(section, option)
                        except:
                            value = cls._config.get(section, option)

                setattr(cls, section + option, value)

    def _readSettingsFromEnvironmentVariables(self):
        """
        Override options values using their registered environment variables.
        """
        for section, attrname, typ, key, env, value in self.iterAllOptions():
            if not env:
                # This option does not have an environment variable name.
                continue
            var = get_env_by_type(typ, env)
            if var is not None:
                setattr(self, attrname, var)

    def _writeSettingsToConfigurationFile(self):
        conf_file_path = os.path.join(xdg_config_home(), "pitivi.conf")

        for (section, attrname, typ, key, env_var, value) in self.iterAllOptions():
            if not self._config.has_section(section):
                self._config.add_section(section)
            if key:
                if value is not None:
                    self._config.set(section, key, str(value))
                else:
                    self._config.remove_option(section, key)
        try:
            file = open(conf_file_path, 'w')
        except IOError as OSError:
            return
        self._config.write(file)
        file.close()

    def storeSettings(self):
        """
        Write settings to the user's local configuration file. Note that only
        those settings which were added with a section and a key value are
        stored.
        """
        self._writeSettingsToConfigurationFile()

    def iterAllOptions(self):
        """
        Iterate over all registered options

        @return: an iterator which yields a tuple of (attrname, type, key,
        environment, value) for each option.
        """
        for section, options in list(self.options.items()):
            for attrname, (typ, key, environment) in list(options.items()):
                yield section, attrname, typ, key, environment, getattr(self, attrname)

    def isDefault(self, attrname):
        return getattr(self, attrname) == self.defaults[attrname]

    def setDefault(self, attrname):
        setattr(self, attrname, self.defaults[attrname])

    @classmethod
    def addConfigOption(cls, attrname, type_=None, section=None, key=None,
                        environment=None, default=None, notify=False,):
        """
        Add a configuration option.

        This function should be called during module initialization, before
        the config file is actually read. By default, only options registered
        beforehand will be loaded.
        See mainwindow.py and medialibrary.py for examples of usage.

        If you want to add configuration options after initialization,
        use the readSettingSectionFromFile method to force reading later on.
        See tabsmanager.py for an example of such a scenario.

        @param attrname: the attribute of this class which represents the option
        @type attrname: C{str}
        @param type_: type of the attribute. Unnecessary if default is given.
        @type type_: a builtin or class
        @param section: The section of the config file under which this option is
        saved. This section must have been added with addConfigSection(). Not
        necessary if key is not given.
        @param key: the key under which this option is to be saved. Can be none if
        this option should not be saved.
        @type key: C{str}
        @param notify: whether or not this attribute should emit notification
        signals when modified (default is False).
        @type notify: C{boolean}
        """
        if section and section not in cls.options:
            raise ConfigError(
                "You must add the section \"%s\" first." % section)
        if key and not section:
            raise ConfigError(
                "You must specify a section for key \"%s\"" % key)
        if section and key in cls.options[section]:
            raise ConfigError("Option \"%s\" is already in use.")
        if hasattr(cls, attrname):
            raise ConfigError("Settings attribute \"%s\" is already in use.")
        if environment and environment in cls.environment:
            raise ConfigError("Settings environment varaible \"%s\" is"
                              "already in use.")
        if not type_ and default is None:
            raise ConfigError("Settings attribute \"%s\" has must have a"
                              " type or a default." % attrname)
        if not type_:
            type_ = type(default)
        if notify:
            notification = Notification(attrname)
            setattr(cls, attrname, notification)
            setattr(cls, "_" + attrname, default)
            GObject.signal_new(notification.signame,
                               cls,
                               GObject.SIGNAL_RUN_LAST,
                               None,
                               ())
        else:
            setattr(cls, attrname, default)
        if section and key:
            cls.options[section][attrname] = type_, key, environment
        cls.environment.add(environment)
        cls.defaults[attrname] = default

    @classmethod
    def addConfigSection(cls, section):
        """
        Add a section to the local config file.

        @param section: The section name. This section must not already exist.
        @type section: C{str}
        """
        if section in cls.options:
            raise ConfigError("Duplicate Section \"%s\"." % section)
        cls.options[section] = {}

    @classmethod
    def notifiesConfigOption(cls, attrname):
        signal_name = Notification.signalName(attrname)
        GObject.signal_lookup(signal_name, cls)
Exemple #55
0
def run(fh=None):
	print """\
pagerprinter v0.1.3+
Copyright 2010 - 2015 Michael Farrell <http://micolous.id.au/>
"""
	# parse config
	c = SafeConfigParser()
	c.read_dict({
		'pagerprinter': {
			'update-freq': '30',
			'backend': 'sacfs',
			'browser': 'firefox',
			'browser-exec': 'firefox',
			'browser-wait': '20',
			'trigger': 'RESPOND',
			'trigger-end': 'MAP',
			'mapper': 'google',
			'print-copies': '1',
			'unit': 'all',
			'home': '141 King William Street, Adelaide SA 5000',
		},
	})

	if fh is not None:
		c.readfp(fh)

	# get a scraper instance
	scraper = get_scraper(
		c.get('pagerprinter', 'backend')
	)(
		c.getint('pagerprinter', 'update-freq')
	)

	# get a browser helper instance
	browser = get_browser(
		c.get('pagerprinter', 'browser')
	)(
		c.get('pagerprinter', 'browser-exec'),
		c.getint('pagerprinter', 'browser-wait')
	)

	trigger = c.get('pagerprinter', 'trigger').lower().strip()
	trigger_end = c.get('pagerprinter', 'trigger-end').lower().strip()
	my_unit = c.get('pagerprinter', 'unit').lower().strip()

	try:
		printer = c.get('pagerprinter', 'printer')
	except NoOptionError:
		printer = None
	print_copies = c.getint('pagerprinter', 'print-copies')
	if print_copies < 1:
		print "ERROR: print-copies is set to less than one.  You probably don't want this."
		return
	mapper = c.get('pagerprinter', 'mapper')

	plugins = []
	if c.has_option('pagerprinter', 'plugins'):
		plugins = [
			get_plugin(x.strip())
			for x
			in c.get('pagerprinter', 'plugins').lower().split(',')
		]

		for plugin in plugins:
			plugin.configure(c)

	mapper = get_map(mapper)

	# special case: all units.
	# may result in dupe printouts
	if my_unit == 'all':
		my_unit = ''

	# home
	home = c.get('pagerprinter', 'home')

	# now, lets setup a handler for these events.
	def page_handler(good_parse, msg, date=None, unit=None):
		if good_parse:
			print "Good parse! %s - %s" % (repr(msg), unit)
			# filter for unit
			if my_unit in unit.lower():
				# this is for me!!!
				print "- This is a message for my unit!"
				# check for trigger
				lmsg = msg.lower()
				if trigger in lmsg:
					# trigger found
					# split by trigger and find address nicely.
					addr = lmsg.split(trigger)[1]

					if trigger_end in lmsg:
						addr = addr.split(trigger_end)[0]

						# Remove the @ symbols in the message, and the ASE device number (#nnn/nnn)
						addr = re.sub(r'#\d{3}/\d{3}|@|\s:\s', '', addr)

						# now split that up into parts, discarding the first
						# which is a description of the event
						addr_p = addr.split(',')[-2:]

						# clone the list for iteration as we well modify in this operation as well
						for i, part in enumerate(list(addr_p)):
							if 'alarm level' in part:
								del addr_p[i]
								break

						# reassemble the address
						addr = ','.join(addr_p)
						del addr_p

						# we have an address.  feed it to the mapping engine
						url = mapper.get_url(home, addr)

						print "- Address: %s" % addr
						print "- URL for directions: %s" % url

						# sending to browser
						browser.print_url(url, printer, print_copies)

						# now, send to plugins
						for plugin in plugins:
							try:
								plugin.execute(msg, unit, addr, date, printer, print_copies)
							except Exception, e:
								print "Exception caught in plugin %s" % type(plugin)
								print e
					else:
						print "- WARNING: End trigger not found!  Skipping..."
				else:
					print "- Trigger not found.  Skipping..."
			else:
				print "- This isn't for my unit.  Skipping..."
		else:
def main(argv):
    options, args = parse_options(argv)

    if not os.path.exists('debian/control'):
        error_exit('must be run from the top level of a source package')
    this_source = None
    with open('debian/control') as control:
        for line in control:
            if line.startswith('Source:'):
                this_source = line[7:].strip()
                break
            elif line == '':
                break
    if this_source is None:
        error_exit('cannot find Source: in debian/control')
    if not this_source.endswith('-meta'):
        error_exit('source package name must be *-meta')
    metapackage = this_source[:-5]

    print("[info] Initialising %s-* package lists update..." % metapackage)

    config = SafeConfigParser()
    with open('update.cfg') as config_file:
        try:
            # >= 3.2
            config.read_file(config_file)
        except AttributeError:
            # < 3.2
            config.readfp(config_file)

    if len(args) > 0:
        dist = args[0]
    else:
        dist = config.get('DEFAULT', 'dist')

    seeds = config.get(dist, 'seeds').split()
    try:
        output_seeds = config.get(dist, 'output_seeds').split()
    except NoOptionError:
        output_seeds = list(seeds)
    architectures = config.get(dist, 'architectures').split()
    try:
        archive_base_default = config.get(dist, 'archive_base/default')
        archive_base_default = re.split(r'[, ]+', archive_base_default)
    except (NoSectionError, NoOptionError):
        archive_base_default = None

    archive_base = {}
    for arch in architectures:
        try:
            archive_base[arch] = config.get(dist, 'archive_base/%s' % arch)
            archive_base[arch] = re.split(r'[, ]+', archive_base[arch])
        except (NoSectionError, NoOptionError):
            if archive_base_default is not None:
                archive_base[arch] = archive_base_default
            else:
                error_exit('no archive_base configured for %s' % arch)

    if options.bzr and config.has_option("%s/bzr" % dist, 'seed_base'):
        seed_base = config.get("%s/bzr" % dist, 'seed_base')
    else:
        seed_base = config.get(dist, 'seed_base')
    seed_base = re.split(r'[, ]+', seed_base)
    if options.bzr and config.has_option("%s/bzr" % dist, 'seed_dist'):
        seed_dist = config.get("%s/bzr" % dist, 'seed_dist')
    elif config.has_option(dist, 'seed_dist'):
        seed_dist = config.get(dist, 'seed_dist')
    else:
        seed_dist = dist
    if config.has_option(dist, 'dists'):
        dists = config.get(dist, 'dists').split()
    else:
        dists = [dist]
    try:
        archive_exceptions = config.get(dist, 'archive_base/exceptions').split()
    except (NoSectionError, NoOptionError):
        archive_exceptions = []

    components = config.get(dist, 'components').split()

    def seed_packages(germinator_method, structure, seed_name):
        if config.has_option(dist, "seed_map/%s" % seed_name):
            mapped_seeds = config.get(dist, "seed_map/%s" % seed_name).split()
        else:
            mapped_seeds = []
            task_seeds_re = re.compile('^Task-Seeds:\s*(.*)', re.I)
            with structure[seed_name] as seed:
                for line in seed:
                    task_seeds_match = task_seeds_re.match(line)
                    if task_seeds_match is not None:
                        mapped_seeds = task_seeds_match.group(1).split()
                        break
            if seed_name not in mapped_seeds:
                mapped_seeds.append(seed_name)
        packages = []
        for mapped_seed in mapped_seeds:
            packages.extend(germinator_method(structure, mapped_seed))
        return packages

    def metapackage_name(structure, seed_name):
        if config.has_option(dist, "metapackage_map/%s" % seed_name):
            return config.get(dist, "metapackage_map/%s" % seed_name)
        else:
            task_meta_re = re.compile('^Task-Metapackage:\s*(.*)', re.I)
            with structure[seed_name] as seed:
                for line in seed:
                    task_meta_match = task_meta_re.match(line)
                    if task_meta_match is not None:
                        return task_meta_match.group(1)
            return "%s-%s" % (metapackage, seed_name)

    debootstrap_version_file = 'debootstrap-version'

    def get_debootstrap_version():
        version_cmd = subprocess.Popen(
            ['dpkg-query', '-W', '--showformat', '${Version}', 'debootstrap'],
            stdout=subprocess.PIPE, universal_newlines=True)
        version, _ = version_cmd.communicate()
        if not version:
            error_exit('debootstrap does not appear to be installed')

        return version

    def debootstrap_packages(arch):
        env = dict(os.environ)
        if 'PATH' in env:
            env['PATH'] = '/usr/sbin:/sbin:%s' % env['PATH']
        else:
            env['PATH'] = '/usr/sbin:/sbin:/usr/bin:/bin'
        debootstrap = subprocess.Popen(
            ['debootstrap', '--arch', arch,
             '--components', ','.join(components),
             '--print-debs', dist, 'debootstrap-dir', archive_base[arch][0]],
            stdout=subprocess.PIPE, env=env, stderr=subprocess.PIPE,
            universal_newlines=True)
        (debootstrap_stdout, debootstrap_stderr) = debootstrap.communicate()
        if debootstrap.returncode != 0:
            error_exit('Unable to retrieve package list from debootstrap; '
                       'stdout: %s\nstderr: %s' %
                       (debootstrap_stdout, debootstrap_stderr))

        # sometimes debootstrap gives empty packages / multiple separators
        packages = [pkg for pkg in debootstrap_stdout.split() if pkg]

        return sorted(packages)

    def check_debootstrap_version():
        if os.path.exists(debootstrap_version_file):
            with open(debootstrap_version_file) as debootstrap:
                old_debootstrap_version = debootstrap.read().strip()
            debootstrap_version = get_debootstrap_version()
            failed = subprocess.call(
                ['dpkg', '--compare-versions',
                 debootstrap_version, 'ge', old_debootstrap_version])
            if failed:
                error_exit('Installed debootstrap is older than in the '
                           'previous version! (%s < %s)' %
                           (debootstrap_version, old_debootstrap_version))

    def update_debootstrap_version():
        with open(debootstrap_version_file, 'w') as debootstrap:
            debootstrap.write(get_debootstrap_version() + '\n')

    def format_changes(items):
        by_arch = defaultdict(set)
        for pkg, arch in items:
            by_arch[pkg].add(arch)
        all_pkgs = sorted(by_arch)
        chunks = []
        for pkg in all_pkgs:
            arches = by_arch[pkg]
            if set(architectures) - arches:
                # only some architectures
                chunks.append('%s [%s]' % (pkg, ' '.join(sorted(arches))))
            else:
                # all architectures
                chunks.append(pkg)
        return ', '.join(chunks)

    germinate_logging(logging.DEBUG)

    check_debootstrap_version()

    additions = defaultdict(list)
    removals = defaultdict(list)
    moves = defaultdict(list)
    metapackage_map = {}
    for architecture in architectures:
        print("[%s] Downloading available package lists..." % architecture)
        germinator = Germinator(architecture)
        archive = germinate.archive.TagFile(
            dists, components, architecture,
            archive_base[architecture], source_mirrors=archive_base_default,
            cleanup=True, archive_exceptions=archive_exceptions)
        germinator.parse_archive(archive)
        debootstrap_base = set(debootstrap_packages(architecture))

        print("[%s] Loading seed lists..." % architecture)
        try:
            structure = SeedStructure(seed_dist, seed_base, options.bzr)
            germinator.plant_seeds(structure, seeds=seeds)
        except SeedError:
            sys.exit(1)

        print("[%s] Merging seeds with available package lists..." %
              architecture)
        for seed_name in output_seeds:
            meta_name = metapackage_name(structure, seed_name)
            metapackage_map[seed_name] = meta_name

            output_filename = os.path.join(
                options.outdir, '%s-%s' % (seed_name, architecture))
            old_list = None
            if os.path.exists(output_filename):
                with open(output_filename) as output:
                    old_list = set(map(str.strip, output.readlines()))
                os.rename(output_filename, output_filename + '.old')

            # work on the depends
            new_list = []
            packages = seed_packages(germinator.get_seed_entries,
                                     structure, seed_name)
            for package in packages:
                if package == meta_name:
                    print("%s/%s: Skipping package %s (metapackage)" %
                          (seed_name, architecture, package))
                elif (seed_name == 'minimal' and
                      package not in debootstrap_base):
                    print("%s/%s: Skipping package %s (package not in "
                          "debootstrap)" % (seed_name, architecture, package))
                elif germinator.is_essential(package):
                    print("%s/%s: Skipping package %s (essential)" %
                          (seed_name, architecture, package))
                else:
                    new_list.append(package)

            new_list.sort()
            with open(output_filename, 'w') as output:
                for package in new_list:
                    output.write(package)
                    output.write('\n')

            # work on the recommends
            old_recommends_list = None
            new_recommends_list = []
            packages = seed_packages(germinator.get_seed_recommends_entries,
                                     structure, seed_name)
            for package in packages:
                if package == meta_name:
                    print("%s/%s: Skipping package %s (metapackage)" %
                          (seed_name, architecture, package))
                    continue
                if seed_name == 'minimal' and package not in debootstrap_base:
                    print("%s/%s: Skipping package %s (package not in "
                          "debootstrap)" % (seed_name, architecture, package))
                else:
                    new_recommends_list.append(package)

            new_recommends_list.sort()
            seed_name_recommends = '%s-recommends' % seed_name
            output_recommends_filename = os.path.join(
                options.outdir, '%s-%s' % (seed_name_recommends, architecture))
            if os.path.exists(output_recommends_filename):
                with open(output_recommends_filename) as output:
                    old_recommends_list = set(
                        map(str.strip, output.readlines()))
                os.rename(
                    output_recommends_filename,
                    output_recommends_filename + '.old')

            with open(output_recommends_filename, 'w') as output:
                for package in new_recommends_list:
                    output.write(package)
                    output.write('\n')

            # Calculate deltas
            merged = defaultdict(int)
            recommends_merged = defaultdict(int)
            if old_list is not None:
                for package in new_list:
                    merged[package] += 1
                for package in old_list:
                    merged[package] -= 1
            if old_recommends_list is not None:
                for package in new_recommends_list:
                    recommends_merged[package] += 1
                for package in old_recommends_list:
                    recommends_merged[package] -= 1

            mergeditems = sorted(merged.items())
            for package, value in mergeditems:
                #print(package, value)
                if value == 1:
                    if recommends_merged.get(package, 0) == -1:
                        moves[package].append([seed_name, architecture])
                        recommends_merged[package] += 1
                    else:
                        additions[package].append([seed_name, architecture])
                elif value == -1:
                    if recommends_merged.get(package, 0) == 1:
                        moves[package].append([seed_name_recommends,
                                               architecture])
                        recommends_merged[package] -= 1
                    else:
                        removals[package].append([seed_name, architecture])

            mergedrecitems = sorted(recommends_merged.items())
            for package, value in mergedrecitems:
                #print(package, value)
                if value == 1:
                    additions[package].append([seed_name_recommends,
                                               architecture])
                elif value == -1:
                    removals[package].append([seed_name_recommends,
                                              architecture])

    with open('metapackage-map', 'w') as metapackage_map_file:
        for seed_name in output_seeds:
            print(seed_name, metapackage_map[seed_name],
                  file=metapackage_map_file)

    if not options.nodch and (additions or removals or moves):
        dch_help = subprocess.Popen(['dch', '--help'], stdout=subprocess.PIPE,
                                    universal_newlines=True)
        try:
            have_U = '-U' in dch_help.stdout.read()
        finally:
            if dch_help.stdout:
                dch_help.stdout.close()
            dch_help.wait()
        if have_U:
            subprocess.check_call(['dch', '-iU', 'Refreshed dependencies'])
        else:
            subprocess.check_call(['dch', '-i', 'Refreshed dependencies'])
        changes = []
        for package in sorted(additions):
            changes.append('Added %s to %s' %
                           (package, format_changes(additions[package])))
        for package in sorted(removals):
            changes.append('Removed %s from %s' %
                           (package, format_changes(removals[package])))
        for package in sorted(moves):
            # TODO: We should really list where it moved from as well, but
            # that gets wordy very quickly, and at the moment this is only
            # implemented for depends->recommends or vice versa. In future,
            # using this for moves between seeds might also be useful.
            changes.append('Moved %s to %s' %
                           (package, format_changes(moves[package])))
        for change in changes:
            print(change)
            subprocess.check_call(['dch', '-a', change])
        update_debootstrap_version()
    else:
        if not options.nodch:
            print("No changes found")

    return 0
Exemple #57
0
class Config(object):
	def __init__(self, config_dir=None, config_file=None):
		self.config_dir = config_dir
		self.config_file = config_file

		if config_dir is None:
			self.config_dir = os.path.join(os.environ.get('HOME', './'), '.pyhn')
		if config_file is None:
			self.config_file = "config"

		if not os.path.exists(self.config_dir):
			os.makedirs(self.config_dir)

		self.config_path = os.path.join(self.config_dir, self.config_file)

		self.parser = SafeConfigParser()
		self.read()

	def read(self):
		self.parser.read(self.config_path)

		# Keybindings
		if not self.parser.has_section('keybindings'):
			self.parser.add_section('keybindings')

		if not self.parser.has_option('keybindings', 'page_up'):
			self.parser.set('keybindings', 'page_up', 'ctrl d')
		if not self.parser.has_option('keybindings', 'page_down'):
			self.parser.set('keybindings', 'page_down', 'ctrl u')
		if not self.parser.has_option('keybindings', 'up'):
			self.parser.set('keybindings', 'up', 'j')
		if not self.parser.has_option('keybindings', 'down'):
			self.parser.set('keybindings', 'down', 'k')
		if not self.parser.has_option('keybindings', 'refresh'):
			self.parser.set('keybindings', 'refresh', 'r,R')
		if not self.parser.has_option('keybindings', 'open_comments'):
			self.parser.set('keybindings', 'open_comments', 'c,C')
		if not self.parser.has_option('keybindings', 'reload_config'):
			self.parser.set('keybindings', 'reload_config', 'ctrl r,ctrl R')

		# Paths
		if not self.parser.has_section('settings'):
			self.parser.add_section('settings')

		if not self.parser.has_option('settings', 'cache'):
			self.parser.set('settings', 'cache', os.path.join(os.environ.get('HOME', './'), '.pyhn', 'cache'))

		# Colors
		if not self.parser.has_section('colors'):
			self.parser.add_section('colors')

		if not self.parser.has_option('colors', 'body'):
			self.parser.set('colors', 'body', 'default||standout')
		if not self.parser.has_option('colors', 'focus'):
			self.parser.set('colors', 'focus', 'black|light green|underline')
		if not self.parser.has_option('colors', 'footer'):
			self.parser.set('colors', 'footer', 'black|light gray')
		if not self.parser.has_option('colors', 'header'):
			self.parser.set('colors', 'header', 'dark gray,bold|white|')
		if not self.parser.has_option('colors', 'title'):
			self.parser.set('colors', 'title', 'dark red,bold|light gray')
		if not self.parser.has_option('colors', 'help'):
			self.parser.set('colors', 'help', 'black,bold|light gray')

		if not os.path.exists(self.config_path):
			self.parser.write(open(self.config_path, 'w'))

	def get_palette(self):
		palette = []
		for item in self.parser.items('colors'):
			name = item[0]
			settings = item[1]
			foreground = ""
			background = ""
			monochrome = ""
			if len(settings.split('|')) == 3:
				foreground = settings.split('|')[0]
				background = settings.split('|')[1]
				monochrome = settings.split('|')[2]
			elif len(settings.split('|')) == 2:
				foreground = settings.split('|')[0]
				background = settings.split('|')[1]
			elif len(settings.split('|')) == 1:
				foreground = settings.split('|')[0]

			palette.append((name,foreground,background,monochrome))
		return palette
Exemple #58
0
class Buildozer(object):

    standard_cmds = ('distclean', 'update', 'debug', 'release',
                     'deploy', 'run', 'serve')

    def __init__(self, filename='buildozer.spec', target=None):
        super(Buildozer, self).__init__()
        self.log_level = 1
        self.environ = {}
        self.specfilename = filename
        self.state = None
        self.build_id = None
        self.config_profile = ''
        self.config = SafeConfigParser(allow_no_value=True)
        self.config.optionxform = lambda value: value
        self.config.getlist = self._get_config_list
        self.config.getlistvalues = self._get_config_list_values
        self.config.getdefault = self._get_config_default
        self.config.getbooldefault = self._get_config_bool

        if exists(filename):
            self.config.read(filename)
            self.check_configuration_tokens()

        # Check all section/tokens for env vars, and replace the
        # config value if a suitable env var exists.
        set_config_from_envs(self.config)

        try:
            self.log_level = int(self.config.getdefault(
                'buildozer', 'log_level', '1'))
        except:
            pass
        self.builddir = self.config.getdefault('buildozer', 'builddir', None)

        self.targetname = None
        self.target = None
        if target:
            self.set_target(target)

    def set_target(self, target):
        '''Set the target to use (one of buildozer.targets, such as "android")
        '''
        self.targetname = target
        m = __import__('buildozer.targets.{0}'.format(target),
                fromlist=['buildozer'])
        self.target = m.get_target(self)
        self.check_build_layout()
        self.check_configuration_tokens()

    def prepare_for_build(self):
        '''Prepare the build.
        '''
        assert(self.target is not None)
        if hasattr(self.target, '_build_prepared'):
            return

        self.info('Preparing build')

        self.info('Check requirements for {0}'.format(self.targetname))
        self.target.check_requirements()

        self.info('Install platform')
        self.target.install_platform()

        self.info('Check application requirements')
        self.check_application_requirements()

        self.info('Check garden requirements')
        self.check_garden_requirements()

        self.info('Compile platform')
        self.target.compile_platform()

        # flag to prevent multiple build
        self.target._build_prepared = True

    def build(self):
        '''Do the build.

        The target can set build_mode to 'release' or 'debug' before calling
        this method.

        (:meth:`prepare_for_build` must have been call before.)
        '''
        assert(self.target is not None)
        assert(hasattr(self.target, '_build_prepared'))

        if hasattr(self.target, '_build_done'):
            return

        # increment the build number
        self.build_id = int(self.state.get('cache.build_id', '0')) + 1
        self.state['cache.build_id'] = str(self.build_id)

        self.info('Build the application #{}'.format(self.build_id))
        self.build_application()

        self.info('Package the application')
        self.target.build_package()

        # flag to prevent multiple build
        self.target._build_done = True

    #
    # Log functions
    #

    def log(self, level, msg):
        if level > self.log_level:
            return
        if USE_COLOR:
            color = COLOR_SEQ(LOG_LEVELS_C[level])
            print(''.join((RESET_SEQ, color, '# ', msg, RESET_SEQ)))
        else:
            print('{} {}'.format(LOG_LEVELS_T[level], msg))

    def debug(self, msg):
        self.log(2, msg)

    def info(self, msg):
        self.log(1, msg)

    def error(self, msg):
        self.log(0, msg)

    #
    # Internal check methods
    #

    def checkbin(self, msg, fn):
        self.debug('Search for {0}'.format(msg))
        if exists(fn):
            return realpath(fn)
        for dn in environ['PATH'].split(':'):
            rfn = realpath(join(dn, fn))
            if exists(rfn):
                self.debug(' -> found at {0}'.format(rfn))
                return rfn
        self.error('{} not found, please install it.'.format(msg))
        exit(1)

    def cmd(self, command, **kwargs):
        # prepare the environ, based on the system + our own env
        env = copy(environ)
        env.update(self.environ)

        # prepare the process
        kwargs.setdefault('env', env)
        kwargs.setdefault('stdout', PIPE)
        kwargs.setdefault('stderr', PIPE)
        kwargs.setdefault('close_fds', True)
        kwargs.setdefault('shell', True)
        kwargs.setdefault('show_output', self.log_level > 1)

        show_output = kwargs.pop('show_output')
        get_stdout = kwargs.pop('get_stdout', False)
        get_stderr = kwargs.pop('get_stderr', False)
        break_on_error = kwargs.pop('break_on_error', True)
        sensible = kwargs.pop('sensible', False)

        if not sensible:
            self.debug('Run {0!r}'.format(command))
        else:
            if type(command) in (list, tuple):
                self.debug('Run {0!r} ...'.format(command[0]))
            else:
                self.debug('Run {0!r} ...'.format(command.split()[0]))
        self.debug('Cwd {}'.format(kwargs.get('cwd')))

        # open the process
        if sys.platform == 'win32':
            kwargs.pop('close_fds', None)
        process = Popen(command, **kwargs)

        # prepare fds
        fd_stdout = process.stdout.fileno()
        fd_stderr = process.stderr.fileno()
        if fcntl:
            fcntl.fcntl(
                fd_stdout, fcntl.F_SETFL,
                fcntl.fcntl(fd_stdout, fcntl.F_GETFL) | os.O_NONBLOCK)
            fcntl.fcntl(
                fd_stderr, fcntl.F_SETFL,
                fcntl.fcntl(fd_stderr, fcntl.F_GETFL) | os.O_NONBLOCK)

        ret_stdout = [] if get_stdout else None
        ret_stderr = [] if get_stderr else None
        while True:
            try:
                readx = select.select([fd_stdout, fd_stderr], [], [])[0]
            except select.error:
                break
            if fd_stdout in readx:
                chunk = process.stdout.read()
                if not chunk:
                    break
                if get_stdout:
                    ret_stdout.append(chunk)
                if show_output:
                    if IS_PY3:
                        stdout.write(chunk.decode('utf-8'))
                    else:
                        stdout.write(chunk)
            if fd_stderr in readx:
                chunk = process.stderr.read()
                if not chunk:
                    break
                if get_stderr:
                    ret_stderr.append(chunk)
                if show_output:
                    if IS_PY3:
                        stderr.write(chunk.decode('utf-8'))
                    else:
                        stderr.write(chunk)

        stdout.flush()
        stderr.flush()

        process.communicate()
        if process.returncode != 0 and break_on_error:
            self.error('Command failed: {0}'.format(command))
            self.error('')
            self.error('Buildozer failed to execute the last command')
            if self.log_level <= 1:
                self.error('If the error is not obvious, please raise the log_level to 2')
                self.error('and retry the latest command.')
            else:
                self.error('The error might be hidden in the log above this error')
                self.error('Please read the full log, and search for it before')
                self.error('raising an issue with buildozer itself.')
            self.error('In case of a bug report, please add a full log with log_level = 2')
            raise BuildozerCommandException()
        if ret_stdout:
            ret_stdout = b''.join(ret_stdout)
        if ret_stderr:
            ret_stderr = b''.join(ret_stderr)
        return (ret_stdout.decode('utf-8', 'ignore') if ret_stdout else None,
                ret_stderr.decode('utf-8') if ret_stderr else None,
                process.returncode)

    def cmd_expect(self, command, **kwargs):
        from pexpect import spawnu

        # prepare the environ, based on the system + our own env
        env = copy(environ)
        env.update(self.environ)

        # prepare the process
        kwargs.setdefault('env', env)
        kwargs.setdefault('show_output', self.log_level > 1)
        sensible = kwargs.pop('sensible', False)
        show_output = kwargs.pop('show_output')

        if show_output:
            kwargs['logfile'] = codecs.getwriter('utf8')(stdout)

        if not sensible:
            self.debug('Run (expect) {0!r}'.format(command))
        else:
            self.debug('Run (expect) {0!r} ...'.format(command.split()[0]))

        self.debug('Cwd {}'.format(kwargs.get('cwd')))
        return spawnu(command, **kwargs)

    def check_configuration_tokens(self):
        '''Ensure the spec file is 'correct'.
        '''
        self.info('Check configuration tokens')
        get = self.config.getdefault
        errors = []
        adderror = errors.append
        if not get('app', 'title', ''):
            adderror('[app] "title" is missing')
        if not get('app', 'source.dir', ''):
            adderror('[app] "source.dir" is missing')

        package_name = get('app', 'package.name', '')
        if not package_name:
            adderror('[app] "package.name" is missing')
        elif package_name[0] in map(str, range(10)):
            adderror('[app] "package.name" may not start with a number.')

        version = get('app', 'version', '')
        version_regex = get('app', 'version.regex', '')
        if not version and not version_regex:
            adderror('[app] One of "version" or "version.regex" must be set')
        if version and version_regex:
            adderror('[app] Conflict between "version" and "version.regex"'
                    ', only one can be used.')
        if version_regex and not get('app', 'version.filename', ''):
            adderror('[app] "version.filename" is missing'
                ', required by "version.regex"')

        orientation = get('app', 'orientation', 'landscape')
        if orientation not in ('landscape', 'portrait', 'all'):
            adderror('[app] "orientation" have an invalid value')

        if errors:
            self.error('{0} error(s) found in the buildozer.spec'.format(
                len(errors)))
            for error in errors:
                print(error)
            exit(1)


    def check_build_layout(self):
        '''Ensure the build (local and global) directory layout and files are
        ready.
        '''
        self.info('Ensure build layout')

        if not exists(self.specfilename):
            print('No {0} found in the current directory. Abandon.'.format(
                    self.specfilename))
            exit(1)

        # create global dir
        self.mkdir(self.global_buildozer_dir)
        self.mkdir(self.global_cache_dir)

        # create local dir
        specdir = dirname(self.specfilename)
        if self.builddir:
            specdir = self.builddir

        self.mkdir(join(specdir, '.buildozer'))
        self.mkdir(join(specdir, 'bin'))
        self.mkdir(self.applibs_dir)
        self.state = JsonStore(join(self.buildozer_dir, 'state.db'))

        if self.targetname:
            target = self.targetname
            self.mkdir(join(self.global_platform_dir, target, 'platform'))
            self.mkdir(join(specdir, '.buildozer', target, 'platform'))
            self.mkdir(join(specdir, '.buildozer', target, 'app'))

    def check_application_requirements(self):
        '''Ensure the application requirements are all available and ready to be
        packaged as well.
        '''
        requirements = self.config.getlist('app', 'requirements', '')
        target_available_packages = self.target.get_available_packages()

        # remove all the requirements that the target can compile
        onlyname = lambda x: x.split('==')[0]
        requirements = [x for x in requirements if onlyname(x) not in
                target_available_packages]

        # did we already installed the libs ?
        if exists(self.applibs_dir) and \
            self.state.get('cache.applibs', '') == requirements:
                self.debug('Application requirements already installed, pass')
                return

        # recreate applibs
        self.rmdir(self.applibs_dir)
        self.mkdir(self.applibs_dir)

        # ok now check the availability of all requirements
        for requirement in requirements:
            self._install_application_requirement(requirement)

        # everything goes as expected, save this state!
        self.state['cache.applibs'] = requirements

    def _install_application_requirement(self, module):
        self._ensure_virtualenv()
        # resetup distribute, just in case
        self.debug('Install distribute')
        self.cmd('curl http://python-distribute.org/distribute_setup.py | venv/bin/python', get_stdout=True, cwd=self.buildozer_dir)

        self.debug('Install requirement {} in virtualenv'.format(module))
        self.cmd('pip install --download-cache={} --target={} {}'.format(
                self.global_cache_dir, self.applibs_dir, module),
                env=self.env_venv,
                cwd=self.buildozer_dir)

    def check_garden_requirements(self):
        '''Ensure required garden packages are available to be included.
        '''
        garden_requirements = self.config.getlist('app',
                'garden_requirements', '')

        # have we installed the garden packages?
        if exists(self.gardenlibs_dir) and \
                self.state.get('cache.gardenlibs', '') == garden_requirements:
            self.debug('Garden requirements already installed, pass')
            return

        # we're going to reinstall all the garden libs.
        self.rmdir(self.gardenlibs_dir)

        # but if we don't have requirements, or if the user removed everything,
        # don't do anything.
        if not garden_requirements:
            self.state['cache.gardenlibs'] = garden_requirements
            return

        self._ensure_virtualenv()
        self.cmd('pip install Kivy-Garden==0.1.1', env=self.env_venv)

        # recreate gardenlibs
        self.mkdir(self.gardenlibs_dir)

        for requirement in garden_requirements:
            self._install_garden_package(requirement)

        # save gardenlibs state
        self.state['cache.gardenlibs'] = garden_requirements

    def _install_garden_package(self, package):
        self._ensure_virtualenv()
        self.debug('Install garden package {} in buildozer_dir'.format(package))
        self.cmd('garden install --app {}'.format(package),
                env=self.env_venv,
                cwd=self.buildozer_dir)

    def _ensure_virtualenv(self):
        if hasattr(self, 'venv'):
            return
        self.venv = join(self.buildozer_dir, 'venv')
        if not self.file_exists(self.venv):
            self.cmd('virtualenv --python=python2.7 ./venv',
                    cwd=self.buildozer_dir)

        # read virtualenv output and parse it
        output = self.cmd('bash -c "source venv/bin/activate && env"',
                get_stdout=True,
                cwd=self.buildozer_dir)
        self.env_venv = copy(self.environ)
        for line in output[0].splitlines():
            args = line.split('=', 1)
            if len(args) != 2:
                continue
            key, value = args
            if key in ('VIRTUAL_ENV', 'PATH'):
                self.env_venv[key] = value
        if 'PYTHONHOME' in self.env_venv:
            del self.env_venv['PYTHONHOME']

        # ensure any sort of compilation will fail
        self.env_venv['CC'] = '/bin/false'
        self.env_venv['CXX'] = '/bin/false'

    def mkdir(self, dn):
        if exists(dn):
            return
        self.debug('Create directory {0}'.format(dn))
        makedirs(dn)

    def rmdir(self, dn):
        if not exists(dn):
            return
        self.debug('Remove directory and subdirectory {}'.format(dn))
        rmtree(dn)

    def file_matches(self, patterns):
        from glob import glob
        result = []
        for pattern in patterns:
            matches = glob(expanduser(pattern.strip()))
            if not matches:
                return
            result.extend(matches)
        return result

    def file_exists(self, *args):
        return exists(join(*args))

    def file_rename(self, source, target, cwd=None):
        if cwd:
            source = join(cwd, source)
            target = join(cwd, target)
        self.debug('Rename {0} to {1}'.format(source, target))
        if not os.path.isdir(os.path.dirname(target)):
            self.error('Rename {0} to {1} fails becaues {2} is not a directory'.format(source, target, os.path.directory(target)))
        rename(source, target)

    def file_copy(self, source, target, cwd=None):
        if cwd:
            source = join(cwd, source)
            target = join(cwd, target)
        self.debug('Copy {0} to {1}'.format(source, target))
        copyfile(source, target)

    def file_extract(self, archive, cwd=None):
        if archive.endswith('.tgz') or archive.endswith('.tar.gz'):
            # XXX tarfile doesn't work for NDK-r8c :(
            #tf = tarfile.open(archive, 'r:*')
            #tf.extractall(path=cwd)
            #tf.close()
            self.cmd('tar xzf {0}'.format(archive), cwd=cwd)
            return

        if archive.endswith('.tbz2') or archive.endswith('.tar.bz2'):
            # XXX same as before
            self.cmd('tar xjf {0}'.format(archive), cwd=cwd)
            return

        if archive.endswith('.zip'):
            archive = join(cwd, archive)
            zf = zipfile.ZipFile(archive)
            zf.extractall(path=cwd)
            zf.close()
            return

        raise Exception('Unhandled extraction for type {0}'.format(archive))

    def file_copytree(self, src, dest):
        print('copy {} to {}'.format(src, dest))
        if os.path.isdir(src):
            if not os.path.isdir(dest):
                os.makedirs(dest)
            files = os.listdir(src)
            for f in files:
                self.file_copytree(
                    os.path.join(src, f),
                    os.path.join(dest, f))
        else:
            copyfile(src, dest)

    def clean_platform(self):
        self.info('Clean the platform build directory')
        if not exists(self.platform_dir):
            return
        rmtree(self.platform_dir)

    def download(self, url, filename, cwd=None):
        def report_hook(index, blksize, size):
            if size <= 0:
                progression = '{0} bytes'.format(index * blksize)
            else:
                progression = '{0:.2f}%'.format(
                        index * blksize * 100. / float(size))
            stdout.write('- Download {}\r'.format(progression))
            stdout.flush()

        url = url + filename
        if cwd:
            filename = join(cwd, filename)
        if self.file_exists(filename):
            unlink(filename)

        self.debug('Downloading {0}'.format(url))
        urlretrieve(url, filename, report_hook)
        return filename

    def get_version(self):
        c = self.config
        has_version = c.has_option('app', 'version')
        has_regex = c.has_option('app', 'version.regex')
        has_filename = c.has_option('app', 'version.filename')

        # version number specified
        if has_version:
            if has_regex or has_filename:
                raise Exception(
                    'version.regex and version.filename conflict with version')
            return c.get('app', 'version')

        # search by regex
        if has_regex or has_filename:
            if has_regex and not has_filename:
                raise Exception('version.filename is missing')
            if has_filename and not has_regex:
                raise Exception('version.regex is missing')

            fn = c.get('app', 'version.filename')
            with open(fn) as fd:
                data = fd.read()
                regex = c.get('app', 'version.regex')
                match = search(regex, data)
                if not match:
                    raise Exception(
                        'Unable to find capture version in {0}\n'
                        ' (looking for `{1}`)'.format(fn, regex))
                version = match.groups()[0]
                self.debug('Captured version: {0}'.format(version))
                return version

        raise Exception('Missing version or version.regex + version.filename')

    def build_application(self):
        self._copy_application_sources()
        self._copy_application_libs()
        self._copy_garden_libs()
        self._add_sitecustomize()

    def _copy_application_sources(self):
        # XXX clean the inclusion/exclusion algo.
        source_dir = realpath(self.config.getdefault('app', 'source.dir', '.'))
        include_exts = self.config.getlist('app', 'source.include_exts', '')
        exclude_exts = self.config.getlist('app', 'source.exclude_exts', '')
        exclude_dirs = self.config.getlist('app', 'source.exclude_dirs', '')
        exclude_patterns = self.config.getlist('app', 'source.exclude_patterns', '')
        app_dir = self.app_dir

        self.debug('Copy application source from {}'.format(source_dir))

        rmtree(self.app_dir)

        for root, dirs, files in walk(source_dir):
            # avoid hidden directory
            if True in [x.startswith('.') for x in root.split(sep)]:
                continue

            # need to have sort-of normalization. Let's say you want to exclude
            # image directory but not images, the filtered_root must have a / at
            # the end, same for the exclude_dir. And then we can safely compare
            filtered_root = root[len(source_dir) + 1:].lower()
            if filtered_root:
                filtered_root += '/'

                # manual exclude_dirs approach
                is_excluded = False
                for exclude_dir in exclude_dirs:
                    if exclude_dir[-1] != '/':
                        exclude_dir += '/'
                    if filtered_root.startswith(exclude_dir):
                        is_excluded = True
                        break
                if is_excluded:
                    continue

                # pattern matching
                for pattern in exclude_patterns:
                    if fnmatch(filtered_root, pattern):
                        is_excluded = True
                        break
                if is_excluded:
                    continue

            for fn in files:
                # avoid hidden files
                if fn.startswith('.'):
                    continue

                # exclusion by pattern matching
                is_excluded = False
                dfn = fn.lower()
                if filtered_root:
                    dfn = join(filtered_root, fn)
                for pattern in exclude_patterns:
                    if fnmatch(dfn, pattern):
                        is_excluded = True
                        break
                if is_excluded:
                    continue

                # filter based on the extension
                # TODO more filters
                basename, ext = splitext(fn)
                if ext:
                    ext = ext[1:]
                    if include_exts and ext not in include_exts:
                        continue
                    if exclude_exts and ext in exclude_exts:
                        continue

                sfn = join(root, fn)
                rfn = realpath(join(app_dir, root[len(source_dir) + 1:], fn))

                # ensure the directory exists
                dfn = dirname(rfn)
                self.mkdir(dfn)

                # copy!
                self.debug('Copy {0}'.format(sfn))
                copyfile(sfn, rfn)

    def _copy_application_libs(self):
        # copy also the libs
        copytree(self.applibs_dir, join(self.app_dir, '_applibs'))

    def _copy_garden_libs(self):
        if exists(self.gardenlibs_dir):
            copytree(self.gardenlibs_dir, join(self.app_dir, 'libs'))

    def _add_sitecustomize(self):
        copyfile(join(dirname(__file__), 'sitecustomize.py'),
                join(self.app_dir, 'sitecustomize.py'))

        main_py = join(self.app_dir, 'service', 'main.py')
        if not self.file_exists(main_py):
            #self.error('Unable to patch main_py to add applibs directory.')
            return

        header = ('import sys, os; '
                  'sys.path = [os.path.join(os.getcwd(),'
                  '"..", "_applibs")] + sys.path\n')
        with open(main_py, 'rb') as fd:
            data = fd.read()
        data = header + data
        with open(main_py, 'wb') as fd:
            fd.write(data)
        self.info('Patched service/main.py to include applibs')

    def namify(self, name):
        '''Return a "valid" name from a name with lot of invalid chars
        (allowed characters: a-z, A-Z, 0-9, -, _)
        '''
        return re.sub('[^a-zA-Z0-9_\-]', '_', name)

    @property
    def root_dir(self):
        return realpath(join(dirname(self.specfilename)))

    @property
    def buildozer_dir(self):
        if self.builddir:
            return join(self.builddir, '.buildozer')
        return join(self.root_dir, '.buildozer')

    @property
    def bin_dir(self):
        return join(self.root_dir, 'bin')

    @property
    def platform_dir(self):
        return join(self.buildozer_dir, self.targetname, 'platform')

    @property
    def app_dir(self):
        return join(self.buildozer_dir, self.targetname, 'app')

    @property
    def applibs_dir(self):
        return join(self.buildozer_dir, 'applibs')

    @property
    def gardenlibs_dir(self):
        return join(self.buildozer_dir, 'libs')

    @property
    def global_buildozer_dir(self):
        return join(expanduser('~'), '.buildozer')

    @property
    def global_platform_dir(self):
        return join(self.global_buildozer_dir, self.targetname, 'platform')

    @property
    def global_packages_dir(self):
        return join(self.global_buildozer_dir, self.targetname, 'packages')

    @property
    def global_cache_dir(self):
        return join(self.global_buildozer_dir, 'cache')

    @property
    def package_full_name(self):
        package_name = self.config.getdefault('app', 'package.name', '')
        package_domain = self.config.getdefault('app', 'package.domain', '')
        if package_domain == '':
            return package_name
        return '{}.{}'.format(package_domain, package_name)


    #
    # command line invocation
    #

    def targets(self):
        for fn in listdir(join(dirname(__file__), 'targets')):
            if fn.startswith('.') or fn.startswith('__'):
                continue
            if not fn.endswith('.py'):
                continue
            target = fn[:-3]
            try:
                m = __import__('buildozer.targets.{0}'.format(target),
                        fromlist=['buildozer'])
                yield target, m
            except NotImplementedError:
                pass
            except:
                raise
                pass

    def usage(self):
        print('Usage:')
        print('    buildozer [--profile <name>] [--verbose] [target] <command>...')
        print('    buildozer --version')
        print('')
        print('Available targets:')
        targets = list(self.targets())
        for target, m in targets:
            doc = m.__doc__.strip().splitlines()[0].strip()
            print('  {0:<18} {1}'.format(target, doc))

        print('')
        print('Global commands (without target):')
        cmds = [x for x in dir(self) if x.startswith('cmd_')]
        for cmd in cmds:
            name = cmd[4:]
            meth = getattr(self, cmd)

            if not meth.__doc__:
                continue
            doc = [x for x in
                    meth.__doc__.strip().splitlines()][0].strip()
            print('  {0:<18} {1}'.format(name, doc))

        print('')
        print('Target commands:')
        print('  clean      Clean the target environment')
        print('  update     Update the target dependencies')
        print('  debug      Build the application in debug mode')
        print('  release    Build the application in release mode')
        print('  deploy     Deploy the application on the device')
        print('  run        Run the application on the device')
        print('  serve      Serve the bin directory via SimpleHTTPServer')

        for target, m in targets:
            mt = m.get_target(self)
            commands = mt.get_custom_commands()
            if not commands:
                continue
            print('')
            print('Target "{0}" commands:'.format(target))
            for command, doc in commands:
                if not doc:
                    continue
                doc = doc.strip().splitlines()[0].strip()
                print('  {0:<18} {1}'.format(command, doc))

        print('')

    def run_default(self):
        self.check_build_layout()
        if 'buildozer:defaultcommand' not in self.state:
            print('No default command set.')
            print('Use "buildozer setdefault <command args...>"')
            print('Use "buildozer help" for a list of all commands"')
            exit(1)
        cmd = self.state['buildozer:defaultcommand']
        self.run_command(cmd)

    def run_command(self, args):
        while args:
            if not args[0].startswith('-'):
                break
            arg = args.pop(0)

            if arg in ('-v', '--verbose'):
                self.log_level = 2

            elif arg in ('-h', '--help'):
                self.usage()
                exit(0)

            elif arg in ('-p', '--profile'):
                self.config_profile = args.pop(0)

            elif arg == '--version':
                print('Buildozer {0}'.format(__version__))
                exit(0)

        self._merge_config_profile()

        self.check_root()

        if not args:
            self.run_default()
            return

        command, args = args[0], args[1:]
        cmd = 'cmd_{0}'.format(command)

        # internal commands ?
        if hasattr(self, cmd):
            getattr(self, cmd)(*args)
            return

        # maybe it's a target?
        targets = [x[0] for x in self.targets()]
        if command not in targets:
            print('Unknown command/target {}'.format(command))
            exit(1)

        self.set_target(command)
        self.target.run_commands(args)

    def check_root(self):
        '''If effective user id is 0, display a warning and require
        user input to continue (or to cancel)'''

        if IS_PY3:
            input_func = input
        else:
            input_func = raw_input

        warn_on_root = self.config.getdefault('buildozer', 'warn_on_root', '1')
        try:
            euid = os.geteuid() == 0
        except AttributeError:
            if sys.platform == 'win32':
                import ctypes
            euid = ctypes.windll.shell32.IsUserAnAdmin() != 0
        if warn_on_root == '1' and euid:
            print('\033[91m\033[1mBuildozer is running as root!\033[0m')
            print('\033[91mThis is \033[1mnot\033[0m \033[91mrecommended, and may lead to problems later.\033[0m')
            cont = None
            while cont not in ('y', 'n'):
                cont = input_func('Are you sure you want to continue [y/n]? ')

            if cont == 'n':
                sys.exit()

    def cmd_init(self, *args):
        '''Create a initial buildozer.spec in the current directory
        '''
        if exists('buildozer.spec'):
            print('ERROR: You already have a buildozer.spec file.')
            exit(1)
        copyfile(join(dirname(__file__), 'default.spec'), 'buildozer.spec')
        print('File buildozer.spec created, ready to customize!')

    def cmd_distclean(self, *args):
        '''Clean the whole Buildozer environment.
        '''
        print("Warning: Your ndk, sdk and all other cached packages will be"
              " removed. Continue? (y/n)")
        if sys.stdin.readline().lower()[0] == 'y':
            self.info('Clean the global build directory')
            if not exists(self.global_buildozer_dir):
                return
            rmtree(self.global_buildozer_dir)

    def cmd_help(self, *args):
        '''Show the Buildozer help.
        '''
        self.usage()

    def cmd_setdefault(self, *args):
        '''Set the default command to do when to arguments are given
        '''
        self.check_build_layout()
        self.state['buildozer:defaultcommand'] = args

    def cmd_version(self, *args):
        '''Show the Buildozer version
        '''
        print('Buildozer {0}'.format(__version__))

    def cmd_serve(self, *args):
        '''Serve the bin directory via SimpleHTTPServer
        '''
        try:
            from http.server import SimpleHTTPRequestHandler
            from socketserver import TCPServer
        except ImportError:
            from SimpleHTTPServer import SimpleHTTPRequestHandler
            from SocketServer import TCPServer

        os.chdir(self.bin_dir)
        handler = SimpleHTTPRequestHandler
        httpd = TCPServer(("", SIMPLE_HTTP_SERVER_PORT), handler)
        print("Serving via HTTP at port {}".format(SIMPLE_HTTP_SERVER_PORT))
        print("Press Ctrl+c to quit serving.")
        httpd.serve_forever()

    #
    # Private
    #

    def _merge_config_profile(self):
        profile = self.config_profile
        if not profile:
            return
        for section in self.config.sections():

            # extract the profile part from the section name
            # example: [app@default,hd]
            parts = section.split('@', 1)
            if len(parts) < 2:
                continue

            # create a list that contain all the profiles of the current section
            # ['default', 'hd']
            section_base, section_profiles = parts
            section_profiles = section_profiles.split(',')
            if profile not in section_profiles:
                continue

            # the current profile is one available in the section
            # merge with the general section, or make it one.
            if not self.config.has_section(section_base):
                self.config.add_section(section_base)
            for name, value in self.config.items(section):
                print('merged ({}, {}) into {} (profile is {})'.format(name,
                        value, section_base, profile))
                self.config.set(section_base, name, value)



    def _get_config_list_values(self, *args, **kwargs):
        kwargs['with_values'] = True
        return self._get_config_list(*args, **kwargs)

    def _get_config_list(self, section, token, default=None, with_values=False):
        # monkey-patch method for ConfigParser
        # get a key as a list of string, seperated from the comma

        # check if an env var exists that should replace the file config
        set_config_token_from_env(section, token, self.config)

        # if a section:token is defined, let's use the content as a list.
        l_section = '{}:{}'.format(section, token)
        if self.config.has_section(l_section):
            values = self.config.options(l_section)
            if with_values:
                return ['{}={}'.format(key, self.config.get(l_section, key)) for
                        key in values]
            else:
                return [x.strip() for x in values]

        values = self.config.getdefault(section, token, '')
        if not values:
            return default
        values = values.split(',')
        if not values:
            return default
        return [x.strip() for x in values]

    def _get_config_default(self, section, token, default=None):
        # monkey-patch method for ConfigParser
        # get an appropriate env var if it exists, else
        # get a key in a section, or the default

        # check if an env var exists that should replace the file config
        set_config_token_from_env(section, token, self.config)

        if not self.config.has_section(section):
            return default
        if not self.config.has_option(section, token):
            return default
        return self.config.get(section, token)

    def _get_config_bool(self, section, token, default=False):
        # monkey-patch method for ConfigParser
        # get a key in a section, or the default

        # check if an env var exists that should replace the file config
        set_config_token_from_env(section, token, self.config)

        if not self.config.has_section(section):
            return default
        if not self.config.has_option(section, token):
            return default
        return self.config.getboolean(section, token)