#!/usr/bin/env python3 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et from __future__ import print_function import sys, os import getopt sys.path.insert(0, os.environ['HOME']+'/tld-ftp-admin/modules') import ftptree import getpass from common import checkdir import ftpio from config import sign_key from sign import is_signed, signpkgs os.umask(0o022) try: opts, args = getopt.getopt(sys.argv[1:], '') except getopt.GetoptError: print("ERR: options error", file=sys.stderr) print("sign.py tree package1 [package2...]", file=sys.stderr) sys.exit(1) if len(args) < 1: print("ERR: missing tree name", file=sys.stderr) print("sign.py tree package1 [package2...]", file=sys.stderr) sys.exit(1) if sign_key == None: print("ERR: sign_key not defined in config", file=sys.stderr) sys.exit(1) treename = args[0] packages = args[1:] checkdir(treename) ftpio.connect('sign') if not ftpio.lock(treename, True): print("ERR: %s tree already locked" % treename, file=sys.stderr) sys.exit(1) files = [] try: if len(packages) < 1: loadall = True else: loadall = False # if no files specified, grab whole tree contents tree = ftptree.FtpTree(treename, loadall = loadall) if loadall: # this is hack, should be a param, not access private .loadedpkgs element tree.mark4moving(tree.loadedpkgs) else: tree.mark4moving(packages) files = tree.rpmfiles() except ftptree.SomeError: # In case of problems we need to unlock the tree before exiting ftpio.unlock(treename) sys.exit(1) ftpio.unlock(treename) print("Checking signatures of %d files from %d packages" % (len(files), len(tree.loadedpkgs))) sign = [] n = c = 0 for file in files: if not is_signed(file): sign.append(file) c += 1 n += 1 print("\r%d/%d %s\033[0K" % (n, c, file), end='') print() if len(sign) == 0: print("No files to sign") sys.exit(0) # http://mail.python.org/pipermail/python-list/2009-February/700658.html def chunk(seq, size, pad=None): ''' Slice a list into consecutive disjoint 'chunks' of length equal to size. The last chunk is padded if necessary. >>> list(chunk(range(1,10),3)) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> list(chunk(range(1,9),3)) [[1, 2, 3], [4, 5, 6], [7, 8, None]] >>> list(chunk(range(1,8),3)) [[1, 2, 3], [4, 5, 6], [7, None, None]] >>> list(chunk(range(1,10),1)) [[1], [2], [3], [4], [5], [6], [7], [8], [9]] >>> list(chunk(range(1,10),9)) [[1, 2, 3, 4, 5, 6, 7, 8, 9]] >>> for X in chunk([],3): print(X) >>> ''' n = len(seq) mod = n % size for i in range(0, n - mod, size): yield seq[i : i + size] if mod: yield seq[-mod:] print("Total %d files to sign" % len(sign)) password = getpass.getpass("Enter signing password: ") try: for x in chunk(sign, 512): print("Signing %d files" % len(x)) signpkgs(x, password) except OSError as e: print("ERR: %s" % e, file=sys.stderr) exit(1)