【问题标题】:ReadTheDocs set version to active via scriptReadTheDocs 通过脚本将版本设置为活动
【发布时间】:2018-02-27 11:44:42
【问题描述】:

我正在尝试以编程方式设置 ReadTheDocs.com(即 ReadTheDocs 的商业端)版本的活动状态。

这个想法是,一个分支在创建时会为其构建文档,而当分支结束时,我们会删除版本文档(或至少停止为其构建)。

显然,后者只是清理而不那么重要(想要,不需要)。但我们强烈希望避免使用项目管理界面将每个分支/版本设置为活动状态。

我一直在尝试使用 RTD 提供的 v2 REST API。我可以从“GET https://readthedocs.com/api/v2/version/”中提取版本数据并找到我想要处理的版本,但我无法发回数据,或者找到让我为给定版本 ID 设置 Version.active=True 的东西他们的 API。

我对如何使用这些 API 不太了解,因此我们将不胜感激。

我正在使用 python 和 requests 库。

【问题讨论】:

    标签: python python-requests read-the-docs


    【解决方案1】:

    我为此搜索了一个解决方案,因为我在自动化与我的 Git 服务器相关的文档的构建过程时遇到了同样的问题。

    最后,我找到了两种不同的方法来更改项目版本并使用脚本将它们设置为活动状态。两个脚本都模拟发送到 read-the-docs 服务器的 http 请求。我有一个带有 http(没有 https)的本地运行实例,它可以工作,但我不知道它是否也适用于 https。 也许有必要通过 Wireshark 捕获数据包并调整脚本。

    第一个脚本(使用 Python):

    def set_version_active_on_rtd():
    
        server_addr =  "http://192.168.1.100:8000"
        project_slug = "myProject"
        rtd_user = 'mylogin'
        rtd_password = 'mypassword'
    
        with requests.session() as s:
            url = server_addr + "/accounts/login/"
            # fetch the login page
            s.get(url)
            if 'csrftoken' in s.cookies:
                # Django 1.6 and up
                csrftoken = s.cookies['csrftoken']
            else:
                # older versions
                csrftoken = s.cookies['csrf']
    
    
            login_data = dict(login=rtd_user, password=rtd_password, csrfmiddlewaretoken=csrftoken, next='/')
            r = s.post(url, data=login_data, headers=dict(Referer=url))
            url = server_addr+"/dashboard/"+project_slug+"/versions/"
            if 'csrftoken' in s.cookies:
                # Django 1.6 and up
                csrftoken = s.cookies['csrftoken']
            else:
                # older versions
                csrftoken = s.cookies['csrf']
    
            '''
            These settings which are saved in version_data, are configured normally with help of the webinterface.
    
            To set a version active, it must be configured with 
                            'version-<version_number>':'on'
            and its privacy must be set like 
                            'privacy-<version_number>':'public'
    
            To disable a version, its privacy has to be set and no other entry with 'on' has to be supplied
            '''
            version_data = {'default-version': 'latest', 'version-latest': 'on', 'privacy-latest' : 'public', 'privacy-master':'public','csrfmiddlewaretoken': csrftoken}
            r = s.post(url, data = version_data, headers=dict(Referer=url))
    

    第二个脚本(bash 和 cUrl):

    #!/bin/bash
    RTD_SERVER='http://192.168.1.100:8000'
    RTD_LOGIN='mylogin'
    RTD_PASSWORD='mypassword'
    RTD_SLUG='myProject'
    
    #fetch login page and save first cookie
    curl -c cookie1.txt "$RTD_SERVER"/accounts/login/ > /dev/null
    
    #extract token from first cookie
    TOKEN1=$(tail -n1 cookie1.txt | awk 'NF>1{print $NF}')
    
    #login and send first cookie and save second cookie
    curl -b cookie1.txt -c cookie2.txt -X POST -d 
    "csrfmiddlewaretoken=$TOKEN1&login=$RTD_LOGIN&\
    password=$RTD_PASSWORD&next=/dashboard/"$RTD_SLUG"/versions/" 
    "$RTD_SERVER"/accounts/login/ > /dev/null
    
    #extract token from second cookie
    TOKEN2=$(tail -n3 cookie2.txt | awk 'NF>1{print $NF}' | head -n1)
    
    # send data for the versions to the rtd server using the second cookie
    curl -b cookie2.txt -X POST -d "csrfmiddlewaretoken=$TOKEN2&default- 
    version=latest&version-master=on&privacy-master=public&\
    version-latest=on&privacy-latest=public" 
    $RTD_SERVER"/dashboard/"$RTD_SLUG"/versions/ > /dev/null
    
    #delete the cookies
    rm cookie1.txt cookie2.txt
    

    要设置默认版本,如果版本未设置为活动,则可能需要运行脚本两次。在第一次运行时激活版本,在第二次运行时将其设置为默认版本。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 2016-07-15
      • 2015-03-18
      相关资源
      最近更新 更多