diff --git a/webservice/bam/application/__init__.py b/webservice/bam/application/__init__.py index 56f39be..19021f0 100644 --- a/webservice/bam/application/__init__.py +++ b/webservice/bam/application/__init__.py @@ -40,9 +40,16 @@ import werkzeug import xml.etree.ElementTree import logging -from flask import Flask, jsonify, abort, request, make_response, url_for, Response +from flask import Flask +from flask import jsonify +from flask import abort +from flask import request +from flask import make_response +from flask import url_for +from flask import Response + from flask.views import MethodView -from flask.ext.restful import Api, Resource, reqparse, fields, marshal +from flask.ext.restful import Api from flask.ext.httpauth import HTTPBasicAuth from flask.ext.sqlalchemy import SQLAlchemy @@ -61,14 +68,11 @@ else: app.config.from_object(config.Development) db = SQLAlchemy(app) - -from application.modules.admin import backend -from application.modules.admin import settings -from application.modules.projects import admin -from application.modules.projects.model import Project, ProjectSetting - log = logging.getLogger("webservice") +from application.modules.resources import DirectoryAPI +from application.modules.resources import FileAPI + if os.environ.get("BAM_VERBOSE"): logging.basicConfig(level=logging.DEBUG) @@ -90,347 +94,5 @@ def unauthorized(): # the default auth dialog -class DirectoryAPI(Resource): - """Displays list of files.""" - - decorators = [auth.login_required] - - def __init__(self): - parser = reqparse.RequestParser() - # parser.add_argument('rate', type=int, help='Rate cannot be converted') - parser.add_argument('path', type=str) - # args = parser.parse_args() - super(DirectoryAPI, self).__init__() - - def get(self, project_name): - - project = Project.query.filter_by(name=project_name).first() - - path = request.args['path'] - if not path: - path = '' - - path_root_abs = project.repository_path - parent_path = '' - - if path != '': - path_root_abs = os.path.join(path_root_abs, path) - parent_path = os.pardir - - if not os.path.isdir(path_root_abs): - return jsonify(message="Path is not a directory %r" % path_root_abs) - - items_list = [] - - for f in os.listdir(path_root_abs): - f_rel = os.path.join(path, f) - f_abs = os.path.join(path_root_abs, f) - - if os.path.isdir(f_abs): - items_list.append((f, f_rel, "dir")) - else: - items_list.append((f, f_rel, "file")) - - project_files = { - "parent_path": parent_path, - "items_list": items_list, - } - - return jsonify(project_files) - # return {'message': 'Display files list'} - - -class FileAPI(Resource): - """Gives acces to a file. Currently requires 2 arguments: - - filepath: the path of the file (relative to the project root) - - the command (info, checkout) - - In the case of checkout we plan to support the following arguments: - --dependencies - --zip (eventually with a compression rate) - - Default behavior for file checkout is to retunr a zipfile with all dependencies. - """ - - decorators = [auth.login_required] - - def __init__(self): - parser = reqparse.RequestParser() - parser.add_argument('filepath', type=str, - help="Filepath cannot be blank!") - parser.add_argument('command', type=str, required=True, - help="Command cannot be blank!") - parser.add_argument('arguments', type=str) - parser.add_argument('files', type=werkzeug.datastructures.FileStorage, - location='files') - # args = parser.parse_args() - - super(FileAPI, self).__init__() - - def get(self, project_name): - filepath = request.args['filepath'] - command = request.args['command'] - command_args = request.args.get('arguments') - if command_args is not None: - command_args = json.loads(command_args) - - project = Project.query.filter_by(name=project_name).first() - - if command == 'info': - r = svn.local.LocalClient(project.repository_path) - - svn_log = r.log_default(None, None, 5, filepath) - svn_log = [l for l in svn_log] - - return jsonify( - filepath=filepath, - log=svn_log) - - elif command == 'checkout': - filepath = os.path.join(project.repository_path, filepath) - - if not os.path.exists(filepath): - return jsonify(message="Path not found %r" % filepath) - elif os.path.isdir(filepath): - return jsonify(message="Path is a directory %r" % filepath) - - def response_message_iter(): - ID_MESSAGE = 1 - ID_PAYLOAD = 2 - import struct - - def report(txt): - txt_bytes = txt.encode('utf-8') - return struct.pack('/file_list', endpoint='file_list') api.add_resource(FileAPI, '//file', endpoint='file') diff --git a/webservice/bam/application/modules/resources/__init__.py b/webservice/bam/application/modules/resources/__init__.py new file mode 100644 index 0000000..e43a853 --- /dev/null +++ b/webservice/bam/application/modules/resources/__init__.py @@ -0,0 +1,373 @@ +import os +import json +import svn.local +import werkzeug +import xml.etree.ElementTree +import logging + +from flask import Flask +from flask import jsonify +from flask import abort +from flask import request +from flask import make_response +from flask import url_for +from flask import Response + +from flask.ext.restful import Api +from flask.ext.restful import Resource +from flask.ext.restful import reqparse +from flask.ext.restful import fields +from flask.ext.restful import marshal + +from application import auth +from application import app +from application import log + +from application.modules.admin import backend +from application.modules.admin import settings +from application.modules.projects import admin +from application.modules.projects.model import Project +from application.modules.projects.model import ProjectSetting + + + +class DirectoryAPI(Resource): + """Displays list of files.""" + + decorators = [auth.login_required] + + def __init__(self): + parser = reqparse.RequestParser() + # parser.add_argument('rate', type=int, help='Rate cannot be converted') + parser.add_argument('path', type=str) + # args = parser.parse_args() + super(DirectoryAPI, self).__init__() + + def get(self, project_name): + + project = Project.query.filter_by(name=project_name).first() + + path = request.args['path'] + if not path: + path = '' + + path_root_abs = project.repository_path + parent_path = '' + + if path != '': + path_root_abs = os.path.join(path_root_abs, path) + parent_path = os.pardir + + if not os.path.isdir(path_root_abs): + return jsonify(message="Path is not a directory %r" % path_root_abs) + + items_list = [] + + for f in os.listdir(path_root_abs): + f_rel = os.path.join(path, f) + f_abs = os.path.join(path_root_abs, f) + + if os.path.isdir(f_abs): + items_list.append((f, f_rel, "dir")) + else: + items_list.append((f, f_rel, "file")) + + project_files = { + "parent_path": parent_path, + "items_list": items_list, + } + + return jsonify(project_files) + # return {'message': 'Display files list'} + + +class FileAPI(Resource): + """Gives acces to a file. Currently requires 2 arguments: + - filepath: the path of the file (relative to the project root) + - the command (info, checkout) + + In the case of checkout we plan to support the following arguments: + --dependencies + --zip (eventually with a compression rate) + + Default behavior for file checkout is to retunr a zipfile with all dependencies. + """ + + decorators = [auth.login_required] + + def __init__(self): + parser = reqparse.RequestParser() + parser.add_argument('filepath', type=str, + help="Filepath cannot be blank!") + parser.add_argument('command', type=str, required=True, + help="Command cannot be blank!") + parser.add_argument('arguments', type=str) + parser.add_argument('files', type=werkzeug.datastructures.FileStorage, + location='files') + # args = parser.parse_args() + + super(FileAPI, self).__init__() + + def get(self, project_name): + filepath = request.args['filepath'] + command = request.args['command'] + command_args = request.args.get('arguments') + if command_args is not None: + command_args = json.loads(command_args) + + project = Project.query.filter_by(name=project_name).first() + + if command == 'info': + r = svn.local.LocalClient(project.repository_path) + + svn_log = r.log_default(None, None, 5, filepath) + svn_log = [l for l in svn_log] + + return jsonify( + filepath=filepath, + log=svn_log) + + elif command == 'checkout': + filepath = os.path.join(project.repository_path, filepath) + + if not os.path.exists(filepath): + return jsonify(message="Path not found %r" % filepath) + elif os.path.isdir(filepath): + return jsonify(message="Path is a directory %r" % filepath) + + def response_message_iter(): + ID_MESSAGE = 1 + ID_PAYLOAD = 2 + import struct + + def report(txt): + txt_bytes = txt.encode('utf-8') + return struct.pack('