Esempio n. 1
0
def main():
    opts = parse_args()
    if not opts.mime_type:
        print("Exactly one mime-type must be given!")
        exit(1)
    cfg = yaml.load(open(opts.config))
    gd = GoogleDrive(
        client_id=cfg['googledrive']['client id'],
        client_secret=cfg['googledrive']['client secret'],
        scopes=[DRIVE_RW_SCOPE],
    )

    # Establish our credentials.
    gd.authenticate()

    # Get information about the specified file.  This will throw
    # an exception if the file does not exist.
    md = gd.get_file_metadata(opts.docid)

    # Precalc some directory and filename constants:
    extension = filetype = MIME_TYPE_EXTENSIONS[opts.mime_type]
    gitrepo = u'%s/%s' % (filetype, md['title'])
    filename = u'content.%s' % extension

    if os.path.exists(gitrepo):
        sys.stderr.write('Error: repo "%s" already exists.\n' % gitrepo)
        sys.exit(1)

    # Initialize the git repository.
    print('Create repository %s' % gitrepo)
    subprocess.call(['git', 'init', gitrepo])
    os.chdir(gitrepo)

    # Iterate over the revisions (from oldest to newest).
    for rev in gd.revisions(opts.docid):
        with open(filename, 'w') as fd:
            if 'exportLinks' in rev and not opts.raw:
                # If the file provides an 'exportLinks' dictionary,
                # download the requested MIME type.
                r = gd.session.get(rev['exportLinks'][opts.mime_type])
            elif 'downloadUrl' in rev:
                # Otherwise, if there is a downloadUrl, use that.
                r = gd.session.get(rev['downloadUrl'])
            else:
                raise KeyError('unable to download revision')

            # Write file content into local file.
            for chunk in r.iter_content():
                fd.write(chunk)

        # Commit changes to repository.
        subprocess.call(['git', 'add', filename])
        subprocess.call([
            'git', 'commit', '-m',
            'url: %s' % rev['selfLink'],
            '--author="%s <%s>"' % (rev['lastModifyingUser']['displayName'],
                                    rev['lastModifyingUser']['emailAddress']),
            '--date="%s"' % rev['modifiedDate']
        ])
Esempio n. 2
0
def main():
    opts = parse_args()
    if not opts.mime_type:
        print("Exactly one mime-type must be given!")
        exit(1)
    cfg = yaml.load(open(opts.config))
    gd = GoogleDrive(
        client_id=cfg['googledrive']['client id'],
        client_secret=cfg['googledrive']['client secret'],
        scopes=[DRIVE_RW_SCOPE],
    )

    # Establish our credentials.
    gd.authenticate()

    # Get information about the specified file.  This will throw
    # an exception if the file does not exist.
    md = gd.get_file_metadata(opts.docid)

    # Initialize the git repository.
    print('Create repository "%(title)s"' % md)
    subprocess.call(['git', 'init', md['title']])
    os.chdir(md['title'])

    # Iterate over the revisions (from oldest to newest).
    for rev in gd.revisions(opts.docid):
        with open('content', 'wb') as fd:
            if 'exportLinks' in rev and not opts.raw:
                # If the file provides an 'exportLinks' dictionary,
                # download the requested MIME type.

                if not (opts.mime_type in rev['exportLinks']):
                    print("Specified mimetype '", opts.mime_type,
                          "' does not exist for specified document.")
                    print("Available mimetypes for this document are:")
                    for key in rev['exportLinks'].keys():
                        print("- ", key)
                    exit(1)

                r = gd.session.get(rev['exportLinks'][opts.mime_type])
            elif 'downloadUrl' in rev:
                # Otherwise, if there is a downloadUrl, use that.
                r = gd.session.get(rev['downloadUrl'])
            else:
                raise KeyError('unable to download revision')

            # Write file content into local file.
            for chunk in r.iter_content():
                fd.write(chunk)

        # Commit changes to repository.
        subprocess.call(['git', 'add', 'content'])
        subprocess.call(
            ['git', 'commit', '-m',
             'revision from %s' % rev['modifiedDate']])
Esempio n. 3
0
def main():
    opts = parse_args()
    if not opts.mime_type:
        print "Exactly one mime-type must be given!"
        exit(1)
    cfg = yaml.load(open(opts.config))
    gd = GoogleDrive(
        client_id=cfg['googledrive']['client id'],
        client_secret=cfg['googledrive']['client secret'],
        scopes=[DRIVE_RW_SCOPE],
    )

    # Establish our credentials.
    gd.authenticate()

    # Get information about the specified file.  This will throw
    # an exception if the file does not exist.
    md = gd.get_file_metadata(opts.docid)

    # Initialize the git repository.
    print 'Create repository "%(title)s"' % md
    subprocess.call(['git', 'init', md['title']])
    os.chdir(md['title'])

    # Iterate over the revisions (from oldest to newest).
    for rev in gd.revisions(opts.docid):
        # print rev
        with open(opts.output, 'w') as fd:
            if 'exportLinks' in rev and not opts.raw:
                # If the file provides an 'exportLinks' dictionary,
                # download the requested MIME type.
                r = gd.session.get(rev['exportLinks'][opts.mime_type])
            elif 'downloadUrl' in rev:
                # Otherwise, if there is a downloadUrl, use that.
                r = gd.session.get(rev['downloadUrl'])
            else:
                raise KeyError('unable to download revision')

            # Write file content into local file.
            for chunk in r.iter_content():
                fd.write(chunk)

        # Commit changes to repository.
        subprocess.call(['git', 'add', opts.output])
        subprocess.call([
            'git',
            'commit',
            '--author="%s <%s>"' %
            (rev.get('lastModifyingUserName'), rev['lastModifyingUser'].get(
                'emailAddress', '')),
            '-m',
            'doc: import revision from %s' % rev['modifiedDate'],
        ])
Esempio n. 4
0
def main():
    opts = parse_args()
    if not opts.mime_types:
        print('At least one mime-type must be given: ensure that you are running with -T, -H, etc.')
        exit(1)
    cfg = yaml.load(open(opts.config))
    gd = GoogleDrive(
            client_id=cfg['googledrive']['client id'],
            client_secret=cfg['googledrive']['client secret'],
            scopes=[DRIVE_RW_SCOPE],
            )

    # Establish our credentials.
    gd.authenticate()

    # Get information about the specified file.  This will throw
    # an exception if the file does not exist.
    md = gd.get_file_metadata(opts.docid)

    if os.path.isdir(md['title']):
        # Find revision matching last commit and process only following revisions
        os.chdir(md['title'])
        print('Update repository "{0}"'.format(md['title'].encode('utf-8')))
        last_commit_message = subprocess.check_output('git log -n 1 --format=%B', shell=True).decode(sys.stdout.encoding)
        print('Last commit: ' + last_commit_message + 'Iterating Google Drive revisions:')
        revision_matched = False
        for rev in gd.revisions(opts.docid):
            if revision_matched:
                print('New revision: ' + rev['modifiedDate'])
                commit_revision(gd, opts, rev)
            if rev['modifiedDate'] in last_commit_message:
                print('Found matching revision: ' + rev['modifiedDate'])
                revision_matched = True
        print('Repository is up to date.')
    else:
        # Initialize the git repository.
        print('Create repository "{0}"'.format(md['title'].encode('utf-8')))
        subprocess.call(['git','init',md['title']])
        os.chdir(md['title'])

        # Iterate over the revisions (from oldest to newest).
        for rev in gd.revisions(opts.docid):
            commit_revision(gd, opts, rev)
Esempio n. 5
0
    # Prepare output repository.
    if os.path.exists(opts.output):
        if opts.force:
            shutil.rmtree(opts.output)
        else:
            logging.error(
                "There is already a file/directory at %s, use --force to overwrite",
                opts.output)
            sys.exit(2)
    os.makedirs(opts.output)

    # Establish our credentials.
    cfg = yaml.load(open(opts.config))
    gd = GoogleDrive(
        client_id=cfg["googledrive"]["client id"],
        client_secret=cfg["googledrive"]["client secret"],
        scopes=[DRIVE_RW_SCOPE],
    )
    gd.authenticate()

    # Scan for events.
    s = EventScanner(gd, opts)
    s.scan(opts.id)
    logging.info("Scan completed, found %d events", len(s.events))
    # Initialize the git repository.
    repo = git.init_repository(opts.output)
    logging.info("Created repository at %s", repo.workdir)
    c = EventCommitter(gd, opts, repo)
    c.commit(s.events)
    logging.info("Complete!")