Esempio n. 1
0
def test_make_directories(mount_dir, sub_done, sub_open):
    with pytest.raises(FileExistsError):
        mkdir(sub_open, 'dir')
    assert isdir(sub_open, 'dir')

    with pytest.raises(PermissionError):
        rm_rf(sub_open, 'dir')

    assert isdir(sub_open, 'dir')
    assert isfile(sub_open, 'dir', 'single_file_work')
    assert isfile(sub_open, 'dir', 'single_file_work_copy')

    with pytest.raises(IsADirectoryError):
        rm(sub_done, 'dir')
    with pytest.raises(OSError):
        rmdir(sub_done, 'dir')
    assert isdir(sub_done, 'dir')

    rm_rf(sub_done, 'dir')

    assert 'dir' not in ls(sub_done)

    mkdir(sub_done, 'dir')
    assert isdir(sub_done, 'dir')

    rmdir(sub_done, 'dir')
    assert 'dir' not in ls(sub_done)
Esempio n. 2
0
def test_deleting_directory_in_fixed(sub_open, mount):
    fdir = join(sub_open, 'new_test_file')
    fdir2 = join(sub_open, 'new_test_file2')
    fdir3 = join(sub_open, 'new_test_file3')

    # Make sure we cannot delete existing files in fixed mode
    assert not isdir(fdir)
    mkdir(fdir)
    assert isdir(fdir)

    mount(fixed=True)

    assert isdir(fdir)
    with pytest.raises(PermissionError):
        rmdir(fdir)
    assert isdir(fdir)

    del fdir

    assert not isdir(fdir2)
    mkdir(fdir2)
    assert isdir(fdir2)
    rename([fdir2], [fdir3])

    assert not isdir(fdir2)
    assert isdir(fdir3)

    mount(fixed=True)
    assert not isdir(fdir3)
Esempio n. 3
0
async def ziprepo(message: types.Message):
    if not message.text.startswith("/repo "):
        return
    logging.info(f"{message.from_user.id} - cmd: {message.text}")
    # TODO Check user input before trying to get the repo from GitHub
    repo = message.text[6:]
    repo_path = repo.split("/")
    temp_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp",
                             *repo_path)
    # TODO Create zip directory just in case
    zip_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "zip",
                            " ".join(repo_path))

    archive = pathlib.Path(f"{zip_path}.zip")
    edited_at = datetime.now()
    if archive.exists():
        edited_at = datetime.fromtimestamp(archive.stat().st_mtime)

    if not archive.exists() or edited_at + timedelta(days=7) < datetime.now():
        # TODO Handle different git errors
        # TODO Clone the repo in parallel to avoid blocking
        try:
            git.Repo.clone_from(f"{GITHUB}{repo}", temp_path, branch="master")
            logging.info(f"Cloned {repo}")
        except git.exc.GitCommandError as e:
            logging.error(f"Failed to clone {repo}: {e}")
            return await message.reply(
                f"There is no `{repo}` repo on GitHub!",
                parse_mode="Markdown",
            )

        # TODO Pack the archive in parallel to avoid blocking
        zipf = zipfile.ZipFile(f"{zip_path}.zip", "w", zipfile.ZIP_DEFLATED)
        helpers.zipdir(temp_path, zipf)
        zipf.close()
        logging.info(f"Created archive for {repo}")

        helpers.rmdir(temp_path)
        logging.info(f"Deleted repository of {repo}")

        edited_at = datetime.fromtimestamp(archive.stat().st_mtime)

    edited_at = edited_at.strftime("%Y-%m-%d %H:%M:%S")
    with open(f"{zip_path}.zip", "rb") as f:
        await message.reply_document(
            document=f,
            caption=f"`{repo}` as you wanted!\nDownloaded at: {edited_at}",
            parse_mode="Markdown",
        )
Esempio n. 4
0
def test_quote_paths(sub_done, path):
    dirs = path.split('/')[:-1]
    if len(dirs):
        mkdir(join(sub_done, *dirs))

    f_path = join(sub_done, path)
    with open(join(sub_done, f_path), 'w') as f:
        assert f.write('abc') == 3

    tmp_path = join(sub_done, 'abc')
    rename([f_path], [tmp_path])
    assert not isfile(f_path)
    assert isfile(tmp_path)

    rename([tmp_path], [f_path])
    assert isfile(f_path)
    assert not isfile(tmp_path)

    rm(f_path)
    if len(dirs):
        rmdir(join(sub_done, *dirs))