Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

XPI views moved out from jetpack

  • Loading branch information...
commit 0f16f5681fcbd98d7cd0c3acf07439669a2603a6 1 parent 470eb15
Piotr Zalewa authored December 10, 2010
13  apps/jetpack/models.py
@@ -22,7 +22,8 @@
22 22
 from jetpack.managers import PackageManager
23 23
 from jetpack.errors import SelfDependencyException, FilenameExistException, \
24 24
         UpdateDeniedException, SingletonCopyException, DependencyException
25  
-from jetpack.xpi_utils import sdk_copy, xpi_build, xpi_remove
  25
+from utils import xpi
  26
+#xpi.sdk_copy, xpi.build, xpi.remove
26 27
 
27 28
 
28 29
 PERMISSION_CHOICES = (
@@ -761,9 +762,9 @@ def build_xpi(self):
761 762
 
762 763
         # TODO: consider SDK staying per PackageRevision...
763 764
         if os.path.isdir(sdk_dir):
764  
-            xpi_remove(sdk_dir)
  765
+            xpi.remove(sdk_dir)
765 766
 
766  
-        sdk_copy(sdk_source, sdk_dir)
  767
+        xpi.sdk_copy(sdk_source, sdk_dir)
767 768
         self.export_keys(sdk_dir)
768 769
         self.export_files_with_dependencies('%s/packages' % sdk_dir)
769 770
 
@@ -780,8 +781,8 @@ def build_xpi_test(self, modules=[]):
780 781
         sdk_source = self.sdk.get_source_dir()
781 782
         # This SDK is always different! - working on unsaved data
782 783
         if os.path.isdir(sdk_dir):
783  
-            xpi_remove(sdk_dir)
784  
-        sdk_copy(sdk_source, sdk_dir)
  784
+            xpi.remove(sdk_dir)
  785
+        xpi.sdk_copy(sdk_source, sdk_dir)
785 786
         self.export_keys(sdk_dir)
786 787
 
787 788
         packages_dir = '%s/packages' % sdk_dir
@@ -800,7 +801,7 @@ def build_xpi_test(self, modules=[]):
800 801
         self.export_attachments(
801 802
             '%s/%s' % (package_dir, self.package.get_data_dir()))
802 803
         self.export_dependencies(packages_dir)
803  
-        return (xpi_build(sdk_dir,
  804
+        return (xpi.build(sdk_dir,
804 805
                           '%s/packages/%s' % (
805 806
                               sdk_dir,
806 807
                               self.package.get_unique_package_name()
4  apps/jetpack/tasks.py
@@ -2,11 +2,11 @@
2 2
 
3 3
 from celery.decorators import task
4 4
 
5  
-from jetpack import xpi_utils
  5
+from utils import xpi
6 6
 
7 7
 log = commonware.log.getLogger('f.tasks')
8 8
 
9 9
 
10 10
 @task(rate_limit='10/s')
11 11
 def xpi_build(sdk_dir, package_dir):
12  
-    xpi_utils.xpi_build(sdk_dir, package_dir)
  12
+    xpi.build(sdk_dir, package_dir)
14  apps/jetpack/urls.py
@@ -133,18 +133,4 @@
133 133
     url(r'^library/remove_dependency/(?P<id_number>[-\w]+)/revision/(?P<revision_number>\d+)/$',
134 134
         'package_remove_library',
135 135
         {'type_id': 'l'},  name='jp_library_revision_remove_library'),
136  
-
137  
-
138  
-    # test Add-on's PackageRevision
139  
-    url(r'^addon/test/(?P<id_number>[-\w]+)/revision/(?P<revision_number>\d+)/$',
140  
-        'package_test_xpi', name='jp_addon_revision_test'),
141  
-    url(r'^addon/xpi/(?P<id_number>[-\w]+)/revision/(?P<revision_number>\d+)/$',
142  
-        'package_download_xpi', name='jp_addon_revision_xpi'),
143  
-
144  
-    # get and remove created XPI
145  
-    url(r'^addon/test_xpi/(?P<sdk_name>.*)/(?P<pkg_name>.*)/(?P<filename>.*)/$',
146  
-        'test_xpi', name='jp_test_xpi'),
147  
-    url(r'^addon/download_xpi/(?P<sdk_name>.*)/(?P<pkg_name>.*)/(?P<filename>.*)/$',
148  
-        'download_xpi', name='jp_download_xpi'),
149  
-    url(r'^addon/rm_xpi/(?P<sdk_name>.*)/$', 'remove_xpi', name='jp_rm_xpi'),
150 136
 )
118  apps/jetpack/views.py
@@ -26,10 +26,8 @@
26 26
 
27 27
 from jetpack.models import Package, PackageRevision, Module, Attachment, SDK
28 28
 from jetpack.package_helpers import get_package_revision
29  
-from jetpack.xpi_utils import xpi_remove
30 29
 from jetpack.errors import FilenameExistException
31 30
 
32  
-
33 31
 log = commonware.log.getLogger('f.jetpack')
34 32
 
35 33
 
@@ -667,119 +665,3 @@ def get_revisions_list_html(r, id_number):
667 665
 # ---------------------------- XPI ---------------------------------
668 666
 
669 667
 
670  
-def package_test_xpi(r, id_number, revision_number=None):
671  
-    """
672  
-    Test XPI from data saved in the database
673  
-    """
674  
-    revision = get_object_with_related_or_404(PackageRevision,
675  
-                        package__id_number=id_number, package__type='a',
676  
-                        revision_number=revision_number)
677  
-
678  
-    # support temporary data
679  
-    if r.POST.get('live_data_testing', False):
680  
-        modules = []
681  
-        for mod in revision.modules.all():
682  
-            if r.POST.get(mod.filename, False):
683  
-                code = r.POST.get(mod.filename, '')
684  
-                if mod.code != code:
685  
-                    mod.code = code
686  
-                    modules.append(mod)
687  
-        (stdout, stderr) = revision.build_xpi_test(modules)
688  
-
689  
-    else:
690  
-        # XXX: added test as build_xpi doesn't return
691  
-        (stdout, stderr) = revision.build_xpi_test()
692  
-
693  
-    if stderr and not settings.DEBUG:
694  
-        # XXX: this should also log the error in file
695  
-        xpi_remove(revision.get_sdk_dir())
696  
-
697  
-    # return XPI url and cfx command stdout and stderr
698  
-    return render_to_response('json/test_xpi_created.json', {
699  
-        'stdout': stdout,
700  
-        'stderr': stderr,
701  
-        'test_xpi_url': reverse('jp_test_xpi', args=[
702  
-            revision.get_sdk_name(),
703  
-            revision.package.get_unique_package_name(),
704  
-            revision.package.name
705  
-        ]),
706  
-        'download_xpi_url': reverse('jp_download_xpi', args=[
707  
-            revision.get_sdk_name(),
708  
-            revision.package.get_unique_package_name(),
709  
-            revision.package.name
710  
-        ]),
711  
-        'rm_xpi_url': reverse('jp_rm_xpi', args=[revision.get_sdk_name()]),
712  
-        'addon_name': '"%s (%s)"' % (
713  
-            revision.package.full_name, revision.get_version_name())
714  
-    }, context_instance=RequestContext(r))
715  
-    #    mimetype='application/json')
716  
-
717  
-
718  
-def package_download_xpi(r, id_number, revision_number=None):
719  
-    """
720  
-    Download XPI.  This package is built asynchronously and we assume it works.
721  
-    and let ``download_xpi`` handle the case where the file is not ready.
722  
-    """
723  
-    revision = get_object_with_related_or_404(PackageRevision,
724  
-                        package__id_number=id_number, package__type='a',
725  
-                        revision_number=revision_number)
726  
-
727  
-    # If this is a retry, we won't rebuild... we'll just wait.
728  
-    retry = r.GET.get('retry')
729  
-    retry_url = reverse('jp_addon_revision_xpi',
730  
-                        args=[id_number, revision_number]) + '?retry=1'
731  
-
732  
-    if not retry:
733  
-        revision.build_xpi()
734  
-
735  
-    return download_xpi(r,
736  
-                        revision.get_sdk_name(),
737  
-                        revision.package.get_unique_package_name(),
738  
-                        revision.package.name,
739  
-                        retry=retry,
740  
-                        retry_url=retry_url,
741  
-                       )
742  
-
743  
-
744  
-def test_xpi(r, sdk_name, pkg_name, filename):
745  
-    """
746  
-    return XPI file for testing
747  
-    """
748  
-    path = '%s-%s/packages/%s' % (settings.SDKDIR_PREFIX, sdk_name, pkg_name)
749  
-    _file = '%s.xpi' % filename
750  
-    mimetype = 'text/plain; charset=x-user-defined'
751  
-
752  
-    try:
753  
-        xpi = open(os.path.join(path, _file), 'rb').read()
754  
-    except Exception, err:
755  
-        log.critical('Error creating Add-on: %s' % str(err))
756  
-        return HttpResponseServerError
757  
-
758  
-    return HttpResponse(xpi, mimetype=mimetype)
759  
-
760  
-
761  
-def download_xpi(r, sdk_name, pkg_name, filename, retry=False, retry_url=None):
762  
-    """Return XPI file for testing."""
763  
-    path = '%s-%s/packages/%s' % (settings.SDKDIR_PREFIX, sdk_name, pkg_name)
764  
-    f = '%s.xpi' % filename
765  
-    # Return file if it exists?
766  
-    if os.path.isfile(os.path.join(path, f)):
767  
-        r = serve(r, f, path, show_indexes=False)
768  
-        r['Content-Type'] = 'application/octet-stream'
769  
-        r['Content-Disposition'] = 'attachment; filename="%s.xpi"' % filename
770  
-    elif retry:
771  
-        r = render_to_response('retry_download.html', dict(url=retry_url),
772  
-                               RequestContext(r))
773  
-    else:
774  
-        r = HttpResponseRedirect(retry_url)
775  
-
776  
-    return r
777  
-
778  
-
779  
-def remove_xpi(r, sdk_name):
780  
-    " remove whole temporary SDK on request "
781  
-    # Validate sdk_name
782  
-    if not validator.is_valid('alphanum_plus', sdk_name):
783  
-        return HttpResponseForbidden("{'error': 'Wrong name'}")
784  
-    xpi_remove('%s-%s' % (settings.SDKDIR_PREFIX, sdk_name))
785  
-    return HttpResponse('{}', mimetype='application/json')
0  apps/xpi/__init__.py
No changes.
3  apps/xpi/models.py
... ...
@@ -0,0 +1,3 @@
  1
+from django.db import models
  2
+
  3
+# Create your models here.
20  apps/jetpack/tests/xpibuild_tests.py → apps/xpi/tests.py
@@ -9,7 +9,7 @@
9 9
 from django.conf import settings
10 10
 
11 11
 from jetpack.models import Module, Package, PackageRevision, SDK
12  
-from jetpack.xpi_utils import sdk_copy, xpi_build
  12
+from utils import xpi
13 13
 from jetpack.cron import find_files, clean_tmp
14 14
 
15 15
 
@@ -138,16 +138,16 @@ def test_addon_export_with_attachment(self):
138 138
         self.failUnless(os.path.isfile(self.attachment_file_name))
139 139
 
140 140
     def test_copying_sdk(self):
141  
-        sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
  141
+        xpi.sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
142 142
         self.failUnless(os.path.isdir(self.SDKDIR))
143 143
 
144 144
     def test_minimal_xpi_creation(self):
145 145
         " xpi build from an addon straight after creation "
146  
-        sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
  146
+        xpi.sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
147 147
         self.addonrev.export_keys(self.SDKDIR)
148 148
         self.addonrev.export_files_with_dependencies(
149 149
             '%s/packages' % self.SDKDIR)
150  
-        out = xpi_build(self.SDKDIR,
  150
+        out = xpi.build(self.SDKDIR,
151 151
                     '%s/packages/%s' % (
152 152
                         self.SDKDIR, self.addon.get_unique_package_name()))
153 153
         # assert no error output
@@ -163,11 +163,11 @@ def test_addon_with_other_modules(self):
163 163
             filename='test_filename',
164 164
             author=self.author
165 165
         )
166  
-        sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
  166
+        xpi.sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
167 167
         self.addonrev.export_keys(self.SDKDIR)
168 168
         self.addonrev.export_files_with_dependencies(
169 169
             '%s/packages' % self.SDKDIR)
170  
-        out = xpi_build(self.SDKDIR,
  170
+        out = xpi.build(self.SDKDIR,
171 171
                         '%s/packages/%s' % (
172 172
                             self.SDKDIR, self.addon.get_unique_package_name()))
173 173
         # assert no error output
@@ -188,11 +188,11 @@ def test_xpi_with_empty_dependency(self):
188 188
         librev = PackageRevision.objects.filter(
189 189
             package__id_number=lib.id_number)[0]
190 190
         self.addonrev.dependency_add(librev)
191  
-        sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
  191
+        xpi.sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
192 192
         self.addonrev.export_keys(self.SDKDIR)
193 193
         self.addonrev.export_files_with_dependencies(
194 194
             '%s/packages' % self.SDKDIR)
195  
-        out = xpi_build(self.SDKDIR,
  195
+        out = xpi.build(self.SDKDIR,
196 196
                     '%s/packages/%s' % (
197 197
                         self.SDKDIR, self.addon.get_unique_package_name()))
198 198
         # assert no error output
@@ -205,11 +205,11 @@ def test_xpi_with_empty_dependency(self):
205 205
     def test_xpi_with_dependency(self):
206 206
         " addon has one dependency with a file "
207 207
         self.addonrev.dependency_add(self.librev)
208  
-        sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
  208
+        xpi.sdk_copy(self.addonrev.sdk.get_source_dir(), self.SDKDIR)
209 209
         self.addonrev.export_keys(self.SDKDIR)
210 210
         self.addonrev.export_files_with_dependencies(
211 211
             '%s/packages' % self.SDKDIR)
212  
-        out = xpi_build(self.SDKDIR,
  212
+        out = xpi.build(self.SDKDIR,
213 213
                     '%s/packages/%s' % (
214 214
                         self.SDKDIR, self.addon.get_unique_package_name()))
215 215
         # assert no error output
18  apps/xpi/urls.py
... ...
@@ -0,0 +1,18 @@
  1
+" XPI URL definitions "
  2
+
  3
+from django.conf.urls.defaults import url, patterns
  4
+
  5
+urlpatterns = patterns('xpi.views',
  6
+    # test Add-on's PackageRevision
  7
+    url(r'^prepare_test/(?P<id_number>[-\w]+)/revision/(?P<revision_number>\d+)/$',
  8
+        'prepare_test', name='jp_addon_revision_test'),
  9
+    url(r'^prepare_download/(?P<id_number>[-\w]+)/revision/(?P<revision_number>\d+)/$',
  10
+        'prepare_download', name='jp_addon_revision_xpi'),
  11
+
  12
+    # get and remove created XPI
  13
+    url(r'^test/(?P<sdk_name>.*)/(?P<pkg_name>.*)/(?P<filename>.*)/$',
  14
+        'get_test', name='jp_test_xpi'),
  15
+    url(r'^download/(?P<sdk_name>.*)/(?P<pkg_name>.*)/(?P<filename>.*)/$',
  16
+        'get_download', name='jp_download_xpi'),
  17
+    url(r'^remove/(?P<sdk_name>.*)/$', 'clean', name='jp_rm_xpi'),
  18
+)
136  apps/xpi/views.py
... ...
@@ -0,0 +1,136 @@
  1
+import os
  2
+import commonware.log
  3
+
  4
+from django.core.urlresolvers import reverse
  5
+from django.views.static import serve
  6
+from django.shortcuts import render_to_response, get_object_or_404
  7
+from django.http import HttpResponseRedirect, HttpResponse, \
  8
+                        HttpResponseForbidden, HttpResponseServerError, \
  9
+                        HttpResponseNotAllowed, Http404
  10
+from django.template import RequestContext
  11
+from django.conf import settings
  12
+
  13
+from base.shortcuts import get_object_with_related_or_404
  14
+from utils import validator, xpi
  15
+
  16
+from jetpack.models import Package, PackageRevision, Module, Attachment, SDK
  17
+
  18
+
  19
+log = commonware.log.getLogger('f.xpi')
  20
+
  21
+def prepare_test(r, id_number, revision_number=None):
  22
+    """
  23
+    Test XPI from data saved in the database
  24
+    """
  25
+    revision = get_object_with_related_or_404(PackageRevision,
  26
+                        package__id_number=id_number, package__type='a',
  27
+                        revision_number=revision_number)
  28
+
  29
+    # support temporary data
  30
+    if r.POST.get('live_data_testing', False):
  31
+        modules = []
  32
+        for mod in revision.modules.all():
  33
+            if r.POST.get(mod.filename, False):
  34
+                code = r.POST.get(mod.filename, '')
  35
+                if mod.code != code:
  36
+                    mod.code = code
  37
+                    modules.append(mod)
  38
+        (stdout, stderr) = revision.build_xpi_test(modules)
  39
+
  40
+    else:
  41
+        # XXX: added test as build_xpi doesn't return
  42
+        (stdout, stderr) = revision.build_xpi_test()
  43
+
  44
+    if stderr and not settings.DEBUG:
  45
+        # XXX: this should also log the error in file
  46
+        xpi.remove(revision.get_sdk_dir())
  47
+
  48
+    # return XPI url and cfx command stdout and stderr
  49
+    return render_to_response('json/test_xpi_created.json', {
  50
+        'stdout': stdout,
  51
+        'stderr': stderr,
  52
+        'test_xpi_url': reverse('jp_test_xpi', args=[
  53
+            revision.get_sdk_name(),
  54
+            revision.package.get_unique_package_name(),
  55
+            revision.package.name
  56
+        ]),
  57
+        'download_xpi_url': reverse('jp_download_xpi', args=[
  58
+            revision.get_sdk_name(),
  59
+            revision.package.get_unique_package_name(),
  60
+            revision.package.name
  61
+        ]),
  62
+        'rm_xpi_url': reverse('jp_rm_xpi', args=[revision.get_sdk_name()]),
  63
+        'addon_name': '"%s (%s)"' % (
  64
+            revision.package.full_name, revision.get_version_name())
  65
+    }, context_instance=RequestContext(r))
  66
+    #    mimetype='application/json')
  67
+
  68
+
  69
+def prepare_download(r, id_number, revision_number=None):
  70
+    """
  71
+    Download XPI.  This package is built asynchronously and we assume it works.
  72
+    and let ``download_xpi`` handle the case where the file is not ready.
  73
+    """
  74
+    revision = get_object_with_related_or_404(PackageRevision,
  75
+                        package__id_number=id_number, package__type='a',
  76
+                        revision_number=revision_number)
  77
+
  78
+    # If this is a retry, we won't rebuild... we'll just wait.
  79
+    retry = r.GET.get('retry')
  80
+    retry_url = reverse('jp_addon_revision_xpi',
  81
+                        args=[id_number, revision_number]) + '?retry=1'
  82
+
  83
+    if not retry:
  84
+        revision.build_xpi()
  85
+
  86
+    return get_download(r,
  87
+                        revision.get_sdk_name(),
  88
+                        revision.package.get_unique_package_name(),
  89
+                        revision.package.name,
  90
+                        retry=retry,
  91
+                        retry_url=retry_url,
  92
+                       )
  93
+
  94
+
  95
+def get_test(r, sdk_name, pkg_name, filename):
  96
+    """
  97
+    return XPI file for testing
  98
+    """
  99
+    path = '%s-%s/packages/%s' % (settings.SDKDIR_PREFIX, sdk_name, pkg_name)
  100
+    _file = '%s.xpi' % filename
  101
+    mimetype = 'text/plain; charset=x-user-defined'
  102
+
  103
+    try:
  104
+        xpi = open(os.path.join(path, _file), 'rb').read()
  105
+    except Exception, err:
  106
+        log.critical('Error creating Add-on: %s' % str(err))
  107
+        return HttpResponseServerError
  108
+
  109
+    return HttpResponse(xpi, mimetype=mimetype)
  110
+
  111
+
  112
+def get_download(r, sdk_name, pkg_name, filename, retry=False, retry_url=None):
  113
+    """Return XPI file for testing."""
  114
+    path = '%s-%s/packages/%s' % (settings.SDKDIR_PREFIX, sdk_name, pkg_name)
  115
+    f = '%s.xpi' % filename
  116
+    # Return file if it exists?
  117
+    if os.path.isfile(os.path.join(path, f)):
  118
+        r = serve(r, f, path, show_indexes=False)
  119
+        r['Content-Type'] = 'application/octet-stream'
  120
+        r['Content-Disposition'] = 'attachment; filename="%s.xpi"' % filename
  121
+    elif retry:
  122
+        r = render_to_response('retry_download.html', dict(url=retry_url),
  123
+                               RequestContext(r))
  124
+    else:
  125
+        r = HttpResponseRedirect(retry_url)
  126
+
  127
+    return r
  128
+
  129
+
  130
+def clean(r, sdk_name):
  131
+    " remove whole temporary SDK on request "
  132
+    # Validate sdk_name
  133
+    if not validator.is_valid('alphanum_plus', sdk_name):
  134
+        return HttpResponseForbidden("{'error': 'Wrong name'}")
  135
+    xpi.remove('%s-%s' % (settings.SDKDIR_PREFIX, sdk_name))
  136
+    return HttpResponse('{}', mimetype='application/json')
1  requirements/production.txt
@@ -13,3 +13,4 @@ Markdown==2.0.3
13 13
 # Celery
14 14
 python-dateutil
15 15
 django-celery
  16
+importlib
1  settings.py
@@ -235,6 +235,7 @@
235 235
     'person',            # user related stuff (profile etc.)
236 236
     'amo',               # currently addons.mozilla.org authentication
237 237
     'jetpack',           # Jetpack functionality
  238
+    'xpi',               # XPI management
238 239
     'api',               # API browser
239 240
     'tutorial',          # Load tutorial templates
240 241
     'cronjobs',
3  urls.py
@@ -42,6 +42,9 @@
42 42
         {'document_root': settings.MEDIA_ROOT}, name='media'),
43 43
 
44 44
     # API Browser
  45
+    (r'^xpi/', include('xpi.urls')),
  46
+
  47
+    # API Browser
45 48
     (r'^api/', include('api.urls')),
46 49
 
47 50
     # Tutorial
4  apps/jetpack/xpi_utils.py → utils/xpi.py
@@ -40,7 +40,7 @@ def sdk_copy(sdk_source, sdk_dir=None):
40 40
     os.chmod('%s/bin/cfx2' % sdk_dir, stat.S_IXUSR | stat.S_IRUSR)
41 41
 
42 42
 
43  
-def xpi_build(sdk_dir, package_dir):
  43
+def build(sdk_dir, package_dir):
44 44
     " build xpi from source in sdk_dir "
45 45
     # set environment
46 46
     #os.environ['CUDDLEFISH_ROOT'] = sdk_dir
@@ -74,6 +74,6 @@ def xpi_build(sdk_dir, package_dir):
74 74
     return out
75 75
 
76 76
 
77  
-def xpi_remove(sdk_dir):
  77
+def remove(sdk_dir):
78 78
     " clear directory "
79 79
     shutil.rmtree(sdk_dir)

0 notes on commit 0f16f56

Please sign in to comment.
Something went wrong with that request. Please try again.