Exemplo n.º 1
0
def update_package():

    import requests,BeautifulSoup
    page = requests.get('http://www.piriform.com/ccleaner/download/standard',headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'}).text
    bs = BeautifulSoup.BeautifulSoup(page)
    download = bs.find(id="BigDownload").a["href"]
    filename = download.rsplit('/',1)[1]

    # on ne telecharge que si on ne l'a pas deja
    if not isfile(filename):
        wget(download, filename)
    else:
        print(u'Le setup %s est déjà présent dans le paquet.' % filename)

    # on enleve les vieux exes
    for fn in glob.glob('*.exe'):
        if fn != filename :
            print('Suppression du vieux exe %s'%fn)
            remove_file(fn)

    vers = get_file_properties(filename)['ProductVersion']
    os.chdir(os.path.dirname(__file__))
    from waptpackage import PackageEntry
    pe = PackageEntry()
    pe.load_control_from_wapt(os.getcwd())
    pe.version = vers + '-0'
    pe.save_control_to_wapt(os.getcwd())
Exemplo n.º 2
0
def get_packages_filenames(packages,
                           with_depends=True,
                           waptconfigfile=None,
                           repo_name='wapt-templates',
                           remoterepo=None):
    """Returns list of package filenames (latest version) and md5 matching comma separated list of packages names and their dependencies
    helps to batch download a list of selected packages using tools like curl or wget

    Args:
        packages (list): list of package entries as dicts or PackageEntry
        with_depends (bool): get recursively the all depends filenames
        waptconfigfile (str): path to wapt ini file
        repo_name : section name in wapt ini file for repo parameters (repo_url, http_proxy, timeout, verify_cert)
        remoterepo (WaptRemoteRepo) : remote repo to query.
                                      Mutually exclusive with waptconfigfile and repo_name
    Returns:
        list: list of (wapt file basename,md5)

    >>> get_packages_filenames(r"c:\users\htouvet\AppData\Local\waptconsole\waptconsole.ini","tis-firefox-esr,tis-flash,tis-wapttest")
    [u'tis-firefox-esr_24.4.0-0_all.wapt', u'tis-flash_12.0.0.77-3_all.wapt', u'tis-wapttest.wapt', u'tis-wapttestsub_0.1.0-1_all.wapt', u'tis-7zip_9.2.0-15_all.wapt']
    """
    result = []
    defaults = {
        'repo_url': 'https://store.wapt.fr/wapt',
        'http_proxy': '',
        'verify_cert': '0',
    }

    if remoterepo is None:
        config = RawConfigParser(defaults=defaults)
        config.read(waptconfigfile)

        remoterepo = WaptRemoteRepo(name=repo_name, config=config)
        remoterepo.update()

    for pe in packages:
        if not isinstance(pe, PackageEntry):
            pe = PackageEntry(**pe)

        result.append((
            pe.filename,
            pe.md5sum,
        ))
        if with_depends and pe.depends:
            depends_list = []
            for depname in ensure_list(pe.depends):
                pe_dep = remoterepo.packages_matching(depname)
                if pe_dep:
                    depends_list.append(pe_dep[-1])
            for (fn, md5) in get_packages_filenames(depends_list,
                                                    remoterepo=remoterepo):
                if not fn in result:
                    result.append((
                        fn,
                        md5,
                    ))
    return result
Exemplo n.º 3
0
def update_sources():

    import urllib2
    import requests

    """I download the file >>>"""

    sock = urllib2.urlopen("http://www.7-zip.org/download.html",timeout=10)
    htmlSource = sock.readlines()
    sock.close()
    for line in htmlSource :
        if  'x64.msi'  in line :
            start = line.find('href=')
            end = line.find('">D')
            x64 = line[start + 6:end]
            x86 = x64.replace("-x64.msi",".msi")
            break

    allmsi = glob.glob('*.msi')
    for msi in allmsi:
        remove_file(msi)

    wget('http://www.7-zip.org/%s' % x64, x64.rsplit('/')[-1])
    wget('http://www.7-zip.org/%s' % x86, x86.rsplit('/')[-1])



    """I retrieve the current version from the msi """

    allmsi = glob.glob('*.msi')
    for msi in allmsi:
        vers = get_msi_properties(msi)['ProductVersion']
        break

    """I write the version in the control file >>>"""

    os.chdir(os.path.dirname(__file__))

    from waptpackage import PackageEntry
    pe = PackageEntry()
    pe.load_control_from_wapt(os.getcwd())

    pe.version = vers + '-0'
    pe.save_control_to_wapt(os.getcwd())
Exemplo n.º 4
0
        ]

    def match(fn, filenames):
        # return True if fn matches one of the pattern in filenames
        if filenames is None:
            return True
        else:
            for pattern in filenames:
                if glob.fnmatch.fnmatch(fn, pattern):
                    return True
            return False

    if filenames is not None:
        files = [fn for fn in zipf.namelist() if match(fn, filenames)]
        zipf.extractall(target, members=files)
    else:
        files = zipf.namelist()
        zipf.extractall(target)

    return [makepath(target, fn.replace('/', os.sep)) for fn in files]


CalledProcessError = subprocess.CalledProcessError

# Specific parameters for install scripts
params = {}
control = PackageEntry()

if __name__ == '__main__':
    pass
Exemplo n.º 5
0
        print("Pas de suppr")
    if len(adddepend)<=0:
        print("Pas d'ajout")

	#Configuration
	certpub="/root/mykey.crt"
	privatekey="/root/mykey.pem"
	passwordkey='password'

	#definition de variable pour la clé privé
	ca_bundle = SSLCABundle()
	signers_bundle = SSLCABundle()
	signers_bundle.add_certificates_from_pem(pem_filename=certpub)
	key = SSLPrivateKey(privatekey)
	#selection de l'ordinateur à modifier
	pe = PackageEntry(waptfile = "/var/www/html/wapt-host/%s.wapt" % uuid_machine)

	depends = pe.depends.split(',')
	conflicts = pe.conflicts.split(',')
	if len(adddepend)>0:
		for dep in adddepend:
			if dep in pe.conflicts:
				conflicts.remove(dep)
		    if not dep in depends:
		        depends.append(dep)

	if not len(removedepend)<=0:
		for dep in removedepend:
			if not dep in pe.conflicts:
				conflicts.append(dep)
		    if dep in depends:
Exemplo n.º 6
0
def build_waptupgrade_package(waptconfigfile,
                              target_directory=None,
                              wapt_server_user=None,
                              wapt_server_passwd=None,
                              key_password=None,
                              sign_digests=None):
    if target_directory is None:
        target_directory = tempfile.gettempdir()

    if not wapt_server_user:
        wapt_server_user = raw_input('WAPT Server user :'******'WAPT Server password :'******'ascii')

    wapt = common.Wapt(config_filename=waptconfigfile,
                       disable_update_server_status=True)
    wapt.dbpath = r':memory:'
    wapt.use_hostpackages = False

    # try to get a progress hook inside waptconsole
    try:
        import waptconsole
        progress_hook = waptconsole.UpdateProgress
    except ImportError:

        def print_progress(show=False, n=0, max=100, msg=''):
            if show:
                print('%s %s/%s\r' % (msg, n, max), end='')
            else:
                if not msg:
                    msg = 'Done'
                print("%s%s" % (msg, ' ' * (80 - len(msg))))

        progress_hook = print_progress

    wapt.progress_hook = progress_hook

    if sign_digests is None:
        sign_digests = wapt.sign_digests

    if not wapt.personal_certificate_path or not os.path.isfile(
            wapt.personal_certificate_path):
        raise Exception(
            u'No personal certificate provided or not found (%s) for signing waptupgrade package'
            % wapt.personal_certificate_path)

    waptget = get_file_properties('wapt-get.exe')
    entry = PackageEntry(waptfile=makepath(wapt.wapt_base_dir, 'waptupgrade'))
    patchs_dir = makepath(entry.sourcespath, 'patchs')
    mkdirs(patchs_dir)
    filecopyto(makepath(wapt.wapt_base_dir, 'waptdeploy.exe'),
               makepath(patchs_dir, 'waptdeploy.exe'))

    entry.package = '%s-waptupgrade' % wapt.config.get(
        'global', 'default_package_prefix')
    rev = entry.version.split('-')[1]
    entry.version = '%s-%s' % (waptget['FileVersion'], rev)
    entry.inc_build()
    entry.save_control_to_wapt()
    entry.build_package(target_directory=target_directory)
    certs = wapt.personal_certificate()
    key = wapt.private_key(private_key_password=key_password)
    if not certs[0].is_code_signing:
        raise Exception(u'%s is not a code signing certificate' %
                        wapt.personal_certificate_path)
    entry.sign_package(private_key=key,
                       certificate=certs,
                       private_key_password=key_password,
                       mds=ensure_list(sign_digests))

    wapt.http_upload_package(entry.localpath,
                             wapt_server_user=wapt_server_user,
                             wapt_server_passwd=wapt_server_passwd,
                             progress_hook=progress_hook)
    return entry.as_dict()
Exemplo n.º 7
0
def edit_hosts_depends(
    waptconfigfile,
    hosts_list,
    append_depends=[],
    remove_depends=[],
    append_conflicts=[],
    remove_conflicts=[],
    sign_certs=None,
    sign_key=None,
    key_password=None,
    wapt_server_user=None,
    wapt_server_passwd=None,
    cabundle=None,
):
    """Add or remove packages from host packages

    Args:

    Returns:
        dict: { updated: of uuid of machines actually updated
                unchanged : list of uuid skipped because of no change needed
                discarded : list of uuid discarded due to errors}

    >>> edit_hosts_depends('c:/wapt/wapt-get.ini','htlaptop.tranquilit.local','toto','tis-7zip','admin','password')
    """
    if sign_certs is None:
        sign_bundle_fn = inifile_readstring(waptconfigfile, u'global',
                                            u'personal_certificate_path')
        sign_bundle = SSLCABundle(sign_bundle_fn)
        sign_certs = sign_bundle.certificates()
        # we assume a unique signer.
        if cabundle is None:
            cabundle = sign_bundle

    if not sign_certs:
        raise Exception(u'No personal signer certificate found in %s' %
                        sign_bundle_fn)

    try:
        import waptconsole
        progress_hook = waptconsole.UpdateProgress
        private_key_password_callback = waptconsole.GetPrivateKeyPassword
    except ImportError as e:

        def print_progress(show=False, n=0, max=100, msg=''):
            if show:
                print('%s %s/%s\r' % (msg, n, max), end='')
            else:
                if not msg:
                    msg = 'Done'
                print("%s%s" % (msg, ' ' * (80 - len(msg))))

        progress_hook = print_progress
        private_key_password_callback = None

    if sign_key is None:
        sign_key = sign_certs[0].matching_key_in_dirs(
            private_key_password=key_password,
            password_callback=private_key_password_callback)

    progress_hook(True, 0, len(hosts_list),
                  'Loading %s hosts packages' % len(hosts_list))

    host_repo = WaptHostRepo(name='wapt-host',
                             host_id=[h['uuid'] for h in hosts_list],
                             cabundle=cabundle)
    host_repo.private_key_password_callback = private_key_password_callback
    host_repo.load_config_from_file(waptconfigfile)
    total_hosts = len(host_repo.packages())
    discarded_uuids = [p.package for p in host_repo.discarded]

    append_depends = ensure_list(append_depends)
    remove_depends = ensure_list(remove_depends)
    append_conflicts = ensure_list(append_conflicts)
    remove_conflicts = ensure_list(remove_conflicts)

    packages = []
    discarded = []
    unchanged = []

    progress_hook(True, 0, len(hosts_list),
                  'Editing %s hosts' % len(hosts_list))
    i = 0
    try:
        for hostrec in hosts_list:
            host_id = hostrec['uuid']
            i += 1
            # don't change discarded packages.
            if host_id in discarded_uuids:
                discarded.append(host_id)
            else:
                host = host_repo.get(host_id)
                if host is None:
                    host = PackageEntry(package=host_id, section='host')

                if progress_hook(True, i, len(hosts_list),
                                 'Editing %s' % host.package):
                    break

                logger.debug(u'Edit host %s : +%s -%s' %
                             (host.package, append_depends, remove_depends))

                depends = host.depends
                conflicts = host.conflicts

                conflicts = add_to_csv_list(conflicts, append_conflicts)
                conflicts = remove_from_csv_list(conflicts, remove_conflicts)
                depends = remove_from_csv_list(depends, ensure_list(conflicts))

                depends = add_to_csv_list(depends, append_depends)
                depends = remove_from_csv_list(depends, remove_depends)
                conflicts = remove_from_csv_list(conflicts,
                                                 ensure_list(depends))

                if depends != host.depends or conflicts != host.conflicts:
                    host.depends = depends
                    host.conflicts = conflicts
                    host.inc_build()
                    host_file = host.build_management_package()
                    host.sign_package(sign_certs, sign_key)
                    packages.append(host)
                else:
                    unchanged.append(host.package)

        # upload all in one step...
        progress_hook(True, 3, 3, 'Upload %s host packages' % len(packages))
        server = WaptServer().load_config_from_file(waptconfigfile)
        server.private_key_password_callback = private_key_password_callback
        server.upload_packages(packages,
                               auth=(wapt_server_user, wapt_server_passwd),
                               progress_hook=progress_hook)

        return dict(updated=[p.package for p in packages],
                    discarded=discarded,
                    unchanged=unchanged)

    finally:
        logger.debug('Cleanup')
        try:
            i = 0
            for s in packages:
                i += 1
                progress_hook(True, i, len(packages), 'Cleanup')
                if os.path.isfile(s.localpath):
                    os.remove(s.localpath)
            progress_hook(False)
        except WindowsError as e:
            logger.critical('Unable to remove temporary directory %s: %s' %
                            (s, repr(e)))
            progress_hook(False)
Exemplo n.º 8
0
def duplicate_from_file(package_filename,
                        new_prefix='test',
                        target_directory=None,
                        authorized_certs=None,
                        set_maturity=None):
    r"""Duplicate a downloaded package to match prefix defined in waptconfigfile
    renames all dependencies

    Returns:
        str: source directory

    >>> from common import Wapt
    >>> wapt = Wapt(config_filename = r'C:\Users\htouvet\AppData\Local\waptconsole\waptconsole.ini')
    >>> sources = duplicate_from_external_repo(wapt.config_filename,r'C:\tranquilit\wapt\tests\packages\tis-wapttest.wapt')
    >>> res = wapt.build_upload(sources,wapt_server_user='******',wapt_server_passwd='password')
    >>> res[0]['package'].depends
    u'test-wapttestsub,test-7zip'
    """
    def rename_package(oldname, prefix):
        sp = oldname.split('-', 1)
        if len(sp) == 2:
            return "%s-%s" % (prefix, sp[-1])
        else:
            return oldname

    source_package = PackageEntry(waptfile=package_filename)
    # authorized_certs is a directoyr instead a list of certificates.
    if authorized_certs is not None and authorized_certs != '' and not isinstance(
            authorized_certs, list):
        bundle = SSLCABundle()
        bundle.add_pems(makepath(authorized_certs, '*.crt'), trust_first=True)
        bundle.add_pems(makepath(authorized_certs, '*.cer'), trust_first=True)
        bundle.add_pems(makepath(authorized_certs, '*.pem'), trust_all=True)
    else:
        bundle = authorized_certs or None

    source_package.unzip_package(target_dir=target_directory, cabundle=bundle)
    source_package.invalidate_signature()

    package = PackageEntry(waptfile=source_package.sourcespath)
    oldname = source_package.package
    package.package = rename_package(oldname, new_prefix)
    package.inc_build()
    if set_maturity is not None:
        package.maturity = set_maturity

    result = package['sourcespath']

    # renames dependencies
    if package.depends:
        newdepends = []
        depends = ensure_list(package.depends)
        for dependname in depends:
            newname = rename_package(dependname, new_prefix)
            newdepends.append(newname)

        package.depends = ','.join(newdepends)

    # renames conflicts
    if package.conflicts:
        newconflicts = []
        conflicts = ensure_list(package.conflicts)
        for dependname in conflicts:
            newname = rename_package(dependname, new_prefix)
            newconflicts.append(newname)

        package.conflicts = ','.join(newconflicts)

    package.save_control_to_wapt()
    return result
Exemplo n.º 9
0
packagedir = 'c:/tranquilit/big-wapt'
os.chdir(packagedir)
for i in range(0, 80):
    root = os.path.join(packagedir, "files", "directory_%i_%s" % (i, "to" * i))
    if not os.path.isdir(root):
        os.makedirs(root)
    for j in range(0, 1000):
        fn = 'fichiers_%i' % j
        if not os.path.isfile(os.path.join(root, fn)):
            open(os.path.join(root, fn), 'wb').write(fn)

if not os.path.isdir(os.path.join(packagedir, 'WAPT')):
    os.makedirs(os.path.join(packagedir, 'WAPT'))

pe = PackageEntry('big')
pe.save_control_to_wapt(packagedir)

setup = """\
from setuphelpers import *

uninstallkey = []

dest = makepath(programfiles,'big')

def install():
    copytree2('files',dest)

"""

open(os.path.join(packagedir, 'setup.py'), 'wb').write(setup)
Exemplo n.º 10
0
def main():
    parser=OptionParser(usage=__doc__,prog = 'wapt-signpackage')
    parser.add_option("-c","--certificate", dest="public_key", default='', help="Path to the PEM RSA certificate to embed identitiy in control. (default: %default)")
    parser.add_option("-k","--private-key", dest="private_key", default='', help="Path to the PEM RSA private key to sign packages.  (default: %default)")
    #parser.add_option("-w","--private-key-passwd", dest="private_key_passwd", default='', help="Path to the password of the private key. (default: %default)")
    parser.add_option("-l","--loglevel", dest="loglevel", default=None, type='choice',  choices=['debug','warning','info','error','critical'], metavar='LOGLEVEL',help="Loglevel (default: warning)")
    parser.add_option("-m","--message-digest", dest="md", default='sha256', help="Message digest type for signatures.  (default: %default)")
    parser.add_option("-s","--scan-packages", dest="doscan", default=False, action='store_true', help="Rescan packages and update local Packages index after signing.  (default: %default)")
    parser.add_option("-r","--remove-setup", dest="removesetup", default=False, action='store_true', help="Remove setup.py.  (default: %default)")
    parser.add_option("-i","--inc-release",    dest="increlease",    default=False, action='store_true', help="Increase release number when building package (default: %default)")
    parser.add_option("--maturity", dest="set_maturity", default=None, help="Set/change package maturity when signing package.  (default: None)")
    parser.add_option(     "--keep-signature-date", dest="keep_signature_date",default=False, action='store_true', help="Keep the current package signature date, and file changetime (default: %default)")
    parser.add_option(     "--if-needed", dest="if_needed", default=False, action='store_true',help="Re-sign package only if needed (default: warning)")
    (options,args) = parser.parse_args()

    loglevel = options.loglevel

    if len(logger.handlers) < 1:
        hdlr = logging.StreamHandler(sys.stderr)
        hdlr.setFormatter(logging.Formatter(
            u'%(asctime)s %(levelname)s %(message)s'))
        logger.addHandler(hdlr)

    if loglevel:
        setloglevel(logger,loglevel)
    else:
        setloglevel(logger,'warning')

    if len(args) < 1:
        print(parser.usage)
        sys.exit(1)

    if not options.public_key and not options.private_key:
        print('ERROR: No certificate found or specified')
        sys.exit(1)

    if options.private_key and os.path.isfile(options.private_key):
        key = SSLPrivateKey(options.private_key)
    else:
        cert = SSLCertificate(options.public_key or options.private_key)
        key = cert.matching_key_in_dirs()

    if not key:
        print('ERROR: No private key found or specified')
        sys.exit(1)

    args = ensure_list(args)

    ca_bundle = SSLCABundle()
    signers_bundle = SSLCABundle()
    signers_bundle.add_certificates_from_pem(pem_filename=options.public_key)

    waptpackages = []
    for arg in args:
        waptpackages.extend(glob.glob(arg))

    errors = []
    package_dirs = []
    for waptpackage in waptpackages:
        package_dir = os.path.abspath(os.path.dirname(waptpackage))
        if not package_dir in package_dirs:
            package_dirs.append(package_dir)

        print('Processing %s'%waptpackage)
        try:
            sign_needed=False
            pe = PackageEntry(waptfile = waptpackage)
            if options.removesetup:
                if pe.has_file('setup.py'):
                    with pe.as_zipfile(mode='a') as waptfile:
                        waptfile.remove('setup.py')
                    sign_needed=True

            if not sign_needed and options.if_needed:
                try:
                    pe.check_control_signature(trusted_bundle=signers_bundle,signers_bundle=signers_bundle)
                    for md in ensure_list(options.md):
                        if not pe.has_file(pe.get_signature_filename(md)):
                            raise Exception('Missing signature for md %s' % md)
                    logger.info('Skipping %s, already signed properly' % pe.asrequirement())
                    sign_needed = False
                except Exception as e:
                    logger.info('Sign is needed for %s because %s' % (pe.asrequirement(),e))
                    sign_needed = True

            if options.increlease:
                pe.inc_build()
                sign_needed = True

            if options.set_maturity is not None and pe.maturity != options.set_maturity:
                pe.maturity = options.set_maturity
                sign_needed = True

            if not options.if_needed or sign_needed:
                pe.sign_package(private_key=key,certificate = signers_bundle.certificates(),mds = ensure_list(options.md),keep_signature_date=options.keep_signature_date)
                newfn = pe.make_package_filename()
                if newfn != pe.filename:
                    newfn_path = os.path.join(package_dir,newfn)
                    if not os.path.isfile(newfn_path):
                        print(u"Renaming file from %s to %s to match new package's properties" % (pe.filename,newfn))
                        os.rename(os.path.join(package_dir,pe.filename),newfn_path)
                    else:
                        print('WARNING: unable to rename file from %s to %s because target already exists' % (pe.filename,newfn))

            print('Done')
        except Exception as e:
            print(u'Error: %s'%ensure_unicode(e.message))
            errors.append([waptpackage,repr(e)])

    if options.doscan:
        for package_dir in package_dirs:
            if os.path.isfile(os.path.join(package_dir,'Packages')):
                print(u'Launching the update of Packages index in %s ...'% ensure_unicode(package_dir))
                repo = WaptLocalRepo(package_dir)
                repo.update_packages_index()
                print('Done')
    else:
        print("Don't forget to rescan your repository with wapt-scanpackages %s" % os.path.dirname(waptpackages[0]))

    if errors:
        print('Package not processed properly: ')
        for fn,error in errors:
            print(u'%s : %s' % (fn,error))

        sys.exit(1)
    else:
        sys.exit(0)
Exemplo n.º 11
0
                        sys.exit(1)

            elif action=='upload-package':
                if len(args)<2:
                    print u"You must provide a package to upload"
                    sys.exit(1)
                waptfiles = []
                for a in args[1:]:
                    waptfiles += glob.glob(a)

                # groups by www target : wapt or wapt-host
                hosts = ('wapt-host',[])
                others = ('wapt',[])
                # split by destination
                for w in waptfiles:
                    p = PackageEntry()
                    p.load_control_from_wapt(w)
                    if p.section == 'host':
                        hosts[1].append(w)
                    else:
                        others[1].append(w)

                for package_group in (hosts,others):
                    if package_group[1]:
                        # add quotes for command line
                        files_list = ['"%s"' % f for f in package_group[1]]
                        cmd_dict =  {'waptfile': files_list,'waptdir':package_group[0]}

                        print mywapt.upload_package(cmd_dict)
                        if package_group != hosts:
                            if mywapt.after_upload:
Exemplo n.º 12
0
def edit_hosts_depends(waptconfigfile,hosts_list,
        append_depends=[],
        remove_depends=[],
        append_conflicts=[],
        remove_conflicts=[],
        sign_certs=None,
        sign_key=None,
        key_password=None,
        wapt_server_user=None,wapt_server_passwd=None,
        cabundle = None,
        ):
    """Add or remove packages from host packages

    Args:

    Returns:
        dict: { updated: of uuid of machines actually updated
                unchanged : list of uuid skipped because of no change needed
                discarded : list of uuid discarded due to errors}

    >>> edit_hosts_depends('c:/wapt/wapt-get.ini','htlaptop.tranquilit.local','toto','tis-7zip','admin','password')
    """
    if sign_certs is None:
        sign_bundle_fn = inifile_readstring(waptconfigfile,u'global',u'personal_certificate_path')
        sign_bundle = SSLCABundle(sign_bundle_fn)
        sign_certs = sign_bundle.certificates()
        # we assume a unique signer.
        if cabundle is None:
            cabundle = sign_bundle

    if not sign_certs:
        raise Exception(u'No personal signer certificate found in %s' % sign_bundle_fn)

    if sign_key is None:
        sign_key = sign_certs[0].matching_key_in_dirs(private_key_password=key_password)

    try:
        import waptconsole
        progress_hook = waptconsole.UpdateProgress
    except ImportError as e:
        def print_progress(show=False,n=0,max=100,msg=''):
            if show:
                print('%s %s/%s\r' % (msg,n,max),end='')
            else:
                if not msg:
                    msg='Done'
                print("%s%s"%(msg,' '*(80-len(msg))))
        progress_hook = print_progress

    hosts_list = ensure_list(hosts_list)

    progress_hook(True,0,len(hosts_list),'Loading %s hosts packages' % len(hosts_list))

    host_repo = WaptHostRepo(name='wapt-host',host_id=hosts_list,cabundle = cabundle)
    host_repo.load_config_from_file(waptconfigfile)
    total_hosts = len(host_repo.packages())
    discarded_uuids = [p.package for p in host_repo.discarded]

    hosts_list = ensure_list(hosts_list)
    append_depends = ensure_list(append_depends)
    remove_depends = ensure_list(remove_depends)
    append_conflicts = ensure_list(append_conflicts)
    remove_conflicts = ensure_list(remove_conflicts)

    packages = []
    discarded = []
    unchanged = []

    progress_hook(True,0,len(hosts_list),'Editing %s hosts' % len(hosts_list))
    i = 0
    try:
        for host_id in hosts_list:
            i+=1
            # don't change discarded packages.
            if host_id in discarded_uuids:
                discarded.append(host_id)
            else:
                host = host_repo.get(host_id)
                if host is None:
                    host = PackageEntry(package=host_id,section='host')

                if progress_hook(True,i,len(hosts_list),'Editing %s' % host.package):
                    break

                logger.debug(u'Edit host %s : +%s -%s'%(
                    host.package,
                    append_depends,
                    remove_depends))


                depends = host.depends
                conflicts = host.conflicts

                conflicts = add_to_csv_list(conflicts,append_conflicts)
                conflicts = remove_from_csv_list(conflicts,remove_conflicts)
                depends = remove_from_csv_list(depends,ensure_list(conflicts))

                depends = add_to_csv_list(depends,append_depends)
                depends = remove_from_csv_list(depends,remove_depends)
                conflicts = remove_from_csv_list(conflicts,ensure_list(depends))

                if depends != host.depends or conflicts != host.conflicts:
                    host.depends = depends
                    host.conflicts = conflicts
                    host.inc_build()
                    host_file = host.build_management_package()
                    host.sign_package(sign_certs,sign_key)
                    packages.append(host)
                else:
                    unchanged.append(host.package)

        # upload all in one step...
        progress_hook(True,3,3,'Upload %s host packages' % len(packages))
        server = WaptServer().load_config_from_file(waptconfigfile)
        server.upload_packages(packages,auth=(wapt_server_user,wapt_server_passwd),progress_hook=progress_hook)
        return dict(updated = [p.package for p in packages],
                    discarded = discarded,
                    unchanged = unchanged)

    finally:
        logger.debug('Cleanup')
        try:
            i = 0
            for s in packages:
                i+=1
                progress_hook(True,i,len(packages),'Cleanup')
                if os.path.isfile(s.localpath):
                    os.remove(s.localpath)
            progress_hook(False)
        except WindowsError as e:
            logger.critical('Unable to remove temporary directory %s: %s'% (s,repr(e)))
            progress_hook(False)
Exemplo n.º 13
0
def build_waptupgrade_package(waptconfigfile,target_directory=None,wapt_server_user=None,wapt_server_passwd=None,key_password=None,sign_digests=None,priority='critical'):
    if target_directory is None:
        target_directory = tempfile.gettempdir()

    if not wapt_server_user:
        wapt_server_user = raw_input('WAPT Server user :'******'WAPT Server password :'******'ascii')

    wapt = common.Wapt(config_filename=waptconfigfile,disable_update_server_status=True)
    wapt.dbpath = r':memory:'
    wapt.use_hostpackages = False

    # try to get a progress hook inside waptconsole
    try:
        import waptconsole
        progress_hook = waptconsole.UpdateProgress
    except ImportError:
        def print_progress(show=False,n=0,max=100,msg=''):
            if show:
                print('%s %s/%s\r' % (msg,n,max),end='')
            else:
                if not msg:
                    msg='Done'
                print("%s%s"%(msg,' '*(80-len(msg))))
        progress_hook = print_progress

    wapt.progress_hook = progress_hook

    if sign_digests is None:
        sign_digests = wapt.sign_digests

    if not wapt.personal_certificate_path or not os.path.isfile(wapt.personal_certificate_path):
        raise Exception(u'No personal certificate provided or not found (%s) for signing waptupgrade package' % wapt.personal_certificate_path)

    waptget = get_file_properties('wapt-get.exe')
    entry = PackageEntry(waptfile = makepath(wapt.wapt_base_dir,'waptupgrade'))
    patchs_dir = makepath(entry.sourcespath,'patchs')
    mkdirs(patchs_dir)
    filecopyto(makepath(wapt.wapt_base_dir,'waptdeploy.exe'),makepath(patchs_dir,'waptdeploy.exe'))

    entry.package = '%s-waptupgrade' % wapt.config.get('global','default_package_prefix')
    rev = entry.version.split('-')[1]
    entry.version = '%s-%s' % (waptget['FileVersion'],rev)
    entry.inc_build()
    entry.save_control_to_wapt()
    entry.build_package(target_directory=target_directory)
    entry.priority = priority
    certs = wapt.personal_certificate()
    key = wapt.private_key(private_key_password=key_password)
    if not certs[0].is_code_signing:
        raise Exception(u'%s is not a code signing certificate' % wapt.personal_certificate_path)
    entry.sign_package(private_key=key,certificate = certs,private_key_password=key_password,mds = ensure_list(sign_digests))

    wapt.http_upload_package(entry.localpath,wapt_server_user=wapt_server_user,wapt_server_passwd=wapt_server_passwd,progress_hook=progress_hook)
    return entry.as_dict()
Exemplo n.º 14
0
def duplicate_from_file(package_filename,new_prefix='test',target_directory=None,authorized_certs=None,set_maturity=None):
    r"""Duplicate a downloaded package to match prefix defined in waptconfigfile
    renames all dependencies

    Returns:
        str: source directory

    >>> from common import Wapt
    >>> wapt = Wapt(config_filename = r'C:\Users\htouvet\AppData\Local\waptconsole\waptconsole.ini')
    >>> sources = duplicate_from_external_repo(wapt.config_filename,r'C:\tranquilit\wapt\tests\packages\tis-wapttest.wapt')
    >>> res = wapt.build_upload(sources,wapt_server_user='******',wapt_server_passwd='password')
    >>> res[0]['package'].depends
    u'test-wapttestsub,test-7zip'
    """
    def rename_package(oldname,prefix):
        sp = oldname.split('-',1)
        if len(sp) == 2:
            return "%s-%s" % (prefix,sp[-1])
        else:
            return oldname

    source_package = PackageEntry(waptfile = package_filename)
    # authorized_certs is a directoyr instead a list of certificates.
    if authorized_certs is not None and authorized_certs != '' and not isinstance(authorized_certs,list):
        bundle = SSLCABundle()
        bundle.add_pems(makepath(authorized_certs,'*.crt'))
        bundle.add_pems(makepath(authorized_certs,'*.cer'))
        bundle.add_pems(makepath(authorized_certs,'*.pem'))
    else:
        bundle = authorized_certs or None

    source_package.unzip_package(target_dir=target_directory,cabundle=bundle)
    source_package.invalidate_signature()

    package = PackageEntry(waptfile = source_package.sourcespath)
    oldname = source_package.package
    package.package = rename_package(oldname,new_prefix)
    package.inc_build()
    if set_maturity is not None:
        package.maturity = set_maturity

    result = package['sourcespath']

    # renames dependencies
    if package.depends:
        newdepends = []
        depends = ensure_list(package.depends)
        for dependname in depends:
            newname = rename_package(dependname,new_prefix)
            newdepends.append(newname)

        package.depends = ','.join(newdepends)

    # renames conflicts
    if package.conflicts:
        newconflicts = []
        conflicts = ensure_list(package.conflicts)
        for dependname in conflicts:
            newname = rename_package(dependname,new_prefix)
            newconflicts.append(newname)

        package.conflicts = ','.join(newconflicts)

    package.save_control_to_wapt()
    return result
Exemplo n.º 15
0
 def add_package(lines):
     package = PackageEntry()
     package.load_control_from_wapt(lines)
     package.filename = package.make_package_filename()
     packages.append(package)
Exemplo n.º 16
0
def get_packages_filenames(packages,
                           with_depends=True,
                           waptconfigfile=None,
                           repo_name='wapt-templates',
                           remoterepo=None,
                           package_request=None,
                           privaterepo=None,
                           local_prefix=None):
    """Returns list of package filenames (latest version) and md5 matching comma separated list of packages names and their dependencies
    helps to batch download a list of selected packages using tools like curl or wget

    Args:
        packages (list): list of package entries as dicts or PackageEntry
        with_depends (bool): get recursively the all depends filenames
        waptconfigfile (str): path to wapt ini file
        repo_name : section name in wapt ini file for repo parameters (repo_url, http_proxy, timeout, verify_cert)
        remoterepo (WaptRemoteRepo) : remote repo to query.
                                      Mutually exclusive with waptconfigfile and repo_name
        privaterepo (WaptRemoteRepo) : local private repo where to check if dependencies already exists
    Returns:
        list: list of (wapt file basename,md5)

    >>> get_packages_filenames(r"c:\users\htouvet\AppData\Local\waptconsole\waptconsole.ini","tis-firefox-esr,tis-flash,tis-wapttest")
    [u'tis-firefox-esr_24.4.0-0_all.wapt', u'tis-flash_12.0.0.77-3_all.wapt', u'tis-wapttest.wapt', u'tis-wapttestsub_0.1.0-1_all.wapt', u'tis-7zip_9.2.0-15_all.wapt']
    """
    result = []
    defaults = {
        'repo_url': 'https://store.wapt.fr/wapt',
        'http_proxy': '',
        'verify_cert': '0',
    }

    try:
        import waptconsole
        progress_hook = waptconsole.UpdateProgress
        private_key_password_callback = waptconsole.GetPrivateKeyPassword
    except ImportError as e:

        def print_progress(show=False, n=0, max=100, msg=''):
            if show:
                print('%s %s/%s\r' % (msg, n, max), end='')
            else:
                if not msg:
                    msg = 'Done'
                print("%s%s" % (msg, ' ' * (80 - len(msg))))

        progress_hook = print_progress
        private_key_password_callback = None

    if remoterepo is None:
        config = RawConfigParser(defaults=defaults)
        config.read(waptconfigfile)

        remoterepo = WaptRemoteRepo(name=repo_name, config=config)
        remoterepo.private_key_password_callback = private_key_password_callback
        remoterepo.update()

    if privaterepo is None and waptconfigfile:
        config = RawConfigParser(defaults=defaults)
        config.read(waptconfigfile)

        privaterepo = WaptRemoteRepo(name='wapt', config=config)
        privaterepo.private_key_password_callback = private_key_password_callback
        privaterepo.update()

    if package_request is not None and isinstance(package_request, dict):
        package_request = PackageRequest(**package_request)

    for pe in packages:
        if not isinstance(pe, PackageEntry):
            pe = PackageEntry(**pe)

        # propagate capa to parent package
        if package_request is None:
            request_filter = PackageRequest()
            if pe.locale != 'all' and pe.locale:
                request_filter.locales = [pe.locale]
            if pe.architecture != 'all' and pe.architecture:
                request_filter.architectures = [pe.architecture]
            if pe.min_os_version:
                request_filter.min_os_version = pe.min_os_version
            if pe.max_os_version:
                request_filter.max_os_version = pe.max_os_version
        else:
            request_filter = package_request

        if pe.filename:
            result.append((
                pe.filename,
                pe.md5sum,
            ))
            if with_depends and pe.depends:
                depends_list = []
                for depname in ensure_list(pe.depends):
                    request_filter.request = depname
                    pe_dep = remoterepo.packages_matching(request_filter)

                    if privaterepo and local_prefix:
                        request_filter.request = change_prefix(
                            depname, local_prefix)
                        pe_local = privaterepo.packages_matching(
                            request_filter)
                    else:
                        pe_local = None

                    if pe_dep and not pe_local:
                        depends_list.append(pe_dep[-1])

                for (fn, md5) in get_packages_filenames(depends_list,
                                                        remoterepo=remoterepo):
                    if not (fn, md5) in result:
                        result.append((
                            fn,
                            md5,
                        ))
    return result
Exemplo n.º 17
0
packagedir = 'c:/tranquilit/big-wapt'
os.chdir(packagedir)
for i in range(0,80):
    root = os.path.join(packagedir,"files","directory_%i_%s" %( i, "to" * i))
    if not os.path.isdir(root):
        os.makedirs(root)
    for j in range(0,1000):
        fn = 'fichiers_%i' % j
        if not os.path.isfile(os.path.join(root,fn)):
            open(os.path.join(root,fn),'wb').write(fn)

if not os.path.isdir(os.path.join(packagedir,'WAPT')):
    os.makedirs(os.path.join(packagedir,'WAPT'))

pe = PackageEntry('big')
pe.save_control_to_wapt(packagedir)

setup = """\
from setuphelpers import *

uninstallkey = []

dest = makepath(programfiles,'big')

def install():
    copytree2('files',dest)

"""

open(os.path.join(packagedir,'setup.py'),'wb').write(setup)
Exemplo n.º 18
0
def build_waptupgrade_package(wapt,
                              mainrepo,
                              sources_directory=None,
                              target_directory=None,
                              wapt_server_user=None,
                              wapt_server_passwd=None,
                              key_password=None,
                              sign_digests=None,
                              priority='critical'):
    if target_directory is None:
        target_directory = tempfile.gettempdir()

    if not wapt_server_user:
        wapt_server_user = raw_input('WAPT Server user :'******'WAPT Server password :'******'ascii')

    if sign_digests is None:
        sign_digests = wapt.sign_digests

    if not wapt.personal_certificate_path or not os.path.isfile(
            wapt.personal_certificate_path):
        raise Exception(
            u'No personal certificate provided or not found (%s) for signing waptupgrade package'
            % wapt.personal_certificate_path)

    waptget = get_file_properties('wapt-get.exe')
    if sources_directory is None:
        sources_directory = makepath(wapt.wapt_base_dir, 'waptupgrade')
    entry = PackageEntry(waptfile=sources_directory)
    patchs_dir = makepath(entry.sourcespath, 'patchs')
    mkdirs(patchs_dir)
    filecopyto(makepath(wapt.wapt_base_dir, 'waptdeploy.exe'),
               makepath(patchs_dir, 'waptdeploy.exe'))
    mainrepo.update()
    entry.package = '%s-waptupgrade' % wapt.config.get(
        'global', 'default_package_prefix')
    existing = mainrepo.packages_matching(PackageRequest(entry.package))
    if existing:
        rev = sorted(existing)[-1].version.split('-')[1]
    else:
        rev = entry.version.split('-')[1]
    entry.version = '%s-%s' % (waptget['FileVersion'], rev)
    entry.inc_build()
    entry.priority = priority
    entry.save_control_to_wapt()
    entry.build_package(target_directory=target_directory)
    certs = wapt.personal_certificate()
    key = wapt.private_key(private_key_password=key_password)
    if not certs[0].is_code_signing:
        raise Exception(u'%s is not a code signing certificate' %
                        wapt.personal_certificate_path)
    entry.sign_package(private_key=key,
                       certificate=certs,
                       private_key_password=key_password,
                       mds=ensure_list(sign_digests))

    wapt.http_upload_package(entry.localpath,
                             wapt_server_user=wapt_server_user,
                             wapt_server_passwd=wapt_server_passwd,
                             progress_hook=wapt.progress_hook)
    mainrepo.update()
    return entry.as_dict()
Exemplo n.º 19
0
def main():
    parser=OptionParser(usage=__doc__,prog = 'wapt-signpackage')
    parser.add_option("-c","--certificate", dest="public_key", default='', help="Path to the PEM RSA certificate to embed identitiy in control. (default: %default)")
    parser.add_option("-k","--private-key", dest="private_key", default='', help="Path to the PEM RSA private key to sign packages.  (default: %default)")
    #parser.add_option("-w","--private-key-passwd", dest="private_key_passwd", default='', help="Path to the password of the private key. (default: %default)")
    parser.add_option("-l","--loglevel", dest="loglevel", default=None, type='choice',  choices=['debug','warning','info','error','critical'], metavar='LOGLEVEL',help="Loglevel (default: warning)")
    parser.add_option("-m","--message-digest", dest="md", default='sha256', help="Message digest type for signatures.  (default: %default)")
    parser.add_option("-s","--scan-packages", dest="doscan", default=False, action='store_true', help="Rescan packages and update local Packages index after signing.  (default: %default)")
    parser.add_option("-r","--remove-setup", dest="removesetup", default=False, action='store_true', help="Remove setup.py.  (default: %default)")
    parser.add_option("-i","--inc-release",    dest="increlease",    default=False, action='store_true', help="Increase release number when building package (default: %default)")
    parser.add_option("--maturity", dest="set_maturity", default=None, help="Set/change package maturity when signing package.  (default: None)")
    parser.add_option(     "--keep-signature-date", dest="keep_signature_date",default=False, action='store_true', help="Keep the current package signature date, and file changetime (default: %default)")
    parser.add_option(     "--if-needed", dest="if_needed", default=False, action='store_true',help="Re-sign package only if needed (default: warning)")
    (options,args) = parser.parse_args()

    loglevel = options.loglevel

    if len(logger.handlers) < 1:
        hdlr = logging.StreamHandler(sys.stderr)
        hdlr.setFormatter(logging.Formatter(
            u'%(asctime)s %(levelname)s %(message)s'))
        logger.addHandler(hdlr)

    if loglevel:
        setloglevel(logger,loglevel)
    else:
        setloglevel(logger,'warning')

    if len(args) < 1:
        print(parser.usage)
        sys.exit(1)

    if not options.public_key and not options.private_key:
        print('ERROR: No certificate found or specified')
        sys.exit(1)

    if options.private_key and os.path.isfile(options.private_key):
        key = SSLPrivateKey(options.private_key)
    else:
        cert = SSLCertificate(options.public_key or options.private_key)
        key = cert.matching_key_in_dirs()

    if not key:
        print('ERROR: No private key found or specified')
        sys.exit(1)

    args = ensure_list(args)

    ca_bundle = SSLCABundle()
    signers_bundle = SSLCABundle()
    signers_bundle.add_certificates_from_pem(pem_filename=options.public_key)

    waptpackages = []
    for arg in args:
        waptpackages.extend(glob.glob(arg))

    errors = []
    package_dirs = []
    for waptpackage in waptpackages:
        package_dir = os.path.abspath(os.path.dirname(waptpackage))
        if not package_dir in package_dirs:
            package_dirs.append(package_dir)

        print('Processing %s'%waptpackage)
        try:
            sign_needed=False
            pe = PackageEntry(waptfile = waptpackage)
            if options.removesetup:
                if pe.has_file('setup.py'):
                    with pe.as_zipfile(mode='a') as waptfile:
                        waptfile.remove('setup.py')
                    sign_needed=True

            if not sign_needed and options.if_needed:
                try:
                    pe.check_control_signature(trusted_bundle=signers_bundle,signers_bundle=signers_bundle)
                    for md in ensure_list(options.md):
                        if not pe.has_file(pe.get_signature_filename(md)):
                            raise Exception('Missing signature for md %s' % md)
                    logger.info('Skipping %s, already signed properly' % pe.asrequirement())
                    sign_needed = False
                except Exception as e:
                    logger.info('Sign is needed for %s because %s' % (pe.asrequirement(),e))
                    sign_needed = True

            if options.increlease:
                pe.inc_build()
                sign_needed = True

            if options.set_maturity is not None and pe.maturity != options.set_maturity:
                pe.maturity = options.set_maturity
                sign_needed = True

            if not options.if_needed or sign_needed:
                pe.sign_package(private_key=key,certificate = signers_bundle.certificates(),mds = ensure_list(options.md),keep_signature_date=options.keep_signature_date)
                newfn = pe.make_package_filename()
                if newfn != pe.filename:
                    newfn_path = os.path.join(package_dir,newfn)
                    if not os.path.isfile(newfn_path):
                        print(u"Renaming file from %s to %s to match new package's properties" % (pe.filename,newfn))
                        shutil.move(os.path.join(package_dir,pe.filename),newfn_path)
                    else:
                        print('WARNING: unable to rename file from %s to %s because target already exists' % (pe.filename,newfn))

            print('Done')
        except Exception as e:
            print(u'Error: %s'%ensure_unicode(e.message))
            errors.append([waptpackage,repr(e)])

    if options.doscan:
        for package_dir in package_dirs:
            if os.path.isfile(os.path.join(package_dir,'Packages')):
                print(u'Launching the update of Packages index in %s ...'% ensure_unicode(package_dir))
                repo = WaptLocalRepo(package_dir)
                repo.update_packages_index()
                print('Done')
    else:
        print("Don't forget to rescan your repository with wapt-scanpackages %s" % os.path.dirname(waptpackages[0]))

    if errors:
        print('Package not processed properly: ')
        for fn,error in errors:
            print(u'%s : %s' % (fn,error))

        sys.exit(1)
    else:
        sys.exit(0)
Exemplo n.º 20
0
def add_ads_groups(waptconfigfile,
                   hostdicts_list,
                   sign_certs=None,
                   sign_key=None,
                   key_password=None,
                   wapt_server_user=None,
                   wapt_server_passwd=None,
                   cabundle=None):

    if sign_certs is None:
        sign_bundle_fn = inifile_readstring(waptconfigfile, u'global',
                                            u'personal_certificate_path')
        sign_bundle = SSLCABundle(sign_bundle_fn)
        sign_certs = sign_bundle.certificates()
        # we assume a unique signer.
        if cabundle is None:
            cabundle = sign_bundle

    if not sign_certs:
        raise Exception(u'No personal signer certificate found in %s' %
                        sign_bundle_fn)

    if sign_key is None:
        sign_key = sign_certs[0].matching_key_in_dirs(
            private_key_password=key_password)

    try:
        import waptconsole
        progress_hook = waptconsole.UpdateProgress
        private_key_password_callback = waptconsole.GetPrivateKeyPassword
    except ImportError as e:

        def print_progress(show=False, n=0, max=100, msg=''):
            if show:
                print('%s %s/%s\r' % (msg, n, max), end='')
            else:
                if not msg:
                    msg = 'Done'
                print("%s%s" % (msg, ' ' * (80 - len(msg))))

        progress_hook = print_progress
        private_key_password_callback = None

    main_repo = WaptRemoteRepo(name='wapt', cabundle=cabundle)
    main_repo.load_config_from_file(waptconfigfile)
    main_repo.private_key_password_callback = private_key_password_callback

    host_repo = WaptHostRepo(name='wapt-host',
                             host_id=[h['uuid'] for h in hostdicts_list],
                             cabundle=cabundle)
    host_repo.load_config_from_file(waptconfigfile)
    host_repo.private_key_password_callback = private_key_password_callback

    total_hosts = len(host_repo.packages())
    discarded_uuids = [p.package for p in host_repo.discarded]

    packages = []
    discarded = []
    unchanged = []

    try:
        progress_hook(True, 0, len(hostdicts_list),
                      'Editing %s hosts' % len(hostdicts_list))
        i = 0
        for h in hostdicts_list:
            try:
                host_package = None
                host_id = h['uuid']
                hostname = h['computer_name']
                groups = get_computer_groups(hostname)
                host_package = host_repo.get(
                    host_id, PackageEntry(package=host_id, section='host'))
                if progress_hook(True, i, len(hostdicts_list),
                                 'Checking %s' % host_package.package):
                    break

                wapt_groups = ensure_list(host_package['depends'])
                additional = [
                    group for group in groups if not group in wapt_groups and (
                        main_repo.get(group, None) is not None)
                ]
                if additional:
                    logger.info(u'Adding %s to %s' %
                                (','.join(additional), host_package.package))
                    if progress_hook(True, i, len(hostdicts_list),
                                     'Editing %s' % host_package.package):
                        break
                    wapt_groups.extend(additional)
                    host_package.depends = ','.join(wapt_groups)
                    host_package.build_management_package()
                    host_package.inc_build()
                    host_file = host_package.build_management_package()
                    host_package.sign_package(sign_certs, sign_key)
                    packages.append(host_package)
                else:
                    unchanged.append(host_package.package)
            except Exception as e:
                if host_package:
                    discarded.append(host_package.package)
                logger.critical(u'Discarding because %s' % ensure_unicode(e))

        # upload all in one step...
        progress_hook(True, 3, 3, 'Upload %s host packages' % len(packages))
        server = WaptServer().load_config_from_file(waptconfigfile)
        server.private_key_password_callback = private_key_password_callback
        server.upload_packages(packages,
                               auth=(wapt_server_user, wapt_server_passwd),
                               progress_hook=progress_hook)
        return dict(updated=packages, discarded=discarded, unchanged=unchanged)

    finally:
        logger.debug('Cleanup')
        try:
            i = 0
            for s in packages:
                i += 1
                progress_hook(True, i, len(packages), 'Cleanup')
                if os.path.isfile(s.localpath):
                    os.remove(s.localpath)
            progress_hook(False)
        except WindowsError as e:
            logger.critical('Unable to remove temporary directory %s: %s' %
                            (s, repr(e)))
            progress_hook(False)
Exemplo n.º 21
0
def main():
    if len(args) == 0:
        print "ERROR : You must provide one action to perform"
        parser.print_usage()
        sys.exit(2)

    action = args[0]

    # Config file
    if not os.path.isfile(config_file):
        logger.error("Error : could not find file : " + config_file +
                     ", please check the path")

    logger.debug('Config file: %s' % config_file)

    defaults = {
        'repositories': '',
        'repo_url': '',
        'default_source_url': '',
        'private_key': '',
        'public_cert': '',
        'default_development_base': 'c:\tranquilit',
        'default_package_prefix': 'tis',
        'default_sources_suffix': 'wapt',
        'default_sources_url': '',
        'upload_cmd': '',
        'wapt_server': '',
        'loglevel': 'info',
    }

    cp = RawConfigParser(defaults=defaults)
    cp.add_section('global')
    cp.read(config_file)

    global loglevel
    if not loglevel and cp.has_option('global', 'loglevel'):
        loglevel = cp.get('global', 'loglevel')
        setloglevel(loglevel)

    mywapt = Wapt(config=cp)
    if options.wapt_url:
        mywapt.wapt_repourl = options.wapt_url

    if options.private_key:
        mywapt.private_key = options.private_key
    else:
        mywapt.private_key = cp.get('global', 'private_key')

    mywapt.dry_run = options.dry_run
    #logger.info("Main wapt Repository %s" % mywapt.wapt_repourl)
    logger.debug('WAPT base directory : %s' % mywapt.wapt_base_dir)
    logger.debug('Package cache dir : %s' % mywapt.packagecachedir)
    logger.debug('WAPT DB Structure version;: %s' % mywapt.waptdb.db_version)

    try:
        params_dict = {}
        try:
            params_dict = json.loads(options.params.replace("'", '"'))
        except:
            raise Exception('Install Parameters must be in json format')

        if action == 'install' or action == 'download':
            if len(args) < 2:
                print "You must provide at least one package name"
                sys.exit(1)

            if os.path.isdir(args[1]) or os.path.isfile(args[1]):
                print "installing WAPT file %s" % args[1]
                if action == 'install':
                    mywapt.install_wapt(args[1], params_dict=params_dict)
            else:
                print "%sing WAPT packages %s" % (action, ','.join(args[1:]))
                if options.update_packages:
                    print "Update package list"
                    mywapt.update()

                result = mywapt.install(
                    args[1:],
                    force=options.force,
                    params_dict=params_dict,
                    download_only=(action == 'download'),
                )
                print "\nResults :"
                if action <> 'download':
                    for k in ('install', 'additional', 'upgrade', 'skipped',
                              'errors'):
                        if result.get(k, []):
                            print "\n=== %s packages ===\n%s" % (
                                k,
                                '\n'.join([
                                    "  %-30s | %s (%s)" %
                                    (s[0], s[1].package, s[1].version)
                                    for s in result[k]
                                ]),
                            )
                else:
                    for k in ('downloaded', 'skipped', 'errors'):
                        if result.get('downloads', {
                                'downloaded': [],
                                'skipped': [],
                                'errors': []
                        })[k]:
                            print "\n=== %s packages ===\n%s" % (
                                k,
                                '\n'.join([
                                    "  %s" % (s, )
                                    for s in result['downloads'][k]
                                ]),
                            )

        elif action == 'download':
            if len(args) < 2:
                print "You must provide at least one package name to download"
                sys.exit(1)
            if options.update_packages:
                print "Update package list"
                mywapt.update()
            print "Downloading packages %s" % (','.join(args[1:]), )
            result = mywapt.download_packages(args[1:],
                                              usecache=not options.force)
            if result['downloaded']:
                print "\nDownloaded packages : \n%s" % "\n".join(
                    ["  %s" % p for p in result['downloaded']])
            if result['skipped']:
                print "Skipped packages : \n%s" % "\n".join(
                    ["  %s" % p for p in result['skipped']])
            if result['errors']:
                logger.critical('Unable to download some files : %s' %
                                (result['errors'], ))
                sys.exit(1)

        elif action == 'show':
            if len(args) < 2:
                print "You must provide at least one package name to show"
                sys.exit(1)
            if os.path.isdir(args[1]) or os.path.isfile(args[1]):
                entry = PackageEntry().load_control_from_wapt(args[1])
                print "%s" % entry
            else:
                print "Display package control data for %s\n" % (','.join(
                    args[1:]), )
                if options.update_packages:
                    print "Update package list"
                    mywapt.update()
                for packagename in args[1:]:
                    entries = mywapt.waptdb.packages_matching(packagename)
                    if entries:
                        for e in entries:
                            print "%s\n" % e.ascontrol(
                                with_non_control_attributes=True)
                    else:
                        print "None packages found matching package name and version"
        elif action == 'show-params':
            if len(args) < 2:
                print "You must provide at one package name to show params for"
                sys.exit(1)
            for packagename in args[1:]:
                params = mywapt.waptdb.params(packagename)
                print "%s" % params

        elif action == 'list-registry':
            print "%-39s%-70s%-20s%-70s" % ('UninstallKey', 'Software',
                                            'Version', 'Uninstallstring')
            print '-' * 39 + '-' * 70 + '-' * 20 + '-' * 70
            for p in setuphelpers.installed_softwares(' '.join(args[1:])):
                print u"%-39s%-70s%-20s%-70s" % (
                    p['key'], p['name'], p['version'], p['uninstall_string'])

        elif action == 'showlog':
            if len(args) < 2:
                print "You must provide at least one package name"
                sys.exit(1)
            for packagename in args[1:]:
                result = mywapt.last_install_log(packagename)
                print "Package : %s\nStatus : %s\n\nInstallation log:\n%s" % (
                    packagename, result['status'], result['log'])

        elif action == 'remove':
            if len(args) < 2:
                print "You must provide at least one package name to remove"
                sys.exit(1)
            for packagename in args[1:]:
                print "Removing %s ..." % (packagename, ),
                mywapt.remove(packagename, force=options.force)
                print "done"

        elif action == 'session-setup':
            if len(args) < 2:
                print "You must provide at least one package to be configured in user's session"
                sys.exit(1)

            for packagename in args[1:]:
                print "Configuring %s ..." % (packagename, ),
                mywapt.session_setup(packagename, params_dict=params_dict)
                print "done"

        elif action == 'uninstall':
            # launch the setup.uninstall() procedure for the given packages
            # can be used when registering in registry a custom install with a python script
            if len(args) < 2:
                print "You must provide at least one package to be uninstalled"
                sys.exit(1)

            for packagename in args[1:]:
                print "uninstalling %s ..." % (packagename, ),
                mywapt.uninstall(packagename, params_dict=params_dict)
                print "uninstall done"

        elif action == 'update':
            print "Update package list"
            result = mywapt.update()
            print "Total packages : %i" % result['count']
            print "Added packages : \n%s" % "\n".join(
                ["  %s (%s)" % p for p in result['added']])
            print "Removed packages : \n%s" % "\n".join(
                ["  %s (%s)" % p for p in result['removed']])
            print "Repositories URL : \n%s" % "\n".join(
                ["  %s" % p for p in result['repos']])

        elif action == 'upgradedb':
            (old, new) = mywapt.waptdb.upgradedb()
            if old == new:
                print "No database upgrade required, current %s, required %s" % (
                    old, mywapt.waptdb.curr_db_version)
            else:
                print "Old version : %s to new : %s" % (old, new)

        elif action == 'upgrade':
            if options.update_packages:
                print "Update package list"
                mywapt.update()
            result = mywapt.upgrade()
            if not result['install'] and not result[
                    'additional'] and not result['upgrade'] and not result[
                        'skipped']:
                print "Nothing to upgrade"
            else:
                for k in ('install', 'additional', 'upgrade', 'skipped',
                          'errors'):
                    if result[k]:
                        print "\n=== %s packages ===\n%s" % (
                            k,
                            '\n'.join([
                                "  %-30s | %s (%s)" %
                                (s[0], s[1].package, s[1].version)
                                for s in result[k]
                            ]),
                        )

            sys.exit(0)

        elif action == 'list-upgrade':
            if options.update_packages:
                print "Update package list"
                mywapt.update()
            q = mywapt.list_upgrade()
            if not q:
                print "Nothing to upgrade"
            else:
                print ppdicttable([p[0] for p in q], [('package', 20),
                                                      ('version', 10)])

        elif action == 'download-upgrade':
            if options.update_packages:
                print "Update package list"
                mywapt.update()
            result = mywapt.download_upgrades()
            print "Downloaded packages : \n%s" % "\n".join(
                ["  %s" % p for p in result['downloaded']])
            print "Skipped packages : \n%s" % "\n".join(
                ["  %s" % p for p in result['skipped']])

            if result['errors']:
                logger.critical('Unable to download some files : %s' %
                                (result['errors'], ))
                sys.exit(1)

        elif action == 'update-packages':
            if len(args) < 2:
                print "You must provide the directory"
                sys.exit(1)
            print update_packages(args[1])

        elif action == 'sources':
            if len(args) < 2:
                print "You must provide the package name"
                sys.exit(1)
            os.startfile(mywapt.get_sources(args[1]))

        elif action == 'make-template':
            if len(args) < 2:
                print "You must provide the installer path"
                sys.exit(1)
            source_dir = mywapt.maketemplate(*args[1:])
            print "Template created. You can build the WAPT package by launching\n  %s build-package %s" % (
                sys.argv[0], source_dir)
            os.startfile(source_dir)

        elif action == 'make-host-template':
            source_dir = mywapt.makehosttemplate(*args[1:])
            print "Template created. You can build the WAPT package by launching\n  %s build-package %s" % (
                sys.argv[0], source_dir)
            os.startfile(source_dir)

        elif action == 'build-package':
            if len(args) < 2:
                print "You must provide at least one source directory for package build"
                sys.exit(1)
            for source_dir in [os.path.abspath(p) for p in args[1:]]:
                if os.path.isdir(source_dir):
                    print('Building  %s' % source_dir)
                    result = mywapt.buildpackage(
                        source_dir,
                        inc_package_release=options.increlease,
                        excludes=options.excludes.split(','))
                    package_fn = result['filename']
                    if package_fn:
                        print "Package content:"
                        for f in result['files']:
                            print " %s" % f[0]
                        print('...done. Package filename %s' % (package_fn, ))

                        def pwd_callback(*args):
                            """Default password callback for opening private keys"""
                            return open(options.private_key_passwd, 'r').read()

                        if mywapt.private_key:
                            print('Signing %s' % package_fn)
                            if options.private_key_passwd:
                                signature = mywapt.signpackage(
                                    package_fn,
                                    excludes=options.excludes.split(','),
                                    callback=pwd_callback)
                            else:
                                signature = mywapt.signpackage(
                                    package_fn,
                                    excludes=options.excludes.split(','))
                            print "Package %s signed : signature :\n%s" % (
                                package_fn, signature)
                        else:
                            logger.warning(
                                'No private key provided, package %s is unsigned !'
                                % package_fn)

                        if mywapt.upload_cmd:
                            print '\nYou can upload to repository with\n  %s upload-package %s ' % (
                                sys.argv[0], package_fn)
                        return 0
                    else:
                        logger.critical('package not created')
                        return 1
                else:
                    logger.critical('Directory %s not found' % source_dir)

        elif action == 'sign-package':
            if len(args) < 2:
                print "You must provide at least one source directory or package to sign"
                sys.exit(1)
            for waptfile in [os.path.abspath(p) for p in args[1:]]:
                if os.path.isdir(waptfile) or os.path.isfile(waptfile):
                    print('Signing %s' % waptfile)
                    signature = mywapt.signpackage(
                        waptfile, excludes=options.excludes.split(','))
                    print "Package %s signed : signature :\n%s" % (waptfile,
                                                                   signature)
                else:
                    logger.critical('Package %s not found' % waptfile)
                    return 1

        elif action == 'upload-package':
            if len(args) < 2:
                print "You must provide a package to upload"
                sys.exit(1)
            waptfiles = []
            for a in args[1:]:
                waptfiles += glob.glob(a)
            waptfile_arg = " ".join(['"%s"' % f for f in waptfiles])
            print setuphelpers.run(mywapt.upload_cmd %
                                   {'waptfile': waptfile_arg})
            if mywapt.after_upload:
                print setuphelpers.run(mywapt.after_upload %
                                       {'waptfile': waptfile_arg})

        elif action == 'search':
            if options.update_packages:
                print "Update package list"
                mywapt.update()
            result = mywapt.waptdb.packages_search(args[1:])
            print ppdicttable(result, (('package', 30), ('version', 10),
                                       ('description', 80)))

        elif action == 'cleanup':
            result = mywapt.cleanup()
            print "Removed files : \n%s" % "\n".join(
                ["  %s" % p for p in result])

        elif action == 'inventory':
            print mywapt.inventory()

        elif action == 'setup-tasks':
            print mywapt.setup_tasks()

        elif action == 'list':

            def cb(fieldname, value):
                if value and fieldname == 'install_date':
                    return value[0:16]
                else:
                    return value

            print ppdicttable(
                mywapt.waptdb.installed_search(args[1:]).values(),
                (('package', 20), ('version', 15), ('install_status', 10),
                 ('install_date', 16), ('description', 80)),
                callback=cb)
        else:
            print 'Unknown action %s' % action
            sys.exit(1)
    except Exception, e:
        print "FATAL ERROR : %s" % e
        if logger.level == logging.DEBUG:
            raise
        sys.exit(3)