8000 mysite project name no longer-hard coded · pythonanywhere/helper_scripts@5ab3394 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ab3394

Browse files
committed
mysite project name no longer-hard coded
1 parent 9cc2e3f commit 5ab3394

7 files changed

+120
-29
lines changed

pythonanywhere/django_project.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import shutil
33
import subprocess
44
from textwrap import dedent
5+
import os
56

67
from pythonanywhere.api import Webapp
78
from pythonanywhere.exceptions import SanityException
@@ -70,10 +71,27 @@ def run_startproject(self, nuke):
7071
])
7172

7273

74+
def find_django_files(self):
75+
try:
76+
for subfolder in self.project_path.iterdir():
77+
if subfolder.is_dir():
78+
if 'settings.py' in os.listdir(subfolder):
79+
self.settings_path = subfolder / 'settings.py'
80+
if 'manage.py' in os.listdir(self.project_path):
81+
self.manage_py_path = self.project_path / 'manage.py'
82+
83+
except FileNotFoundError:
84+
raise SanityException('Could not find your settings.py')
85+
if not hasattr(self, 'settings_path'):
86+
raise SanityException('Could not find your settings.py')
87+
if not hasattr(self, 'manage_py_path'):
88+
raise SanityException('Could not find your manage.py')
89+
90+
7391
def update_settings_file(self):
7492
print(snakesay('Updating settings.py'))
7593

76-
with open(self.project_path / 'mysite/settings.py') as f:
94+
with open(self.settings_path) as f:
7795
settings = f.read()
7896
new_settings = settings.replace(
7997
'ALLOWED_HOSTS = []',
@@ -86,15 +104,15 @@ def update_settings_file(self):
86104
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
87105
"""
88106
)
89-
with open(self.project_path / 'mysite' / 'settings.py', 'w') as f:
107+
with open(self.settings_path, 'w') as f:
90108
f.write(new_settings)
91109

92110

93111
def run_collectstatic(self):
94112
print(snakesay('Running collectstatic'))
95113
subprocess.check_call([
96114
Path(self.virtualenv_path) / 'bin/python',
97-
self.project_path / 'manage.py',
115+
self.manage_py_path,
98116
'collectstatic',
99117
'--noinput',
100118
])

scripts/pa_autoconfigure_django.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def main(repo_url, domain, python_version, nuke):
3232
project.sanity_checks(nuke=nuke)
3333
project.download_repo(repo_url, nuke=nuke),
3434
project.create_virtualenv(python_version, nuke=nuke)
35+
project.find_django_files()
3536
project.update_settings_file()
3637
project.run_collectstatic()
3738
project.create_webapp(nuke=nuke)

scripts/pa_start_django_webapp_with_virtualenv.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def main(domain, django_version, python_version, nuke):
2828
project.sanity_checks(nuke=nuke)
2929
project.create_virtualenv(python_version, django_version, nuke=nuke)
3030
project.run_startproject(nuke=nuke)
31+
project.find_django_files()
3132
project.update_settings_file()
3233
project.run_collectstatic()
3334
project.create_webapp(nuke=nuke)

tests/test_django_project.py

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import tempfile
55
from textwrap import dedent
66
import pytest
7+
import shutil
8+
import subprocess
79

810
import pythonanywhere.django_project
911
from pythonanywhere.django_project import DjangoProject
@@ -205,14 +207,76 @@ def test_nuke_option_deletes_directory_first(self, mock_subprocess, fake_home):
205207
assert not old_file.exists()
206208

207209

210+
@pytest.fixture
211+
def non_nested_submodule():
212+
subprocess.check_call(['git', 'submodule', 'update', '--init', '--recursive'])
213+
submodule_path = Path(__file__).parents[1] / 'submodules' / 'example-django-project'
214+
subprocess.check_call(
215+
['git', 'checkout', 'master'],
216+
cwd=submodule_path
217+
)
218+
yield submodule_path
219+
subprocess.check_call(
220+
['git', 'checkout', 'master'],
221+
cwd=submodule_path
222+
)
223+
224+
225+
226+
227+
class TestFindDjangoFiles:
228+
229+
def test_non_nested(self, fake_home, non_nested_submodule):
230+
project = DjangoProject('mydomain.com')
231+
shutil.copytree(non_nested_submodule, project.project_path)
232+
expected_settings_path = project.project_path / 'myproject/settings.py'
233+
assert expected_settings_path.exists()
234+
expected_manage_py = project.project_path / 'manage.py'
235+
assert expected_manage_py.exists()
236+
237+
project.find_django_files()
238+
239+
assert project.settings_path == expected_settings_path
240+
assert project.manage_py_path == expected_manage_py
241+
242+
243+
def test_raises_if_empty_project_folder(self, fake_home):
244+
project = DjangoProject('mydomain.com')
245+
with pytest.raises(SanityException) as e:
246+
project.find_django_files()
247+
248+
assert 'Could not find your settings.py' in str(e.value)
249+
250+
251+
def test_raises_if_no_settings_in_any_subfolders(self, fake_home):
252+
project = DjangoProject('mydomain.com')
253+
not_this_folder = project.project_path / 'not_this_folder'
254+
not_this_folder.mkdir(parents=True)
255+
with pytest.raises(SanityException) as e:
256+
project.find_django_files()
257+
258+
assert 'Could not find your settings.py' in str(e.value)
259+
260+
261+
def test_raises_if_manage_py_not_found(self, fake_home, non_nested_submodule):
262+
project = DjangoProject('mydomain.com')
263+
shutil.copytree(non_nested_submodule, project.project_path)
264+
expected_manage_py = project.project_path / 'manage.py'
265+
assert expected_manage_py.exists()
266+
expected_manage_py.unlink()
267+
with pytest.raises(SanityException) as e:
268+
project.find_django_files()
269+
270+
assert 'Could not find your manage.py' in str(e.value)
271+
208272

209273
class TestUpdateSettingsFile:
210274

211275
def test_adds_STATIC_and_MEDIA_config_to_settings(self):
212276
project = DjangoProject('mydomain.com')
213-
project.project_path = Path(tempfile.mkdtemp())
214-
(project.project_path / 'mysite').mkdir(parents=True)
215-
with open(project.project_path / 'mysite/settings.py', 'w') as f:
277+
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
278+
279+
with open(project.settings_path, 'w') as f:
216280
f.write(dedent(
217281
"""
218282
# settings file
@@ -223,10 +287,9 @@ def test_adds_STATIC_and_MEDIA_config_to_settings(self):
223287

224288
project.update_settings_file()
225289

226-
with open(project.project_path / 'mysite/settings.py') as f:
227-
contents = f.read()
290+
with open(project.settings_path) as f:
291+
lines = f.read().split('\n')
228292

229-
lines = contents.split('\n')
230293
assert "STATIC_URL = '/static/'" in lines
231294
assert "MEDIA_URL = '/media/'" in lines
232295
assert "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" in lines
@@ -235,9 +298,9 @@ def test_adds_STATIC_and_MEDIA_config_to_settings(self):
235298

236299
def test_adds_domain_to_ALLOWED_HOSTS(self):
237300
project = DjangoProject('mydomain.com')
238-
project.project_path = Path(tempfile.mkdtemp())
239-
(project.project_path / 'mysite').mkdir(parents=True)
240-
with open(project.project_path / 'mysite/settings.py', 'w') as f:
301+
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
302+
303+
with open(project.settings_path, 'w') as f:
241304
f.write(dedent(
242305
"""
243306
# settings file
@@ -248,10 +311,8 @@ def test_adds_domain_to_ALLOWED_HOSTS(self):
248311

249312
project.update_settings_file()
250313

251-
with open(project.project_path / 'mysite/settings.py') as f:
252-
contents = f.read()
253-
254-
lines = contents.split('\n')
314+
with open(project.settings_path) as f:
315+
lines = f.read().split('\n')
255316

256317
assert "ALLOWED_HOSTS = ['mydomain.com']" in lines
257318

@@ -261,10 +322,14 @@ class TestRunCollectStatic:
261322

262323
def test_runs_manage_py_in_correct_virtualenv(self, mock_subprocess, fake_home):
263324
project = DjangoProject('mydomain.com')
264-
project.virtualenv_path = '/path/to/virtualenv'
325+
project.virtualenv_path = Path('/path/to/virtualenv')
326+
project.manage_py_path = Path('/path/to/manage.py')
265327
project.run_collectstatic()
266328
assert mock_subprocess.check_call.call_args == call([
267-
Path(project.virtualenv_path) / 'bin/python', project.project_path / 'manage.py', 'collectstatic', '--noinput'
329+
project.virtualenv_path / 'bin/python',
330+
project.manage_py_path,
331+
'collectstatic',
332+
'--noinput'
268333
])
269334

270335

tests/test_pa_autoconfigure_django.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_calls_all_stuff_in_right_order(self):
1818
call.sanity_checks(nuke='nuke option'),
1919
call.download_repo('repo.url', nuke='nuke option'),
2020
call.create_virtualenv('python.version', nuke='nuke option'),
21+
call.find_django_files(),
2122
call.update_settings_file(),
2223
call.run_collectstatic(),
2324
call.create_webapp(nuke='nuke option'),
@@ -48,33 +49,37 @@ def test_lowercases_username(self):
4849

4950
@pytest.mark.slowtest
5051
def test_actually_works_against_example_repo(self, fake_home, virtualenvs_folder, api_token):
52+
repo = 'https://github.com/hjwp/example-django-project.git'
53+
domain = 'mydomain.com'
5154
with patch('scripts.pa_autoconfigure_django.DjangoProject.update_wsgi_file'):
5255
with patch('pythonanywhere.api.call_api'):
53-
main(
54-
'https://github.com/hjwp/example-django-project.git',
55-
'mydomain.com',
56-
'2.7',
57-
nuke=False,
58-
)
56+
main(repo, domain, '2.7', nuke=False)
57+
58+
expected_django_version = '1.11.1'
59+
expected_virtualenv = virtualenvs_folder / domain
60+
expected_project_path = fake_home / domain
61+
django_project_name = 'myproject'
62+
expected_settings_path = expected_project_path / django_project_name / 'settings.py'
5963

6064
django_version = subprocess.check_output([
61-
virtualenvs_folder / 'mydomain.com/bin/python',
65+
expected_virtualenv / 'bin/python',
6266
'-c'
6367
'import django; print(django.get_version())'
6468
]).decode().strip()
65-
assert django_version == '1.11.1'
69+
assert django_version == expected_django_version
6670

67-
with open(fake_home / 'mydomain.com/mysite/settings.py') as f:
71+
with open(expected_settings_path) as f:
6872
lines = f.read().split('\n')
6973
assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" in lines
7074
assert "ALLOWED_HOSTS = ['mydomain.com']" in lines
7175

72-
assert 'base.css' in os.listdir(fake_home / 'mydomain.com/static/admin/css')
76+
assert 'base.css' in os.listdir(fake_home / domain / 'static/admin/css')
7377

7478

7579

7680

7781
def xtest_todos():
82+
assert not 'mysite hard-coded'
7883
assert not 'existing-project sanity checks eg settings.py not found, requirements empty'
7984
assert not 'nuke option shouldnt barf if nothing to nuke'
8085
assert not 'SECRET_KEY'

tests/test_pa_start_django_webapp_with_virtualenv.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def test_calls_all_stuff_in_right_order(self):
2020
call.sanity_checks(nuke='nuke option'),
2121
call.create_virtualenv('python.version', 'django.version', nuke='nuke option'),
2222
call.run_startproject(nuke='nuke option'),
23+
call.find_django_files(),
2324
call.update_settings_file(),
2425
call.run_collectstatic(),
2526
call.create_webapp(nuke='nuke option'),

0 commit comments

Comments
 (0)
0