de-duplicate code, add generic modules dir
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# ***** BEGIN GPL LICENSE BLOCK *****
|
# ***** BEGIN GPL LICENSE BLOCK *****
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or
|
# This program is free software; you can redistribute it and/or
|
||||||
@@ -21,13 +22,19 @@
|
|||||||
Blender asset manager
|
Blender asset manager
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO
|
|
||||||
# * checkout command (store some handy json info!)
|
if __name__ != "__main__":
|
||||||
# * commit command
|
raise Exception("must be imported directly")
|
||||||
# ** staging for svn commit
|
|
||||||
# ** svn commit
|
# ------------------
|
||||||
# ** handle errors
|
# Ensure module path
|
||||||
# * definition of project (.bam (like .git)) ... json
|
import os
|
||||||
|
import sys
|
||||||
|
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
|
||||||
|
# --------
|
||||||
|
|
||||||
|
|
||||||
class bam_config:
|
class bam_config:
|
||||||
@@ -78,7 +85,7 @@ class bam_config:
|
|||||||
bam_config.find_basedir(cwd=cwd)
|
bam_config.find_basedir(cwd=cwd)
|
||||||
filepath = os.path.join(basedir, id_)
|
filepath = os.path.join(basedir, id_)
|
||||||
|
|
||||||
with open(filepath) as f:
|
with open(filepath, 'w') as f:
|
||||||
import json
|
import json
|
||||||
json.dump(
|
json.dump(
|
||||||
data, f, ensure_ascii=False,
|
data, f, ensure_ascii=False,
|
||||||
@@ -94,21 +101,6 @@ class bam_utils:
|
|||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
raise RuntimeError("%s should not be instantiated" % cls)
|
raise RuntimeError("%s should not be instantiated" % cls)
|
||||||
|
|
||||||
# Copied from 'packer.py', TODO(cam) de-duplicate
|
|
||||||
@staticmethod
|
|
||||||
def sha1_for_file(fn, block_size=1 << 20):
|
|
||||||
with open(fn, 'rb') as f:
|
|
||||||
import hashlib
|
|
||||||
sha1 = hashlib.new('sha1')
|
|
||||||
while True:
|
|
||||||
data = f.read(block_size)
|
|
||||||
if not data:
|
|
||||||
break
|
|
||||||
sha1.update(data)
|
|
||||||
return sha1.hexdigest()
|
|
||||||
# end code duplication
|
|
||||||
# --------------------
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def session_find_url():
|
def session_find_url():
|
||||||
return "http://localhost:5000"
|
return "http://localhost:5000"
|
0
modules/bam_utils/__init__.py
Normal file
0
modules/bam_utils/__init__.py
Normal file
58
modules/bam_utils/system.py
Normal file
58
modules/bam_utils/system.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
# ***** 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 *****
|
||||||
|
|
||||||
|
_USE_COLOR = True
|
||||||
|
if _USE_COLOR:
|
||||||
|
color_codes = {
|
||||||
|
'black': '\033[0;30m',
|
||||||
|
'bright_gray': '\033[0;37m',
|
||||||
|
'blue': '\033[0;34m',
|
||||||
|
'white': '\033[1;37m',
|
||||||
|
'green': '\033[0;32m',
|
||||||
|
'bright_blue': '\033[1;34m',
|
||||||
|
'cyan': '\033[0;36m',
|
||||||
|
'bright_green': '\033[1;32m',
|
||||||
|
'red': '\033[0;31m',
|
||||||
|
'bright_cyan': '\033[1;36m',
|
||||||
|
'purple': '\033[0;35m',
|
||||||
|
'bright_red': '\033[1;31m',
|
||||||
|
'yellow': '\033[0;33m',
|
||||||
|
'bright_purple':'\033[1;35m',
|
||||||
|
'dark_gray': '\033[1;30m',
|
||||||
|
'bright_yellow':'\033[1;33m',
|
||||||
|
'normal': '\033[0m',
|
||||||
|
}
|
||||||
|
|
||||||
|
def colorize(msg, color=None):
|
||||||
|
return (color_codes[color] + msg + color_codes['normal'])
|
||||||
|
else:
|
||||||
|
def colorize(msg, color=None):
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
|
def sha1_from_file(fn, block_size=1 << 20):
|
||||||
|
with open(fn, 'rb') as f:
|
||||||
|
import hashlib
|
||||||
|
sha1 = hashlib.new('sha1')
|
||||||
|
while True:
|
||||||
|
data = f.read(block_size)
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
sha1.update(data)
|
||||||
|
return sha1.hexdigest()
|
||||||
|
|
@@ -22,6 +22,16 @@ import blendfile_path_walker
|
|||||||
|
|
||||||
TIMEIT = True
|
TIMEIT = True
|
||||||
|
|
||||||
|
# ------------------
|
||||||
|
# Ensure module path
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
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
|
||||||
|
# --------
|
||||||
|
|
||||||
|
|
||||||
def pack(blendfile_src, blendfile_dst, mode='FILE',
|
def pack(blendfile_src, blendfile_dst, mode='FILE',
|
||||||
deps_remap=None, paths_remap=None, paths_uuid=None,
|
deps_remap=None, paths_remap=None, paths_uuid=None,
|
||||||
@@ -47,6 +57,8 @@ def pack(blendfile_src, blendfile_dst, mode='FILE',
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
from bam_utils.system import colorize
|
||||||
|
|
||||||
path_temp_files = set()
|
path_temp_files = set()
|
||||||
path_copy_files = set()
|
path_copy_files = set()
|
||||||
|
|
||||||
@@ -162,24 +174,15 @@ def pack(blendfile_src, blendfile_dst, mode='FILE',
|
|||||||
paths_remap[os.path.basename(blendfile_src).decode('utf-8')] = blendfile_src.decode('utf-8')
|
paths_remap[os.path.basename(blendfile_src).decode('utf-8')] = blendfile_src.decode('utf-8')
|
||||||
|
|
||||||
if paths_uuid is not None:
|
if paths_uuid is not None:
|
||||||
# TODO, multi-process SHA1 calculation (or better cache)
|
from bam_utils.system import sha1_from_file
|
||||||
def sha1_for_file(fn, block_size=1 << 20):
|
|
||||||
with open(fn, 'rb') as f:
|
|
||||||
import hashlib
|
|
||||||
sha1 = hashlib.new('sha1')
|
|
||||||
while True:
|
|
||||||
data = f.read(block_size)
|
|
||||||
if not data:
|
|
||||||
break
|
|
||||||
sha1.update(data)
|
|
||||||
return sha1.hexdigest()
|
|
||||||
|
|
||||||
for src, dst in path_copy_files:
|
for src, dst in path_copy_files:
|
||||||
paths_uuid[os.path.relpath(dst, base_dir_dst).decode('utf-8')] = sha1_for_file(src)
|
paths_uuid[os.path.relpath(dst, base_dir_dst).decode('utf-8')] = sha1_from_file(src)
|
||||||
# XXX, better way to store temp target
|
# XXX, better way to store temp target
|
||||||
blendfile_dst_tmp = temp_remap_cb(blendfile_src, 0)
|
blendfile_dst_tmp = temp_remap_cb(blendfile_src, 0)
|
||||||
paths_uuid[os.path.basename(blendfile_src).decode('utf-8')] = sha1_for_file(blendfile_dst_tmp)
|
paths_uuid[os.path.basename(blendfile_src).decode('utf-8')] = sha1_from_file(blendfile_dst_tmp)
|
||||||
del blendfile_dst_tmp
|
del blendfile_dst_tmp
|
||||||
|
del sha1_from_file
|
||||||
|
|
||||||
|
|
||||||
# --------------------
|
# --------------------
|
||||||
@@ -325,33 +328,3 @@ def main():
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
# TODO(cam) de-duplicate
|
|
||||||
USE_COLOR = True
|
|
||||||
if USE_COLOR:
|
|
||||||
color_codes = {
|
|
||||||
'black': '\033[0;30m',
|
|
||||||
'bright_gray': '\033[0;37m',
|
|
||||||
'blue': '\033[0;34m',
|
|
||||||
'white': '\033[1;37m',
|
|
||||||
'green': '\033[0;32m',
|
|
||||||
'bright_blue': '\033[1;34m',
|
|
||||||
'cyan': '\033[0;36m',
|
|
||||||
'bright_green': '\033[1;32m',
|
|
||||||
'red': '\033[0;31m',
|
|
||||||
'bright_cyan': '\033[1;36m',
|
|
||||||
'purple': '\033[0;35m',
|
|
||||||
'bright_red': '\033[1;31m',
|
|
||||||
'yellow': '\033[0;33m',
|
|
||||||
'bright_purple':'\033[1;35m',
|
|
||||||
'dark_gray': '\033[1;30m',
|
|
||||||
'bright_yellow':'\033[1;33m',
|
|
||||||
'normal': '\033[0m',
|
|
||||||
}
|
|
||||||
|
|
||||||
def colorize(msg, color=None):
|
|
||||||
return (color_codes[color] + msg + color_codes['normal'])
|
|
||||||
else:
|
|
||||||
def colorize(msg, color=None):
|
|
||||||
return msg
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -17,10 +17,20 @@
|
|||||||
#
|
#
|
||||||
# ***** END GPL LICENCE BLOCK *****
|
# ***** END GPL LICENCE BLOCK *****
|
||||||
|
|
||||||
|
# ------------------
|
||||||
|
# Ensure module path
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
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
|
||||||
|
# --------
|
||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import svn.local
|
import svn.local
|
||||||
import pprint
|
|
||||||
import werkzeug
|
import werkzeug
|
||||||
|
|
||||||
from flask import Flask, jsonify, abort, request, make_response, url_for, Response
|
from flask import Flask, jsonify, abort, request, make_response, url_for, Response
|
||||||
@@ -121,7 +131,7 @@ class FileAPI(Resource):
|
|||||||
parser.add_argument('command', type=str, required=True,
|
parser.add_argument('command', type=str, required=True,
|
||||||
help="Command cannot be blank!")
|
help="Command cannot be blank!")
|
||||||
parser.add_argument('arguments', type=str)
|
parser.add_argument('arguments', type=str)
|
||||||
parser.add_argument('files', type=werkzeug.datastructures.FileStorage,
|
parser.add_argument('files', type=werkzeug.datastructures.FileStorage,
|
||||||
location='files')
|
location='files')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -199,7 +209,7 @@ class FileAPI(Resource):
|
|||||||
if file and self.allowed_file(file.filename):
|
if file and self.allowed_file(file.filename):
|
||||||
local_client = svn.local.LocalClient(app.config['STORAGE_PATH'])
|
local_client = svn.local.LocalClient(app.config['STORAGE_PATH'])
|
||||||
# TODO, add the merge operation to a queue. Later on, the request could stop here
|
# 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
|
# and all the next steps could be done in another loop, or triggered again via
|
||||||
# another request
|
# another request
|
||||||
filename = werkzeug.secure_filename(file.filename)
|
filename = werkzeug.secure_filename(file.filename)
|
||||||
tmp_filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
tmp_filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
||||||
@@ -223,12 +233,12 @@ class FileAPI(Resource):
|
|||||||
|
|
||||||
# TODO, dry run commit (using committ message)
|
# TODO, dry run commit (using committ message)
|
||||||
# Seems not easily possible with SVN
|
# Seems not easily possible with SVN
|
||||||
result = local_client.run_command('status',
|
result = local_client.run_command('status',
|
||||||
[local_client.info()['entry_path'], '--xml'],
|
[local_client.info()['entry_path'], '--xml'],
|
||||||
combine=True)
|
combine=True)
|
||||||
|
|
||||||
# Commit command
|
# Commit command
|
||||||
result = local_client.run_command('commit',
|
result = local_client.run_command('commit',
|
||||||
[local_client.info()['entry_path'], '--message', arguments['message']],
|
[local_client.info()['entry_path'], '--message', arguments['message']],
|
||||||
combine=True)
|
combine=True)
|
||||||
|
|
||||||
@@ -285,31 +295,3 @@ class FileAPI(Resource):
|
|||||||
api.add_resource(FilesListAPI, '/file_list', endpoint='file_list')
|
api.add_resource(FilesListAPI, '/file_list', endpoint='file_list')
|
||||||
api.add_resource(FileAPI, '/file', endpoint='file')
|
api.add_resource(FileAPI, '/file', endpoint='file')
|
||||||
|
|
||||||
USE_COLOR = True
|
|
||||||
if USE_COLOR:
|
|
||||||
color_codes = {
|
|
||||||
'black': '\033[0;30m',
|
|
||||||
'bright_gray': '\033[0;37m',
|
|
||||||
'blue': '\033[0;34m',
|
|
||||||
'white': '\033[1;37m',
|
|
||||||
'green': '\033[0;32m',
|
|
||||||
'bright_blue': '\033[1;34m',
|
|
||||||
'cyan': '\033[0;36m',
|
|
||||||
'bright_green': '\033[1;32m',
|
|
||||||
'red': '\033[0;31m',
|
|
||||||
'bright_cyan': '\033[1;36m',
|
|
||||||
'purple': '\033[0;35m',
|
|
||||||
'bright_red': '\033[1;31m',
|
|
||||||
'yellow': '\033[0;33m',
|
|
||||||
'bright_purple':'\033[1;35m',
|
|
||||||
'dark_gray': '\033[1;30m',
|
|
||||||
'bright_yellow':'\033[1;33m',
|
|
||||||
'normal': '\033[0m',
|
|
||||||
}
|
|
||||||
|
|
||||||
def colorize(msg, color=None):
|
|
||||||
return (color_codes[color] + msg + color_codes['normal'])
|
|
||||||
else:
|
|
||||||
def colorize(msg, color=None):
|
|
||||||
return msg
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user