#!/usr/bin/python3 # # "THE BEER-WARE LICENSE" (Revision 42): # Matěj Cepl, wrote this file. As long as you retain # this notice you can do whatever you want with this stuff. If we meet # some day, and you think this stuff is worth it, you can buy me a beer # in return. Matěj Cepl # # Instructions for setting up appropriate folders on # file.rdu.redhat.com see https://mojo.redhat.com/docs/DOC-14590 # See also variable URL_VARIANTS below for necessary configuration. import logging import os import os.path import subprocess import sys import urllib.parse logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', level=logging.INFO) COMPRESSED_EXTENSIONS = ['.jpg', '.webm', '.ogg', '.gz', '.bz2', '.zip', '.xz', '.odp', '.odt', '.ods', '.mp4', '.mp3', '.png', '.pdf', '.apk', '.rpm', '.epub', '.mkv', '.avi', '.jar', '.opus', '.ogv', '.flac', '.msi', '.m4a'] # THE VARIABLE URL_VARIANTS IS MEANT TO BE EDITED. Change various # attributes to correspond to your situation. # If new section is added (as here I have also 'tmp' for fedorapeople.org) # and the script has a symlink named as the section (so I have tmp as # a symlink to this script) it saves the file to the location defined in # such section when called under the other name. # So, `tmp filename` saves file filename to fedorapeople for me. URL_VARIANTS = { 'barstool': { 'base_url': 'mcepl@shell.eng.rdu.redhat.com:public_html/', 'target_url': 'http://file.rdu.redhat.com/~mcepl/', 'shorten_api': 'https://url.corp.redhat.com/new?' }, 'wotan': { 'base_url': 'wotan:Export/', 'target_url': 'https://w3.suse.de/~mcepl/' }, 'tmp': { 'base_url': 'fedorapeople.org:public_html/tmp/', 'target_url': 'https://mcepl.fedorapeople.org/tmp/', # 'shorten_api': 'http://is.gd/create.php?format=simple&url=' 'shorten_api': 'https://da.gd/s?url=' } } cmdname = os.path.basename(sys.argv[0]).lower() config = URL_VARIANTS[cmdname] if not os.path.exists(sys.argv[1]): sys.exit(1) for processed_file in sys.argv[1:]: st = os.stat(processed_file).st_size file_ext = os.path.splitext(processed_file)[1] logging.debug('size of {} is {:,}.'.format(processed_file, st)) logging.debug('extension is {}'.format(file_ext)) if st > 1000000 and file_ext.lower() not in COMPRESSED_EXTENSIONS: logging.debug( "file {} should be compressed ({:,} b)".format( processed_file, st)) try: ret = subprocess.check_call( ['gzip', '--keep', '--best', processed_file]) except subprocess.CalledProcessError: logging.error('Compressing the file failed!') raise fname = processed_file + '.gz' COMPRESSED = True else: logging.debug( 'file {} should not be compressed ({:,} b)'.format( processed_file, st)) fname = processed_file COMPRESSED = False os.chmod(fname, 0o0644) # To make scp happy modified_fname = fname.replace(':', '_') os.rename(fname, modified_fname) logging.debug('Unshorten fname = {}'.format(modified_fname)) subprocess.check_call(['scp', '{}'.format(modified_fname), config['base_url']]) target_URL = config['target_url'] + os.path.basename(modified_fname) # Make target_URL into a safe URL safe_target_URL = urllib.parse.quote(target_URL).replace('%3A', ':', 1) print(safe_target_URL) # curl -s 'http://is.gd/create.php?format=simple&url=www.example.com' # http://is.gd/MOgh5q if 'shorten_api' in config: shortened_URL = subprocess.check_output( ['curl', '-s', config['shorten_api'] + safe_target_URL]).decode().strip() print(shortened_URL) # The script should have no side-effects if COMPRESSED: os.unlink(modified_fname) else: os.rename(modified_fname, fname)