diff --git a/gratipay/chomp/__init__.py b/gratipay/chomp/__init__.py new file mode 100644 index 0000000000..de9ad36528 --- /dev/null +++ b/gratipay/chomp/__init__.py @@ -0,0 +1,61 @@ +"""Process npm artifacts. + +The main function is exposed as a console script named `chomp` via setup.py. + +""" +from __future__ import absolute_import, division, print_function, unicode_literals + +import argparse +import sys + +import requests +from gratipay.utils import markdown + + +def from_npm(package): + """Given an npm package dict, return a dict of info and a list of emails. + """ + out= {} + out['name'] = package['name'] + out['description'] = package['description'] + out['long_description'] = markdown.marky(package['readme']) + out['long_description_raw'] = package['readme'] + out['long_description_type'] = 'x-text/marky-markdown' + + emails = [] + for key in ('authors', 'maintainers'): + for person in package.get(key, []): + if type(person) is dict: + email = person.get('email') + if email: + emails.append(email) + + return out, emails + + +def process_catalog(catalog): + SQL = '' + return SQL + + +def fetch_catalog(): + r = requests.get('https://registry.npmjs.com/-/all') + r.raise_for_status() + return r.json() + + +def update_database(SQL): + pass + + +def parse_args(argv): + p = argparse.ArgumentParser() + p.add_argument( 'if_modified_since' + , help='a number of minutes in the past, past which we need new updates' + ) + return p.parse_args(argv) + + +def main(argv=sys.argv): + ims = parse_args(argv[1:]).if_modified_since + process_catalog(fetch_catalog(), ims) diff --git a/gratipay/models/package.py b/gratipay/models/package.py new file mode 100644 index 0000000000..af5b0be893 --- /dev/null +++ b/gratipay/models/package.py @@ -0,0 +1,12 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + +from postgres import orm + + +class Package(orm.Model): + typ_name = 'packages'; + + @classmethod + def from_platform_and_name(cls, platform, name): + return cls.db.one("SELECT packages.*::packages from packages where " + "platform=%s and name=%s;", (platform, name,)) diff --git a/gratipay/utils/markdown.py b/gratipay/utils/markdown.py index a1826f93e6..9adfa05c2b 100644 --- a/gratipay/utils/markdown.py +++ b/gratipay/utils/markdown.py @@ -1,5 +1,6 @@ from markupsafe import Markup import misaka as m # http://misaka.61924.nl/ +import subprocess def render(markdown): return Markup(m.html( @@ -7,3 +8,6 @@ def render(markdown): extensions=m.EXT_AUTOLINK | m.EXT_STRIKETHROUGH | m.EXT_NO_INTRA_EMPHASIS, render_flags=m.HTML_SKIP_HTML | m.HTML_TOC | m.HTML_SMARTYPANTS | m.HTML_SAFELINK )) + +def marky(markdown): + return subprocess.call() diff --git a/gratipay/wireup.py b/gratipay/wireup.py index 8aa1dd220e..a64f9d2a8b 100644 --- a/gratipay/wireup.py +++ b/gratipay/wireup.py @@ -29,6 +29,7 @@ from gratipay.elsewhere.openstreetmap import OpenStreetMap from gratipay.elsewhere.twitter import Twitter from gratipay.elsewhere.venmo import Venmo +from gratipay.models.npm_package import Package from gratipay.models.account_elsewhere import AccountElsewhere from gratipay.models.community import Community from gratipay.models.country import Country @@ -56,7 +57,8 @@ def db(env): maxconn = env.database_maxconn db = GratipayDB(dburl, maxconn=maxconn) - for model in (AccountElsewhere, Community, Country, ExchangeRoute, Participant, Team): + models = (AccountElsewhere, Community, Country, ExchangeRoute, Participant, Team, Package) + for model in models: db.register_model(model) gratipay.billing.payday.Payday.db = db diff --git a/setup.py b/setup.py index 888240d06d..e4615bd19f 100644 --- a/setup.py +++ b/setup.py @@ -9,6 +9,7 @@ , entry_points = { 'console_scripts' : [ 'payday=gratipay.cli:payday' , 'fake_data=gratipay.utils.fake_data:main' + , 'chomp=gratipay.chomp:main' ] } ) diff --git a/sql/branch.sql b/sql/branch.sql new file mode 100644 index 0000000000..f1d643ab82 --- /dev/null +++ b/sql/branch.sql @@ -0,0 +1,26 @@ +BEGIN; + + CREATE TABLE package_managers + ( id bigserial PRIMARY KEY + , name text NOT NULL UNIQUE + ); + + CREATE TABLE packages + ( id bigserial PRIMARY KEY + , package_manager_id bigint NOT NULL + , name text NOT NULL + , description text NOT NULL DEFAULT '' + , long_description text NOT NULL DEFAULT '' + , long_description_raw text NOT NULL DEFAULT '' + , long_description_type text NOT NULL DEFAULT '' + , mtime timestamp with time zone NOT NULL + , UNIQUE (package_manager_id, name) + ); + + CREATE TABLE package_emails + ( package_id bigint NOT NULL + , email text NOT NULL + , UNIQUE (package_id, email) + ) + +END;