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

fix: 对环境判断以处理开发环境由于配置文件路径不存在导致的异常 #225

Merged
merged 5 commits into from Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -5,4 +5,5 @@ __pycache__
.idea/

# Docs out folder
site/
site/
venv
2 changes: 1 addition & 1 deletion kubespider/api/values.py
Expand Up @@ -5,7 +5,6 @@

from api import types


FILE_TYPE_TO_PATH = {
types.FILE_TYPE_COMMON: "Common",
types.FILE_TYPE_VIDEO_TV: "TV",
Expand All @@ -19,6 +18,7 @@
CFG_BASE_PATH = os.path.join(os.getenv('HOME'), '.config/')
CFG_TEMPLATE_PATH = os.path.join(os.getenv('HOME'), '.config_template/')


class Config(str, Enum):
SOURCE_PROVIDER = 'source_provider.yaml'
DOWNLOAD_PROVIDER = 'download_provider.yaml'
Expand Down
8 changes: 8 additions & 0 deletions kubespider/core/config_handler.py
Expand Up @@ -16,6 +16,7 @@
from api.values import Config
from api import values
from utils.config_reader import YamlFileSectionConfigReader, YamlFileConfigReader
from kubespider.utils import helper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-from kubespider.utils import helper
+from utils import helper


import source_provider.mikanani_source_provider.provider as mikanani_source_provider
import source_provider.btbtt12_disposable_source_provider.provider as btbtt12_disposable_source_provider
Expand Down Expand Up @@ -151,6 +152,13 @@ def prepare_config() -> None:
return

logging.info("Config files(%s) miss, try to init them", ','.join(miss_cfg))

# local run, make sure the current working directory like this {repo_root}/kubespider/kubespider
if not helper.is_running_in_docker():
if not os.path.exists(values.CFG_BASE_PATH):
os.makedirs(values.CFG_BASE_PATH)
values.CFG_TEMPLATE_PATH = os.path.join(os.path.dirname(os.getcwd()), '.config/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> os.path.join(os.path.dirname(os.getcwd()), '.config/')
'/Users/.config/'
>>> ^D
richardli1598@richardli1598 [~]
-> % pwd
/Users/richardli1598

这个是预期行为吗?是不是至少应该是 /Users/richardli1598/.config? 那么是不是如下代码更合适:

>>> os.path.join(os.getcwd(), '.config/')
'/Users/richardli1598/.config/'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是预期行为吧,这里源代码结构一般clone下来是有两层kubespider目录吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

例如:代码clone下来在/Users/ljt/code/kubespider 目录下,那么os.getcwd()应该是/Users/ljt/code/kubespider/kubespider,那么os.path.dirname(os.getcwd())就是/Users/ljt/code/kubespider,即代码根目录,join上.config 就是默认配置模板目录

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,感谢解释。
这里麻烦加点注释,说明一下,运行时,需要在 {repo_root}/kubespider/kubespider 下面运行,其他地方运行可能不行

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释和校验容器运行方式按照你说的格式改了,然后我看了下,ci报错好像不是我那段代码报错,我pr里面有个pylint 的style错误,好像是多加了一行空格,这个我也改了。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外,这个导入为啥总失败啊,我本地运行都可以。python真的太神奇了

这个应该如下修改就行:

-from kubespider.utils import helper
+from utils import helper

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外,这个导入为啥总失败啊,我本地运行都可以。python真的太神奇了

这个应该如下修改就行:

-from kubespider.utils import helper
+from utils import helper

嗯这个我修改了,但是我看这个pylint还有导入包顺序限制?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯,现在ok了,一般顺序是 系统,第三方,本项目库

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯,现在ok了

额,开源新手,github这些流程还不太会。麻烦你了

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

欢迎下次贡献,一起共建!


for cfg in miss_cfg:
template_cfg = values.CFG_TEMPLATE_PATH + cfg
target_cfg = values.CFG_BASE_PATH + cfg
Expand Down
5 changes: 5 additions & 0 deletions kubespider/utils/helper.py
Expand Up @@ -3,6 +3,7 @@
import logging
import cgi
import urllib
import os
from urllib.parse import urlparse
from urllib import request

Expand Down Expand Up @@ -91,3 +92,7 @@ def download_torrent_file(url: str, controller: request.OpenerDirector) -> str:
except Exception as err:
logging.error("Download torrent file error:%s", err)
return None

def is_running_in_docker() -> bool:
# check is running in docker container
return os.path.exists('/.dockerenv')