Created
July 27, 2010 11:59
-
-
Save mhl/492126 to your computer and use it in GitHub Desktop.
A script for uploading a local file to be web visible, outputting a URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3.1 | |
import sys | |
import os | |
import urllib.parse | |
from subprocess import call | |
if len(sys.argv) != 2: | |
print("Usage: {} [FILE-OR-DIRECTORY]".format(sys.argv[0]),file=sys.stderr) | |
sys.exit(1) | |
def vcall(command): | |
print("+"+" ".join(command)) | |
return call(command) | |
source = sys.argv[1] | |
username = "mark" | |
host = "sphinx.mythic-beasts.com" | |
directory = "/home/mark/public_html/" | |
base_url = "http://mythic-beasts.com/~mark/" | |
# From http://stackoverflow.com/questions/35817/whats-the-best-way-to-escape-os-system-calls-in-python | |
def shellquote(s): | |
return "'" + s.replace("'", "'\\''") + "'" | |
for_ssh = "{}@{}".format(username,host) | |
for_scp = for_ssh + ":" + shellquote(directory) | |
destination_path = os.path.join(directory,os.path.basename(source)) | |
if 0 == vcall(["ssh", | |
for_ssh, | |
"[ -e {} ]".format(shellquote(destination_path))]): | |
print("That's already been enwebbed",file=sys.stderr) | |
sys.exit(1) | |
if 0 != vcall(["scp","-r",source,for_scp]): | |
print("Failed to copy over the file or directory",file=sys.stderr) | |
sys.exit(1) | |
if os.path.isdir(source): | |
# Add a .htaccess that allows indexes: | |
htaccess = os.path.join(destination_path,".htaccess") | |
if 0 != vcall(["ssh", | |
for_ssh, | |
"echo 'Options +Indexes' > {}".format(shellquote(htaccess))]): | |
print("Failed to add .htaccess file",file=sys.stderr) | |
sys.exit(1) | |
# And make everything readable: | |
if 0 != vcall(["ssh", | |
for_ssh, | |
"chmod a+rX -R {}".format(shellquote(destination_path))]): | |
print("Failed to make the file or directory readable",file=sys.stderr) | |
sys.exit(1) | |
print("===> "+base_url+urllib.parse.quote(os.path.basename(source))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment