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

438 lines
15 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 *****
"""
Environment vars:
- BAM_VERBOSE, set to get debug logging.
"""
# ------------------
# 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-11-06 15:40:13 +01:00
import xml.etree.ElementTree
2014-11-20 12:29:29 +01:00
import logging
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
2014-11-05 18:52:18 +01:00
from flask.ext.sqlalchemy import SQLAlchemy
2014-10-16 16:10:25 +02:00
app = Flask(__name__)
api = Api(app)
auth = HTTPBasicAuth()
2014-11-20 15:21:11 +01:00
try:
import config
except ImportError:
config = None
if config is None:
app.config["ALLOWED_EXTENSIONS"] = {'txt', 'mp4', 'png', 'jpg', 'jpeg', 'gif', 'blend', 'zip'}
else:
app.config.from_object(config.Development)
2014-11-05 18:52:18 +01:00
db = SQLAlchemy(app)
from application.modules.admin import backend
from application.modules.admin import settings
from application.modules.projects import admin
2014-11-07 12:08:00 +01:00
from application.modules.projects.model import Project, ProjectSetting
2014-10-17 09:33:16 +02:00
2014-11-20 12:29:29 +01:00
log = logging.getLogger("webservice")
if os.environ.get("BAM_VERBOSE"):
logging.basicConfig(level=logging.DEBUG)
2014-11-20 12:29:29 +01:00
2014-11-05 10:42:59 +01:00
2014-10-16 16:10:25 +02:00
@auth.get_password
def get_password(username):
2014-11-07 15:19:09 +01:00
# Temporarily override API access
# TODO (fsiddi) check against users table
return ''
2014-10-16 16:10:25 +02:00
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
2014-11-05 16:49:31 +01:00
class DirectoryAPI(Resource):
2014-10-16 16:10:25 +02:00
"""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()
2014-11-23 23:19:10 +01:00
# parser.add_argument('rate', type=int, help='Rate cannot be converted')
2014-10-23 14:45:21 +02:00
parser.add_argument('path', type=str)
args = parser.parse_args()
2014-11-05 16:49:31 +01:00
super(DirectoryAPI, self).__init__()
2014-10-17 09:33:16 +02:00
2014-11-05 16:05:45 +01:00
def get(self, project_name):
2014-10-23 14:45:21 +02:00
2014-11-05 18:52:18 +01:00
project = Project.query.filter_by(name=project_name).first()
2014-10-23 14:45:21 +02:00
path = request.args['path']
if not path:
path = ''
2014-11-06 18:41:09 +01:00
path_root_abs = project.repository_path
2014-10-23 14:45:21 +02:00
parent_path = ''
if path != '':
2014-11-06 18:41:09 +01:00
path_root_abs = os.path.join(path_root_abs, path)
2014-10-23 14:45:21 +02:00
parent_path = os.pardir
2014-11-06 18:41:09 +01:00
if not os.path.isdir(path_root_abs):
return jsonify(message="Path is not a directory %r" % path_root_abs)
2014-10-23 14:45:21 +02:00
items_list = []
2014-11-06 18:41:09 +01:00
for f in os.listdir(path_root_abs):
f_rel = os.path.join(path, f)
f_abs = os.path.join(path_root_abs, f)
2014-10-23 14:45:21 +02:00
2014-11-06 18:41:09 +01:00
if os.path.isdir(f_abs):
items_list.append((f, f_rel, "dir"))
2014-10-23 19:51:52 +02:00
else:
2014-11-06 18:41:09 +01:00
items_list.append((f, f_rel, "file"))
2014-10-23 14:45:21 +02:00
2014-11-06 17:42:35 +01:00
project_files = {
"parent_path": parent_path,
"items_list": items_list,
}
2014-10-23 14:45:21 +02:00
return jsonify(project_files)
2014-11-21 20:58:44 +01:00
# 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-11-05 16:05:45 +01:00
def get(self, project_name):
2014-10-29 19:11:29 +01:00
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)
2014-10-29 19:11:29 +01:00
2014-11-05 18:52:18 +01:00
project = Project.query.filter_by(name=project_name).first()
2014-10-29 19:11:29 +01:00
if command == 'info':
2014-11-05 18:52:18 +01:00
r = svn.local.LocalClient(project.repository_path)
2014-10-29 19:11:29 +01:00
svn_log = r.log_default(None, None, 5, filepath)
svn_log = [l for l in svn_log]
2014-10-30 14:37:05 +01:00
2014-10-29 19:11:29 +01:00
return jsonify(
filepath=filepath,
log=svn_log)
2014-10-29 19:11:29 +01:00
elif command == 'checkout':
2014-11-05 18:52:18 +01:00
filepath = os.path.join(project.repository_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
2014-11-06 13:15:16 +01:00
# weak! (ignore original opened file)
2014-11-04 21:46:18 +01:00
filepath_zip = tempfile.mkstemp(suffix=".zip")
2014-11-06 13:15:16 +01:00
os.close(filepath_zip[0])
filepath_zip = filepath_zip[1]
yield from self.pack_fn(
filepath, filepath_zip,
project.repository_path,
command_args['all_deps'],
report,
)
2014-11-04 21:46:18 +01:00
# TODO, handle fail
2014-11-06 13:15:16 +01:00
if not os.path.exists(filepath_zip):
2014-11-04 21:46:18 +01:00
yield report("%s: %r\n" % (colorize("failed to extract", color='red'), filepath))
return
2014-11-06 13:15:16 +01:00
with open(filepath_zip, 'rb') as f:
2014-11-04 21:46:18 +01:00
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-11-05 16:05:45 +01:00
def put(self, project_name):
2014-11-05 18:52:18 +01:00
project = Project.query.filter_by(name=project_name).first()
2014-10-30 16:47:03 +01:00
command = request.args['command']
command_args = request.args.get('arguments')
if command_args is not None:
command_args = json.loads(command_args)
2014-10-30 16:47:03 +01:00
file = request.files['file']
2014-11-07 12:08:00 +01:00
# Get the value of the first (and only) result for the specified project setting
2014-11-21 20:58:44 +01:00
svn_password = next((setting.value
for setting in project.settings
if setting.name == 'svn_password'))
svn_default_user = next((setting.value
for setting in project.settings
if setting.name == 'svn_default_user'))
2014-11-07 12:08:00 +01:00
2014-11-07 15:19:09 +01:00
# We get the actual username from the http headers
svn_user = auth.username()
2014-11-07 12:08:00 +01:00
# If the setting does not exist, stop here and prevent any other operation
if not svn_password:
return make_response(jsonify(
{'message': 'SVN missing password settings'}), 500)
2014-10-30 16:47:03 +01:00
if file and self.allowed_file(file.filename):
2014-11-07 11:37:45 +01:00
os.makedirs(project.upload_path, exist_ok=True)
2014-11-05 18:52:18 +01:00
local_client = svn.local.LocalClient(project.repository_path)
2014-10-30 19:23:57 +01:00
# 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-05 18:52:18 +01:00
tmp_filepath = os.path.join(project.upload_path, filename)
2014-11-04 15:15:04 +01:00
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():
2014-11-06 16:47:16 +01:00
assert(os.path.exists(os.path.join(extract_tmp_dir, src_file_path)))
src_file_path_abs = os.path.join(extract_tmp_dir, src_file_path)
dst_file_path_abs = os.path.join(project.repository_path, dst_file_path)
os.makedirs(os.path.dirname(dst_file_path_abs), exist_ok=True)
shutil.move(src_file_path_abs, dst_file_path_abs)
2014-10-30 19:23:57 +01:00
2014-11-06 14:33:44 +01:00
# TODO, dry run commit (using commit message)
2014-11-06 15:40:13 +01:00
# Seems not easily possible with SVN, so we might just smartly use svn status
result = local_client.run_command('status',
[local_client.info()['entry_path'], '--xml'],
2014-10-30 19:23:57 +01:00
combine=True)
2014-11-06 15:40:13 +01:00
# We parse the svn status xml output
root = xml.etree.ElementTree.fromstring(result)
2014-11-06 15:40:13 +01:00
# Loop throught every entry reported by the svn status command
for e in root.iter('entry'):
file_path = e.attrib['path']
item_status = e.find('wc-status').attrib['item']
# We add each unversioned file to SVN
if item_status == 'unversioned':
result = local_client.run_command('add',
2014-11-21 20:58:44 +01:00
[file_path, ])
2014-11-06 15:40:13 +01:00
2014-11-20 12:29:29 +01:00
with open(os.path.join(extract_tmp_dir, '.bam_paths_ops.json'), 'r') as path_ops:
path_ops = json.load(path_ops)
log.debug(path_ops)
for file_path, operation in path_ops.items():
2014-11-20 21:08:13 +01:00
# TODO(fsiddi), collect all file paths and remove after
2014-11-20 12:29:29 +01:00
if operation == 'D':
2014-11-20 16:34:35 +00:00
file_path_abs = os.path.join(project.repository_path, file_path)
assert(os.path.exists(file_path_abs))
2014-11-20 12:29:29 +01:00
result = local_client.run_command('rm',
2014-11-21 20:58:44 +01:00
[file_path_abs, ])
2014-11-07 12:08:00 +01:00
2014-10-30 19:23:57 +01:00
# Commit command
result = local_client.run_command('commit',
2014-11-21 20:58:44 +01:00
[local_client.info()['entry_path'],
2014-12-02 15:16:16 +01:00
'--no-auth-cache',
'--message', command_args['message'],
2014-11-07 15:19:09 +01:00
'--username', svn_user,
2014-11-07 12:08:00 +01:00
'--password', svn_password],
combine=True)
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
def pack_fn(filepath, filepath_zip, paths_remap_relbase, all_deps, report):
2014-11-06 17:45:51 +01:00
"""
'paths_remap_relbase' is the project path,
we want all paths to be relative to this so we don't get server path included.
"""
2014-10-23 23:29:44 +02:00
import os
2014-11-05 14:48:10 +01:00
import blendfile_pack
2014-10-23 23:29:44 +02:00
2014-11-06 17:45:51 +01:00
assert(os.path.exists(filepath) and not os.path.isdir(filepath))
log.info(" Source path: %r" % filepath)
log.info(" Zip path: %r" % filepath_zip)
2014-10-23 23:29:44 +02:00
2014-11-06 13:15:16 +01:00
deps_remap = {}
paths_remap = {}
paths_uuid = {}
if filepath.endswith(".blend"):
# find the path relative to the project's root
blendfile_src_dir_fakeroot = os.path.dirname(os.path.relpath(filepath, paths_remap_relbase))
try:
yield from blendfile_pack.pack(
filepath.encode('utf-8'), filepath_zip.encode('utf-8'), mode='ZIP',
paths_remap_relbase=paths_remap_relbase.encode('utf-8'),
# TODO(cam) this just means the json is written in the zip
deps_remap=deps_remap, paths_remap=paths_remap, paths_uuid=paths_uuid,
all_deps=all_deps,
report=report,
blendfile_src_dir_fakeroot=blendfile_src_dir_fakeroot.encode('utf-8'),
)
except:
log.exception("Error packing the blend file")
return
else:
# non blend-file
2014-12-11 16:27:05 +01:00
from bam_utils.system import uuid_from_file
paths_uuid[os.path.basename(filepath)] = uuid_from_file(filepath)
del uuid_from_file
import zipfile
with zipfile.ZipFile(filepath_zip, 'w', zipfile.ZIP_DEFLATED) as zip_handle:
zip_handle.write(
filepath,
arcname=os.path.basename(filepath),
)
del zipfile
2014-11-06 13:15:16 +01:00
2014-11-20 21:08:13 +01:00
# simple case
paths_remap[os.path.basename(filepath)] = os.path.basename(filepath)
if os.path.isfile(filepath):
paths_remap["."] = os.path.relpath(os.path.dirname(filepath), paths_remap_relbase)
else:
# TODO(cam) directory support
paths_remap["."] = os.path.relpath(filepath, paths_remap_relbase)
2014-11-06 13:15:16 +01:00
# TODO, avoid reopening zipfile
# append json info to zip
import zipfile
with zipfile.ZipFile(filepath_zip, 'a', zipfile.ZIP_DEFLATED) as zip_handle:
import json
def write_dict_as_json(f, dct):
2014-11-06 13:15:16 +01:00
zip_handle.writestr(
f,
2014-11-06 13:15:16 +01:00
json.dumps(dct,
check_circular=False,
# optional (pretty)
sort_keys=True, indent=4, separators=(',', ': '),
).encode('utf-8'))
2014-11-06 14:33:44 +01:00
write_dict_as_json(".bam_deps_remap.json", deps_remap)
write_dict_as_json(".bam_paths_remap.json", paths_remap)
write_dict_as_json(".bam_paths_uuid.json", paths_uuid)
2014-11-06 13:15:16 +01:00
del write_dict_as_json
# done writing json!
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-11-05 16:49:31 +01:00
api.add_resource(DirectoryAPI, '/<project_name>/file_list', endpoint='file_list')
2014-11-05 16:05:45 +01:00
api.add_resource(FileAPI, '/<project_name>/file', endpoint='file')