ReadTheDocs通过脚本将版本设置为活动状态

时间:2018-02-27 11:44:43

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

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

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

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

我一直在尝试使用RTD提供的v2 REST API。我可以从" GET https://readthedocs.com/api/v2/version/"中提取版本数据。并找到我想要的版本,但我无法发回数据,或者找不到允许我为其API中的给定版本ID设置Version.active = True的内容。

我对如何使用这些API并不是很了解,所以我们非常感谢您的帮助。

我正在使用python和请求库。

1 个答案:

答案 0 :(得分:0)

我为此寻找了解决方案,因为在与Git服务器相关的文档的自动构建过程中,我遇到了同样的问题。

最后,我发现了两种不同的方式来更改项目版本并将其设置为通过脚本激活。这两个脚本都模拟发送到文档读取服务器的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

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

希望有帮助

相关问题