Example #1
0
def update_mirror(site_name, doc_name, data, directory='', dry_run=False):
    """administer promoting the converted document in the given directory
    to the mirror site
    some additions are only saved in the mirror html hence the data argument
    otherwise we could get it from the target location

    raise AttributeError if no name provided
    create a new entry if it's the first time
    """
    if not doc_name:
        raise AttributeError('no_name')
    if dry_run:
        return
    if not directory:
        directory = '/'
    sitedoc = _get_site_doc(site_name)
    dts = datetime.datetime.utcnow()
    sitedoc['docs'][directory][doc_name]['mirror'] = {'updated': dts}
    _update_site_doc(site_name, sitedoc['docs'])

    path = DB_WEBROOT / site_name
    if directory != '/':
        path /= directory
    if not path.exists():
        path.mkdir(parents=True)
    path /= doc_name
    ext = LOC2EXT['dest']
    if path.suffix != ext:
        path = path.with_suffix(ext)
    sett = read_settings(site_name)
    if not path.exists() and not sett.get('seflinks', False):
        path.touch()
    save_to(path, data, sett)
Example #2
0
def update_mirror(site_name, doc_name, data, directory='', dry_run=True):
    """administer promoting the converted document in the given directory
    to the mirror site
    some additions are only saved in the mirror html hence the data argument
    otherwise we could get it from the target location

    raise AttributeError if no name provided
    create a new entry if it's the first time
    """
    if not doc_name:
        raise AttributeError('no_name')
    if not data:
        raise AttributeError('no_contents')
    if not directory:
        directory = '/'
    doc_name = pathlib.Path(doc_name).stem

    siteid = _get_site_id(site_name)
    if siteid is None:
        raise FileNotFoundError('no_site')
    dirid = _get_dir_id(siteid, directory)
    if dirid is None:
        raise FileNotFoundError('no_subdir')
    doc_id = _get_doc_ids(dirid, doc_name)[0]
    if doc_id is None:
        raise FileNotFoundError("no_document")
    if dry_run:
        return

    cur = conn.cursor()
    dts = datetime.datetime.utcnow()
    cur.execute(
        'update {} set mirror_updated = %s where id = %s;'.format(TABLES[3]),
        (dts, doc_id))
    conn.commit()
    cur.close()

    path = WEBROOT / site_name
    if directory != '/':
        path /= directory
    if not path.exists():
        path.mkdir(parents=True)
    path /= doc_name
    ext = LOC2EXT['dest']
    if path.suffix != ext:
        path = path.with_suffix(ext)
    sett = read_settings(site_name)
    seflinks = sett.get('seflinks', False)
    if not path.exists() and not seflinks:
        path.touch()
    save_to(path, data, seflinks)
Example #3
0
    def test_save_to(self, monkeypatch, capsys):
        tmpfilename = '/tmp/dmlfsaveto'

        def mock_open_err(self, *args, **kwargs):
            raise OSError

        def mock_open(self, *args, **kwargs):
            return open(tmpfilename, 'w')

        def mock_mkdir(self, *args, **kwargs):
            print('called mkdir for `{}`'.format(self))

        def mock_replace(self, *args, **kwargs):
            print('called replace for `{}` with `{}`'.format(self, args[0]))

        def read_and_remove(filename):
            with open(filename) as read_file:
                data = read_file.read()
            os.unlink(filename)
            return data

        fulldocname = dmlf.WEBROOT / 'testsite' / 'docname'
        monkeypatch.setattr(dmlf, 'read_settings', mock_settings_seflinks_no)
        monkeypatch.setattr(dmlf.pathlib.Path, 'exists', return_false)
        monkeypatch.setattr(dmlf.pathlib.Path, 'open', mock_open_err)
        assert dmlf.save_to(fulldocname, 'data') == 'OSError without message'
        monkeypatch.setattr(dmlf, 'read_settings', mock_settings_seflinks_yes)
        monkeypatch.setattr(dmlf.shutil, 'copyfile', mock_copyfile)
        monkeypatch.setattr(dmlf.pathlib.Path, 'mkdir', mock_mkdir)
        monkeypatch.setattr(dmlf.pathlib.Path, 'open', mock_open)
        assert dmlf.save_to(fulldocname, 'data') == ''
        assert capsys.readouterr().out == ''
        assert read_and_remove(tmpfilename) == 'data'
        monkeypatch.setattr(dmlf.pathlib.Path, 'exists', return_true)
        fulldocname = dmlf.WEBROOT / 'testsite' / 'docname.rst'
        assert dmlf.save_to(fulldocname, 'data') == ''
        assert capsys.readouterr(
        ).out == 'called copyfile: from `{}` to `{}`\n'.format(
            str(fulldocname),
            str(fulldocname) + '.bak')
        assert read_and_remove(tmpfilename) == 'data'
        monkeypatch.setattr(dmlf.pathlib.Path, 'exists', return_false)
        fulldocname = dmlf.WEBROOT / 'testsite' / 'docname.html'
        assert dmlf.save_to(fulldocname, 'data') == ''
        assert capsys.readouterr().out == 'called mkdir for `{}`\n'.format(
            str(fulldocname).replace('.html', ''))
        assert read_and_remove(tmpfilename) == 'data'
        monkeypatch.setattr(dmlf.pathlib.Path, 'exists', return_true)
        monkeypatch.setattr(dmlf.pathlib.Path, 'is_dir', return_false)
        monkeypatch.setattr(dmlf.pathlib.Path, 'replace', mock_replace)
        assert dmlf.save_to(fulldocname, 'data') == ''
        assert capsys.readouterr().out == ''.join(
            ('called replace for `{}` with `{}`\n'.format(
                str(fulldocname).replace('.html', ''),
                str(fulldocname).replace('.html', '.bak')),
             'called mkdir for `{}`\n'.format(
                 str(fulldocname).replace('.html', '')),
             'called copyfile: from `{}` to `{}`\n'.format(
                 str(fulldocname).replace('.html', '/index.html'),
                 str(fulldocname).replace('.html', '/index.html.bak'))))
        assert read_and_remove(tmpfilename) == 'data'