Skip to content

Commit ac6a5a8

Browse files
committed
Models and management command to handle sniper dependencies
1 parent 913ce00 commit ac6a5a8

File tree

6 files changed

+223
-3
lines changed

6 files changed

+223
-3
lines changed

main/admin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
admin.site.register(Component)
55
admin.site.register(Tag)
66
admin.site.register(Contributor)
7-
admin.site.register(Contribution)
7+
admin.site.register(Contribution)
8+
admin.site.register(CSSDependency)
9+
admin.site.register(JSDependency)

main/management/commands/updatecomponents.py

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,83 @@
88
GITHUB_CLIENT_SECRET = ''
99
from datetime import datetime
1010
import pytz
11+
import ast
12+
13+
# Get sniper data
14+
'''
15+
https://rawgit.com/cytoscape/cytoscape.js/master/package.json
16+
"sniper" key, has "js" and "css". Search for "first"
17+
'''
18+
19+
def get_commit_hash(commits_url):
20+
# import ast
21+
22+
commits_url = commits_url.split('{')[0] + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
23+
print commits_url
24+
response = urllib.urlopen(commits_url)
25+
data = json.loads(response.read())[0]
26+
return data['sha']
27+
28+
def update_visualizations(component, commit_hash):
29+
github_url_list = component.github_url.split('?')[0].split('/')
30+
owner = github_url_list[4]
31+
repo_name = github_url_list[5]
32+
# print "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + "/package.json"
33+
try:
34+
sniper_data = json.load(urllib.urlopen("https://cdn.rawgit.com/" + str(owner) +
35+
'/' + str(repo_name) + "/" + commit_hash + "/package.json"))["sniper"]
36+
except:
37+
return
38+
try:
39+
buildJS = sniper_data["buildJS"]
40+
except:
41+
buildJS = []
42+
try:
43+
js = sniper_data["js"]
44+
except:
45+
js = []
46+
js_dependencies = buildJS + js
47+
try:
48+
buildCSS = sniper_data["buildCSS"]
49+
except:
50+
buildCSS = []
51+
try:
52+
css = sniper_data["css"]
53+
except:
54+
css = []
55+
css_dependencies = buildCSS + css
56+
for dependency in js_dependencies:
57+
if dependency.startswith('https://'):
58+
dependency, created = JSDependency.objects.get_or_create(component=component, js_url=dependency)
59+
elif dependency.startswith('/'):
60+
dependency = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + dependency
61+
dependency, created = JSDependency.objects.get_or_create(component=component, js_url=dependency)
62+
else:
63+
dependency = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + "/" + dependency
64+
dependency, created = JSDependency.objects.get_or_create(component=component, js_url=dependency)
65+
for dependency in css_dependencies:
66+
if dependency.startswith('https://'):
67+
dependency, created = CSSDependency.objects.get_or_create(component=component, css_url=dependency)
68+
elif dependency.startswith('/'):
69+
dependency = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + dependency
70+
dependency, created = CSSDependency.objects.get_or_create(component=component, css_url=dependency)
71+
else:
72+
dependency = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + "/" + dependency
73+
dependency, created = CSSDependency.objects.get_or_create(component=component, css_url=dependency)
74+
try:
75+
sniperData = SniperData.objects.get(component=component)
76+
except:
77+
sniperData = SniperData.objects.create(component=component)
78+
try:
79+
no_browserify = sniper_data['noBrowserify']
80+
sniperData.no_browserify = no_browserify
81+
if no_browserify:
82+
sniperData.wzrd_url = '#'
83+
else:
84+
sniperData.wzrd_url = "https://wzrd.in/bundle/" + component.name
85+
except:
86+
pass
87+
sniperData.save()
1188

1289
def get_github_data(github_url):
1390
response = urllib.urlopen(github_url)
@@ -18,6 +95,7 @@ def get_npm_data():
1895
# response = urllib2.urlopen()
1996
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
2097
req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=keywords:biojs,bionode&size=500", headers=hdr)
98+
# req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=biojs-vis-msa&size=1", headers=hdr)
2199
response = urllib2.urlopen(req)
22100
data = json.load(response)
23101
return data
@@ -28,7 +106,7 @@ def get_contributors_data(contributors_url):
28106
return data
29107

30108
def get_downloads(downloads_url):
31-
import ast
109+
# import ast
32110
print downloads_url
33111
response = urllib.urlopen(downloads_url)
34112
downloads = 0
@@ -126,6 +204,23 @@ def handle(self, *args, **options):
126204
_component.created_time = aware_date
127205
except:
128206
pass
207+
# try:
208+
str_date = github_data['updated_at']
209+
req_date = datetime.strptime(str_date, "%Y-%m-%dT%H:%M:%SZ") #This object is timezone unaware
210+
aware_date = pytz.utc.localize(req_date) #This object is now timezone aware
211+
# if _component.github_update_time:
212+
# if aware_date > _component.github_update_time:
213+
# _component.github_updated_time = aware_date
214+
# latest_commit_hash = get_commit_hash(github_data['commits_url'])
215+
# _component.latest_commit_hash = latest_commit_hash
216+
# update_visualizations(_component, latest_commit_hash)
217+
# else:
218+
_component.github_update_time = aware_date
219+
latest_commit_hash = get_commit_hash(github_data['commits_url'])
220+
_component.latest_commit_hash = latest_commit_hash
221+
update_visualizations(_component, latest_commit_hash)
222+
# except:
223+
# pass
129224
_component.save()
130225
print str(github_data['contributors_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
131226
try:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.13 on 2018-06-20 22:34
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('main', '0010_auto_20180616_1659'),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='CSSDependency',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('css_url', models.URLField(null=True)),
21+
('component', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.Component')),
22+
],
23+
),
24+
migrations.CreateModel(
25+
name='JSDependency',
26+
fields=[
27+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
28+
('js_url', models.URLField(null=True)),
29+
('component', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.Component')),
30+
],
31+
),
32+
migrations.CreateModel(
33+
name='Visualization',
34+
fields=[
35+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
36+
('name', models.CharField(max_length=50)),
37+
('url', models.URLField(null=True)),
38+
('component', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.Component')),
39+
],
40+
),
41+
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.13 on 2018-06-24 09:30
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('main', '0011_cssdependency_jsdependency_visualization'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='component',
17+
name='github_update_time',
18+
field=models.DateTimeField(null=True),
19+
),
20+
migrations.AddField(
21+
model_name='component',
22+
name='latest_commit_hash',
23+
field=models.CharField(max_length=40, null=True),
24+
),
25+
]

main/migrations/0013_sniperdata.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.13 on 2018-06-25 07:32
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('main', '0012_auto_20180624_0930'),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='SniperData',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('no_browserify', models.BooleanField(default=False)),
21+
('wzrd_url', models.URLField(null=True)),
22+
('component', models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.Component')),
23+
],
24+
),
25+
]

main/models.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Component(models.Model):
4141
npm_url = models.URLField(null=True)
4242
homepage_url = models.URLField(null=True)
4343
license = models.CharField(max_length=50, null=True)
44+
github_update_time = models.DateTimeField(null=True)
45+
latest_commit_hash = models.CharField(max_length=40, null=True)
4446

4547
def save(self, *args, **kwargs):
4648
if not self.url_name:
@@ -67,4 +69,34 @@ def __unicode__(self):
6769
class Contribution(models.Model):
6870
contributor = models.ForeignKey(Contributor, on_delete=models.SET_NULL, null=True)
6971
component = models.ForeignKey(Component, on_delete=models.SET_NULL, null=True, related_name='contributions')
70-
contributions = models.IntegerField(default=0)
72+
contributions = models.IntegerField(default=0)
73+
74+
class Visualization(models.Model):
75+
name = models.CharField(max_length=50)
76+
component = models.ForeignKey(Component, null=True, on_delete=models.SET_NULL)
77+
url = models.URLField(null=True)
78+
79+
def __unicode__(self):
80+
return self.name + '-' + str(self.component.name)
81+
82+
class JSDependency(models.Model):
83+
js_url = models.URLField(null=True)
84+
component = models.ForeignKey(Component, null=True, on_delete=models.SET_NULL)
85+
86+
def __unicode__(self):
87+
return str(self.component)
88+
89+
class CSSDependency(models.Model):
90+
css_url = models.URLField(null=True)
91+
component = models.ForeignKey(Component, null=True, on_delete=models.SET_NULL)
92+
93+
def __unicode__(self):
94+
return str(self.component)
95+
96+
class SniperData(models.Model):
97+
no_browserify = models.BooleanField(default=False)
98+
wzrd_url = models.URLField(null=True)
99+
component = models.OneToOneField(Component, on_delete=models.SET_NULL, null=True)
100+
101+
def __unicode__(self):
102+
return str(self.component)

0 commit comments

Comments
 (0)