Skip to content
This repository was archived by the owner on Mar 27, 2019. It is now read-only.

Commit b14ff97

Browse files
committed
add workspace that clones repo
1 parent c6b0cff commit b14ff97

File tree

8 files changed

+117
-2
lines changed

8 files changed

+117
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ __pycache__/
66
src
77
.cache/
88
python-langserver-cache/
9+
python-cloned-projects-cache/

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ lint:
99

1010
test:
1111
pipenv run pytest test_langserver.py
12-
cd ./test && pipenv run pytest test_*.py
12+
cd ./test && pipenv run pytest test_clone_workspace.py

Pipfile

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jedi = {git = "git://github.com/sourcegraph/jedi.git", editable = true, ref = "9
1111
requirements = {git = "git://github.com/sourcegraph/requirements-parser.git", editable = true, ref = "69f1a9cb916b2995843c3ea9b988da46c9dd65c7"}
1212
opentracing = "*"
1313
lightstep = "*"
14+
"delegator.py" = "*"
1415

1516

1617
[dev-packages]

Pipfile.lock

+22-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

langserver/clone_workspace.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import logging
2+
import os
3+
import os.path
4+
from .config import GlobalConfig
5+
from .fs import FileSystem
6+
from shutil import rmtree
7+
8+
log = logging.getLogger(__name__)
9+
10+
11+
class CloneWorkspace:
12+
13+
def __init__(self, fs: FileSystem, project_root: str,
14+
original_root_path: str= ""):
15+
16+
self.PROJECT_ROOT = project_root
17+
self.repo = None
18+
self.hash = None
19+
20+
if original_root_path.startswith(
21+
"git://") and "?" in original_root_path:
22+
repo_and_hash = original_root_path.split("?")
23+
self.repo = repo_and_hash[0]
24+
original_root_path = self.repo
25+
self.hash = repo_and_hash[1]
26+
27+
if original_root_path.startswith("git://"):
28+
original_root_path = original_root_path[6:]
29+
30+
# turn the original root path into something that can be used as a
31+
# file/path name or cache key
32+
self.key = original_root_path.replace("/", ".").replace("\\", ".")
33+
if self.hash:
34+
self.key = ".".join((self.key, self.hash))
35+
36+
# TODO: allow different Python versions per project/workspace
37+
self.PYTHON_PATH = GlobalConfig.PYTHON_PATH
38+
self.CLONED_PROJECT_PATH = os.path.join(
39+
GlobalConfig.CLONED_PROJECT_PATH, self.key)
40+
log.debug("Setting Python path to %s", self.PYTHON_PATH)
41+
log.debug("Setting Cloned Project path to %s",
42+
self.CLONED_PROJECT_PATH)
43+
44+
self.fs = fs
45+
46+
def clone_project(self):
47+
"""
48+
Clones the project from the provided filesystem into the local
49+
cache
50+
"""
51+
all_files = self.fs.walk(self.PROJECT_ROOT)
52+
for file_path, file_contents in self.fs.batch_open(all_files, parent_span=None):
53+
# strip the leading '/' so that we can join it properly
54+
file_path = os.path.relpath(file_path, "/")
55+
56+
cache_file_path = os.path.join(self.CLONED_PROJECT_PATH, file_path)
57+
58+
os.makedirs(os.path.dirname(cache_file_path), exist_ok=True)
59+
60+
with open(cache_file_path, "w") as f:
61+
f.write(file_contents)
62+
63+
def cleanup(self):
64+
log.info("Removing cloned project cache %s", self.CLONED_PROJECT_PATH)
65+
rmtree(self.CLONED_PROJECT_PATH, ignore_errors=True)

langserver/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ class GlobalConfig:
77

88
PYTHON_PATH = distutils.sysconfig.get_python_lib(standard_lib=True)
99
PACKAGES_PARENT = "python-langserver-cache"
10+
CLONED_PROJECT_PATH = "python-cloned-projects-cache"
1011
STDLIB_REPO_URL = "git://github.com/python/cpython"
1112
STDLIB_SRC_PATH = "Lib"

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exclude =
44
__pycache__,
55
test_langserver.py,
66
*python-langserver-cache*,
7+
*python-cloned-projects-cache*,
78
# don't run flake8 on repo submodules
89
test/repos/*
910
max-line-length=100
@@ -14,6 +15,7 @@ exclude =
1415
__pycache__,
1516
test_langserver.py,
1617
*python-langserver-cache*,
18+
*python-cloned-projects-cache*,
1719
# don't run flake8 on repo submodules
1820
test/repos/*
1921
max-line-length=100

test/test_clone_workspace.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from langserver.clone_workspace import CloneWorkspace
2+
from langserver.fs import TestFileSystem
3+
import delegator
4+
import pytest
5+
6+
7+
@pytest.fixture()
8+
def test_data():
9+
repoPath = "repos/fizzbuzz_service"
10+
workspace = CloneWorkspace(TestFileSystem(repoPath), repoPath, repoPath)
11+
yield (workspace, repoPath)
12+
workspace.cleanup()
13+
14+
15+
class TestCloningWorkspace:
16+
def test_clone(self, test_data):
17+
workspace, repoPath = test_data
18+
workspace.clone_project()
19+
20+
c = delegator.run("diff -r {} {}".format(repoPath,
21+
workspace.CLONED_PROJECT_PATH))
22+
assert c.out == ""
23+
assert c.err == ""
24+
assert c.return_code == 0

0 commit comments

Comments
 (0)