2011-01-06 02:46:04 +03:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
2012-05-21 15:12:37 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-01-06 02:46:04 +03:00
|
|
|
#
|
|
|
|
# When run directly, this script expects the following environment variables
|
|
|
|
# to be set:
|
|
|
|
# UPLOAD_PATH : path on that host to put the files in
|
2015-08-26 16:46:08 +03:00
|
|
|
#
|
2018-04-19 19:30:31 +03:00
|
|
|
# Files are simply copied to UPLOAD_PATH.
|
2011-01-06 02:46:04 +03:00
|
|
|
#
|
|
|
|
# All files to be uploaded should be passed as commandline arguments to this
|
|
|
|
# script. The script takes one other parameter, --base-path, which you can use
|
|
|
|
# to indicate that files should be uploaded including their paths relative
|
|
|
|
# to the base path.
|
|
|
|
|
2017-10-07 17:45:22 +03:00
|
|
|
import sys
|
|
|
|
import os
|
2015-09-11 18:35:00 +03:00
|
|
|
import shutil
|
2011-01-06 02:46:04 +03:00
|
|
|
from optparse import OptionParser
|
|
|
|
|
2017-10-07 17:45:22 +03:00
|
|
|
|
2011-01-06 02:46:04 +03:00
|
|
|
def OptionalEnvironmentVariable(v):
|
|
|
|
"""Return the value of the environment variable named v, or None
|
|
|
|
if it's unset (or empty)."""
|
|
|
|
if v in os.environ and os.environ[v] != "":
|
|
|
|
return os.environ[v]
|
|
|
|
return None
|
|
|
|
|
2017-10-07 17:45:22 +03:00
|
|
|
|
2011-01-06 02:46:04 +03:00
|
|
|
def FixupMsysPath(path):
|
|
|
|
"""MSYS helpfully translates absolute pathnames in environment variables
|
|
|
|
and commandline arguments into Windows native paths. This sucks if you're
|
|
|
|
trying to pass an absolute path on a remote server. This function attempts
|
|
|
|
to un-mangle such paths."""
|
|
|
|
if "OSTYPE" in os.environ and os.environ["OSTYPE"] == "msys":
|
2022-01-06 09:49:47 +03:00
|
|
|
# sort of awful, find out where our shell is (should be in msys2/usr/bin
|
|
|
|
# or msys/bin) and strip the first part of that path out of the other path
|
2011-01-06 02:46:04 +03:00
|
|
|
if "SHELL" in os.environ:
|
|
|
|
sh = os.environ["SHELL"]
|
|
|
|
msys = sh[: sh.find("/bin")]
|
|
|
|
if path.startswith(msys):
|
|
|
|
path = path[len(msys) :]
|
|
|
|
return path
|
|
|
|
|
2017-10-07 17:45:22 +03:00
|
|
|
|
2015-09-11 18:35:00 +03:00
|
|
|
def GetBaseRelativePath(path, local_file, base_path):
|
2011-01-06 02:46:04 +03:00
|
|
|
"""Given a remote path to upload to, a full path to a local file, and an
|
|
|
|
optional full path that is a base path of the local file, construct the
|
|
|
|
full remote path to place the file in. If base_path is not None, include
|
|
|
|
the relative path from base_path to file."""
|
2018-04-19 05:43:15 +03:00
|
|
|
if base_path is None or not local_file.startswith(base_path):
|
|
|
|
return path
|
2018-04-19 19:30:31 +03:00
|
|
|
|
2011-01-06 02:46:04 +03:00
|
|
|
dir = os.path.dirname(local_file)
|
|
|
|
# strip base_path + extra slash and make it unixy
|
2017-10-07 17:45:22 +03:00
|
|
|
dir = dir[len(base_path) + 1 :].replace("\\", "/")
|
2011-01-06 02:46:04 +03:00
|
|
|
return path + dir
|
|
|
|
|
2017-10-07 17:45:22 +03:00
|
|
|
|
2018-04-19 19:31:00 +03:00
|
|
|
def CopyFilesLocally(path, files, verbose=False, base_path=None):
|
2015-09-11 18:35:00 +03:00
|
|
|
"""Copy each file in the list of files to `path`. The `base_path` argument is treated
|
|
|
|
as it is by UploadFiles."""
|
|
|
|
if not path.endswith("/"):
|
|
|
|
path += "/"
|
|
|
|
if base_path is not None:
|
|
|
|
base_path = os.path.abspath(base_path)
|
|
|
|
for file in files:
|
|
|
|
file = os.path.abspath(file)
|
|
|
|
if not os.path.isfile(file):
|
|
|
|
raise IOError("File not found: %s" % file)
|
|
|
|
# first ensure that path exists remotely
|
|
|
|
target_path = GetBaseRelativePath(path, file, base_path)
|
|
|
|
if not os.path.exists(target_path):
|
|
|
|
os.makedirs(target_path)
|
|
|
|
if verbose:
|
2017-10-07 17:45:22 +03:00
|
|
|
print("Copying " + file + " to " + target_path)
|
2015-09-11 18:35:00 +03:00
|
|
|
shutil.copy(file, target_path)
|
|
|
|
|
2017-10-07 17:45:22 +03:00
|
|
|
|
2011-01-06 02:46:04 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
path = OptionalEnvironmentVariable("UPLOAD_PATH")
|
2015-08-26 16:46:08 +03:00
|
|
|
|
2011-01-06 02:46:04 +03:00
|
|
|
if sys.platform == "win32":
|
|
|
|
if path is not None:
|
|
|
|
path = FixupMsysPath(path)
|
|
|
|
|
|
|
|
parser = OptionParser(usage="usage: %prog [options] <files>")
|
|
|
|
parser.add_option(
|
|
|
|
"-b",
|
|
|
|
"--base-path",
|
2015-08-05 22:43:15 +03:00
|
|
|
action="store",
|
2017-10-07 17:45:22 +03:00
|
|
|
help="Preserve file paths relative to this path when uploading. "
|
|
|
|
"If unset, all files will be uploaded directly to UPLOAD_PATH.",
|
|
|
|
)
|
2011-01-06 02:46:04 +03:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
if len(args) < 1:
|
2017-10-07 17:45:22 +03:00
|
|
|
print("You must specify at least one file to upload")
|
2011-01-06 02:46:04 +03:00
|
|
|
sys.exit(1)
|
2018-04-19 05:43:15 +03:00
|
|
|
|
2011-01-06 02:46:04 +03:00
|
|
|
try:
|
2018-04-19 19:30:31 +03:00
|
|
|
CopyFilesLocally(path, args, base_path=options.base_path, verbose=True)
|
2017-10-07 17:45:22 +03:00
|
|
|
except IOError as strerror:
|
|
|
|
print(strerror)
|
2011-01-06 02:46:04 +03:00
|
|
|
sys.exit(1)
|