forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
60 lines (50 loc) · 1.81 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Utility functions for use in tests."""
from __future__ import absolute_import
import logging
from os import chdir, environ, getcwd
from os.path import abspath, join as pjoin
from shutil import copytree
import subprocess
from tempfile import mkdtemp
from django_dynamic_fixture import new
from django.contrib.auth.models import User
log = logging.getLogger(__name__)
def check_output(l, env=()):
if env == ():
output = subprocess.Popen(l, stdout=subprocess.PIPE).communicate()[0]
else:
output = subprocess.Popen(l, stdout=subprocess.PIPE,
env=env).communicate()[0]
return output
def make_test_git():
directory = mkdtemp()
path = getcwd()
sample = abspath(pjoin(path, 'rtd_tests/fixtures/sample_repo'))
directory = pjoin(directory, 'sample_repo')
copytree(sample, directory)
env = environ.copy()
env['GIT_DIR'] = pjoin(directory, '.git')
chdir(directory)
log.info(check_output(['git', 'init'] + [directory], env=env))
log.info(check_output(['git', 'add', '.'], env=env))
log.info(check_output(['git', 'commit', '-m"init"'], env=env))
chdir(path)
return directory
def make_test_hg():
directory = mkdtemp()
path = getcwd()
sample = abspath(pjoin(path, 'rtd_tests/fixtures/sample_repo'))
directory = pjoin(directory, 'sample_repo')
copytree(sample, directory)
chdir(directory)
hguser = 'Test User <test-user@readthedocs.org>'
log.info(check_output(['hg', 'init'] + [directory]))
log.info(check_output(['hg', 'add', '.']))
log.info(check_output(['hg', 'commit', '-u', hguser, '-m"init"']))
chdir(path)
return directory
def create_user(username, password, **kwargs):
user = new(User, username=username, **kwargs)
user.set_password(password)
user.save()
return user