Skip to content

Commit 540fa57

Browse files
author
Joseph Atkins-Turkish
committed
Remove support for AWS_ENABLED==False
1 parent 86fb8be commit 540fa57

File tree

10 files changed

+53
-163
lines changed

10 files changed

+53
-163
lines changed

app.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"AWS_ACCESS_KEY_ID": {
88
"required": true
99
},
10-
"AWS_ENABLED": "yes",
1110
"AWS_S3_BUILDS_BUCKET": "builds-staging.cloudpebble.net",
1211
"AWS_S3_EXPORT_BUCKET": "export-staging.cloudpebble.net",
1312
"AWS_S3_SOURCE_BUCKET": "source-staging.cloudpebble.net",

cloudpebble/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@
341341
MAILCHIMP_API_KEY = _environ.get('MAILCHIMP_API_KEY', None)
342342
MAILCHIMP_LIST_ID = _environ.get('MAILCHIMP_LIST_ID', None)
343343

344-
AWS_ENABLED = 'AWS_ENABLED' in _environ
345344
AWS_ACCESS_KEY_ID = _environ.get('AWS_ACCESS_KEY_ID', None)
346345
AWS_SECRET_ACCESS_KEY = _environ.get('AWS_SECRET_ACCESS_KEY', None)
347346

ide/api/project.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ def save_project_dependencies(request, project_id):
263263
else:
264264
send_td_event('cloudpebble_save_project_settings', request=request, project=project)
265265

266+
266267
@require_POST
267268
@login_required
268269
@json_view

ide/api/resource.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
from django.conf import settings
32
from django.contrib.auth.decorators import login_required
43
from django.db import transaction, IntegrityError
54
from django.http import HttpResponse, HttpResponseRedirect
@@ -238,13 +237,8 @@ def show_resource(request, project_id, resource_id, variant):
238237
}
239238
content_disposition = "attachment; filename=\"%s\"" % resource.file_name
240239
content_type = content_types[resource.kind]
241-
if settings.AWS_ENABLED:
242-
headers = {
243-
'response-content-disposition': content_disposition,
244-
'Content-Type': content_type
245-
}
246-
return HttpResponseRedirect(s3.get_signed_url('source', variant.s3_path, headers=headers))
247-
else:
248-
response = HttpResponse(open(variant.local_filename), content_type=content_type)
249-
response['Content-Disposition'] = content_disposition
250-
return response
240+
headers = {
241+
'response-content-disposition': content_disposition,
242+
'Content-Type': content_type
243+
}
244+
return HttpResponseRedirect(s3.get_signed_url('source', variant.s3_path, headers=headers))

ide/models/build.py

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import uuid
22
import json
3-
import shutil
4-
import os
5-
import os.path
63
from django.conf import settings
74
from django.db import models
85
from ide.models.project import Project
@@ -42,19 +39,10 @@ class BuildResult(IdeModel):
4239
finished = models.DateTimeField(blank=True, null=True)
4340

4441
def _get_dir(self):
45-
if settings.AWS_ENABLED:
46-
return '%s/' % self.uuid
47-
else:
48-
path = '%s%s/%s/%s/' % (settings.MEDIA_ROOT, self.uuid[0], self.uuid[1], self.uuid)
49-
if not os.path.exists(path):
50-
os.makedirs(path)
51-
return path
42+
return '%s/' % self.uuid
5243

5344
def get_url(self):
54-
if settings.AWS_ENABLED:
55-
return "%s%s/" % (settings.MEDIA_URL, self.uuid)
56-
else:
57-
return '%s%s/%s/%s/' % (settings.MEDIA_URL, self.uuid[0], self.uuid[1], self.uuid)
45+
return "%s%s/" % (settings.MEDIA_URL, self.uuid)
5846

5947
@property
6048
def pbw(self):
@@ -88,46 +76,24 @@ def get_debug_info_filename(self, platform, kind):
8876
return self._get_dir() + self.DEBUG_INFO_MAP[platform][kind]
8977

9078
def save_build_log(self, text):
91-
if not settings.AWS_ENABLED:
92-
with open(self.build_log, 'w') as f:
93-
f.write(text)
94-
else:
95-
s3.save_file('builds', self.build_log, text, public=True, content_type='text/plain')
79+
s3.save_file('builds', self.build_log, text, public=True, content_type='text/plain')
9680

9781
def read_build_log(self):
98-
if not settings.AWS_ENABLED:
99-
with open(self.build_log, 'r') as f:
100-
return f.read()
101-
else:
102-
return s3.read_file('builds', self.build_log)
82+
return s3.read_file('builds', self.build_log)
10383

10484
def save_debug_info(self, json_info, platform, kind):
10585
text = json.dumps(json_info)
106-
if not settings.AWS_ENABLED:
107-
with open(self.get_debug_info_filename(platform, kind), 'w') as f:
108-
f.write(text)
109-
else:
110-
s3.save_file('builds', self.get_debug_info_filename(platform, kind), text, public=True, content_type='application/json')
86+
s3.save_file('builds', self.get_debug_info_filename(platform, kind), text, public=True, content_type='application/json')
11187

11288
def save_package(self, package_path):
113-
if not settings.AWS_ENABLED:
114-
shutil.move(package_path, self.package)
115-
else:
116-
filename = '%s.tar.gz' % self.project.app_short_name.replace('/', '-')
117-
s3.upload_file('builds', self.package, package_path, public=True, download_filename=filename, content_type='application/gzip')
89+
filename = '%s.tar.gz' % self.project.app_short_name.replace('/', '-')
90+
s3.upload_file('builds', self.package, package_path, public=True, download_filename=filename, content_type='application/gzip')
11891

11992
def save_pbw(self, pbw_path):
120-
if not settings.AWS_ENABLED:
121-
shutil.move(pbw_path, self.pbw)
122-
else:
123-
s3.upload_file('builds', self.pbw, pbw_path, public=True, download_filename='%s.pbw' % self.project.app_short_name.replace('/','-'))
93+
s3.upload_file('builds', self.pbw, pbw_path, public=True, download_filename='%s.pbw' % self.project.app_short_name.replace('/','-'))
12494

12595
def save_simplyjs(self, javascript):
126-
if not settings.AWS_ENABLED:
127-
with open(self.simplyjs, 'w') as f:
128-
f.write(javascript)
129-
else:
130-
s3.save_file('builds', self.simplyjs, javascript, public=True, content_type='text/javascript')
96+
s3.save_file('builds', self.simplyjs, javascript, public=True, content_type='text/javascript')
13197

13298
def get_sizes(self):
13399
sizes = {}

ide/models/meta.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.db.models.signals import pre_save
33
from django.dispatch import receiver
44

5+
56
class IdeModel(models.Model):
67
class Meta:
78
abstract = True

ide/models/s3file.py

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import shutil
2-
import os
31
import logging
42

53
from django.utils.translation import ugettext as _
@@ -18,7 +16,6 @@ class S3File(IdeModel):
1816
bucket_name = 'source'
1917
folder = None
2018
project = None
21-
_create_local_if_not_exists = False
2219

2320
@property
2421
def padded_id(self):
@@ -37,41 +34,11 @@ def s3_id(self):
3734
def s3_path(self):
3835
return '%s/%s' % (self.folder, self.s3_id)
3936

40-
def _get_contents_local(self):
41-
try:
42-
return open(self.local_filename).read()
43-
except IOError:
44-
if self._create_local_if_not_exists:
45-
return ''
46-
else:
47-
raise
48-
49-
def _save_string_local(self, string):
50-
if not os.path.exists(os.path.dirname(self.local_filename)):
51-
os.makedirs(os.path.dirname(self.local_filename))
52-
with open(self.local_filename, 'wb') as out:
53-
out.write(string)
54-
55-
def _copy_to_path_local(self, path):
56-
try:
57-
shutil.copy(self.local_filename, path)
58-
except IOError as err:
59-
if err.errno == 2 and self._crete_local_if_not_exists:
60-
open(path, 'w').close() # create the file if it's missing.
61-
else:
62-
raise
63-
6437
def get_contents(self):
65-
if not settings.AWS_ENABLED:
66-
return self._get_contents_local()
67-
else:
68-
return s3.read_file(self.bucket_name, self.s3_path)
38+
return s3.read_file(self.bucket_name, self.s3_path)
6939

7040
def save_string(self, string):
71-
if not settings.AWS_ENABLED:
72-
self._save_string_local(string)
73-
else:
74-
s3.save_file(self.bucket_name, self.s3_path, string)
41+
s3.save_file(self.bucket_name, self.s3_path, string)
7542
if self.project:
7643
self.project.last_modified = now()
7744
self.project.save()
@@ -85,10 +52,7 @@ def save_text(self, content):
8552
self.save_string(content.encode('utf-8'))
8653

8754
def copy_to_path(self, path):
88-
if not settings.AWS_ENABLED:
89-
self._copy_to_path_local(path)
90-
else:
91-
s3.read_file_to_filesystem(self.bucket_name, self.s3_path, path)
55+
s3.read_file_to_filesystem(self.bucket_name, self.s3_path, path)
9256

9357
class Meta(IdeModel.Meta):
9458
abstract = True
@@ -97,13 +61,7 @@ class Meta(IdeModel.Meta):
9761
@receiver(post_delete)
9862
def delete_file(sender, instance, **kwargs):
9963
if issubclass(sender, S3File):
100-
if settings.AWS_ENABLED:
101-
try:
102-
s3.delete_file(sender.bucket_name, instance.s3_path)
103-
except:
104-
logger.exception("Failed to delete S3 file")
105-
else:
106-
try:
107-
os.unlink(instance.local_filename)
108-
except OSError:
109-
pass
64+
try:
65+
s3.delete_file(sender.bucket_name, instance.s3_path)
66+
except:
67+
logger.exception("Failed to delete S3 file")

ide/models/textfile.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class TextFile(S3File):
1010
""" TextFile adds support to S3File for last-modified timestamps and code folding """
1111
last_modified = models.DateTimeField(blank=True, null=True, auto_now=True)
1212
folded_lines = models.TextField(default="[]")
13-
_create_local_if_not_exists = True
1413

1514
def was_modified_since(self, expected_modification_time):
1615
if isinstance(expected_modification_time, int):

ide/tasks/archive.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,9 @@ def create_archive(project_id):
6161

6262
send_td_event('cloudpebble_export_project', project=project)
6363

64-
if not settings.AWS_ENABLED:
65-
outfile = '%s%s/%s.zip' % (settings.EXPORT_DIRECTORY, u, prefix)
66-
os.makedirs(os.path.dirname(outfile), 0755)
67-
shutil.copy(filename, outfile)
68-
os.chmod(outfile, 0644)
69-
return '%s%s/%s.zip' % (settings.EXPORT_ROOT, u, prefix)
70-
else:
71-
outfile = '%s/%s.zip' % (u, prefix)
72-
s3.upload_file('export', outfile, filename, public=True, content_type='application/zip')
73-
return '%s%s' % (settings.EXPORT_ROOT, outfile)
64+
outfile = '%s/%s.zip' % (u, prefix)
65+
s3.upload_file('export', outfile, filename, public=True, content_type='application/zip')
66+
return '%s%s' % (settings.EXPORT_ROOT, outfile)
7467

7568

7669
@task(acks_late=True)
@@ -309,4 +302,4 @@ def make_valid_filename(zip_entry):
309302
'reason': str(e)
310303
}
311304
}, user=project.owner)
312-
raise
305+
raise

utils/s3.py

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,33 @@ def __init__(self):
2727
self.s3 = None
2828

2929
def configure(self):
30-
if settings.AWS_ENABLED:
31-
if settings.AWS_S3_FAKE_S3 is None:
32-
# The host must be manually specified in Python 2.7.9+ due to
33-
# https://github.com/boto/boto/issues/2836 this bug in boto with .s in
34-
# bucket names.
35-
host = settings.AWS_S3_HOST if settings.AWS_S3_HOST else NoHostProvided
36-
37-
self.s3 = boto.connect_s3(
38-
settings.AWS_ACCESS_KEY_ID,
39-
settings.AWS_SECRET_ACCESS_KEY,
40-
host=host,
41-
calling_format=OrdinaryCallingFormat()
42-
)
43-
else:
44-
host, port = (settings.AWS_S3_FAKE_S3.split(':', 2) + [80])[:2]
45-
port = int(port)
46-
self.s3 = boto.connect_s3("key_id", "secret_key", is_secure=False, port=port,
47-
host=host, calling_format=OrdinaryCallingFormat())
48-
_ensure_bucket_exists(self.s3, settings.AWS_S3_SOURCE_BUCKET)
49-
_ensure_bucket_exists(self.s3, settings.AWS_S3_EXPORT_BUCKET)
50-
_ensure_bucket_exists(self.s3, settings.AWS_S3_BUILDS_BUCKET)
51-
52-
self.buckets = {
53-
'source': self.s3.get_bucket(settings.AWS_S3_SOURCE_BUCKET),
54-
'export': self.s3.get_bucket(settings.AWS_S3_EXPORT_BUCKET),
55-
'builds': self.s3.get_bucket(settings.AWS_S3_BUILDS_BUCKET),
56-
}
57-
self.configured = True
30+
if settings.AWS_S3_FAKE_S3 is None:
31+
# The host must be manually specified in Python 2.7.9+ due to
32+
# https://github.com/boto/boto/issues/2836 this bug in boto with .s in
33+
# bucket names.
34+
host = settings.AWS_S3_HOST if settings.AWS_S3_HOST else NoHostProvided
35+
36+
self.s3 = boto.connect_s3(
37+
settings.AWS_ACCESS_KEY_ID,
38+
settings.AWS_SECRET_ACCESS_KEY,
39+
host=host,
40+
calling_format=OrdinaryCallingFormat()
41+
)
5842
else:
59-
self.s3 = None
60-
self.buckets = None
43+
host, port = (settings.AWS_S3_FAKE_S3.split(':', 2) + [80])[:2]
44+
port = int(port)
45+
self.s3 = boto.connect_s3("key_id", "secret_key", is_secure=False, port=port,
46+
host=host, calling_format=OrdinaryCallingFormat())
47+
_ensure_bucket_exists(self.s3, settings.AWS_S3_SOURCE_BUCKET)
48+
_ensure_bucket_exists(self.s3, settings.AWS_S3_EXPORT_BUCKET)
49+
_ensure_bucket_exists(self.s3, settings.AWS_S3_BUILDS_BUCKET)
50+
51+
self.buckets = {
52+
'source': self.s3.get_bucket(settings.AWS_S3_SOURCE_BUCKET),
53+
'export': self.s3.get_bucket(settings.AWS_S3_EXPORT_BUCKET),
54+
'builds': self.s3.get_bucket(settings.AWS_S3_BUILDS_BUCKET),
55+
}
56+
self.configured = True
6157

6258
def __getitem__(self, item):
6359
if settings.TESTING:
@@ -70,38 +66,24 @@ def __getitem__(self, item):
7066
_buckets = BucketHolder()
7167

7268

73-
def _requires_aws(fn):
74-
if settings.AWS_ENABLED:
75-
return fn
76-
else:
77-
def complain(*args, **kwargs):
78-
raise Exception("AWS_ENABLED must be True to call %s" % fn.__name__)
79-
80-
return complain
81-
82-
83-
@_requires_aws
8469
def read_file(bucket_name, path):
8570
bucket = _buckets[bucket_name]
8671
key = bucket.get_key(path)
8772
return key.get_contents_as_string()
8873

8974

90-
@_requires_aws
9175
def read_file_to_filesystem(bucket_name, path, destination):
9276
bucket = _buckets[bucket_name]
9377
key = bucket.get_key(path)
9478
key.get_contents_to_filename(destination)
9579

9680

97-
@_requires_aws
9881
def delete_file(bucket_name, path):
9982
bucket = _buckets[bucket_name]
10083
key = bucket.get_key(path)
10184
key.delete()
10285

10386

104-
@_requires_aws
10587
def save_file(bucket_name, path, value, public=False, content_type='application/octet-stream'):
10688
bucket = _buckets[bucket_name]
10789
key = Key(bucket)
@@ -115,7 +97,6 @@ def save_file(bucket_name, path, value, public=False, content_type='application/
11597
key.set_contents_from_string(value, policy=policy, headers={'Content-Type': content_type})
11698

11799

118-
@_requires_aws
119100
def upload_file(bucket_name, dest_path, src_path, public=False, content_type='application/octet-stream',
120101
download_filename=None):
121102
bucket = _buckets[bucket_name]
@@ -137,7 +118,6 @@ def upload_file(bucket_name, dest_path, src_path, public=False, content_type='ap
137118
key.set_contents_from_filename(src_path, policy=policy, headers=headers)
138119

139120

140-
@_requires_aws
141121
def get_signed_url(bucket_name, path, headers=None):
142122
bucket = _buckets[bucket_name]
143123
key = bucket.get_key(path)

0 commit comments

Comments
 (0)