Example #1
0
def get_cookie_jar(cookies_file):
    cj = cookielib.MozillaCookieJar()
    cookies = load_cookies_file(cookies_file)

    cj._really_load(cookies, 'StringIO.cookies', False, False)

    return cj
Example #2
0
def write_cookies_to_cache(cj, username):

    mkdir_p(PATH_COOKIES, 0o700)
    path = get_cookies_cache_path(username)
    cached_cj = cookielib.MozillaCookieJar()
    for cookie in cj:
        cached_cj.set_cookie(cookie)
    cached_cj.save(path)
Example #3
0
def get_cookie_jar(cookies_file):
    cj = cookielib.MozillaCookieJar()
    cookies = load_cookies_file(cookies_file)

    # nasty hack: cj.load() requires a filename not a file, but if I use
    # stringio, that file doesn't exist. I used NamedTemporaryFile before,
    # but encountered problems on Windows.
    cj._really_load(cookies, 'StringIO.cookies', False, False)

    return cj
Example #4
0
def write_cookies_to_cache(cj, username):
    """
    Saves the RequestsCookieJar to disk in the Mozilla cookies.txt file
    format.  This prevents us from repeated authentications on the
    accounts.coursera.org and class.coursera.org/class_name sites.
    """
    mkdir_p(PATH_COOKIES, 0o700)
    path = get_cookies_cache_path(username)
    cached_cj = cookielib.MozillaCookieJar()
    for cookie in cj:
        cached_cj.set_cookie(cookie)
    cached_cj.save(path)
Example #5
0
def get_session():
    """
    Create a session with TLS v1.2 certificate.
    """

    session = requests.Session()
    cj = cookielib.MozillaCookieJar()
    cj.load('cookies.txt', ignore_expires=True)
    session.cookies = cj
    session.mount('https://', TLSAdapter())

    return session
Example #6
0
    def setUp(self):
        """Sets up the gerrit instances in a class-specific temp dir."""
        self.saved_params = {}
        os.environ['HOME'] = self.tempdir

        # Create gerrit instance.
        gi = self.gerrit_instance = self._create_gerrit_instance(self.tempdir)

        # This --global will use our tempdir $HOME we set above, not the real ~/.
        cros_build_lib.run(
            ['git', 'config', '--global', 'http.cookiefile', gi.cookies_path],
            quiet=True)

        jar = cookielib.MozillaCookieJar(gi.cookies_path)
        jar.load(ignore_expires=True)

        def GetCookies(host, _path):
            return dict((c.name, urllib.parse.unquote(c.value)) for c in jar
                        if c.domain == host and c.path == '/'
                        and c.name in gi.cookie_names)

        self.PatchObject(gob_util, 'GetCookies', GetCookies)

        site_params = config_lib.GetSiteParams()

        # Make all chromite code point to the test server.
        self.patched_params = {
            'EXTERNAL_GOB_HOST':
            gi.git_host,
            'EXTERNAL_GERRIT_HOST':
            gi.gerrit_host,
            'EXTERNAL_GOB_URL':
            gi.git_url,
            'EXTERNAL_GERRIT_URL':
            gi.gerrit_url,
            'INTERNAL_GOB_HOST':
            gi.git_host,
            'INTERNAL_GERRIT_HOST':
            gi.gerrit_host,
            'INTERNAL_GOB_URL':
            gi.git_url,
            'INTERNAL_GERRIT_URL':
            gi.gerrit_url,
            'AOSP_GOB_HOST':
            gi.git_host,
            'AOSP_GERRIT_HOST':
            gi.gerrit_host,
            'AOSP_GOB_URL':
            gi.git_url,
            'AOSP_GERRIT_URL':
            gi.gerrit_url,
            'MANIFEST_URL':
            '%s/%s' % (gi.git_url, site_params.MANIFEST_PROJECT),
            'MANIFEST_INT_URL':
            '%s/%s' % (gi.git_url, site_params.MANIFEST_INT_PROJECT),
            'GIT_REMOTES': {
                site_params.EXTERNAL_REMOTE: gi.gerrit_url,
                site_params.INTERNAL_REMOTE: gi.gerrit_url,
                site_params.CHROMIUM_REMOTE: gi.gerrit_url,
                site_params.CHROME_REMOTE: gi.gerrit_url
            }
        }

        for k in self.patched_params.keys():
            self.saved_params[k] = site_params.get(k)

        site_params.update(self.patched_params)
Example #7
0
def resolve_url(url, directory=None, permissions=None):
    """
    Resolves a URL to a local file, and returns the path to
    that file.
    """

    u = urlparse(url)

    # create the name of the destination file
    if directory is None:
        directory = os.getcwd()
    filename = os.path.join(directory, os.path.basename(u.path))

    if u.scheme == '' or u.scheme == 'file':
        # for regular files, make a direct copy
        if os.path.isfile(u.path):
            if os.path.isfile(filename):
                # check to see if src and dest are the same file
                src_inode = os.stat(u.path)[stat.ST_INO]
                dst_inode = os.stat(filename)[stat.ST_INO]
                if src_inode != dst_inode:
                    shutil.copy(u.path, filename)
            else:
                shutil.copy(u.path, filename)
        else:
            errmsg = "Cannot open file %s from URL %s" % (u.path, url)
            raise ValueError(errmsg)

    elif u.scheme == 'http' or u.scheme == 'https':
        s = requests.Session()
        s.mount(
            str(u.scheme) + '://',
            requests.adapters.HTTPAdapter(max_retries=5))

        # look for an ecp cookie file and load the cookies
        cookie_dict = {}
        ecp_file = '/tmp/ecpcookie.u%d' % os.getuid()
        if os.path.isfile(ecp_file):
            cj = cookielib.MozillaCookieJar()
            cj.load(ecp_file, ignore_discard=True, ignore_expires=True)
        else:
            cj = []

        for c in cj:
            if c.domain == u.netloc:
                # load cookies for this server
                cookie_dict[c.name] = c.value
            elif u.netloc == "code.pycbc.phy.syr.edu" and \
              c.domain == "git.ligo.org":
                # handle the redirect for code.pycbc to git.ligo.org
                cookie_dict[c.name] = c.value

        r = s.get(url, cookies=cookie_dict, allow_redirects=True)
        if r.status_code != 200:
            errmsg = "Unable to download %s\nError code = %d" % (url,
                                                                 r.status_code)
            raise ValueError(errmsg)

        # if we are downloading from git.ligo.org, check that we
        # did not get redirected to the sign-in page
        if u.netloc == 'git.ligo.org' or u.netloc == 'code.pycbc.phy.syr.edu':
            # Check if we have downloaded a binary file.
            if istext(r.content):
                soup = BeautifulSoup(r.content, 'html.parser')
                desc = soup.findAll(attrs={"property": "og:url"})
                if len(desc) and \
                  desc[0]['content'] == 'https://git.ligo.org/users/sign_in':
                    raise ValueError(ecp_cookie_error.format(url))

        output_fp = open(filename, 'wb')
        output_fp.write(r.content)
        output_fp.close()

    else:
        # TODO: We could support other schemes such as gsiftp by
        # calling out to globus-url-copy
        errmsg = "Unknown URL scheme: %s\n" % (u.scheme)
        errmsg += "Currently supported are: file, http, and https."
        raise ValueError(errmsg)

    if not os.path.isfile(filename):
        errmsg = "Error trying to create file %s from %s" % (filename, url)
        raise ValueError(errmsg)

    if permissions:
        if os.access(filename, os.W_OK):
            os.chmod(filename, permissions)
        else:
            # check that the file has at least the permissions requested
            s = os.stat(filename)[stat.ST_MODE]
            if (s & permissions) != permissions:
                errmsg = "Could not change permissions on %s (read-only)" % url
                raise ValueError(errmsg)

    return filename
Example #8
0
import getpass
import os

from pprint import pprint
from six.moves import http_cookiejar, input
from usereg import usereg

if __name__ == '__main__':
    jar = http_cookiejar.MozillaCookieJar('cookie.txt')
    if jar.filename and os.path.isfile(jar.filename):
        jar.load(ignore_discard=True, ignore_expires=True)

    agent = usereg(jar)
    if agent.checklogin()['error']:
        username = input('Username: ')
        password = getpass.getpass()
        agent.login(username, password)
    pprint(agent.user_info())
    pprint(agent.online_user_ipv4())

    if jar.filename:
        jar.save(ignore_discard=True, ignore_expires=True)
Example #9
0
    def __init__(self, *args, **kwargs):
        ttk.Frame.__init__(self, *args, **kwargs)
        self.init_settings()

        self.cookie = cookielib.MozillaCookieJar(self.cookie_filename)
        if (os.path.isfile(self.cookie_filename)):
            self.cookie.load(ignore_discard=True, ignore_expires=True)
        self.opener = urllib.request.build_opener(
            urllib.request.HTTPCookieProcessor(self.cookie))

        self.students = []
        self.username = StringVar()
        self.password = StringVar()
        self.course_listvar = StringVar()
        self.assignment_listvar = StringVar()
        self.student_listvar = StringVar()

        ttk.Label(self, text='Username: '******'Password: '******'*')
        self.password_entry.grid(column=2, row=2, sticky=(W, E))
        self.login_button = ttk.Button(self, text='Login', command=self.login)
        self.login_button.grid(column=2, row=3, stick=E)

        self.course_listbox = Listbox(self,
                                      height=6,
                                      width=35,
                                      listvariable=self.course_listvar,
                                      relief='sunken')
        self.course_listbox.grid(column=3,
                                 row=1,
                                 rowspan=3,
                                 padx=(5, 0),
                                 stick=(N, W, E, S))
        self.course_listbox.configure(exportselection=False)
        self.course_listbox.bind('<<ListboxSelect>>', self.load_assignments)
        s = ttk.Scrollbar(self,
                          orient=VERTICAL,
                          command=self.course_listbox.yview)
        s.grid(column=4, row=1, rowspan=3, sticky=(N, S))
        self.course_listbox['yscrollcommand'] = s.set

        self.assignment_listbox = Listbox(self,
                                          height=6,
                                          width=35,
                                          listvariable=self.assignment_listvar,
                                          relief='sunken')
        self.assignment_listbox.grid(column=5,
                                     row=1,
                                     rowspan=3,
                                     padx=(5, 0),
                                     stick=(N, W, E, S))
        self.assignment_listbox.configure(exportselection=False)
        self.assignment_listbox.bind('<<ListboxSelect>>', self.load_students)
        s = ttk.Scrollbar(self,
                          orient=VERTICAL,
                          command=self.assignment_listbox.yview)
        s.grid(column=6, row=1, rowspan=3, sticky=(N, S))
        self.assignment_listbox['yscrollcommand'] = s.set

        self.student_listbox = Listbox(self,
                                       height=20,
                                       listvariable=self.student_listvar,
                                       relief='sunken',
                                       font=font.Font(family='Consolas',
                                                      size=11))
        self.student_listbox.grid(column=1,
                                  row=4,
                                  columnspan=5,
                                  pady=(10, 5),
                                  stick=(N, W, E, S))
        self.student_listbox.configure(exportselection=False)
        s = ttk.Scrollbar(self,
                          orient=VERTICAL,
                          command=self.student_listbox.yview)
        s.grid(column=6, row=4, pady=(10, 5), sticky=(N, S))
        self.student_listbox['yscrollcommand'] = s.set

        self.readtxt_button = ttk.Button(self,
                                         text='读入成绩',
                                         command=self.read_txt)
        self.readtxt_button.grid(column=1, row=5, stick=E)
        self.dopublish_button = ttk.Button(self,
                                           text='确认发布',
                                           command=self.do_publish)
        self.dopublish_button.grid(column=2, row=5, stick=W)
        self.exportcsv_button = ttk.Button(self,
                                           text='导出成绩',
                                           command=self.export_csv)
        self.exportcsv_button.grid(column=3, row=5, stick=E)

        self.grid_columnconfigure(5, weight=1)
        self.grid_rowconfigure(4, weight=1)

        self.try_load_courses()