Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
866d73ca
Commit
866d73ca
authored
Dec 13, 2019
by
Julien Chaumond
Browse files
[cli] Upload is now compatible with folders
parent
d4614729
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
14 deletions
+43
-14
transformers/commands/user.py
transformers/commands/user.py
+43
-14
No files found.
transformers/commands/user.py
View file @
866d73ca
...
@@ -19,8 +19,8 @@ class UserCommands(BaseTransformersCLICommand):
...
@@ -19,8 +19,8 @@ class UserCommands(BaseTransformersCLICommand):
list_parser
.
set_defaults
(
func
=
lambda
args
:
ListObjsCommand
(
args
))
list_parser
.
set_defaults
(
func
=
lambda
args
:
ListObjsCommand
(
args
))
# upload
# upload
upload_parser
=
parser
.
add_parser
(
'upload'
)
upload_parser
=
parser
.
add_parser
(
'upload'
)
upload_parser
.
add_argument
(
'
file
'
,
type
=
str
,
help
=
'Local
file
path of the file to upload.'
)
upload_parser
.
add_argument
(
'
path
'
,
type
=
str
,
help
=
'Local path of the
folder or individual
file to upload.'
)
upload_parser
.
add_argument
(
'--filename'
,
type
=
str
,
default
=
None
,
help
=
'Optional: override object filename on S3.'
)
upload_parser
.
add_argument
(
'--filename'
,
type
=
str
,
default
=
None
,
help
=
'Optional: override
individual
object filename on S3.'
)
upload_parser
.
set_defaults
(
func
=
lambda
args
:
UploadCommand
(
args
))
upload_parser
.
set_defaults
(
func
=
lambda
args
:
UploadCommand
(
args
))
...
@@ -138,28 +138,57 @@ class ListObjsCommand(BaseUserCommand):
...
@@ -138,28 +138,57 @@ class ListObjsCommand(BaseUserCommand):
class
UploadCommand
(
BaseUserCommand
):
class
UploadCommand
(
BaseUserCommand
):
def
walk_dir
(
self
,
rel_path
):
"""
Recursively list all files in a folder.
"""
entries
:
List
[
os
.
DirEntry
]
=
list
(
os
.
scandir
(
rel_path
))
files
=
[
(
os
.
path
.
join
(
os
.
getcwd
(),
f
.
path
),
# filepath
f
.
path
# filename
)
for
f
in
entries
if
f
.
is_file
()
]
for
f
in
entries
:
if
f
.
is_dir
():
files
+=
self
.
walk_dir
(
f
.
path
)
return
files
def
run
(
self
):
def
run
(
self
):
token
=
HfFolder
.
get_token
()
token
=
HfFolder
.
get_token
()
if
token
is
None
:
if
token
is
None
:
print
(
"Not logged in"
)
print
(
"Not logged in"
)
exit
(
1
)
exit
(
1
)
filepath
=
os
.
path
.
join
(
os
.
getcwd
(),
self
.
args
.
file
)
local_path
=
os
.
path
.
abspath
(
self
.
args
.
path
)
filename
=
self
.
args
.
filename
if
self
.
args
.
filename
is
not
None
else
os
.
path
.
basename
(
filepath
)
if
os
.
path
.
isdir
(
local_path
):
print
(
if
self
.
args
.
filename
is
not
None
:
"About to upload file {} to S3 under filename {}"
.
format
(
raise
ValueError
(
"Cannot specify a filename override when uploading a folder."
)
ANSI
.
bold
(
filepath
),
ANSI
.
bold
(
filename
)
rel_path
=
os
.
path
.
basename
(
local_path
)
files
=
self
.
walk_dir
(
rel_path
)
elif
os
.
path
.
isfile
(
local_path
):
filename
=
self
.
args
.
filename
if
self
.
args
.
filename
is
not
None
else
os
.
path
.
basename
(
local_path
)
files
=
[(
local_path
,
filename
)]
else
:
raise
ValueError
(
"Not a valid file or directory: {}"
.
format
(
local_path
))
for
filepath
,
filename
in
files
:
print
(
"About to upload file {} to S3 under filename {}"
.
format
(
ANSI
.
bold
(
filepath
),
ANSI
.
bold
(
filename
)
)
)
)
)
choice
=
input
(
"Proceed? [Y/n] "
).
lower
()
choice
=
input
(
"Proceed? [Y/n] "
).
lower
()
if
not
(
choice
==
""
or
choice
==
"y"
or
choice
==
"yes"
):
if
not
(
choice
==
""
or
choice
==
"y"
or
choice
==
"yes"
):
print
(
"Abort"
)
print
(
"Abort"
)
exit
()
exit
()
print
(
print
(
ANSI
.
bold
(
"Uploading... This might take a while if file
is
large"
)
ANSI
.
bold
(
"Uploading... This might take a while if file
s are
large"
)
)
)
access_url
=
self
.
_api
.
presign_and_upload
(
for
filepath
,
filename
in
files
:
token
=
token
,
filename
=
filename
,
filepath
=
filepath
access_url
=
self
.
_api
.
presign_and_upload
(
)
token
=
token
,
filename
=
filename
,
filepath
=
filepath
print
(
"Your file now lives at:"
)
)
print
(
access_url
)
print
(
"Your file now lives at:"
)
print
(
access_url
)
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