Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
3pia
VISPA
VISPA web
Commits
327eb18c
Commit
327eb18c
authored
Feb 21, 2013
by
murban
Browse files
filesystem updates
parent
f257473c
Changes
4
Hide whitespace changes
Inline
Side-by-side
vispa/controller/__init__.py
View file @
327eb18c
# -*- coding: utf-8 -*-
# imports
from
vispa
import
models
,
vfs
from
vispa
import
models
,
filesystem
from
vispa.plugins.rpc
import
RpcProcess
import
cherrypy
...
...
@@ -115,12 +115,12 @@ class AbstractController(object):
# error case
raise
Exception
(
'TypeError: conversion with unknown type flag.'
)
def
_get_
v
fs_instance
(
self
,
username
,
workspace_id
=
None
):
def
_get_fs_instance
(
self
,
username
,
workspace_id
=
None
):
workspace_id
=
workspace_id
or
self
.
get
(
'workspace_id'
)
or
cherrypy
.
config
.
get
(
'rpc.default_workspace_id'
,
0
)
key
=
(
str
(
username
),
workspace_id
)
if
key
in
self
.
_
v
fs_instances
.
keys
():
instance
=
self
.
_
v
fs_instances
[
key
]
if
key
in
self
.
_fs_instances
.
keys
():
instance
=
self
.
_fs_instances
[
key
]
if
isinstance
(
instance
,
RpcProcess
):
if
instance
.
online
():
return
instance
...
...
@@ -129,11 +129,11 @@ class AbstractController(object):
if
workspace_id
:
workspace
=
models
.
workspace
.
Workspace
.
get_by_id
(
cherrypy
.
request
.
db
,
workspace_id
)
instance
=
cherrypy
.
engine
.
publish
(
'rpc_get'
,
'vispa.
vfs.Virtual
FileSystem'
,
False
,
None
,
workspace_id
).
pop
()
instance
=
cherrypy
.
engine
.
publish
(
'rpc_get'
,
'vispa.
filesystem.
FileSystem'
,
False
,
None
,
workspace_id
).
pop
()
instance
.
setup
(
str
(
workspace
.
basedir
),
str
(
username
))
else
:
instance
=
vfs
.
Virtual
FileSystem
()
instance
.
setup
(
cherrypy
.
vispaServer
.
var_dir
,
str
(
username
)
)
instance
=
filesystem
.
FileSystem
()
instance
.
setup
(
cherrypy
.
vispaServer
.
var_dir
)
self
.
_
v
fs_instances
[
key
]
=
instance
self
.
_fs_instances
[
key
]
=
instance
return
instance
vispa/controller/platform.py
View file @
327eb18c
...
...
@@ -5,7 +5,7 @@ import vispa
from
vispa.helpers
import
browser
from
vispa.controller
import
AbstractController
,
StaticController
from
vispa.controller.ajax
import
AjaxController
from
vispa.controller.
vfs
import
V
FSController
from
vispa.controller.
filesystem
import
FSController
from
vispa.controller.error
import
ErrorController
from
vispa.controller.bus
import
BusController
from
vispa.models.stats
import
AccessStats
...
...
@@ -25,7 +25,7 @@ class PlatformController(AbstractController):
'''
AbstractController
.
__init__
(
self
,
platform
)
self
.
ajax
=
AjaxController
(
platform
)
self
.
vfs
=
V
FSController
(
platform
)
self
.
vfs
=
FSController
(
platform
)
self
.
error
=
ErrorController
()
self
.
static
=
StaticController
(
'static'
)
self
.
extensions
=
StaticController
(
'vispa/extensions'
)
...
...
vispa/filesystem.py
View file @
327eb18c
# @package FileSystem
# @package FileSystem
# Imports
import
os
...
...
@@ -24,31 +25,20 @@ class FileSystem(object):
# allowed extensions
self
.
allowed_extensions
=
FileSystem
.
FILE_EXTENSIONS
def
setup
(
self
,
basedir
,
username
):
# the data path
self
.
data_path
=
os
.
path
.
join
(
basedir
,
"data"
)
# the main paths
self
.
user_path
=
os
.
path
.
join
(
self
.
data_path
,
"user"
)
self
.
public_path
=
os
.
path
.
join
(
self
.
data_path
,
"public"
)
self
.
shared_path
=
os
.
path
.
join
(
self
.
data_path
,
"shared"
)
self
.
examples_path
=
os
.
path
.
join
(
self
.
data_path
,
"examples"
)
self
.
exercises_path
=
os
.
path
.
join
(
self
.
data_path
,
"exercises"
)
paths
=
[
self
.
data_path
,
self
.
user_path
,
self
.
public_path
,
self
.
shared_path
,
self
.
examples_path
,
self
.
exercises_path
]
def
setup
(
self
,
basedir
):
if
not
os
.
path
.
isdir
(
basedir
):
raise
Exception
(
"Basedir ("
+
str
(
basedir
)
+
") does not exist!"
)
# the basedir
self
.
basedir
=
os
.
path
.
join
(
basedir
,
".vispa"
)
# the job file path
self
.
job_file_path
=
os
.
path
.
join
(
self
.
basedir
,
".jobs"
)
paths
=
[
self
.
basedir
,
self
.
job_file_path
]
for
path
in
paths
:
if
not
os
.
path
.
isdir
(
path
):
os
.
makedirs
(
path
,
0700
)
if
username
is
None
or
username
==
""
:
return
upath
=
os
.
path
.
join
(
self
.
user_path
,
username
)
if
not
os
.
path
.
isdir
(
upath
):
os
.
makedirs
(
upath
,
0700
)
upath
=
os
.
path
.
join
(
self
.
public_path
,
username
)
if
not
os
.
path
.
isdir
(
upath
):
os
.
makedirs
(
upath
,
0700
)
def
getMimeType
(
self
,
filepath
):
def
get_mime_type
(
self
,
filepath
):
mime
,
encoding
=
guess_type
(
filepath
)
if
mime
is
not
None
:
return
mime
...
...
@@ -57,7 +47,7 @@ class FileSystem(object):
return
FileSystem
.
ADDITIONAL_MIMES
[
ext
]
return
None
def
check
F
ile
E
xtension
(
self
,
path
,
extensions
=
[]):
def
check
_f
ile
_e
xtension
(
self
,
path
,
extensions
=
[]):
if
(
len
(
extensions
)
==
0
):
return
True
for
elem
in
extensions
:
...
...
@@ -66,7 +56,7 @@ class FileSystem(object):
return
True
return
False
def
exists
(
self
,
username
,
path
,
pathtype
=
None
,
fileextension
=
None
,
permissionmode
=
"r"
):
def
exists
(
self
,
path
,
pathtype
=
None
,
fileextension
=
None
,
permissionmode
=
"r"
):
if
isinstance
(
permissionmode
,
str
)
and
permissionmode
.
lower
()
not
in
[
"r"
,
"w"
]:
return
False
...
...
@@ -102,7 +92,7 @@ class FileSystem(object):
return
True
def
get
F
ile
L
ist
(
self
,
username
,
path
,
deep
,
ext_filter
=
[],
reverse
=
False
):
def
get
_f
ile
_l
ist
(
self
,
path
,
deep
,
ext_filter
=
[],
reverse
=
False
):
dirlist
=
os
.
listdir
(
path
)
returnlist
=
[]
for
elem
in
dirlist
:
...
...
@@ -118,7 +108,7 @@ class FileSystem(object):
continue
returnlist
.
append
({
'name'
:
elem
,
'type'
:
'folder'
,
'parent'
:
path
+
'/'
,
'extension'
:
''
,
'mtime'
:
mtime
,
'size'
:
size
,
"path"
:
os
.
path
.
join
(
path
,
elem
)})
if
deep
:
returnlist
.
extend
(
self
.
get
F
ile
L
ist
(
username
,
fullPath
,
deep
,
ext_filter
,
reverse
))
returnlist
.
extend
(
self
.
get
_f
ile
_l
ist
(
fullPath
,
deep
,
ext_filter
,
reverse
))
else
:
if
elem
.
startswith
(
"."
):
continue
...
...
@@ -141,14 +131,14 @@ class FileSystem(object):
return
filelist
def
cut
S
lashs
(
self
,
path
):
def
cut
_s
lashs
(
self
,
path
):
path
=
path
[
1
:]
if
path
.
startswith
(
"/"
)
else
path
if
path
==
""
:
return
path
path
=
path
[:
-
1
]
if
path
.
endswith
(
"/"
)
else
path
return
path
def
create
F
older
(
self
,
path
,
name
):
def
create
_f
older
(
self
,
path
,
name
):
# folder with the same name existent?
fullpath
=
os
.
path
.
join
(
path
,
name
)
if
os
.
path
.
isdir
(
fullpath
):
...
...
@@ -159,7 +149,7 @@ class FileSystem(object):
#raise Exception("You don't have the permission to create this folder!")
raise
Exception
(
str
(
e
))
def
create
F
ile
(
self
,
path
,
name
):
def
create
_f
ile
(
self
,
path
,
name
):
# file with the same name existent?
fullpath
=
os
.
path
.
join
(
path
,
name
)
if
os
.
path
.
exists
(
fullpath
):
...
...
@@ -171,7 +161,7 @@ class FileSystem(object):
raise
Exception
(
str
(
e
))
def
rename
F
older
(
self
,
path
,
name
):
def
rename
_f
older
(
self
,
path
,
name
):
# file or folder
if
not
os
.
path
.
isdir
(
path
):
raise
Exception
(
"Renaming file with folder function!"
)
...
...
@@ -184,7 +174,7 @@ class FileSystem(object):
os
.
renames
(
path
,
fullpath
)
def
rename
F
ile
(
self
,
path
,
name
):
def
rename
_f
ile
(
self
,
path
,
name
):
# file or folder
if
os
.
path
.
isdir
(
path
):
raise
Exception
(
"Renaming folder with file function!"
)
...
...
@@ -196,8 +186,8 @@ class FileSystem(object):
os
.
renames
(
path
,
fullpath
)
def
remove
(
self
,
v
path
):
if
isinstance
(
v
path
,
list
):
def
remove
(
self
,
path
):
if
isinstance
(
path
,
list
):
for
p
in
path
:
self
.
remove
(
p
)
return
True
...
...
@@ -220,11 +210,11 @@ class FileSystem(object):
archive
=
ZipFile
(
fullpath
,
"w"
)
v
path
=
v
path
[
1
:]
if
v
path
.
startswith
(
"/"
)
else
v
path
path
=
path
if
path
.
startswith
(
"/"
)
else
path
for
p
in
paths
:
if
p
is
None
or
p
==
""
:
continue
p
=
p
[
1
:]
if
v
p
.
startswith
(
"/"
)
else
p
p
=
p
if
p
.
startswith
(
os
.
sep
)
else
os
.
sep
+
path
if
os
.
path
.
isdir
(
p
):
for
elem
in
os
.
listdir
(
p
):
fullp
=
os
.
path
.
join
(
p
,
elem
)
...
...
@@ -239,71 +229,53 @@ class FileSystem(object):
archive
.
close
()
def
paste
(
self
,
v
path
,
v
target
,
cut
):
if
isinstance
(
v
target
,
list
):
for
v
p
in
v
target
:
self
.
paste
(
username
,
v
path
,
v
p
,
cut
)
def
paste
(
self
,
path
,
target
,
cut
):
if
isinstance
(
target
,
list
):
for
p
in
target
:
self
.
paste
(
path
,
p
,
cut
)
return
True
# permission?
if
not
self
.
checkPermission
(
username
,
vpath
,
'w'
):
raise
Exception
(
"You don't have the permission in this folder!"
)
ppath
=
self
.
translateVirtualPath
(
username
,
vpath
)
ptarget
=
self
.
translateVirtualPath
(
username
,
vtarget
)
fulltarget
=
os
.
path
.
join
(
ppath
,
ptarget
.
split
(
"/"
)[
-
1
])
fulltarget
=
os
.
path
.
join
(
path
,
target
.
split
(
"/"
)[
-
1
])
if
os
.
path
.
exists
(
fulltarget
):
raise
Exception
(
"Name already in use!"
)
if
os
.
path
.
isdir
(
p
target
):
shutil
.
copytree
(
p
target
,
fulltarget
)
if
os
.
path
.
isdir
(
target
):
shutil
.
copytree
(
target
,
fulltarget
)
if
cut
:
shutil
.
rmtree
(
p
target
)
shutil
.
rmtree
(
target
)
else
:
shutil
.
copy2
(
p
target
,
p
path
)
shutil
.
copy2
(
target
,
path
)
if
cut
:
os
.
remove
(
ptarget
)
def
saveFileContent
(
self
,
username
,
vPath
,
content
,
force
=
True
):
#check write permissions
if
not
self
.
checkPermission
(
username
,
vPath
,
'w'
):
return
False
,
"Permission denied!"
os
.
remove
(
target
)
pPath
=
self
.
translateVirtualPath
(
username
,
vPath
)
def
save_file_content
(
self
,
path
,
content
,
force
=
True
):
#check if file already exists
if
os
.
path
.
exists
(
p
P
ath
)
and
not
force
:
return
False
,
"The file '%s' already exists!"
%
vP
ath
if
os
.
path
.
exists
(
path
)
and
not
force
:
return
False
,
"The file '%s' already exists!"
%
p
ath
out
=
open
(
p
P
ath
,
"w"
)
out
=
open
(
path
,
"w"
)
for
line
in
content
.
data
:
out
.
write
(
line
)
out
.
close
()
return
True
,
"File saved!"
def
getFileContent
(
self
,
username
,
vPath
):
# permissions?
if
not
self
.
checkPermission
(
username
,
vPath
,
'r'
):
return
False
,
"Permission denied!"
pPath
=
self
.
translateVirtualPath
(
username
,
vPath
)
f
=
open
(
pPath
,
"r"
)
def
get_file_content
(
self
,
path
):
f
=
open
(
path
,
"r"
)
content
=
f
.
read
()
f
.
close
()
content
=
" "
if
content
==
""
else
content
return
xmlrpclib
.
Binary
(
content
),
"File opened!"
def
isbrowserfile
(
self
,
path
):
def
is
_
browser
_
file
(
self
,
path
):
extension
=
path
.
split
(
"."
)[
-
1
]
return
extension
in
Virtual
FileSystem
.
BROWSER_EXTENSIONS
return
extension
in
FileSystem
.
BROWSER_EXTENSIONS
def
handle
F
ile
N
ame
C
ollision
(
self
,
name
,
p
P
ath
):
def
handle
_f
ile
_n
ame
_c
ollision
(
self
,
name
,
path
):
# collision?
files
=
os
.
listdir
(
p
P
ath
)
files
=
os
.
listdir
(
path
)
if
name
not
in
files
:
return
name
...
...
@@ -339,33 +311,7 @@ class FileSystem(object):
newname
=
"%s_1%s"
%
(
prename
,
""
if
extension
==
""
else
"."
+
extension
)
# return
return
self
.
handleFileNameCollision
(
newname
,
pPath
)
"""
def handleDownload(self, username, vPath):
if not self.exists(username, vPath, pathtype="file"):
raise Exception("The file '%s' does not exist or<br />you don't have the permission to read this file."%vPath)
return None
pPath = self.translateVirtualPath(username, vPath)
payload = open(pPath, "r")
output = cStringIO.StringIO()
for line in payload:
output.write(line)
output.seek(0)
data = output.read()
output.close()
# get the content type depending on the file extension
ext = vPath.split(".")[-1]
mimetype = self.getMimeType(vPath)
if mimetype is None:
raise Exception("The file extension '%s' is not supported by this server"%ext)
return None
return data, mimetype, self.isbrowserfile(vPath)
"""
return
self
.
handle_file_name_collision
(
newname
,
path
)
##
# Creates a unique ID
...
...
vispa/platform.py
View file @
327eb18c
...
...
@@ -12,7 +12,7 @@ import vispa
import
vispa.extensions
from
vispa.extensions
import
AbstractExtension
from
vispa.models.workspace
import
Workspace
from
vispa.
vfs
import
Virtual
FileSystem
from
vispa.
filesystem
import
FileSystem
from
vispa.helpers
import
browser
from
vispa.models.user
import
User
from
vispa.controller.platform
import
PlatformController
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment