Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/smc_pyutil/smc_pyutil/api.py
Views: 285
1
# -*- coding: utf-8 -*-
2
3
# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
4
# License: AGPLv3 s.t. "Commons Clause" – read LICENSE.md for details
5
6
# This code will work in both Python 2 and Python 3 to get
7
# the history of edits of a file in a project, using the
8
# project HTTP API.
9
10
from __future__ import absolute_import
11
12
13
def get_syncdoc_history(path, patches=False):
14
"""
15
Get the history of all edits to the given file. The path
16
to the file must be relative to the HOME directory of the
17
project, e.g., for the file $HOME/foo/a.md, set path to
18
"foo/a.md".
19
20
If patches is False (the default), only the lengths of patches
21
are included in the history. If patches is True, the actual
22
patches themselves are included.
23
"""
24
import json, os, requests
25
if 'COCALC_SECRET_TOKEN' in os.environ:
26
secret_token_file = os.environ['COCALC_SECRET_TOKEN']
27
else:
28
# fallback for cc-in-cc dev and cocalc-docker:
29
secret_token_file = os.path.join(os.environ['SMC'], 'secret_token')
30
secret_token = open(secret_token_file).read().strip()
31
port_file = os.path.join(os.environ['SMC'], "local_hub/api_server.port")
32
if not os.path.exists(port_file):
33
raise RuntimeError("restart your project to start the api server")
34
port = open(port_file).read().strip()
35
data = {"path": path}
36
if patches:
37
data['patches'] = True
38
x = json.loads(
39
requests.post('http://localhost:%s/api/v1/get_syncdoc_history' % port,
40
auth=(secret_token, ''),
41
data=data).text)
42
if 'error' in x:
43
raise RuntimeError(x['error'])
44
else:
45
return x['history']
46
47