auto-run-on-remote


Nameauto-run-on-remote JSON
Version 0.4 PyPI version JSON
download
home_pagehttps://github.com/ydf0509/auto_run_on_remote
Summaryauto run current python script on remote server
upload_time2023-03-08 01:28:54
maintainerydf
docs_urlNone
authorbfzs
requires_python
licenseBSD License
keywords auto_run_on_remote
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 1 安装

pip install auto_run_on_remote

# 2 auto_run_on_remote 介绍

```
全名字含义应该是 auto_run_current_python_script_on_remote_server

在本机运行脚本自动在远程机器上运行。

自动上传文件夹 自动创建文件夹 自动远程机器上运行python脚本。
原理是 run_current_script_on_remote() 函数中自动利用 sys._getframe(1).f_code.co_filename 获取当前文件位置,所以不用传参告诉函数当前脚本的位置。

比pycahrm专业版复杂的配置调用远程python解释器更方便,因为pycahrm专业版如果设置了远程解释器启动脚本时候很卡,
启动ide界面更卡,因为ide会去读远程解释器的所有安装的python包,真的是太卡了。
而这个在远程运行启动速度就很快,丝毫不会造成ide卡顿。
还有就是破解最新版pycahrm麻烦老失效,用社区版就好了
```

# 3 用法如下
```
在项目的任意目录层级下的脚本中运行 run_current_script_on_remote(),则脚本会自动运行在远程机器。
当第一次运行脚本时候,会自动在你当项目的根目录生成 auto_run_on_remote_config.py 配置文件,然后,你自己按需修改其中的值。
以后运行run_current_script_on_remote()会自动读取到 auto_run_on_remote_config.py  中的配置。
```

```python
import time
import sys

from auto_run_on_remote import run_current_script_on_remote


run_current_script_on_remote()
# 以下的代码的print都不是在当前机器打印的,是在远程机器上打印的。

for i in range(10):
    print(f'嘻嘻 {i},通过文件路径和python解释器路径,可以发现这句话是在远程机器打印出来的, {__file__} ,{sys.executable} ')
    time.sleep(1)

```



代码运行截图,我本机是win10,可以看到代码是运行在linux的。
[![hXvZff.png](https://z3.ax1x.com/2021/09/10/hXvZff.png)](https://imgtu.com/i/hXvZff)


# 4 配置文件 auto_run_on_remote_config.py 介绍
```python
"""
这个配置文件是自动生成到你的项目根目录的。
"""

import sys

# 项目根目录文件夹,这个一般不需要改,会根据PYTHONPATH智能获取。
# pycahrm自动添加了项目根目录到第一个PYTHONPATH,如果是cmd命令启动这先设置PYTHONPATH环境变量。
# windows设置  set PYTHONPATH=你当前python项目根目录,然后敲击你的python运行命令
# linux设置    export PYTHONPATH=你当前python项目根目录,然后敲击你的python运行命令
PYTHON_PROJ_DIR_LOCAL = sys.path[1]

# 这是远程机器的账号密码配置。把这个配置文件加到gitignore就不会泄漏了。
HOST = '192.168.6.133'
PORT = 22
USER = 'ydf'
PASSWORD = '123456'

PYTHON_INTERPRETER = 'python3'  # 如果你安装了四五个python环境,可以直接指定远程解释器的绝对路径  例如 /opt/minicondadir/ens/env35/python

FORBID_DEPLOY_FROM_LINUX = True # 一般生产机器是linux,是否禁止从linux部署到别的机器,这样可以防止你从生产环境远程到测试环境,配置后,即使生产环境的代码有远程部署,也不会执行远程部署而是直接运行。

# 上传文件夹的配置,具体可以看paramiko_util.py里面的代码。
PATH_PATTERN_EXLUDED_TUPLE = ('/.git/', '/.idea/', '/dist/', '/build/')  # 路径中如果有这些就自动过滤不上传
FILE_SUFFIX_TUPLE_EXLUDED = ('.pyc', '.log', '.gz')  # 这些后缀的文件不上传
ONLY_UPLOAD_WITHIN_THE_LAST_MODIFY_TIME = 3650 * 24 * 60 * 60  # 只有在这个时间之内修改的文件才上传。如果项目比较大,可以第一次完整上传,之后再把这个时间改小。
FILE_VOLUME_LIMIT = 1000 * 1000  # 大于这个体积的文件不上传,单位b。
SFTP_LOG_LEVEL = 20  # 文件夹上传时候的日志级别。10 logging.DEBUG ,20 logging.INFO 30 logging.WaRNING,如果要看为什么某个文件上传失败,可以设置debug级别。

EXTRA_SHELL_STR = ''  # 远程执行命令之前,可以自定义执行的shell语句,一般例如可以设置啥环境变量什么的。


```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ydf0509/auto_run_on_remote",
    "name": "auto-run-on-remote",
    "maintainer": "ydf",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "ydf0509@sohu.com",
    "keywords": "auto_run_on_remote",
    "author": "bfzs",
    "author_email": "ydf0509@sohu.com",
    "download_url": "https://files.pythonhosted.org/packages/08/ac/ee5b9c7195bbf8e53064e2ad9726e8b070b9d91ad36a24abf9cfd081b2bb/auto_run_on_remote-0.4.tar.gz",
    "platform": "all",
    "description": "# 1 \u5b89\u88c5\r\n\r\npip install auto_run_on_remote\r\n\r\n# 2 auto_run_on_remote \u4ecb\u7ecd\r\n\r\n```\r\n\u5168\u540d\u5b57\u542b\u4e49\u5e94\u8be5\u662f auto_run_current_python_script_on_remote_server\r\n\r\n\u5728\u672c\u673a\u8fd0\u884c\u811a\u672c\u81ea\u52a8\u5728\u8fdc\u7a0b\u673a\u5668\u4e0a\u8fd0\u884c\u3002\r\n\r\n\u81ea\u52a8\u4e0a\u4f20\u6587\u4ef6\u5939 \u81ea\u52a8\u521b\u5efa\u6587\u4ef6\u5939 \u81ea\u52a8\u8fdc\u7a0b\u673a\u5668\u4e0a\u8fd0\u884cpython\u811a\u672c\u3002\r\n\u539f\u7406\u662f run_current_script_on_remote() \u51fd\u6570\u4e2d\u81ea\u52a8\u5229\u7528 sys._getframe(1).f_code.co_filename \u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u4f4d\u7f6e\uff0c\u6240\u4ee5\u4e0d\u7528\u4f20\u53c2\u544a\u8bc9\u51fd\u6570\u5f53\u524d\u811a\u672c\u7684\u4f4d\u7f6e\u3002\r\n\r\n\u6bd4pycahrm\u4e13\u4e1a\u7248\u590d\u6742\u7684\u914d\u7f6e\u8c03\u7528\u8fdc\u7a0bpython\u89e3\u91ca\u5668\u66f4\u65b9\u4fbf\uff0c\u56e0\u4e3apycahrm\u4e13\u4e1a\u7248\u5982\u679c\u8bbe\u7f6e\u4e86\u8fdc\u7a0b\u89e3\u91ca\u5668\u542f\u52a8\u811a\u672c\u65f6\u5019\u5f88\u5361\uff0c\r\n\u542f\u52a8ide\u754c\u9762\u66f4\u5361\uff0c\u56e0\u4e3aide\u4f1a\u53bb\u8bfb\u8fdc\u7a0b\u89e3\u91ca\u5668\u7684\u6240\u6709\u5b89\u88c5\u7684python\u5305\uff0c\u771f\u7684\u662f\u592a\u5361\u4e86\u3002\r\n\u800c\u8fd9\u4e2a\u5728\u8fdc\u7a0b\u8fd0\u884c\u542f\u52a8\u901f\u5ea6\u5c31\u5f88\u5feb\uff0c\u4e1d\u6beb\u4e0d\u4f1a\u9020\u6210ide\u5361\u987f\u3002\r\n\u8fd8\u6709\u5c31\u662f\u7834\u89e3\u6700\u65b0\u7248pycahrm\u9ebb\u70e6\u8001\u5931\u6548\uff0c\u7528\u793e\u533a\u7248\u5c31\u597d\u4e86\r\n```\r\n\r\n# 3 \u7528\u6cd5\u5982\u4e0b\r\n```\r\n\u5728\u9879\u76ee\u7684\u4efb\u610f\u76ee\u5f55\u5c42\u7ea7\u4e0b\u7684\u811a\u672c\u4e2d\u8fd0\u884c run_current_script_on_remote(),\u5219\u811a\u672c\u4f1a\u81ea\u52a8\u8fd0\u884c\u5728\u8fdc\u7a0b\u673a\u5668\u3002\r\n\u5f53\u7b2c\u4e00\u6b21\u8fd0\u884c\u811a\u672c\u65f6\u5019\uff0c\u4f1a\u81ea\u52a8\u5728\u4f60\u5f53\u9879\u76ee\u7684\u6839\u76ee\u5f55\u751f\u6210 auto_run_on_remote_config.py \u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\uff0c\u4f60\u81ea\u5df1\u6309\u9700\u4fee\u6539\u5176\u4e2d\u7684\u503c\u3002\r\n\u4ee5\u540e\u8fd0\u884crun_current_script_on_remote()\u4f1a\u81ea\u52a8\u8bfb\u53d6\u5230 auto_run_on_remote_config.py  \u4e2d\u7684\u914d\u7f6e\u3002\r\n```\r\n\r\n```python\r\nimport time\r\nimport sys\r\n\r\nfrom auto_run_on_remote import run_current_script_on_remote\r\n\r\n\r\nrun_current_script_on_remote()\r\n# \u4ee5\u4e0b\u7684\u4ee3\u7801\u7684print\u90fd\u4e0d\u662f\u5728\u5f53\u524d\u673a\u5668\u6253\u5370\u7684\uff0c\u662f\u5728\u8fdc\u7a0b\u673a\u5668\u4e0a\u6253\u5370\u7684\u3002\r\n\r\nfor i in range(10):\r\n    print(f'\u563b\u563b {i},\u901a\u8fc7\u6587\u4ef6\u8def\u5f84\u548cpython\u89e3\u91ca\u5668\u8def\u5f84\uff0c\u53ef\u4ee5\u53d1\u73b0\u8fd9\u53e5\u8bdd\u662f\u5728\u8fdc\u7a0b\u673a\u5668\u6253\u5370\u51fa\u6765\u7684, {__file__} ,{sys.executable} ')\r\n    time.sleep(1)\r\n\r\n```\r\n\r\n\r\n\r\n\u4ee3\u7801\u8fd0\u884c\u622a\u56fe\uff0c\u6211\u672c\u673a\u662fwin10\uff0c\u53ef\u4ee5\u770b\u5230\u4ee3\u7801\u662f\u8fd0\u884c\u5728linux\u7684\u3002\r\n[![hXvZff.png](https://z3.ax1x.com/2021/09/10/hXvZff.png)](https://imgtu.com/i/hXvZff)\r\n\r\n\r\n# 4 \u914d\u7f6e\u6587\u4ef6 auto_run_on_remote_config.py \u4ecb\u7ecd\r\n```python\r\n\"\"\"\r\n\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u662f\u81ea\u52a8\u751f\u6210\u5230\u4f60\u7684\u9879\u76ee\u6839\u76ee\u5f55\u7684\u3002\r\n\"\"\"\r\n\r\nimport sys\r\n\r\n# \u9879\u76ee\u6839\u76ee\u5f55\u6587\u4ef6\u5939\uff0c\u8fd9\u4e2a\u4e00\u822c\u4e0d\u9700\u8981\u6539\uff0c\u4f1a\u6839\u636ePYTHONPATH\u667a\u80fd\u83b7\u53d6\u3002\r\n# pycahrm\u81ea\u52a8\u6dfb\u52a0\u4e86\u9879\u76ee\u6839\u76ee\u5f55\u5230\u7b2c\u4e00\u4e2aPYTHONPATH\uff0c\u5982\u679c\u662fcmd\u547d\u4ee4\u542f\u52a8\u8fd9\u5148\u8bbe\u7f6ePYTHONPATH\u73af\u5883\u53d8\u91cf\u3002\r\n# windows\u8bbe\u7f6e  set PYTHONPATH=\u4f60\u5f53\u524dpython\u9879\u76ee\u6839\u76ee\u5f55,\u7136\u540e\u6572\u51fb\u4f60\u7684python\u8fd0\u884c\u547d\u4ee4\r\n# linux\u8bbe\u7f6e    export PYTHONPATH=\u4f60\u5f53\u524dpython\u9879\u76ee\u6839\u76ee\u5f55,\u7136\u540e\u6572\u51fb\u4f60\u7684python\u8fd0\u884c\u547d\u4ee4\r\nPYTHON_PROJ_DIR_LOCAL = sys.path[1]\r\n\r\n# \u8fd9\u662f\u8fdc\u7a0b\u673a\u5668\u7684\u8d26\u53f7\u5bc6\u7801\u914d\u7f6e\u3002\u628a\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u52a0\u5230gitignore\u5c31\u4e0d\u4f1a\u6cc4\u6f0f\u4e86\u3002\r\nHOST = '192.168.6.133'\r\nPORT = 22\r\nUSER = 'ydf'\r\nPASSWORD = '123456'\r\n\r\nPYTHON_INTERPRETER = 'python3'  # \u5982\u679c\u4f60\u5b89\u88c5\u4e86\u56db\u4e94\u4e2apython\u73af\u5883\uff0c\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u8fdc\u7a0b\u89e3\u91ca\u5668\u7684\u7edd\u5bf9\u8def\u5f84  \u4f8b\u5982 /opt/minicondadir/ens/env35/python\r\n\r\nFORBID_DEPLOY_FROM_LINUX = True # \u4e00\u822c\u751f\u4ea7\u673a\u5668\u662flinux\uff0c\u662f\u5426\u7981\u6b62\u4ecelinux\u90e8\u7f72\u5230\u522b\u7684\u673a\u5668\uff0c\u8fd9\u6837\u53ef\u4ee5\u9632\u6b62\u4f60\u4ece\u751f\u4ea7\u73af\u5883\u8fdc\u7a0b\u5230\u6d4b\u8bd5\u73af\u5883\uff0c\u914d\u7f6e\u540e\uff0c\u5373\u4f7f\u751f\u4ea7\u73af\u5883\u7684\u4ee3\u7801\u6709\u8fdc\u7a0b\u90e8\u7f72\uff0c\u4e5f\u4e0d\u4f1a\u6267\u884c\u8fdc\u7a0b\u90e8\u7f72\u800c\u662f\u76f4\u63a5\u8fd0\u884c\u3002\r\n\r\n# \u4e0a\u4f20\u6587\u4ef6\u5939\u7684\u914d\u7f6e\uff0c\u5177\u4f53\u53ef\u4ee5\u770bparamiko_util.py\u91cc\u9762\u7684\u4ee3\u7801\u3002\r\nPATH_PATTERN_EXLUDED_TUPLE = ('/.git/', '/.idea/', '/dist/', '/build/')  # \u8def\u5f84\u4e2d\u5982\u679c\u6709\u8fd9\u4e9b\u5c31\u81ea\u52a8\u8fc7\u6ee4\u4e0d\u4e0a\u4f20\r\nFILE_SUFFIX_TUPLE_EXLUDED = ('.pyc', '.log', '.gz')  # \u8fd9\u4e9b\u540e\u7f00\u7684\u6587\u4ef6\u4e0d\u4e0a\u4f20\r\nONLY_UPLOAD_WITHIN_THE_LAST_MODIFY_TIME = 3650 * 24 * 60 * 60  # \u53ea\u6709\u5728\u8fd9\u4e2a\u65f6\u95f4\u4e4b\u5185\u4fee\u6539\u7684\u6587\u4ef6\u624d\u4e0a\u4f20\u3002\u5982\u679c\u9879\u76ee\u6bd4\u8f83\u5927\uff0c\u53ef\u4ee5\u7b2c\u4e00\u6b21\u5b8c\u6574\u4e0a\u4f20\uff0c\u4e4b\u540e\u518d\u628a\u8fd9\u4e2a\u65f6\u95f4\u6539\u5c0f\u3002\r\nFILE_VOLUME_LIMIT = 1000 * 1000  # \u5927\u4e8e\u8fd9\u4e2a\u4f53\u79ef\u7684\u6587\u4ef6\u4e0d\u4e0a\u4f20\uff0c\u5355\u4f4db\u3002\r\nSFTP_LOG_LEVEL = 20  # \u6587\u4ef6\u5939\u4e0a\u4f20\u65f6\u5019\u7684\u65e5\u5fd7\u7ea7\u522b\u300210 logging.DEBUG ,20 logging.INFO 30 logging.WaRNING,\u5982\u679c\u8981\u770b\u4e3a\u4ec0\u4e48\u67d0\u4e2a\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25\uff0c\u53ef\u4ee5\u8bbe\u7f6edebug\u7ea7\u522b\u3002\r\n\r\nEXTRA_SHELL_STR = ''  # \u8fdc\u7a0b\u6267\u884c\u547d\u4ee4\u4e4b\u524d\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6267\u884c\u7684shell\u8bed\u53e5\uff0c\u4e00\u822c\u4f8b\u5982\u53ef\u4ee5\u8bbe\u7f6e\u5565\u73af\u5883\u53d8\u91cf\u4ec0\u4e48\u7684\u3002\r\n\r\n\r\n```\r\n",
    "bugtrack_url": null,
    "license": "BSD License",
    "summary": "auto run current python script on remote server",
    "version": "0.4",
    "project_urls": {
        "Homepage": "https://github.com/ydf0509/auto_run_on_remote"
    },
    "split_keywords": [
        "auto_run_on_remote"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "08acee5b9c7195bbf8e53064e2ad9726e8b070b9d91ad36a24abf9cfd081b2bb",
                "md5": "ff15a9cb9f0918f2502b1a09c93c5e6d",
                "sha256": "833929058a9d89d19af3340546675e7902b4816326816d4b2af9e89d961ff77b"
            },
            "downloads": -1,
            "filename": "auto_run_on_remote-0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "ff15a9cb9f0918f2502b1a09c93c5e6d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9946,
            "upload_time": "2023-03-08T01:28:54",
            "upload_time_iso_8601": "2023-03-08T01:28:54.295020Z",
            "url": "https://files.pythonhosted.org/packages/08/ac/ee5b9c7195bbf8e53064e2ad9726e8b070b9d91ad36a24abf9cfd081b2bb/auto_run_on_remote-0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-08 01:28:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ydf0509",
    "github_project": "auto_run_on_remote",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "auto-run-on-remote"
}
        
Elapsed time: 0.06362s