This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4159 from gratipay/readmes
Here's a first crack at syncing readmes
- Loading branch information
Showing
8 changed files
with
129 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
import sys | ||
|
||
import requests | ||
|
||
from gratipay.utils import markdown | ||
from gratipay.utils.threaded_map import threaded_map | ||
from threading import Lock | ||
|
||
|
||
log_lock = Lock() | ||
|
||
def log(*a, **kw): | ||
with log_lock: | ||
print(*a, file=sys.stderr, **kw) | ||
|
||
|
||
def http_fetch(package_name): | ||
r = requests.get('https://registry.npmjs.com/' + package_name) | ||
if r.status_code != 200: | ||
log(r.status_code, 'for', package_name) | ||
return None | ||
return r.json() | ||
|
||
|
||
def Syncer(db): | ||
def sync(dirty, fetch=http_fetch): | ||
"""Update all info for one package. | ||
""" | ||
log(dirty.name) | ||
full = fetch(dirty.name) | ||
if not full: | ||
return # try again later | ||
assert full['name'] == dirty.name | ||
|
||
db.run(''' | ||
UPDATE packages | ||
SET readme=%s | ||
, readme_raw=%s | ||
, readme_type=%s | ||
WHERE package_manager=%s | ||
AND name=%s | ||
''', ( markdown.marky(full['readme']) | ||
, full['readme'] | ||
, 'x-markdown/npm' | ||
, dirty.package_manager | ||
, dirty.name | ||
)) | ||
|
||
return sync | ||
|
||
|
||
def sync_all(db): | ||
dirty = db.all('SELECT package_manager, name FROM packages WHERE readme_raw IS NULL ' | ||
'ORDER BY package_manager DESC, name DESC') | ||
threaded_map(Syncer(db), dirty, 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from multiprocessing.dummy import Pool as ThreadPool | ||
|
||
|
||
class ExceptionWrapped(Exception): | ||
pass | ||
|
||
|
||
def threaded_map(func, iterable, threads=5): | ||
pool = ThreadPool(threads) | ||
def g(*a, **kw): | ||
# Without this wrapper we get a traceback from inside multiprocessing. | ||
try: | ||
return func(*a, **kw) | ||
except Exception as e: | ||
import traceback | ||
raise ExceptionWrapped(e, traceback.format_exc()) | ||
try: | ||
r = pool.map(g, iterable) | ||
except ExceptionWrapped as e: | ||
print(e.args[1]) | ||
raise e.args[0] | ||
pool.close() | ||
pool.join() | ||
return r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN; | ||
ALTER TABLE packages ALTER COLUMN readme_raw DROP NOT NULL; | ||
ALTER TABLE packages ALTER COLUMN readme_raw SET DEFAULT NULL; | ||
UPDATE packages SET readme_raw=NULL; | ||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters