class DynProtService(ServiceBase): protocols = {} @rpc(String(values=protocols.keys(), encoding='ascii'), _returns=DateTime, _patterns=[HttpPattern('/get_utc_time.<prot>')]) def get_utc_time(ctx, prot): DynProtService.protocols[prot](ctx) return datetime.utcnow()
class IndexService(ServiceBase): @rpc(_returns=Array(Index), _patterns=[HttpPattern("/", verb="GET")]) def index(ctx): return [ Index( Updated=package.package_cdate, Package=AnyUri.Value(text=package.package_name, href=package.latest_release.rdf_about), Description=html.fromstring(package.package_description), ) for package in ctx.udc.session.query(Package) ]
class RdfService(ServiceBase): @rpc(Unicode, Unicode, _returns=Project, _patterns=[ HttpPattern("/<project_name>/doap.rdf"), HttpPattern("/<project_name>/<version>/doap.rdf"), ]) def get_doap(ctx, project_name, version): package = ctx.udc.session.query(Package).filter_by( package_name=project_name).one() return Project(about=package.package_name, name=package.package_name, created=package.package_cdate, shortdesc=package.package_description, homepage=package.package_home_page, developer=Developer( Person=Person(name=package.owners[0].person_name, mbox=package.owners[0].person_email)), release=(Release( about=rel.rdf_about, Version=Version( **{ "name": package.package_name, "created": rel.release_cdate, "revision": rel.release_version, 'file-release': rel.distributions[0].file_name, "resource": 'files/%s/%s#%s' % ( rel.distributions[0].file_path, rel.distributions[0].file_name, rel.distributions[0].dist_md5, ) })) for rel in package.releases))
class SomeService(ServiceBase): @srpc(Integer, _returns=Integer, _patterns=[HttpPattern('/%s/<some_int>' % _fragment)]) def some_call(some_int): assert some_int == _int
class RootService(ServiceBase): FILES_ROOT = None # To be set by main() @rpc(Unicode,#name Unicode, #license Unicode, #author Unicode, #home_page File, #content Unicode, #comment Unicode, #download_url Unicode, #platform Unicode, #description Unicode, #metadata_version Unicode, #author_email Unicode, #md5_digest Unicode, #filetype Unicode, #pyversion Unicode, #summary Unicode, #version Unicode, #protocol_version String(encoding='ascii'), #action _patterns=[HttpPattern("/",verb="POST")], _in_variable_names={'action': ':action'}) def register(ctx, name, license, author, home_page, content, comment, download_url, platform, description, metadata_version, author_email, md5_digest, filetype, pyversion, summary, version, protcol_version, # <= this is not a typo action): pth = os.path.join(name, version) def generate_package(): return Package( package_name=name, package_cdate=datetime.date.today(), package_description=process_description(description, 'utf8'), rdf_about=os.path.join("/", name), package_license=license, package_home_page=home_page ) def generate_person(): return Person(person_name=author, person_email=author_email, ) def generate_release(): return Release( rdf_about=os.path.join("/", name, version), release_version=version, release_cdate=datetime.date.today(), release_summary=summary , meta_version=metadata_version, release_platform=platform, ) def write_package_content(file_path): if not os.path.exists(path): os.makedirs(os.path.dirname(file_path)) f = open(file_path,"w") for d in content.data: f.write(d) f.close() package = ctx.udc.session.query(Package).filter_by(package_name=name).first() release = None if package is not None: release = ctx.udc.session.query(Release).join(Package).filter( sql.and_( Package.package_name == name, Release.release_version == version, )).first() if action == "submit": if package is None: package = generate_package() package.owners.append(generate_person()) ctx.udc.session.add(package) if release is None: package.releases.append(generate_release()) ctx.udc.session.commit() elif action == "file_upload": content.name = os.path.basename(content.name) path = os.path.join(RootService.FILES_ROOT, pth) file_path = os.path.join(path, content.name) if package is None: package = generate_package() package.owners.append(generate_person()) ctx.udc.session.add(package) if release is None: release = generate_release() package.releases.append(release) release.distributions.append(Distribution( file_name=content.name, file_path=pth, dist_download_url=download_url, dist_comment=comment, dist_file_type=filetype, dist_md5=md5_digest, py_version=pyversion, protocol_version=protcol_version, )) ctx.udc.session.flush() if os.path.isfile(file_path): raise ArgumentError((name, version )) write_package_content(file_path) ctx.udc.session.commit()
class HtmlService(ServiceBase): @rpc(Unicode, Unicode, _returns=Unicode, _patterns=[ HttpPattern("/<project_name>"), HttpPattern("/<project_name>/"), HttpPattern("/<project_name>/<version>"), HttpPattern("/<project_name>/<version>/"), ]) def download_html(ctx, project_name, version): ctx.transport.mime_type = "text/html" own_url = reconstruct_url(ctx.transport.req_env, path=False, query_string=False) try: ctx.udc.session.query(Package).filter_by( package_name=project_name).one() except NoResultFound: cache_package(project_name, own_url) download = HtmlPage(TPL_DOWNLOAD) download.title = project_name if version: release = ctx.udc.session.query(Release).join(Package).filter( sql.and_( Package.package_name == project_name, Release.release_version == version, )).one() if len(release.distributions) == 0: cache_package("%s==%s" % (project_name, version), own_url) ctx.udc.session.refresh(release) download.link.attrib["href"] = "%s/doap.rdf" % (release.rdf_about) download.h1 = '%s-%s' % (project_name, version) download.a = release.distributions[0].file_name download.a.attrib["href"] = "/files/%s/%s#md5=%s" % ( release.distributions[0].file_path, release.distributions[0].file_name, release.distributions[0].dist_md5, ) else: package = ctx.udc.session.query(Package) \ .filter_by(package_name=project_name).one() if len(package.latest_release.distributions) == 0: cache_package(project_name, own_url) ctx.udc.session.refresh(package) download = HtmlPage(TPL_DOWNLOAD) download.link.attrib["href"] = '%s/doap.rdf' % ( package.latest_release.rdf_about) download.h1 = project_name download.a = package.latest_release.distributions[0].file_name download.a.attrib["href"] = "/files/%s/%s#md5=%s" % ( package.latest_release.distributions[0].file_path, package.latest_release.distributions[0].file_name, package.latest_release.distributions[0].dist_md5) return html.tostring(download.html) @rpc( Unicode, Unicode, Unicode, _returns=File, _patterns=[HttpPattern("/files/<project_name>/<version>/<file_name>")]) def download_file(ctx, project_name, version, file_name): repository_path = os.path.abspath(os.path.join(FILES_PATH)) file_path = os.path.join(repository_path, project_name, version, file_name) file_path = os.path.abspath(file_path) if not file_path.startswith(repository_path): # This request tried to read data from where it's not supposed to raise RequestNotAllowed(repr([project_name, version, file_name])) return File.Value(name=file_name, path=file_path)