Skip to content

Commit dff3007

Browse files
author
Dennis Schwartz
committed
Re-wrote update_visualizations function.
1 parent 604a480 commit dff3007

File tree

2 files changed

+60
-80
lines changed

2 files changed

+60
-80
lines changed

main/management/commands/updatecomponents.py

Lines changed: 59 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_npm_data():
2727
data = json.load(response)
2828
return data
2929

30-
def send_GET_request(user, password, url):
30+
def send_GET_request(url, user=None, password=None):
3131
request = urllib2.Request(url)
3232
if (user is not None and password is not None):
3333
base64string = base64.encodestring('%s:%s' % (user, password)).replace('\n', '')
@@ -37,94 +37,70 @@ def send_GET_request(user, password, url):
3737
def create_jsdelivr_link(owner, repo, file_path, commit=None):
3838
return str('https://cdn.jsdelivr.net/gh/' + owner + '/' + repo + ('@' + commit if commit else '') + file_path)
3939

40-
def build_dependency_link(owner, repo, dependency, commit=None):
41-
if (re.match('^(?:[a-z]+:)?//', dependency)):
42-
print('Absolute dependency!', dependency)
43-
return dependency
44-
else:
45-
print('relative dependency!', dependency)
46-
return create_jsdelivr_link(owner, repo, dependency, commit)
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]
4746

4847
### store the dependency urls and snippet urls
4948
def update_visualizations(component, commit_hash, test=False):
50-
github_url_list = component.github_url.split('?')[0].split('/')
51-
owner = github_url_list[4]
52-
repo_name = github_url_list[5]
49+
owner, repo_name = get_owner_and_repo_from_github_url(component.github_url)
5350
try:
5451
url = create_jsdelivr_link(owner, repo_name, '/package.json', commit_hash)
55-
# url = str('https://cdn.jsdelivr.net/gh/' + str(owner) + '/' + str(repo_name) + '@' + commit_hash + '/package.json')
56-
response = send_GET_request(None, None, url)
52+
response = send_GET_request(url)
5753
package = json.load(response)
5854
sniper_data = package["sniper"]
5955
except KeyError:
6056
print('No sniper info in ', repo_name)
6157
return
62-
# buildJS and js are thrown together and then separated by string matching?
63-
try:
64-
buildJS = sniper_data["buildJS"]
65-
except:
66-
buildJS = []
67-
try:
68-
js = sniper_data["js"]
69-
except:
70-
js = []
71-
js_dependencies = buildJS + js
72-
try:
73-
buildCSS = sniper_data["buildCSS"]
74-
except:
75-
buildCSS = []
76-
try:
77-
css = sniper_data["css"]
78-
except:
79-
css = []
80-
css_dependencies = buildCSS + css
81-
js_deps = map(lambda l: build_dependency_link(owner, repo_name, l, commit_hash), js_dependencies)
82-
css_deps = map(lambda l: build_dependency_link(owner, repo_name, l, commit_hash), css_dependencies)
83-
try:
84-
sniperData = SniperData.objects.get(component=component)
85-
except:
86-
sniperData = SniperData.objects.create(component=component)
87-
try:
88-
no_browserify = sniper_data['noBrowserify']
89-
sniperData.no_browserify = no_browserify
90-
except:
91-
pass
92-
try:
93-
if no_browserify:
94-
sniperData.wzrd_url = '#'
95-
else:
96-
sniperData.wzrd_url = "https://wzrd.in/bundle/" + component.name
97-
except:
98-
sniperData.wzrd_url = "https://wzrd.in/bundle/" + component.name
99-
try:
100-
snippets_dir_name = sniper_data['snippets'][0]
101-
sniperData.snippets_dir_name = snippets_dir_name
102-
except:
103-
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+
77+
sniperData.wzrd_url = '#' if sniperData.no_browserify else 'https://wzrd.in/bundle/' + component.name
78+
if 'snippets' in sniper_data:
79+
sniperData.snippets_dir_name = sniper_data['snippets'][0]
10480
sniperData.save()
10581

10682
### For Snippets URLs
10783
try:
84+
url = str('https://api.github.com/repos/' + owner + '/' + repo_name + '/contents/' + sniperData.snippets_dir_name + '?ref=master')
10885
if not test:
109-
print ("https://api.github.com/repos/" + str(owner) + "/" + str(repo_name) + "/contents/" + sniperData.snippets_dir_name + "?ref=master&client_id="
110-
+ GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET)
111-
snippets_data = urllib.urlopen("https://api.github.com/repos/" + str(owner) + "/" + str(repo_name) + "/contents/" + sniperData.snippets_dir_name + "?ref=master&client_id="
112-
+ GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET)
113-
snippets = json.loads(snippets_data.read())
114-
for snippet in snippets:
115-
if not snippet['name'].endswith('.js'):
116-
continue
117-
url = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + "/" + sniperData.snippets_dir_name + "/" + snippet['name']
118-
try:
119-
name = snippet['name'].split('.')[0]
120-
req_snippet = Snippet.objects.get(name=name, sniperData=sniperData)
121-
req_snippet.url = url
122-
req_snippet.save()
123-
except:
124-
name = snippet['name'].split('.')[0]
125-
Snippet.objects.create(name=name, sniperData=sniperData, url=url)
126-
except:
127-
pass
86+
print(url)
87+
snippets_data = send_GET_request(url, GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
88+
snippets = json.load(snippets_data)
89+
filtered_snippets = filter(lambda s: s['name'].endswith('.js'), snippets)
90+
except Exception as e:
91+
print('ERROR: Something went wrong getting snipper data!')
92+
print(e)
93+
94+
95+
for snip in filtered_snippets:
96+
try:
97+
url = create_jsdelivr_link(owner, repo_name, str('/' + sniperData.snippets_dir_name + '/' + snip['name']), commit_hash)
98+
name = snip.get('name', '').split('.')[0]
99+
Snippet.objects.create(name=name, url=url, sniperData=sniperData)
100+
print('here!')
101+
except Exception as e:
102+
print('ERROR: Something went wrong creating a new Snippet')
103+
print(e)
128104

129105
class Command(BaseCommand):
130106
# during --help
@@ -194,7 +170,7 @@ def handle(self, *args, **options):
194170
if _component.github_url:
195171
print (_component.github_url)
196172
try:
197-
response = send_GET_request(GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, _component.github_url)
173+
response = send_GET_request(_component.github_url, GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
198174
github_data = json.load(response)
199175
except urllib2.HTTPError as e:
200176
print('Error getting github data!')
@@ -234,20 +210,24 @@ def handle(self, *args, **options):
234210
# else:
235211
_component.github_update_time = aware_date
236212
try:
237-
response = send_GET_request(GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, github_data['commits_url'].split('{')[0])
213+
response = send_GET_request(github_data['commits_url'].split('{')[0], GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
238214
latest_commit = json.load(response)[0]
239215
latest_commit_hash = latest_commit['sha']
240216
_component.latest_commit_hash = latest_commit_hash
241-
update_visualizations(_component, latest_commit_hash)
242217
except:
243218
print('Error getting commit hash!')
244219
pass
220+
try:
221+
update_visualizations(_component, latest_commit_hash)
222+
except Exception as e:
223+
print('Error updating visualisations!')
224+
print(e)
245225
# except:
246226
# pass
247227
_component.save()
248228
print (str(github_data['contributors_url']))
249229
try:
250-
response = send_GET_request(GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, str(github_data['contributors_url']))
230+
response = send_GET_request(str(github_data['contributors_url']), GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
251231
contributors_data = json.load(response)
252232
except:
253233
continue
@@ -270,7 +250,7 @@ def handle(self, *args, **options):
270250
except:
271251
print ('Error')
272252
continue
273-
response = send_GET_request(GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, github_data['downloads_url'])
253+
response = send_GET_request(github_data['downloads_url'], GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
274254
downloads_array = json.load(response)
275255
_component.downloads = len(downloads_array)
276256
_component.commits = commits

main/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class SniperData(models.Model):
100100
wzrd_url = models.URLField(null=True)
101101
component = models.OneToOneField(Component, on_delete=models.SET_NULL, null=True)
102102
snippets_dir_name = models.CharField(max_length=100, default='snippets')
103-
103+
104104
def __unicode__(self):
105105
return str(self.component)
106106

0 commit comments

Comments
 (0)