Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support updating existing credentials #163

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ reg-creds

.. code-block:: text

usage: ss-manager reg-creds [-h] creds_path
usage: ss-manager reg-creds [-h] [creds_path]

Google Docs の API credentials を登録します。詳しい登録方法は :ref:`register_credentials` をご覧ください。

Expand Down
15 changes: 15 additions & 0 deletions docs/register_credentials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Google Docs API を使用可能にする
.. warning::
**Google Docs にある問題文を扱いたい場合は、この操作が必須となります。** 問題文がすべてローカル環境に存在する場合はこの操作は不要です。

Google Docs にある問題文を初めて扱う場合の操作
==============================================

作業ディレクトリ ``WORKING_DIR`` に対して、以下で説明する credentials というものを登録します。

- `Google Docs - Quickstart <https://developers.google.com/docs/api/quickstart/python>`_ の手順通りに進め、API を使える状態にします。リンク先のサンプルを実行できるかどうかで動作確認が可能です。
Expand Down Expand Up @@ -34,3 +37,15 @@ Google Docs API を使用可能にする
:class: highlight

$ ss-manager reg-creds CREDS_PATH

credentials をすでに利用していてエラーが出る場合の操作
======================================================

credentials をすでに利用したことがあっても、有効期限が切れたなどの理由で再登録が必要な場合があります。

以下のコマンドを打つことで、再登録できます。不具合が発生する場合は、「Google Docs にある問題文を初めて扱う場合の操作」を再度行ってください。

.. code-block:: bash
:class: highlight

$ ss-manager reg-creds
50 changes: 29 additions & 21 deletions statements_manager/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import argparse
import pathlib
import pickle
Expand Down Expand Up @@ -88,7 +90,9 @@ def get_parser() -> argparse.ArgumentParser:
)
subparser.add_argument(
"creds_path",
help="path to credentials file (json)\n"
nargs="?",
help="path to credentials file (json). "
"if creds_path is not specified, update existing credentials.\n"
"how to create credentials file: "
"see https://statements-manager.readthedocs.io/ja/stable/register_credentials.html",
)
Expand All @@ -111,35 +115,39 @@ def subcommand_run(


def subcommand_reg_creds(
creds_path: str,
creds_path: str | None,
) -> None:
# 引数は実在するものでなければならない
if not pathlib.Path(creds_path).exists():
logger.error(f"credentials '{creds_path}' does not exist")
raise IOError(f"credentials '{creds_path}' does not exist")

# 隠しディレクトリ
homedir = str(pathlib.Path.home())
hidden_dir = pathlib.Path(homedir, ".ss-manager")
logger.info("register credentials")
if not hidden_dir.exists():
logger.info(f"create hidden directory: {hidden_dir}")
hidden_dir.mkdir()

# 上書きが発生する場合は確認する
creds_savepath = hidden_dir / "credentials.json"
token_path = hidden_dir / "token.pickle"
if token_path.exists() and not ask_ok(
f"{hidden_dir} already exists. Rewrite this?", default_response=False
):
return
if creds_path is not None:
if not hidden_dir.exists():
logger.info(f"create hidden directory: {hidden_dir}")
hidden_dir.mkdir()

# 上書きが発生する場合は確認する
if token_path.exists() and not ask_ok(
f"{hidden_dir} already exists. Rewrite this?", default_response=False
):
return
else:
creds_path = str(creds_savepath.resolve())

logger.info("register credentials")
if not pathlib.Path(creds_path).exists():
logger.error(f"credentials '{creds_path}' does not exist")
raise IOError(f"credentials '{creds_path}' does not exist")

# ファイルを登録
token = create_token(creds_path)
with open(token_path, "wb") as f:
pickle.dump(token, f)
creds_savepath = hidden_dir / "credentials.json"
shutil.copy2(creds_path, creds_savepath)
logger.info("copied credentials successfully.")
if not creds_savepath.exists() or not creds_savepath.samefile(creds_path):
shutil.copy2(creds_path, creds_savepath)
logger.info("copied credentials successfully.")
else:
logger.info("registered credentials successfully.")
logger.debug("reg-creds command ended successfully.")


Expand Down
3 changes: 2 additions & 1 deletion statements_manager/src/convert_task_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def get_docs_contents(self, problem_id: str) -> Tuple[ContentsStatus, str]:
# どのパスでも生成できなかったらエラー
logger.error("cannot get docs contents")
logger.warning(
"tips: try 'ss-manager reg-creds' before running on docs mode.\n"
"tips: try 'ss-manager reg-creds CREDS_PATH' before running on docs mode.\n"
"if you have already registered credentials, try 'ss-manager reg-creds'.\n"
"how to create credentials file: "
"see https://statements-manager.readthedocs.io/ja/stable/register_credentials.html"
)
Expand Down
Loading