small changes
[popcorn.git] / popcorn-client
blobf8928670ae14c24a83418172e08f5ad1c04d0453
1 #!/usr/bin/python
3 # Copyright (c) 2009 Pavol Rusnak
5 # Permission is hereby granted, free of charge, to any person
6 # obtaining a copy of this software and associated documentation
7 # files (the "Software"), to deal in the Software without
8 # restriction, including without limitation the rights to use,
9 # copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the
11 # Software is furnished to do so, subject to the following
12 # conditions:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 # OTHER DEALINGS IN THE SOFTWARE.
26 import os
27 import re
28 import rpm
29 import platform
30 import time
31 import httplib
33 package_name = 'popcorn-client'
34 popcorn_server = 'stats.opensuse.org'
35 pathre = '^/(bin|sbin|usr/bin|usr/sbin|usr/games|opt/kde3/bin|opt/kde3/sbin|opt/gnome/bin|opt/gnome/sbin|lib|lib64|usr/lib|usr/lib64|include|boot)/'
37 class Statistics:
38 package_ver = 'unknown'
39 arch = platform.machine()
40 packages = []
41 now = int(time.time())
43 def __init__(self):
44 ts = rpm.TransactionSet()
45 mi = ts.dbMatch('name', package_name)
46 for h in mi:
47 self.package_ver = h['version']
48 break
50 def fill_packages(self):
51 pathreprg = re.compile(pathre)
52 ts = rpm.TransactionSet()
53 mi = ts.dbMatch()
54 for h in mi:
55 name = h['name']
56 if name == 'gpg-pubkey': continue
57 installed = h[rpm.RPMTAG_INSTALLTIME]
58 (accessed, atime) = (0, 0)
59 for file in h[rpm.RPMTAG_FILENAMES]:
60 if not pathreprg.match(file): continue
61 try:
62 (atime, ) = os.stat(file)[7:8]
63 if atime > accessed:
64 accessed = atime
65 except:
66 pass
67 self.packages.append( (max(self.now-installed, 0), '-' if accessed==0 else max(self.now-accessed, 0), name) )
69 def serialize(self):
70 ret = ''
71 ret += '%s\n%s\n%s\n' % (self.now, self.arch, self.package_ver)
72 for pkg in self.packages:
73 ret += '%s %s %s\n' % pkg
74 return ret
76 class Client:
77 def submit(self, body, compress = False):
78 conn = httplib.HTTPConnection(popcorn_server)
79 conn.putrequest('POST', '/popcorn')
80 conn.putheader('content-type', 'application/octet-stream')
81 conn.putheader('content-length', str(len(body)))
82 conn.endheaders()
83 conn.send(body)
85 stats = Statistics()
86 stats.fill_packages()
88 # client = Client()
89 # client.submit( stats.serialize() )
91 print stats.serialize()