summaryrefslogtreecommitdiffstats
path: root/files/download/last-sync
blob: fd43dc77afc7364b13a8879bcf310caba4b84489 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/python

# Copyright (C) 2014 by Adrian Reber
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import requests
import time
import sys
import getopt

fedora = 'org.fedoraproject.prod.bodhi.updates.fedora.sync'
epel = 'org.fedoraproject.prod.bodhi.updates.epel.sync'

branched = 'org.fedoraproject.prod.compose.branched.rsync.complete'
rawhide = 'org.fedoraproject.prod.compose.rawhide.rsync.complete'

base_url = 'https://apps.fedoraproject.org/datagrepper/raw'


topics =  []
# default time interval to query for syncs: 1 day
delta = 86400
# return 0 and no output if a sync happened during <delta>
# if no sync happened 1 is returned
quiet = False
secondary = False
rawtime = False

def usage():
        print
        print "last-sync queries the Fedora Message Bus if new data is available on the public servers"
        print
        print "Usage: last-sync [options]"
        print
        print "Options:"
        print "  -a, --all                 query all possible releases (default)"
        print "                            (fedora, epel, branched, rawhide)"
        print "  -f, --fedora              only query if fedora has been updated during <delta>"
        print "  -e, --epel                only query if epel has been updated"
        print "  -b, --branched            only query if the branched off release"
        print "                            has been updated"
        print "  -r, --rawhide             only query if rawhide has been updated"
        print "  -q, --quiet               do not print out any informations"
        print "  -t, --time                print date in seconds since 1970-01-01"
        print "  -d DELTA, --delta=DELTA   specify the time interval which should be used"
        print "                            for the query (default: 86400)"


# -a -f -e -b -r -s -q -d
def parse_args():
        global topics
        global delta
        global quiet
        global secondary
        global rawtime
        try:
                opts, args = getopt.getopt(sys.argv[1:], "afhebrsqtd:", ["all", "fedora", "epel", "rawhide", "branched", "secondary", "quiet", "time", "delta="])
        except getopt.GetoptError as err:
                print str(err)
                usage()
                sys.exit(2)

        for option, argument in opts:
                if option in ("-a", "--all"):
                        topics =  [ fedora, epel, branched, rawhide ]
                        secondary = True
                if option in ("-f", "--fedora"):
                        topics.append(fedora)
                if option in ("-e", "--epel"):
                        topics.append(epel)
                if option in ("-r", "--rawhide"):
                        topics.append(rawhide)
                if option in ("-b", "--branched"):
                        topics.append(branched)
                if option in ("-s", "--secondary"):
                        topics.append(rawhide)
                        secondary = True
                if option in ("-q", "--quiet"):
                        quiet = True
                if option in ("-t", "--time"):
                        rawtime = True
                if option in ("-d", "--delta"):
                        delta = argument
                if option in ("-h"):
                        usage();
                        sys.exit(0)



def getKey(item):
        return item[1]

def create_url(url, topics, delta):
        topic = ""
        for i in topics:
                topic += "&topic=%s" % i
        return '%s?delta=%s%s' % (url, delta, topic)

parse_args()

if topics == []:
        topics =  [ fedora, epel, branched, rawhide ]
        secondary = True

i = 0
data = None
while i < 5:
        try:
                data = requests.get(create_url(base_url, topics, delta), timeout=1).json()
                break
        except:
                pass

if not data:
        sys.exit(1)

repos = []

for i in range(0, data['count']):
        try:
                repo = "%s-%s" % (data['raw_messages'][i]['msg']['repo'], data['raw_messages'][i]['msg']['release'])
        except:
                # the rawhide and branch sync message has no repo information
                arch = data['raw_messages'][i]['msg']['arch']
                if arch == '':
                        arch = 'primary'
                elif not secondary:
                        continue
                repo = "%s-%s" % (data['raw_messages'][i]['msg']['branch'], arch)

        repos.append([repo, data['raw_messages'][i]['timestamp']])

if quiet == False:
        for repo, timestamp in sorted(repos, key=getKey):
                if rawtime == True:
                   # this is useful if you want to compare the timestamp in seconds versus string
                   print "%s: %s" % (repo, timestamp)
                else:
                   print "%s: %s" % (repo, time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(timestamp)))

if data['count'] > 0:
        sys.exit(0)
else:
        sys.exit(1)