Skip to content

Commit 1dbab59

Browse files
author
Ubuntu
committed
Updated requirements
2 parents 4a3b0f7 + 4b8d459 commit 1dbab59

File tree

11 files changed

+373
-271
lines changed

11 files changed

+373
-271
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:2.7
2+
3+
COPY . .
4+
5+
RUN echo \
6+
&& pip install -r requirements.txt

biojs/settings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
# SECURITY WARNING: keep the secret key used in production secret!
2323
SECRET_KEY = '$&&+$xz)50khd6q+aq&95r1$2urmmdv37=-*lu3v-wwh46829t'
2424

25+
GITHUB_CLIENT_ID = os.environ.get('GITHUB_CLIENT_ID') or ''
26+
GITHUB_CLIENT_SECRET = os.environ.get('GITHUB_CLIENT_SECRET') or ''
27+
2528
# SECURITY WARNING: don't run with debug turned on in production!
2629
try:
2730
from config import *
@@ -41,11 +44,13 @@
4144
'django.contrib.sessions',
4245
'django.contrib.messages',
4346
'django.contrib.staticfiles',
47+
'corsheaders',
4448
'main',
4549
'rest_framework',
4650
]
4751

4852
MIDDLEWARE = [
53+
'corsheaders.middleware.CorsMiddleware',
4954
'django.middleware.security.SecurityMiddleware',
5055
'django.contrib.sessions.middleware.SessionMiddleware',
5156
'django.middleware.common.CommonMiddleware',
@@ -152,3 +157,12 @@
152157

153158
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
154159
MEDIA_URL = '/backend_media/'
160+
161+
# CORS settings
162+
163+
CORS_ORIGIN_WHITELIST = (
164+
'biojs.net',
165+
'biojs.io',
166+
'localhost:8080',
167+
'127.0.0.1:9000'
168+
)

docker-compose.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '2'
2+
services:
3+
biojs-backend:
4+
command: /bin/sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000 --verbosity 3"
5+
image: biojs-backend-base
6+
working_dir: /opt
7+
environment:
8+
GITHUB_CLIENT_SECRET: 289807644cc6b4afe50b4bb2c5afcd7056047de0
9+
GITHUB_CLIENT_ID: DennisSchwartz
10+
volumes:
11+
- .:/opt
12+
ports:
13+
- 8000:8000
14+
networks:
15+
- service
16+
networks:
17+
service:
Lines changed: 104 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,180 +1,107 @@
11
from django.core.management import BaseCommand
2-
import urllib2, json, urllib
2+
import urllib2, json, urllib, base64
33
from main.models import *
44
try:
5-
from biojs.config import *
5+
from biojs.settings import GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET
66
except:
7+
print('ERROR: Could not load config!')
78
GITHUB_CLIENT_ID = ''
89
GITHUB_CLIENT_SECRET = ''
910
from datetime import datetime
1011
import pytz
1112
import ast
13+
import re
1214

1315
# Get sniper data
1416
'''
1517
https://rawgit.com/cytoscape/cytoscape.js/master/package.json
1618
"sniper" key, has "js" and "css". Search for "first"
1719
'''
1820

19-
def get_commit_hash(commits_url):
20-
commits_url = commits_url.split('{')[0] + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
21-
print (commits_url)
22-
response = urllib.urlopen(commits_url)
23-
data = json.loads(response.read())[0]
24-
return data['sha']
21+
def get_npm_data():
22+
# response = urllib2.urlopen()
23+
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
24+
req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=keywords:biojs,bionode&size=500", headers=hdr)
25+
# req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=biojs-vis-msa&size=1", headers=hdr)
26+
response = urllib2.urlopen(req)
27+
data = json.load(response)
28+
return data
29+
30+
def send_GET_request(url, user=None, password=None):
31+
request = urllib2.Request(url)
32+
if (user is not None and password is not None):
33+
base64string = base64.encodestring('%s:%s' % (user, password)).replace('\n', '')
34+
request.add_header('Authorization', 'Basic %s' % base64string)
35+
return urllib2.urlopen(request)
36+
37+
def create_jsdelivr_link(owner, repo, file_path, commit=None):
38+
return str('https://cdn.jsdelivr.net/gh/' + owner + '/' + repo + ('@' + commit if commit else '') + file_path)
39+
40+
def is_absolute_dependency(dep):
41+
return re.match('^(?:[a-z]+:)?//', dep) is not None
42+
43+
def get_owner_and_repo_from_github_url(url):
44+
split_url = url.split('?')[0].split('/')
45+
return split_url[4], split_url[5]
2546

2647
### store the dependency urls and snippet urls
2748
def update_visualizations(component, commit_hash, test=False):
28-
github_url_list = component.github_url.split('?')[0].split('/')
29-
owner = github_url_list[4]
30-
repo_name = github_url_list[5]
49+
owner, repo_name = get_owner_and_repo_from_github_url(component.github_url)
3150
try:
32-
sniper_data = json.load(urllib.urlopen("https://cdn.rawgit.com/" + str(owner) +
33-
'/' + str(repo_name) + "/" + commit_hash + "/package.json"))["sniper"]
34-
except:
51+
url = create_jsdelivr_link(owner, repo_name, '/package.json', commit_hash)
52+
response = send_GET_request(url)
53+
package = json.load(response)
54+
sniper_data = package["sniper"]
55+
except KeyError:
56+
print('No sniper info in ', repo_name)
3557
return
36-
try:
37-
buildJS = sniper_data["buildJS"]
38-
except:
39-
buildJS = []
40-
try:
41-
js = sniper_data["js"]
42-
except:
43-
js = []
44-
js_dependencies = buildJS + js
45-
try:
46-
buildCSS = sniper_data["buildCSS"]
47-
except:
48-
buildCSS = []
49-
try:
50-
css = sniper_data["css"]
51-
except:
52-
css = []
53-
css_dependencies = buildCSS + css
54-
for dependency in js_dependencies:
55-
if dependency.startswith('http') :
56-
dependency, created = JSDependency.objects.get_or_create(component=component, js_url=dependency)
57-
elif dependency.startswith('/'):
58-
dependency_string = dependency
59-
if 'build/' in dependency_string:
60-
continue
61-
dependency = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + dependency
62-
try:
63-
req_dependency = JSDependency.objects.get(component=component, sniper_data_value=dependency_string)
64-
req_dependency.js_url = dependency
65-
req_dependency.save()
66-
except:
67-
req_dependency = JSDependency.objects.create(component=component, sniper_data_value=dependency_string, js_url=dependency)
68-
else:
69-
dependency_string = dependency
70-
if 'build/' in dependency_string:
71-
continue
72-
dependency = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + "/" + dependency
73-
try:
74-
req_dependency = JSDependency.objects.get(component=component, sniper_data_value=dependency_string)
75-
req_dependency.js_url = dependency
76-
req_dependency.save()
77-
except:
78-
req_dependency = JSDependency.objects.create(component=component, sniper_data_value=dependency_string, js_url=dependency)
79-
for dependency in css_dependencies:
80-
if dependency.startswith('http'):
81-
dependency, created = CSSDependency.objects.get_or_create(component=component, css_url=dependency)
82-
elif dependency.startswith('/'):
83-
dependency_string = dependency
84-
if 'build/' in dependency_string:
85-
continue
86-
dependency = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + dependency
87-
try:
88-
req_dependency = CSSDependency.objects.get(component=component, sniper_data_value=dependency_string)
89-
req_dependency.css_url = dependency
90-
req_dependency.save()
91-
except:
92-
req_dependency = CSSDependency.objects.create(component=component, sniper_data_value=dependency_string, css_url=dependency)
93-
else:
94-
dependency_string = dependency
95-
if 'build/' in dependency_string:
96-
continue
97-
dependency = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + dependency
98-
try:
99-
req_dependency = CSSDependency.objects.get(component=component, sniper_data_value=dependency_string)
100-
req_dependency.css_url = dependency
101-
req_dependency.save()
102-
except:
103-
req_dependency = CSSDependency.objects.create(component=component, sniper_data_value=dependency_string, css_url=dependency)
104-
try:
105-
sniperData = SniperData.objects.get(component=component)
106-
except:
107-
sniperData = SniperData.objects.create(component=component)
108-
try:
109-
no_browserify = sniper_data['noBrowserify']
110-
sniperData.no_browserify = no_browserify
111-
except:
112-
pass
113-
try:
114-
if no_browserify:
115-
sniperData.wzrd_url = '#'
116-
else:
117-
sniperData.wzrd_url = "https://wzrd.in/bundle/" + component.name
118-
except:
119-
sniperData.wzrd_url = "https://wzrd.in/bundle/" + component.name
120-
try:
121-
snippets_dir_name = sniper_data['snippets'][0]
122-
sniperData.snippets_dir_name = snippets_dir_name
123-
except:
124-
pass
58+
buildJS = sniper_data.get('buildJS', [])
59+
js = sniper_data.get('js', [])
60+
buildCSS = sniper_data.get('buildCSS', [])
61+
css = sniper_data.get('css', [])
62+
# Move absolute links from js to buildJS and same for css
63+
buildJS = buildJS + filter(lambda l: is_absolute_dependency(l), js)
64+
js = filter(lambda l: not is_absolute_dependency(l), js)
65+
buildCSS = buildCSS + filter(lambda l: is_absolute_dependency(l), css)
66+
css = filter(lambda l: not is_absolute_dependency(l), css)
67+
# Save to db
68+
for dep in buildJS:
69+
JSDependency.objects.create(component=component, js_url=dep, sniper_data_value=dep)
70+
for dep in buildCSS:
71+
CSSDependency.objects.create(component=component, css_url=dep, sniper_data_value=dep)
72+
73+
sniperData, created = SniperData.objects.get_or_create(component=component)
74+
if 'noBrowserify' in sniper_data:
75+
sniperData.no_browserify = sniper_data['noBrowserify']
76+
elif len(js) == 0 and len(css) == 0:
77+
sniperData.no_browserify = True
78+
79+
sniperData.wzrd_url = '#' if sniperData.no_browserify else 'https://wzrd.in/bundle/' + component.name
80+
if 'snippets' in sniper_data:
81+
sniperData.snippets_dir_name = sniper_data['snippets'][0]
12582
sniperData.save()
12683

12784
### For Snippets URLs
12885
try:
86+
url = str('https://api.github.com/repos/' + owner + '/' + repo_name + '/contents/' + sniperData.snippets_dir_name + '?ref=master')
12987
if not test:
130-
print ("https://api.github.com/repos/" + str(owner) + "/" + str(repo_name) + "/contents/" + sniperData.snippets_dir_name + "?ref=master&client_id="
131-
+ GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET)
132-
snippets_data = urllib.urlopen("https://api.github.com/repos/" + str(owner) + "/" + str(repo_name) + "/contents/" + sniperData.snippets_dir_name + "?ref=master&client_id="
133-
+ GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET)
134-
snippets = json.loads(snippets_data.read())
135-
for snippet in snippets:
136-
if not snippet['name'].endswith('.js'):
137-
continue
138-
url = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + "/" + sniperData.snippets_dir_name + "/" + snippet['name']
139-
try:
140-
name = snippet['name'].split('.')[0]
141-
req_snippet = Snippet.objects.get(name=name, sniperData=sniperData)
142-
req_snippet.url = url
143-
req_snippet.save()
144-
except:
145-
name = snippet['name'].split('.')[0]
146-
Snippet.objects.create(name=name, sniperData=sniperData, url=url)
147-
except:
148-
pass
88+
print(url)
89+
snippets_data = send_GET_request(url, GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
90+
snippets = json.load(snippets_data)
91+
filtered_snippets = filter(lambda s: s['name'].endswith('.js'), snippets)
92+
except Exception as e:
93+
print('ERROR: Something went wrong getting snippets data!')
94+
print(e)
14995

150-
def get_github_data(github_url):
151-
response = urllib.urlopen(github_url)
152-
data = json.load(response)
153-
return data
154-
155-
def get_npm_data():
156-
# response = urllib2.urlopen()
157-
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
158-
req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=keywords:biojs,bionode&size=500", headers=hdr)
159-
# req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=biojs-vis-msa&size=1", headers=hdr)
160-
response = urllib2.urlopen(req)
161-
data = json.load(response)
162-
return data
163-
164-
def get_contributors_data(contributors_url):
165-
response = urllib.urlopen(contributors_url)
166-
data = json.load(response)
167-
return data
168-
169-
def get_downloads(downloads_url):
170-
print (downloads_url)
171-
response = urllib.urlopen(downloads_url)
172-
downloads = 0
173-
data = ast.literal_eval(response.read())
174-
for download in data:
175-
downloads += int(download['download_count'])
176-
return downloads
17796

97+
for snip in filtered_snippets:
98+
try:
99+
url = create_jsdelivr_link(owner, repo_name, str('/' + sniperData.snippets_dir_name + '/' + snip['name']), commit_hash)
100+
name = snip.get('name', '').split('.')[0]
101+
Snippet.objects.update_or_create(name=name, url=url, sniperData=sniperData)
102+
except Exception as e:
103+
print('ERROR: Something went wrong creating a new Snippet')
104+
print(e)
178105

179106
class Command(BaseCommand):
180107
# during --help
@@ -228,7 +155,7 @@ def handle(self, *args, **options):
228155
try:
229156
github_url = component_data['links']['repository']
230157
url_list = github_url.split('/')
231-
_component.github_url = 'https://api.github.com/repos/' + str(url_list[3]) + '/' + str(url_list[4]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
158+
_component.github_url = 'https://api.github.com/repos/' + str(url_list[3]) + '/' + str(url_list[4])
232159
except:
233160
pass
234161
try:
@@ -244,8 +171,15 @@ def handle(self, *args, **options):
244171
if _component.github_url:
245172
print (_component.github_url)
246173
try:
247-
github_data = get_github_data(_component.github_url)
248-
except:
174+
response = send_GET_request(_component.github_url, GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
175+
github_data = json.load(response)
176+
except urllib2.HTTPError as e:
177+
print('Error getting github data!')
178+
print(e)
179+
continue
180+
except Exception as e:
181+
print('Unexpected error accessing Github!')
182+
print(e)
249183
continue
250184
_component.stars = github_data['stargazers_count']
251185
_component.forks = github_data['forks']
@@ -277,17 +211,25 @@ def handle(self, *args, **options):
277211
# else:
278212
_component.github_update_time = aware_date
279213
try:
280-
latest_commit_hash = get_commit_hash(github_data['commits_url'])
214+
response = send_GET_request(github_data['commits_url'].split('{')[0], GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
215+
latest_commit = json.load(response)[0]
216+
latest_commit_hash = latest_commit['sha']
217+
_component.latest_commit_hash = latest_commit_hash
281218
except:
282-
continue
283-
_component.latest_commit_hash = latest_commit_hash
284-
update_visualizations(_component, latest_commit_hash)
219+
print('Error getting commit hash!')
220+
pass
221+
try:
222+
update_visualizations(_component, latest_commit_hash)
223+
except Exception as e:
224+
print('Error updating visualisations!')
225+
print(e)
285226
# except:
286227
# pass
287228
_component.save()
288-
print (str(github_data['contributors_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET)
229+
print (str(github_data['contributors_url']))
289230
try:
290-
contributors_data = get_contributors_data(str(github_data['contributors_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET)
231+
response = send_GET_request(str(github_data['contributors_url']), GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
232+
contributors_data = json.load(response)
291233
except:
292234
continue
293235
commits = 0
@@ -307,12 +249,11 @@ def handle(self, *args, **options):
307249
commits += _contribution.contributions
308250
count +=1
309251
except:
310-
print 'Error'
252+
print ('Error')
311253
continue
312-
try:
313-
_component.downloads = get_downloads(str(github_data['downloads_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET)
314-
except:
315-
pass
254+
response = send_GET_request(github_data['downloads_url'], GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
255+
downloads_array = json.load(response)
256+
_component.downloads = len(downloads_array)
316257
_component.commits = commits
317258
_component.no_of_contributors = count
318259
_component.save()

0 commit comments

Comments
 (0)