示例#1
0
    def test_service_name_with_embedded_version(self):
        # Basic test. If there were no exception raised here,
        # launchpadlib would make a request to
        # /version-foo/version-foo.
        version = "version-foo"
        root = uris.service_roots['staging'] + version
        try:
            Launchpad(None, None, None, service_root=root, version=version)
        except ValueError as e:
            self.assertTrue(str(e).startswith(
                "It looks like you're using a service root that incorporates "
                'the name of the web service version ("version-foo")'))
        else:
            raise AssertionError(
                "Expected a ValueError that was not thrown!")

        # Make sure the problematic URL is caught even if it has a
        # slash on the end.
        root += '/'
        self.assertRaises(ValueError, Launchpad, None, None, None,
                          service_root=root, version=version)

        # Test that the default version has the same problem
        # when no explicit version is specified
        default_version = NoNetworkLaunchpad.DEFAULT_VERSION
        root = uris.service_roots['staging'] + default_version + '/'
        self.assertRaises(ValueError, Launchpad, None, None, None,
                          service_root=root)
示例#2
0
def launchpadlib_for(consumer_name,
                     person=None,
                     permission=OAuthPermission.WRITE_PRIVATE,
                     context=None,
                     version="devel",
                     service_root="http://api.launchpad.dev/"):
    """Create a Launchpad object for the given person.

    :param consumer_name: An OAuth consumer name.
    :param person: A person (or the name of a person) for whom to create
        or find credentials.
    :param permission: An OAuthPermission (or its token) designating
        the level of permission the credentials should have.
    :param context: The OAuth context for the credentials.
    :param version: The version of the web service to access.
    :param service_root: The root URL of the web service to access.

    :return: A launchpadlib Launchpad object.
    """
    if person is None:
        token = AnonymousAccessToken()
        credentials = Credentials(consumer_name, access_token=token)
    else:
        credentials = launchpadlib_credentials_for(consumer_name, person,
                                                   permission, context)
    transaction.commit()
    cache = tempfile.mkdtemp(prefix='launchpadlib-cache-')
    zope.testing.cleanup.addCleanUp(_clean_up_cache, (cache, ))
    return Launchpad(credentials,
                     None,
                     None,
                     service_root=service_root,
                     version=version,
                     cache=cache)
示例#3
0
def create_session():
    lplib_cachedir = os.path.expanduser("~/.cache/launchpadlib/")
    hydrazine_cachedir = os.path.expanduser("~/.cache/hydrazine/")
    rrd_dir = os.path.expanduser("~/.cache/hydrazine/rrd")
    for d in [lplib_cachedir, hydrazine_cachedir, rrd_dir]:
        if not os.path.isdir(d):
            os.makedirs(d, mode=0700)

    hydrazine_credentials_filename = os.path.join(hydrazine_cachedir,
        'credentials')
    if os.path.exists(hydrazine_credentials_filename):
        credentials = Credentials()
        credentials.load(file(
            os.path.expanduser("~/.cache/hydrazine/credentials"),
            "r"))
        trace('loaded existing credentials')
        return Launchpad(credentials, service_root,
            lplib_cachedir)
        # TODO: handle the case of having credentials that have expired etc
    else:
        launchpad = Launchpad.get_token_and_login(
            'Hydrazine',
            service_root,
            lplib_cachedir)
        trace('saving credentials...')
        launchpad.credentials.save(file(
            hydrazine_credentials_filename,
            "w"))
        return launchpad
示例#4
0
 def test_service_name_with_embedded_version(self):
     # Basic test. If there were no exception raised here,
     # launchpadlib would make a request to
     # /version-foo/version-foo.
     version = "version-foo"
     root = uris.service_roots['staging'] + version
     try:
         Launchpad(None, None, None, service_root=root, version=version)
     except ValueError, e:
         self.assertTrue(str(e).startswith(
             "It looks like you're using a service root that incorporates "
             'the name of the web service version ("version-foo")'))
示例#5
0
 def get_lp(self):
     launchpad = False
     cachedir = os.path.expanduser('~/.launchpadlib/cache')
     if not os.path.exists(cachedir):
             os.makedirs(cachedir,0700)
     credfile = os.path.expanduser('~/.launchpadlib/credentials')
     try:
             credentials = Credentials()
             credentials.load(open(credfile))
             launchpad = Launchpad(credentials, EDGE_SERVICE_ROOT, cachedir)
     except:
             launchpad = Launchpad.get_token_and_login(sys.argv[0], EDGE_SERVICE_ROOT, cachedir)
     return launchpad
示例#6
0
    def get_lp(self):
        if not os.path.isdir(self.cachedir):
            os.makedirs(self.cachedir)

        if not os.path.isfile(self.credential_file):
            launchpad = Launchpad.get_token_and_login(self.application,
                                                      STAGING_SERVICE_ROOT,
                                                      self.cachedir)
            launchpad.credentials.save(file(self.credential_file, "w"))
        else:
            credentials = Credentials()
            credentials.load(open(self.credential_file))
            launchpad = Launchpad(credentials, STAGING_SERVICE_ROOT,
                                  self.cachedir)
        return launchpad
示例#7
0
def get_launchpad():
    if not HAVE_LPLIB:
        return None
    try:
        return Launchpad.login_anonymously("bzr-builddeb",
            service_root=LPNET_SERVICE_ROOT)
    except AttributeError: # older version of launchpadlib
        creds_path = os.path.join(config_dir(), "builddeb.lp_creds.txt")
        if not os.path.exists(creds_path):
            return None
        creds = Credentials("bzr-builddeb")
        f = open(creds_path)
        try:
            creds.load(f)
        finally:
            f.close()
        return Launchpad(creds, service_root=LPNET_SERVICE_ROOT)
def connect():
    cachedir = os.path.expanduser('~/.launchpadlib/cache')
    if not os.path.exists(cachedir):
        os.makedirs(cachedir, 0700)

    credfile = os.path.expanduser('~/.launchpadlib/credentials')
    try:
        credentials = Credentials()
        credentials.load(open(credfile))
        launchpad = Launchpad(credentials, EDGE_SERVICE_ROOT, cachedir)
    except:
        launchpad = Launchpad.get_token_and_login(sys.argv[0],
                                                  EDGE_SERVICE_ROOT, cachedir)
        credfd = tempfile.NamedTemporaryFile(dir=os.path.dirname(credfile))
        launchpad.credentials.save(credfd)
        os.link(credfd.name, credfile)
        credfd.close()
    return launchpad
示例#9
0
 def login(self):
     if self.launchpad is not None:
         return
     cachedir = os.path.expanduser('~/.cache/launchpadlib')
     creddir = os.path.expanduser('~/.config/launchpadlib')
     credfile = creddir + '/credentials.txt'
     root = EDGE_SERVICE_ROOT
     if not os.path.exists(cachedir):
         os.makedirs(cachedir, 0700)
     try:
         credentials = Credentials()
         credentials.load(open(credfile))
         self.launchpad = Launchpad(credentials, root, cachedir)
     except:
         self.launchpad = Launchpad.get_token_and_login('igotu2gpx-launchpad-tools',
                 root, cachedir)
         if not os.path.exists(creddir):
             os.makedirs(creddir, 0700)
         self.launchpad.credentials.save(open(credfile, "w", 0600))
     self.project = self.launchpad.projects['igotu2gpx']
示例#10
0
 def get_lp(self):
     launchpad = False
     if not os.path.isdir(self.cachedir):
         try:
             os.makedirs(self.cachedir)
         except:
             raise
     if not os.path.isfile(self.lp_credential_file):
         try:
             launchpad = Launchpad.get_token_and_login(
                 'openerp', EDGE_SERVICE_ROOT, self.cachedir)
             launchpad.credentials.save(file(self.lp_credential_file, "w"))
         except:
             print 'Service Unavailable !'
     else:
         credentials = Credentials()
         credentials.load(open(self.lp_credential_file))
         launchpad = Launchpad(credentials, EDGE_SERVICE_ROOT,
                               self.cachedir)
     return launchpad
示例#11
0
    def launchpad(self):
        '''Return Launchpad instance.'''

        if self.__launchpad:
            return self.__launchpad

        if self.options.get('staging') or os.getenv('APPORT_STAGING'):
            launchpad_instance = STAGING_SERVICE_ROOT
        else:
            launchpad_instance = EDGE_SERVICE_ROOT

        auth_dir = os.path.dirname(self.auth)
        if auth_dir and not os.path.isdir(auth_dir):
            os.makedirs(auth_dir)

        try:
            if os.path.exists(self.auth):
                # use existing credentials
                credentials = Credentials()
                credentials.load(open(self.auth))
                self.__launchpad = Launchpad(credentials, launchpad_instance,
                                             self.__lpcache)
            else:
                # get credentials and save them
                try:
                    self.__launchpad = Launchpad.get_token_and_login(
                        'apport-collect', launchpad_instance, self.__lpcache)
                except HTTPError, e:
                    print >> sys.stderr, 'Error connecting to Launchpad: %s\nYou have to allow "Change anything" privileges.' % str(
                        e)
                    sys.exit(1)
                f = open(self.auth, 'w')
                os.chmod(self.auth, 0600)
                self.__launchpad.credentials.save(f)
                f.close()
        except (socket.error, ServerNotFoundError), e:
            print >> sys.stderr, 'Error connecting to Launchpad: %s' % str(e)
            sys.exit(99)  # transient error
示例#12
0
def lp_login():
    cachedir = os.path.expanduser('~/.cache/launchpadlib/')
    if not os.path.isdir(cachedir):
        os.makedirs(cachedir)
    creddir = os.path.expanduser("~/.cache/lp_credentials")
    if not os.path.isdir(creddir):
        os.makedirs(creddir)
        os.chmod(creddir, 0o700)

    credpath = os.path.join(creddir, 'close_launchpad_bugs.txt')
    try:
        credfile = open(credpath, 'r')
        credentials = Credentials()
        credentials.load(credfile)
        credfile.close()
        launchpad = Launchpad(credentials, EDGE_SERVICE_ROOT, cachedir)
    except IOError:
        launchpad = Launchpad.get_token_and_login('close_launchpad_bugs',
                                                  EDGE_SERVICE_ROOT, cachedir)
        credfile = open(credpath, 'w')
        launchpad.credentials.save(credfile)
        credfile.close()

    return launchpad
示例#13
0
    except Exception, e:
        self.msg(dst, 'Could not find lp bug %i, %s' % (bug_id, str(e)))
        return

    owner = str(bug.owner).split('~', 1)[1]
    self.msg(
        dst,
        str('LP bug #%i, %s (%s) by %s' %
            (bug_id, bug.title, bug.self_link, owner)))


# this is the module initialization process if we are imported.
if __name__ == 'lpbugs':
    creds = Credentials()
    creds.load(open('%s/lpcreds.txt' % CONFDIR))
    LP = Launchpad(creds, EDGE_SERVICE_ROOT, CACHEDIR)
    core.MODULES.append(__name__)
    log('%s Loaded Successfully' % __name__)

# This is the setup initialization process
if __name__ == '__main__':
    if not os.path.exists(CACHEDIR):
        print "Creating conf and cache dir"
        try:
            os.makedirs(CACHEDIR)
        except Exception, e:
            print 'Error creating directories, %s' % str(e)
            sys.exit(1)

    if os.path.exists('%s/lpcreds.txt' % CONFDIR):
        creds = Credentials()