+ try:
+ bn = os.path.basename(k)
+ target = os.path.join(OutDir, bn)
+ if os.path.isdir(target):
+ safe_rmtree(dst)
+ else:
+ posix.remove(target)
+ except:
+ pass
+ DoLink(global_dir, OutDir, "last_update.trace")
+
+
+def getLastLDAPChangeTime(l):
+ mods = l.search_s('cn=log',
+ ldap.SCOPE_ONELEVEL,
+ '(&(&(!(reqMod=activity-from*))(!(reqMod=activity-pgp*)))(|(reqType=add)(reqType=delete)(reqType=modify)(reqType=modrdn)))',
+ ['reqEnd'])
+
+ last = 0
+
+ # Sort the list by reqEnd
+ sorted_mods = sorted(mods, key=lambda mod: mod[1]['reqEnd'][0].split('.')[0])
+ # Take the last element in the array
+ last = sorted_mods[-1][1]['reqEnd'][0].split('.')[0]
+
+ return last
+
+def getLastKeyringChangeTime():
+ krmod = 0
+ for k in Keyrings:
+ mt = os.path.getmtime(k)
+ if mt > krmod:
+ krmod = mt
+
+ return int(krmod)
+
+def getLastBuildTime(gdir):
+ cache_last_ldap_mod = 0
+ cache_last_unix_mod = 0
+ cache_last_run = 0
+
+ try:
+ fd = open(os.path.join(gdir, "last_update.trace"), "r")
+ cache_last_mod=fd.read().split()
+ try:
+ cache_last_ldap_mod = cache_last_mod[0]
+ cache_last_unix_mod = int(cache_last_mod[1])
+ cache_last_run = int(cache_last_mod[2])
+ except IndexError, ValueError:
+ pass
+ fd.close()
+ except IOError, e:
+ if e.errno == errno.ENOENT:
+ pass
+ else:
+ raise e
+
+ return (cache_last_ldap_mod, cache_last_unix_mod, cache_last_run)
+
+def mq_notify(options, message):
+ options.section = 'dsa-udgenerate'
+ options.config = '/etc/dsa/pubsub.conf'
+
+ config = Config(options)
+ conf = {
+ 'rabbit_userid': config.username,
+ 'rabbit_password': config.password,
+ 'rabbit_virtual_host': config.vhost,
+ 'rabbit_hosts': ['pubsub02.debian.org', 'pubsub01.debian.org'],
+ 'use_ssl': False
+ }
+
+ msg = {
+ 'message': message,
+ 'timestamp': int(time.time())
+ }
+ conn = None
+ try:
+ conn = Connection(conf=conf)
+ conn.topic_send(config.topic,
+ json.dumps(msg),
+ exchange_name=config.exchange,
+ timeout=5)
+ finally:
+ if conn:
+ conn.close()
+
+def ud_generate():
+ parser = optparse.OptionParser()
+ parser.add_option("-g", "--generatedir", dest="generatedir", metavar="DIR",
+ help="Output directory.")
+ parser.add_option("-f", "--force", dest="force", action="store_true",
+ help="Force generation, even if no update to LDAP has happened.")
+ parser.add_option("-m", "--mq", action="store_true", default=False,
+ help="Send update trigger over MQ")
+
+ (options, args) = parser.parse_args()
+ if len(args) > 0:
+ parser.print_help()
+ sys.exit(1)
+
+ if options.generatedir is not None:
+ generate_dir = os.environ['UD_GENERATEDIR']
+ elif 'UD_GENERATEDIR' in os.environ:
+ generate_dir = os.environ['UD_GENERATEDIR']
+ else:
+ generate_dir = GenerateDir
+
+
+ lockf = os.path.join(generate_dir, 'ud-generate.lock')
+ lock = get_lock( lockf )
+ if lock is None:
+ sys.stderr.write("Could not acquire lock %s.\n"%(lockf))
+ sys.exit(1)
+
+ l = make_ldap_conn()
+
+ time_started = int(time.time())
+ ldap_last_mod = getLastLDAPChangeTime(l)
+ unix_last_mod = getLastKeyringChangeTime()
+ cache_last_ldap_mod, cache_last_unix_mod, last_run = getLastBuildTime(generate_dir)
+
+ need_update = (ldap_last_mod > cache_last_ldap_mod) or (unix_last_mod > cache_last_unix_mod) or (time_started - last_run > MAX_UD_AGE)
+
+ fd = open(os.path.join(generate_dir, "last_update.trace"), "w")
+ if need_update or options.force:
+ msg = 'Update forced' if options.force else 'Update needed'
+ generate_all(generate_dir, l)
+ if options.mq:
+ mq_notify(options, msg)
+ last_run = int(time.time())
+ fd.write("%s\n%s\n%s\n" % (ldap_last_mod, unix_last_mod, last_run))
+ fd.close()
+ sys.exit(0)
+
+
+if __name__ == "__main__":
+ if 'UD_PROFILE' in os.environ:
+ import cProfile
+ import pstats
+ cProfile.run('ud_generate()', "udg_prof")
+ p = pstats.Stats('udg_prof')
+ ##p.sort_stats('time').print_stats()
+ p.sort_stats('cumulative').print_stats()