This repository has been archived on 2023-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-asset-manager/webservice/bam/application/__init__.py

277 lines
9.1 KiB
Python
Raw Normal View History

2014-10-29 19:11:29 +01:00
#!/usr/bin/env python3
2014-10-16 16:10:25 +02:00
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# ------------------
# Ensure module path
import os
import sys
2014-11-05 14:48:10 +01:00
path = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "..", "modules"))
if path not in sys.path:
sys.path.append(path)
del os, sys, path
# --------
2014-10-23 14:45:21 +02:00
import os
2014-10-30 19:23:57 +01:00
import json
2014-10-29 19:11:29 +01:00
import svn.local
2014-10-30 16:47:03 +01:00
import werkzeug
2014-10-23 14:45:21 +02:00
2014-10-23 19:51:52 +02:00
from flask import Flask, jsonify, abort, request, make_response, url_for, Response
2014-10-16 16:10:25 +02:00
from flask.views import MethodView
from flask.ext.restful import Api, Resource, reqparse, fields, marshal
from flask.ext.httpauth import HTTPBasicAuth
app = Flask(__name__)
api = Api(app)
auth = HTTPBasicAuth()
2014-10-23 14:45:21 +02:00
import config
app.config.from_object(config.Development)
2014-10-17 09:33:16 +02:00
2014-11-05 10:42:59 +01:00
2014-10-16 16:10:25 +02:00
@auth.get_password
def get_password(username):
if username == 'bam':
return 'bam'
return None
2014-10-17 09:33:16 +02:00
2014-10-16 16:10:25 +02:00
@auth.error_handler
def unauthorized():
2014-10-17 09:33:16 +02:00
return make_response(jsonify({'message': 'Unauthorized access'}), 403)
# return 403 instead of 401 to prevent browsers from displaying
2014-10-16 16:10:25 +02:00
# the default auth dialog
class FilesListAPI(Resource):
"""Displays list of files."""
2014-10-23 19:51:52 +02:00
2014-10-16 16:10:25 +02:00
decorators = [auth.login_required]
def __init__(self):
2014-10-23 14:45:21 +02:00
parser = reqparse.RequestParser()
#parser.add_argument('rate', type=int, help='Rate cannot be converted')
parser.add_argument('path', type=str)
args = parser.parse_args()
2014-10-16 16:10:25 +02:00
super(FilesListAPI, self).__init__()
2014-10-17 09:33:16 +02:00
2014-10-16 16:10:25 +02:00
def get(self):
2014-10-23 14:45:21 +02:00
path = request.args['path']
if not path:
path = ''
absolute_path_root = app.config['STORAGE_PATH']
parent_path = ''
if path != '':
absolute_path_root = os.path.join(absolute_path_root, path)
parent_path = os.pardir
items_list = []
for f in os.listdir(absolute_path_root):
relative_path = os.path.join(path, f)
absolute_path = os.path.join(absolute_path_root, f)
2014-10-23 19:51:52 +02:00
if os.path.isdir(absolute_path):
2014-10-30 22:50:30 +01:00
items_list.append((f, relative_path, "dir"))
2014-10-23 19:51:52 +02:00
else:
2014-10-30 22:50:30 +01:00
items_list.append((f, relative_path, "file"))
2014-10-23 14:45:21 +02:00
project_files = dict(
parent_path=parent_path,
items_list=items_list)
return jsonify(project_files)
#return {'message': 'Display files list'}
2014-10-16 16:10:25 +02:00
class FileAPI(Resource):
2014-10-30 14:53:34 +01:00
"""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.
"""
2014-10-16 16:10:25 +02:00
decorators = [auth.login_required]
2014-10-17 09:33:16 +02:00
2014-10-16 16:10:25 +02:00
def __init__(self):
2014-10-23 14:45:21 +02:00
parser = reqparse.RequestParser()
2014-10-30 16:47:03 +01:00
parser.add_argument('filepath', type=str,
2014-10-23 19:51:52 +02:00
help="Filepath cannot be blank!")
2014-10-29 19:11:29 +01:00
parser.add_argument('command', type=str, required=True,
help="Command cannot be blank!")
2014-10-30 19:23:57 +01:00
parser.add_argument('arguments', type=str)
parser.add_argument('files', type=werkzeug.datastructures.FileStorage,
2014-10-30 16:47:03 +01:00
location='files')
2014-10-23 14:45:21 +02:00
args = parser.parse_args()
2014-10-23 19:51:52 +02:00
2014-10-16 16:10:25 +02:00
super(FileAPI, self).__init__()
2014-10-23 14:45:21 +02:00
def get(self):
2014-10-29 19:11:29 +01:00
filepath = request.args['filepath']
command = request.args['command']
if command == 'info':
r = svn.local.LocalClient(app.config['STORAGE_PATH'])
log = r.log_default(None, None, 5, filepath)
log = [l for l in log]
2014-10-30 14:37:05 +01:00
2014-10-29 19:11:29 +01:00
return jsonify(
filepath=filepath,
log=log)
elif command == 'checkout':
2014-10-30 14:53:34 +01:00
filepath = os.path.join(app.config['STORAGE_PATH'], filepath)
2014-10-23 23:29:44 +02:00
2014-10-30 22:38:05 +01:00
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)
2014-11-04 21:46:18 +01:00
def response_message_iter():
ID_MESSAGE = 1
ID_PAYLOAD = 2
import struct
def report(txt):
txt_bytes = txt.encode('utf-8')
return struct.pack('<II', ID_MESSAGE, len(txt_bytes)) + txt_bytes
yield b'BAM\0'
# pack the file!
import tempfile
filepath_zip = tempfile.mkstemp(suffix=".zip")
yield from self.pack_fn(filepath, filepath_zip, report)
# TODO, handle fail
if not os.path.exists(filepath_zip[-1]):
yield report("%s: %r\n" % (colorize("failed to extract", color='red'), filepath))
return
with open(filepath_zip[-1], 'rb') as f:
f.seek(0, os.SEEK_END)
f_size = f.tell()
f.seek(0, os.SEEK_SET)
yield struct.pack('<II', ID_PAYLOAD, f_size)
while True:
data = f.read(1024)
if not data:
break
yield data
# return Response(f, direct_passthrough=True)
return Response(response_message_iter(), direct_passthrough=True)
2014-10-23 23:29:44 +02:00
2014-10-30 14:53:34 +01:00
else:
2014-10-30 22:38:05 +01:00
return jsonify(message="Command unknown")
2014-10-30 19:23:57 +01:00
2014-10-30 16:47:03 +01:00
def put(self):
command = request.args['command']
2014-10-30 19:23:57 +01:00
arguments = ''
if 'arguments' in request.args:
arguments = json.loads(request.args['arguments'])
2014-10-30 16:47:03 +01:00
file = request.files['file']
if file and self.allowed_file(file.filename):
2014-10-30 19:23:57 +01:00
local_client = svn.local.LocalClient(app.config['STORAGE_PATH'])
# TODO, add the merge operation to a queue. Later on, the request could stop here
# and all the next steps could be done in another loop, or triggered again via
2014-10-30 19:23:57 +01:00
# another request
2014-10-30 16:47:03 +01:00
filename = werkzeug.secure_filename(file.filename)
2014-11-04 15:15:04 +01:00
tmp_filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(tmp_filepath)
2014-10-30 19:23:57 +01:00
# TODO, once all files are uploaded, unpack and run the tasklist (copy, add, remove
# files on a filesystem level and subsequently as svn commands)
2014-11-04 15:15:04 +01:00
import zipfile
extract_tmp_dir = os.path.splitext(tmp_filepath)[0]
2014-11-05 11:24:57 +01:00
with open(tmp_filepath, 'rb') as zip_file:
zip_handle = zipfile.ZipFile(zip_file)
zip_handle.extractall(extract_tmp_dir)
2014-11-05 11:24:57 +01:00
del zip_file, zip_handle
del zipfile
with open(os.path.join(extract_tmp_dir, '.bam_paths_remap.json'), 'r') as path_remap:
path_remap = json.load(path_remap)
import shutil
2014-11-05 15:47:16 +01:00
for src_file_path, dst_file_path in path_remap.items():
shutil.move(os.path.join(extract_tmp_dir, src_file_path), dst_file_path)
2014-10-30 19:23:57 +01:00
# TODO, dry run commit (using committ message)
# Seems not easily possible with SVN
result = local_client.run_command('status',
[local_client.info()['entry_path'], '--xml'],
2014-10-30 19:23:57 +01:00
combine=True)
# Commit command
result = local_client.run_command('commit',
[local_client.info()['entry_path'], '--message', arguments['message']],
combine=True)
2014-10-30 19:23:57 +01:00
print(result)
2014-10-30 16:47:03 +01:00
2014-10-30 19:23:57 +01:00
return jsonify(message=result)
2014-10-30 16:47:03 +01:00
else:
return jsonify(message='File not allowed')
2014-10-23 23:29:44 +02:00
@staticmethod
2014-11-04 21:46:18 +01:00
def pack_fn(filepath, filepath_zip, report):
2014-10-23 23:29:44 +02:00
import os
2014-10-30 22:38:05 +01:00
assert(os.path.exists(filepath) and not os.path.isdir(filepath))
2014-11-05 14:48:10 +01:00
import blendfile_pack
2014-10-23 23:29:44 +02:00
2014-10-30 15:07:02 +01:00
print(" Source path:", filepath)
print(" Zip path:", filepath_zip)
2014-10-23 23:29:44 +02:00
try:
2014-11-05 14:48:10 +01:00
yield from blendfile_pack.pack(
2014-11-04 21:46:18 +01:00
filepath.encode('utf-8'), filepath_zip[-1].encode('utf-8'), mode='ZIP',
# TODO(cam) this just means the json is written in the zip
deps_remap={}, paths_remap={}, paths_uuid={},
report=report)
2014-10-23 23:29:44 +02:00
return filepath_zip[-1]
except:
import traceback
traceback.print_exc()
return None
2014-10-30 16:47:03 +01:00
@staticmethod
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
2014-10-23 23:29:44 +02:00
2014-10-23 22:28:45 +02:00
api.add_resource(FilesListAPI, '/file_list', endpoint='file_list')
2014-10-23 14:45:21 +02:00
api.add_resource(FileAPI, '/file', endpoint='file')
2014-11-04 21:46:18 +01:00