aboutsummaryrefslogtreecommitdiffstats
path: root/tmp.py
blob: 67e0145a12e7478f49235fd08d3f5972b1637fe0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python3
#
# "THE BEER-WARE LICENSE" (Revision 42):
# Matěj Cepl, <mcepl@cepl.eu> 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)