path listing working
This commit is contained in:
@@ -17,8 +17,12 @@
|
|||||||
#
|
#
|
||||||
# ***** END GPL LICENCE BLOCK *****
|
# ***** END GPL LICENCE BLOCK *****
|
||||||
|
|
||||||
|
"""
|
||||||
|
Blender asset manager
|
||||||
|
"""
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
# * checkout command
|
# * checkout command (store some handy json info!)
|
||||||
# * commit command
|
# * commit command
|
||||||
# ** staging for svn commit
|
# ** staging for svn commit
|
||||||
# ** svn commit
|
# ** svn commit
|
||||||
@@ -26,6 +30,7 @@
|
|||||||
# * definition of project (.bam (like .git)) ... json
|
# * definition of project (.bam (like .git)) ... json
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class bam_utils:
|
class bam_utils:
|
||||||
# fake module
|
# fake module
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
@@ -36,12 +41,82 @@ class bam_utils:
|
|||||||
def session_find_url():
|
def session_find_url():
|
||||||
return "http://localhost:5000"
|
return "http://localhost:5000"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def session_request_url(req_path):
|
||||||
|
# TODO, get from config
|
||||||
|
BAM_SERVER = "http://localhost:5000"
|
||||||
|
result = "%s/%s" % (BAM_SERVER, req_path)
|
||||||
|
print(result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def checkout(paths):
|
||||||
|
import sys
|
||||||
|
import requests
|
||||||
|
|
||||||
|
# TODO(cam) multiple paths
|
||||||
|
path = paths[0]
|
||||||
|
del paths
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
"filepath": path,
|
||||||
|
"command": "checkout",
|
||||||
|
}
|
||||||
|
r = requests.get(
|
||||||
|
bam_utils.session_request_url("file"),
|
||||||
|
params=payload,
|
||||||
|
auth=("bam", "bam"),
|
||||||
|
stream=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
if r.status_code not in {200,}:
|
||||||
|
# TODO(cam), make into reusable function?
|
||||||
|
print("Error %d:\n%s" % (r.status_code, next(r.iter_content(chunk_size=1024)).decode('utf-8')))
|
||||||
|
return
|
||||||
|
|
||||||
|
# TODO(cam) how to tell if we get back a message payload? or real data???
|
||||||
|
local_filename = payload['filepath'].split('/')[-1]
|
||||||
|
|
||||||
|
if 1:
|
||||||
|
local_filename += ".zip"
|
||||||
|
|
||||||
|
with open(local_filename, 'wb') as f:
|
||||||
|
for chunk in r.iter_content(chunk_size=1024):
|
||||||
|
if chunk: # filter out keep-alive new chunks
|
||||||
|
f.write(chunk)
|
||||||
|
f.flush()
|
||||||
|
|
||||||
|
sys.stdout.write(".")
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
print("Written:", local_filename)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def list_dir(paths):
|
||||||
|
import sys
|
||||||
|
import requests
|
||||||
|
|
||||||
|
path = paths[0]
|
||||||
|
del paths
|
||||||
|
|
||||||
|
# TODO(cam) multiple paths
|
||||||
|
payload = {
|
||||||
|
"path": path,
|
||||||
|
}
|
||||||
|
r = requests.get(
|
||||||
|
bam_utils.session_request_url("file_list"),
|
||||||
|
params=payload,
|
||||||
|
auth=("bam", "bam"),
|
||||||
|
stream=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
for chunk in r.iter_content(chunk_size=1024):
|
||||||
|
if chunk: # filter out keep-alive new chunks
|
||||||
|
sys.stdout.write(chunk.decode('utf-8'))
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
def subcommand_checkout_cb(args):
|
def subcommand_checkout_cb(args):
|
||||||
print(args)
|
bam_utils.checkout(args.paths)
|
||||||
|
|
||||||
|
|
||||||
def subcommand_commit_cb(args):
|
def subcommand_commit_cb(args):
|
||||||
print(args)
|
print(args)
|
||||||
@@ -56,7 +131,7 @@ def subcommand_revert_cb(args):
|
|||||||
|
|
||||||
|
|
||||||
def subcommand_list_cb(args):
|
def subcommand_list_cb(args):
|
||||||
print(args)
|
bam_utils.list_dir(args.paths)
|
||||||
|
|
||||||
|
|
||||||
def subcommand_status_cb(args):
|
def subcommand_status_cb(args):
|
||||||
@@ -74,9 +149,14 @@ def create_argparse_checkout(subparsers):
|
|||||||
|
|
||||||
def create_argparse_commit(subparsers):
|
def create_argparse_commit(subparsers):
|
||||||
subparse = subparsers.add_parser("commit", aliases=("ci",))
|
subparse = subparsers.add_parser("commit", aliases=("ci",))
|
||||||
|
subparse.add_argument(
|
||||||
|
"-m", "--message", dest="message", metavar='MESSAGE',
|
||||||
|
help="Commit message",
|
||||||
|
)
|
||||||
subparse.add_argument(
|
subparse.add_argument(
|
||||||
"paths", nargs="*", help="paths to commit",
|
"paths", nargs="*", help="paths to commit",
|
||||||
)
|
)
|
||||||
|
|
||||||
subparse.set_defaults(func=subcommand_commit_cb)
|
subparse.set_defaults(func=subcommand_commit_cb)
|
||||||
|
|
||||||
|
|
||||||
@@ -120,8 +200,8 @@ def create_argparse():
|
|||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
usage_text = (
|
usage_text = (
|
||||||
"BAM! (Blender Asset Manager) " +
|
"BAM! (Blender Asset Manager)\n" +
|
||||||
os.path.basename(__file__)
|
__doc__
|
||||||
)
|
)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description=usage_text)
|
parser = argparse.ArgumentParser(description=usage_text)
|
||||||
|
@@ -144,19 +144,25 @@ class FileAPI(Resource):
|
|||||||
elif command == 'checkout':
|
elif command == 'checkout':
|
||||||
filepath = os.path.join(app.config['STORAGE_PATH'], filepath)
|
filepath = os.path.join(app.config['STORAGE_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)
|
||||||
|
|
||||||
# pack the file!
|
# pack the file!
|
||||||
print("PACKING")
|
print("PACKING")
|
||||||
filepath_zip = self.pack_fn(filepath)
|
filepath_zip = self.pack_fn(filepath)
|
||||||
|
|
||||||
# TODO, handle fail
|
# TODO, handle fail
|
||||||
if filepath_zip is None:
|
if filepath_zip is None:
|
||||||
pass
|
return jsonify(message="Path not found %r" % filepath)
|
||||||
|
|
||||||
f = open(filepath_zip, 'rb')
|
f = open(filepath_zip, 'rb')
|
||||||
return Response(f, direct_passthrough=True)
|
return Response(f, direct_passthrough=True)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return jsonify(message='Command unknown')
|
return jsonify(message="Command unknown")
|
||||||
|
|
||||||
def put(self):
|
def put(self):
|
||||||
command = request.args['command']
|
command = request.args['command']
|
||||||
@@ -196,10 +202,9 @@ class FileAPI(Resource):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pack_fn(filepath):
|
def pack_fn(filepath):
|
||||||
import tempfile
|
|
||||||
filepath_zip = tempfile.mkstemp(suffix=".zip")
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
assert(os.path.exists(filepath) and not os.path.isdir(filepath))
|
||||||
|
|
||||||
modpath = \
|
modpath = \
|
||||||
os.path.normpath(
|
os.path.normpath(
|
||||||
os.path.abspath(
|
os.path.abspath(
|
||||||
@@ -215,8 +220,10 @@ class FileAPI(Resource):
|
|||||||
sys.path.append(modpath)
|
sys.path.append(modpath)
|
||||||
del modpath
|
del modpath
|
||||||
|
|
||||||
|
import tempfile
|
||||||
import packer
|
import packer
|
||||||
|
|
||||||
|
filepath_zip = tempfile.mkstemp(suffix=".zip")
|
||||||
print(" Source path:", filepath)
|
print(" Source path:", filepath)
|
||||||
print(" Zip path:", filepath_zip)
|
print(" Zip path:", filepath_zip)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user