added server files, altered technical
[popcorn.git] / popcorn-client
blob1ecab9b99bd9028237dfc906c399ae6ef6082b57
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 httplib
31 import time
33 package_name = 'popcorn-client'
34 popcorn_server = 'stats.opensuse.org'
36 days_recent = 30
37 days_unused = 30
39 pathre = '^/(bin|boot|lib|lib64|opt/gnome/bin|opt/gnome/sbin|opt/kde3/bin|opt/kde3/sbin|sbin|usr/bin|usr/games|usr/include|usr/lib|usr/lib64|usr/libexec|usr/sbin|usr/share)/'
41 class Statistics:
42 package_ver = 'unknown'
43 arch = platform.machine()
44 packages = []
46 def __init__(self):
47 ts = rpm.TransactionSet()
48 mi = ts.dbMatch('name', package_name)
49 for h in mi:
50 self.package_ver = h['version']
51 break
53 def fill_packages(self):
54 pathreprg = re.compile(pathre)
55 now = int(time.time())
56 ts = rpm.TransactionSet()
57 mi = ts.dbMatch()
58 for h in mi:
59 name = h['name']
60 if name == 'gpg-pubkey': continue
61 installed = h[rpm.RPMTAG_INSTALLTIME]
62 (accessed, atime) = (0, 0)
63 for file in h[rpm.RPMTAG_FILENAMES]:
64 if not pathreprg.match(file): continue
65 try:
66 (atime, ) = os.stat(file)[7:8]
67 if atime > accessed:
68 accessed = atime
69 except:
70 pass
71 if accessed == 0:
72 cat = 'nof' # no-files
73 else:
74 if now - installed < 30 * 86400:
75 cat = 'rec' # recent
76 else:
77 if now - accessed < 30 * 86400:
78 cat = 'vot' # voted
79 else:
80 cat ='old' # old
81 self.packages.append( (cat, name) )
83 def serialize(self):
84 ret = ''
85 ret += 'START %s %s\n' % (self.package_ver, self.arch)
86 for pkg in self.packages:
87 ret += '%s %s\n' % pkg
88 ret += 'END\n'
89 return ret
91 class Client:
92 def submit(self, body, compress = False):
93 conn = httplib.HTTPConnection(popcorn_server)
94 conn.putrequest('POST', '/popcorn')
95 conn.putheader('content-type', 'application/octet-stream')
96 conn.putheader('content-length', str(len(body)))
97 conn.endheaders()
98 conn.send(body)
100 stats = Statistics()
101 stats.fill_packages()
103 # client = Client()
104 # client.submit( stats.serialize() )
106 print stats.serialize(),