WAPT Setuphelpers Usage

setuphelpers.all_files(rootdir, pattern=None)[source]

Recursively return all files from rootdir and sub directories matching the (dos style) pattern (example: *.exe)

setuphelpers.application_data()[source]
setuphelpers.autoremove_apt()[source]
setuphelpers.autoremove_yum()[source]
setuphelpers.copytree2(src, dst, ignore=None, onreplace=<function default_skip>, oncopy=<function default_oncopy>, enable_replace_at_reboot=True)[source]

Copy src directory to dst directory. dst is created if it doesn’t exists src can be relative to installation temporary dir

oncopy is called for each file copy. if False is returned, copy is skipped onreplace is called when a file will be overwritten.

Args:

src (str): path to source directory (absolute path or relative to package extraction tempdir) dst (str): path to target directory (created if not present) ignore (func) : callback func(root_dir,filenames) which returns names to ignore onreplace (func) : callback func(src,dst):boolean called when a file will be replaced to decide what to do.

default is to not replace if target exists. can be default_overwrite or default_overwrite_older or custom function.

oncopy (func)callback func(msg,src,dst) called when a file is copied.

default is to log in debug level the operation

enable_replace_at_reboot (boolean): if True, files which are locked will be scheduled for replace at next reboot

Returns:

Raises:

>>> copytree2(r'c:\tranquilit\wapt\tests',r'c:\tranquilit\wapt\tests2')
>>> isdir(r'c:\tranquilit\wapt\tests2')
True
>>> remove_tree(r'c:\tranquilit\wapt\tests2')
>>> isdir(r'c:\tranquilit\wapt\tests2')
False
setuphelpers.currentdate()[source]

date as string YYYYMMDD

>>> currentdate()
'20161102'
setuphelpers.currentdatetime()[source]

date/time as YYYYMMDD-hhmmss

>>> currentdatetime()
'20161102-193600'
setuphelpers.dateof(adatetime)[source]
setuphelpers.datetime2isodate(adatetime=None)[source]
setuphelpers.default_gateway()[source]

Returns default ipv4 current gateway

setuphelpers.default_oncopy(msg, src, dst)[source]
setuphelpers.default_overwrite(src, dst)[source]
setuphelpers.default_overwrite_older(src, dst)[source]
setuphelpers.default_skip(src, dst)[source]
setuphelpers.dir_is_empty(path)[source]

Check if a directory is empty

setuphelpers.dmi_info()[source]
setuphelpers.ensure_dir(filename)[source]

Be sure the directory of filename exists on disk. Create it if not

The intermediate directories are created either.

Args:

filename (str): path to a future file for which to create directory.

Returns:

None

setuphelpers.ensure_list(csv_or_list, ignore_empty_args=True, allow_none=False)[source]

if argument is not a list, return a list from a csv string

Args:

csv_or_list (list or str): ignore_empty_args (bool): if True, empty string found in csv are not appended to the list. allow_none (bool): if True, if csv_or_list is None, return None, else return empty list/

Returns:

list

setuphelpers.ensure_unicode(data)[source]

Return a unicode string from data object It is sometimes difficult to know in advance what we will get from command line application output.

This is to ensure we get a (not always accurate) representation of the data mainly for logging purpose.

Args:

data: either str or unicode or object having a __str__ or WindowsError or Exception

Returns:

unicode: unicode string representing the data

>>> ensure_unicode(str('éé'))
u'éé'
>>> ensure_unicode(u'éé')
u'éé'
>>> ensure_unicode(Exception("test"))
u'Exception: test'
>>> ensure_unicode(Exception())
u'Exception: '
setuphelpers.error(reason)[source]

Raise a WAPT fatal error

setuphelpers.file_is_locked(path, timeout=5)[source]

Check if a file is locked. waits timout seconds for the release

setuphelpers.filecopyto(filename, target)[source]

Copy file from absolute or package temporary directory to target directory

If file is dll or exe, logs the original and new version.

Args:
filename (str): absolute path to file to copy,

or relative path to temporary package install content directory.

target (str) : absolute path to target directory where to copy file.

target is either a full filename or a directory name if filename is .exe or .dll, logger prints version numbers

>>> if not os.path.isfile('c:/tmp/fc.test'):
...     with open('c:/tmp/fc.test','wb') as f:
...         f.write('test')
>>> if not os.path.isdir('c:/tmp/target'):
...    os.mkdir('c:/tmp/target')
>>> if os.path.isfile('c:/tmp/target/fc.test'):
...    os.unlink('c:/tmp/target/fc.test')
>>> filecopyto('c:/tmp/fc.test','c:/tmp/target')
>>> os.path.isfile('c:/tmp/target/fc.test')
True
setuphelpers.fileisodate(filename)[source]

Returns last update date time from filename in local time

setuphelpers.find_all_files(rootdir, include_patterns=None, exclude_patterns=None, include_dirs=None, exclude_dirs=None, excludes_full=None)[source]

Generator which recursively find all files from rootdir and sub directories matching the (dos style) patterns (example: *.exe)

Args;

rootdir (str): root dir where to start looking for files include_patterns (str or list) : list of glob pattern of files to return exclude_patterns (str or list) : list of glob pattern of files to exclude

(if a file is both in include and exclude, it is excluded)

include_dirs (str or list) : list of glob directory patterns to return exclude_dirs (str or list) : list of glob directory patterns to exclude

(if a dir is both in include and exclude, it is excluded)

excludes_full (list) : list of exact (relative to package root) filepathes to exclude from manifest.

>>> for fn in find_all_files('c:\tmp','*.txt'):
        print(fn)
>>>
setuphelpers.find_processes(process_name)[source]

Return list of Process names process_name

Args:

process_name (str): process name to lookup

Returns:

list: list of processes (Process) named process_name or process_name.exe

>>> [p.pid for p in find_processes('explorer')]
[2756, 4024]
setuphelpers.get_code_name_version()[source]
setuphelpers.get_computer_groups()[source]

Try to find the computer in the Active Directory and return the list of groups

setuphelpers.get_computername()[source]

Return host name in lowercase (without domain part)

setuphelpers.get_current_user()[source]

Get the login name for the current user. >>> get_current_user() u’htouvet’

setuphelpers.get_default_gateways()[source]
setuphelpers.get_disk_free_space(filepath)[source]

Returns the number of free bytes on the drive that filepath is on

setuphelpers.get_distrib_linux()[source]
setuphelpers.get_distrib_version()[source]
setuphelpers.get_dns_servers()[source]
setuphelpers.get_domain_from_socket()[source]

Return main DNS domain of the computer

Returns:

str: domain name

>>> get_domain_from_socket()
u'tranquilit.local'
setuphelpers.get_domain_info(ldap_auth_server=None, use_ssl=True, force_tgt=True, hostname=None, domain=None, verify_cert_ldap=False)[source]

Return dict ad_site , ou and groups Warning : Please note that the search for gssapi does not work if the reverse dns recording is not available for ad

setuphelpers.get_file_properties(fname, ignore_warning=True)[source]

Read all properties of the given file return them as a dictionary.

Args:

fname : path to Windows executable or DLL

Returns:

dict: properties of executable

>>> xp = get_file_properties(r'c:\windows\explorer.exe')
>>> 'FileVersion' in xp and 'FileDescription' in xp
True
setuphelpers.get_fqdn()[source]
setuphelpers.get_groups(user)[source]
setuphelpers.get_hostname()[source]
setuphelpers.get_kernel_version()[source]
setuphelpers.get_language(full_locale=False, separator='_')[source]

Get the os default locale (example: fr, en, pl, etc.)

>>> get_language()
'fr'
>>> get_language(full_locale=True)
'fr_FR'
>>> get_language(full_locale=True, separator='-').lower()
'fr-fr'
setuphelpers.get_last_logged_on_user()[source]
setuphelpers.get_local_IPs()[source]
setuphelpers.get_loggedinusers()[source]
setuphelpers.get_main_ip(host=None, hostv6=None)[source]
setuphelpers.get_os_name()[source]

Get the name of the current running operating system

Returns:

str: Windows, Linux, Darwin

>>> get_os_name()
'Windows'
setuphelpers.get_os_version()[source]
setuphelpers.get_proxies()[source]

Return system proxy with the urllib python library

>>> get_proxies()
{'http': 'http://srvproxy.ad.domain.lan:8080',
'https': 'http://srvproxy.ad.domain.lan:8080'}
setuphelpers.get_sha256(afile='', BLOCK_SIZE=1048576)[source]
setuphelpers.host_info()[source]
setuphelpers.host_info_common_unix()[source]

Read main workstation informations, returned as a dict

Returns:

dict: main properties of host, networking and windows system

Changed in version 1.4.1: returned keys changed : dns_domain -> dnsdomain

>>> hi = host_info()
>>> 'computer_fqdn' in hi and 'connected_ips' in hi and 'computer_name' in hi and 'mac' in hi
True
setuphelpers.host_metrics()[source]

Frequently updated host data

setuphelpers.hours_minutes(hours)[source]
setuphelpers.httpdatetime2isodate(httpdate)[source]

Convert a date string as returned in http headers or mail headers to isodate (UTC)

>>> import requests
>>> last_modified = requests.head('http://wapt/wapt/Packages',headers={'cache-control':'no-cache','pragma':'no-cache'}).headers['last-modified']
>>> len(httpdatetime2isodate(last_modified)) == 19
True
setuphelpers.inifile_deleteoption(inifilename, section, key)[source]

Remove a key within the section of the inifile

Args:

inifilename (str): Path to the ini file section (str): section key (str): value key of option to remove

Returns:

boolean : True if the key/option has been removed

>>> inifile_writestring('c:/tranquilit/wapt/tests/test.ini','global','version','1.1.2')
>>> print inifile_hasoption('c:/tranquilit/wapt/tests/test.ini','global','version')
True
>>> print inifile_deleteoption('c:/tranquilit/wapt/tests/test.ini','global','version')
False
setuphelpers.inifile_deletesection(inifilename, section)[source]

Remove a section within the inifile

Args:

inifilename (str): Path to the ini file section (str): section to remove

Returns:

boolean : True if the section has been removed

setuphelpers.inifile_hasoption(inifilename, section, key)[source]

Check if an option is present in section of the inifile

Args:

inifilename (str): Path to the ini file section (str): section key (str): value key to check

Returns:

boolean : True if the key exists

>>> inifile_writestring('c:/tranquilit/wapt/tests/test.ini','global','version','1.1.2')
>>> print inifile_hasoption('c:/tranquilit/wapt/tests/test.ini','global','version')
True
>>> print inifile_hasoption('c:/tranquilit/wapt/tests/test.ini','global','dontexist')
False
setuphelpers.inifile_hassection(inifilename, section)[source]

Check if an option is present in section of the inifile

Args:

inifilename (str): Path to the ini file section (str): section

Returns:

boolean : True if the key exists

>>> inifile_writestring('c:/tranquilit/wapt/tests/test.ini','global','version','1.1.2')
>>> print inifile_hassection('c:/tranquilit/wapt/tests/test.ini','global')
True
setuphelpers.inifile_readstring(inifilename, section, key, default=None)[source]

Read a string parameter from inifile

>>> inifile_writestring('c:/tranquilit/wapt/tests/test.ini','global','version','1.1.2')
>>> print inifile_readstring('c:/tranquilit/wapt/tests/test.ini','global','version')
1.1.2
>>> print inifile_readstring('c:/tranquilit/wapt/tests/test.ini','global','undefaut','defvalue')
defvalue
setuphelpers.inifile_writestring(inifilename, section, key, value)[source]

Write a string parameter to inifile

>>> inifile_writestring('c:/tranquilit/wapt/tests/test.ini','global','version','1.1.1')
>>> print inifile_readstring('c:/tranquilit/wapt/tests/test.ini','global','version')
1.1.1
setuphelpers.install_apt(package, allow_unauthenticated=False)[source]

Install package from APT repositories

setuphelpers.install_deb(path_to_deb)[source]

Install .deb package from file

setuphelpers.install_package_if_needed(package_name)[source]
setuphelpers.install_required_dependencies_apt()[source]
setuphelpers.install_rpm(package)[source]
setuphelpers.install_yum(package)[source]
setuphelpers.installed_softwares(keywords=None, name=None, ignore_empty_names=True)[source]

Return list of installed software from apt or rpm

Args:

keywords (str or list): string to lookup in key, display_name or publisher fields

Returns:
list of dicts: [{‘key’, ‘name’, ‘version’, ‘install_date’, ‘install_location’

‘uninstall_string’, ‘publisher’,’system_component’}]

setuphelpers.is32()[source]
setuphelpers.is64()[source]
setuphelpers.isARM()[source]
setuphelpers.isARM64()[source]
setuphelpers.isLinux64()
setuphelpers.is_debian_based()
setuphelpers.is_linux64()[source]
setuphelpers.is_redhat_based()
setuphelpers.is_rhel_based()
setuphelpers.is_valid_ipv4_address(address)[source]
setuphelpers.isdir(s)[source]

Return true if the pathname refers to an existing directory.

setuphelpers.isfile(path)[source]

Test whether a path is a regular file

setuphelpers.isodate2datetime(isodatestr)[source]
setuphelpers.isrunning(processname)[source]

Check if a process is running,

>>> isrunning('explorer')
True
setuphelpers.json_load_file(json_file, encoding='utf-8')[source]

Get content of a json file

Args:

path (str): path to the file

Returns:

dictionary (dict)

setuphelpers.json_write_file(json_file, data, indent=4, sort_keys=False, encoding='utf-8')[source]

Write dictionary to a json file

Args:

json_file (str): path to json file data (dict): dictionary content indent (str or list of str): tabulation size ; default: 4 spaces sort_keys (bool): alphabetical order or not encoding (bool): file encoding

setuphelpers.killalltasks(process_names, include_children=True)[source]

Kill the task by their process_names

>>> killalltasks('firefox')
setuphelpers.killtree(pid, including_parent=True)[source]
setuphelpers.listening_sockets(low_ports=False, include_loc=True, kind='all')[source]
setuphelpers.local_drives()[source]
setuphelpers.local_group_members(groupname)[source]
setuphelpers.local_group_memberships(username)[source]

List the local groups a user is member Of

setuphelpers.local_groups()[source]
setuphelpers.makepath(*p)[source]

Create a path given the components passed, but with saner defaults than os.path.join - In particular, removes ending path separators (backslashes) from components. Path functions will be called automatically

>>> makepath("c:", "Windows", "system32")
'c:\\Windows\\system32'
>>> makepath(system32())
'C:\\WINDOWS\\system32'
>>> system32()
'C:\\WINDOWS\\system32'
>>> system32
<function system32 at 0x063EBE79>
>>> makepath(system32)
'C:\\WINDOWS\\system32'
setuphelpers.mkdirs(path)[source]

Create directory path if it doesn’t exists yet Creates intermediate directories too.

>>> mkdirs("C:\Program Files (x86)\wapt")
u'C:\Program Files (x86)\wapt'
setuphelpers.networking()[source]

return a list of (iface,mac,{addr,broadcast,netmask})

setuphelpers.processes_for_file(filepath, open_files=True, dll=True)[source]

Generator returning processes currently having a open file descriptor for filepath

If not running as System account, can not access system processes.

Args:

filepath (str): file path or pattern (glob *)

Returns:

iterator psutil.Process

setuphelpers.purge_deb(deb_name)[source]
setuphelpers.remove_file(path)[source]

Try to remove a single file or symlink log a warning msg if file doesn’t exist log a critical msg if file can’t be removed

Args:

path (str): path to file

>>> remove_file(r'c:\tmp\fc.txt')
setuphelpers.remove_tree(*args, **kwargs)[source]

Convenience function to delete a directory tree, with any error not ignored by default. Pass ignore_errors=False to access possible errors.

Args:

path (str): path to directory to remove ignore_errors (boolean) : default to False. Set it to True to ignore exceptions on children deletion onerror (func) : hook called with (func, path, exc)

on each delete exception. Should raise if stop is required.

setuphelpers.run(cmd, shell=True, timeout=600, accept_returncodes=[0, 3010], on_write=None, pidlist=None, return_stderr=True, **kwargs)[source]

Run the command cmd in a shell and return the output and error text as string

Args:

cmd : command and arguments, either as a string or as a list of arguments shell (boolean) : True is assumed timeout (int) : maximum time to wait for cmd completion is second (default = 600)

a TimeoutExpired exception is raised if tiemout is reached.

on_writecallback when a new line is printed on stdout or stderr by the subprocess

func(unicode_line). arg is enforced to unicode

accept_returncodes (list) : list of return code which are considered OK default = (0, 3010) pidlist (list): external list where to append the pid of the launched process. return_stderr (bool or list) : if True, the error lines are returned to caller in result.

if a list is provided, the error lines are appended to this list

all other parameters from the psutil.Popen constructor are accepted

Returns:
RunOutputbytes like output of stdout and optionnaly stderr streams.

returncode attribute

Raises:

CalledProcessError: if return code of cmd is not in accept_returncodes list TimeoutExpired: if process is running for more than timeout time.

Changed in version 1.3.9: return_stderr parameters to disable stderr or get it in a separate list return value has a returncode attribute to

Changed in version 1.4.0: output is not forced to unicode

Changed in version 1.4.1: error code 1603 is no longer accepted by default.

Changed in version 1.5.1: If cmd is unicode, encode it to default filesystem encoding before running it.

>>> run(r'dir /B c:\windows\explorer.exe')
'explorer.exe\r\n'
>>> out = []
>>> pids = []
>>> def getlines(line):
...    out.append(line)
>>> run(r'dir /B c:\windows\explorer.exe',pidlist=pids,on_write=getlines)
u'explorer.exe\r\n'
>>> print out
['explorer.exe\r\n']
>>> try:
...     run(r'ping /t 127.0.0.1',timeout=3)
... except TimeoutExpired:
...     print('timeout')
timeout
setuphelpers.run_notfatal(*cmd, **args)[source]

Runs the command and wait for it termination, returns output Ignore exit status code of command, return ‘’ instead

Changed in version 1.4.0: output is now enforced to unicode

setuphelpers.running_on_ac()[source]
setuphelpers.shell_launch(cmd)[source]

Launch a command (without arguments) but doesn’t wait for its termination

>>> open('c:/tmp/test.txt','w').write('Test line')
>>> shell_launch('c:/tmp/test.txt')
setuphelpers.systemd_restart_service(servicename)[source]
setuphelpers.systemd_start_service(servicename)[source]
setuphelpers.systemd_status_service(servicename)[source]
setuphelpers.systemd_stop_service(servicename)[source]
setuphelpers.time2display(adatetime)[source]
setuphelpers.type_debian()[source]
setuphelpers.type_redhat()[source]
setuphelpers.uninstall_apt(package, autoremove=False)[source]

Remove package with APT

setuphelpers.uninstall_yum(package)[source]
setuphelpers.unzip(zipfn, target=None, filenames=None, extract_with_full_paths=True)[source]

Unzip the files from zipfile with patterns in filenames to target directory

Args:

zipfn (str) : path to zipfile. (can be relative to temporary unzip location of package) target (str) : target location. Defaults to dirname(zipfile) + basename(zipfile) filenames (str or list of str): list of filenames / glob patterns (path sep is normally a slash) extract_with_full_paths (bool): keeping or not the subfolders of the archive as is

Returns:

list : list of extracted files

>>> unzip(r'C:\example\tis-7zip_9.2.0-15_all.wapt')
['C:\\example\\tis-7zip_9.2.0-15_all\\7z920-x64.msi',
 'C:\\example\\tis-7zip_9.2.0-15_all\\7z920.msi',
 'C:\\example\\tis-7zip_9.2.0-15_all\\setup.py',
 'C:\\example\\tis-7zip_9.2.0-15_all\\WAPT/control',
 'C:\\example\\tis-7zip_9.2.0-15_all\\WAPT/wapt.psproj',
 'C:\\example\\tis-7zip_9.2.0-15_all\\WAPT/manifest.sha256',
 'C:\\example\\tis-7zip_9.2.0-15_all\\WAPT/signature']
>>> unzip(r'C:\example\tis-7zip_9.2.0-15_all.wapt', filenames=['*.msi','*.py'])
['C:\\example\\tis-7zip_9.2.0-15_all\\7z920-x64.msi',
 'C:\\example\\tis-7zip_9.2.0-15_all\\7z920.msi',
 'C:\\example\\tis-7zip_9.2.0-15_all\\setup.py']
>>> unzip(r'C:\example\tis-7zip_9.2.0-15_all.wapt', target='test', filenames=['*.msi','*.py'])
['C:\\example\\test\\7z920-x64.msi',
 'C:\\example\\test\\7z920.msi',
 'C:\\example\\test\\setup.py']
>>> unzip(r'C:\example\tis-7zip_9.2.0-15_all.wapt', filenames='WAPT/*')
['C:\\example\\tis-7zip_9.2.0-15_all\\WAPT/control',
 'C:\\example\\tis-7zip_9.2.0-15_all\\WAPT/wapt.psproj',
 'C:\\example\\tis-7zip_9.2.0-15_all\\WAPT/manifest.sha256',
 'C:\\example\\tis-7zip_9.2.0-15_all\\WAPT/signature']
>>> unzip(r'C:\example\tis-7zip_9.2.0-15_all.wapt', filenames='WAPT/control')
['C:\\example\\tis-7zip_9.2.0-15_all\\WAPT\\control']
>>> unzip('tis-7zip_9.2.0-15_all.wapt', target='.', filenames='WAPT/control')
['.\\WAPT\\control']
>>> unzip(r'C:\example\tis-7zip_9.2.0-15_all.wapt', target=r'C:\example\', filenames='WAPT/control')
['C:\\example\\WAPT\\control']
>>> unzip('tis-7zip_9.2.0-15_all.wapt', target=basedir, filenames='WAPT/control')
['C:\\example\\WAPT\\control']
>>> unzip(r'C:\example\tis-7zip_9.2.0-15_all.wapt', filenames='WAPT/control', extract_with_full_paths=False)
['C:\\example\\control']

New in version 1.3.11.

Changed in version 2.2: added extract_with_full_paths parameter

setuphelpers.update_apt()[source]
setuphelpers.update_yum()[source]
setuphelpers.upgrade_apt()[source]
setuphelpers.upgrade_yum()[source]
setuphelpers.user_appdata()

Return the local appdata profile of current user

Returns:

str: path like u’/home/user/.config/’

setuphelpers.user_home_directory()[source]
setuphelpers.user_local_appdata()[source]

Return the local appdata profile of current user

Returns:

str: path like u’/home/user/.config/’

setuphelpers.wget(url, target=None, printhook=None, proxies=None, connect_timeout=10, download_timeout=None, verify_cert=None, referer=None, user_agent=None, cert=None, resume=False, md5=None, sha1=None, sha256=None, cache_dir=None, requests_session=None, limit_bandwidth=None)[source]

Copy the contents of a file from a given URL to a local file.

Args:

url (str): URL to document target (str) : full file path of downloaded file. If None, put in a temporary dir with supplied url filename (final part of url) proxies (dict) : proxies to use. eg {‘http’:’http://wpad:3128’,’https’:’http://wpad:3128’} timeout (int) : seconds to wait for answer before giving up auth (list) : (user,password) to authenticate with basic auth verify_cert (bool or str) : either False, True (verify with embedded CA list), or path to a directory or PEM encoded CA bundle file

to check https certificate signature against.

cert (list) : tuple/list of (x509certfilename,pemkeyfilename,key password) for authenticating the client. If key is not encrypted, password must be None referer (str): user_agent: resume (bool): md5 (str) : sha1 (str) : sha256 (str) : cache_dir (str) : if file exists here, and md5 matches, copy from here instead of downloading. If not, put a copy of the file here after downloading.

requests_session (request.Session) : predefined request session to use instead of building one from scratch from proxies, cert, verfify_cert

Returns:

str : path to downloaded file

>>> respath = wget('http://wapt.tranquil.it/wapt/tis-firefox_28.0.0-1_all.wapt','c:\\tmp\\test.wapt',proxies={'http':'http://proxy:3128'})
???
>>> os.stat(respath).st_size>10000
True
>>> respath = wget('http://localhost:8088/runstatus','c:\\tmp\\test.json')
???
setuphelpers.wgets(url, proxies: Optional[dict] = None, verify_cert=None, referer=None, user_agent=None, timeout=None, cert=None, requests_session=None, as_json=False) str[source]

Return the content of a remote resource as a string / bytes or dict with a http get request.

Raise an exception if remote data can’t be retrieved.

Args:

url (str): http(s) url proxies (dict): proxy configuration as requests requires it {‘http’: url, ‘https’:url} verify_cert (bool or str) : verfiy server certificate, path to trusted CA bundle cert (tuple of 3 str) : (cert_path, key_path, key password) client side authentication.

requests_session (request.Session) : predefined request session to use instead of building one from scratch

Returns:

str or bytes or dict : content of remote resource. str or bytes or json depending of the encoding and the Content-Type.

>>> data = wgets('https://wapt/ping')
>>> "msg" in data
True

PackageEntry Class

class setuphelpers.PackageEntry(package='', version='0', repo='', waptfile=None, section='base', **kwargs)[source]

Manage package attributes coming from either control files in WAPT package, local DB, or developement dir.

Methods to build, unzip, sign or check a package. Methods to sign the control attributes and check them.

>>> pe = PackageEntry('testgroup','0')
>>> pe.depends = 'tis-7zip'
>>> pe.section = 'group'
>>> pe.description = 'A test package'
>>> print(pe.ascontrol())
package           : testgroup
version           : 0
architecture      : all
section           : group
priority          : optional
maintainer        :
description       : A test package
depends           : tis-7zip
conflicts         :
maturity          :
locale            :
min_wapt_version  :
sources           :
installed_size    :
signer            :
signer_fingerprint:
signature_date    :
signed_attributes :
>>>
as_control_bytes()[source]

Return this package entry metadata as bytes as saved in package control file

Return:

bytes: lines with key: value

as_zipfile(mode='r')[source]

Return a CustomZipFile for this package for read only operations

ascontrol(with_repo_attributes=False, with_empty_attributes=False)[source]

Return control attributes and values as stored in control packages file

Each attribute on a line with key : value If value is multiline, new line begin with a space.

Args:

with_repo_attributes (bool) : if True, include md5sum and filename (for Packages index only) with_empty_attributes (bool) : weither to include attribute with empty value too or only

non empty and/or signed attributes

Returns:

str: lines of attr: value

asrequirement()[source]

Return package and version for designing this package in depends or install actions

Returns:

str: “packagename (=version)”

build_management_package(target_directory=None)[source]

Build the WAPT package from attributes only, without setup.py stores the result in target_directory.

self.sourcespath must be None. Package will contain only a control file.

Args:
target_directory (str): where to create Zip wapt file.

if None, temp dir will be used.

Returns:

str: path to zipped Wapt file. It is unsigned.

>>> pe = PackageEntry('testgroup','0',description='Test package',maintainer='Hubert',sources='https://dev/svn/testgroup',architecture='x86')
>>> waptfn = pe.build_management_package()
>>> key = SSLPrivateKey('c:/private/htouvet.pem',password='monmotdepasse')
>>> crt = SSLCertificate('c:/private/htouvet.crt')
>>> pe.sign_package(crt,key)
>>> pe.unzip_package()
'c:\users\htouvet\appdata\local\temp\waptob4gcd'
>>> ca = SSLCABundle('c:/wapt/ssl')
>>> pe.check_control_signature(ca)
<SSLCertificate cn=u'htouvet' issuer=u'tranquilit-ca-test' validity=2017-06-28 - 2027-06-26 Code-Signing=True CA=True>
build_manifest(exclude_filenames=None, block_size=1048576, forbidden_files=[], waptzip=None, excludes=[])[source]

Calc the manifest of a wapt package

Args:
forbidden_files (list): list of relative files which must not be present for the manifest to be built

(if one is found, build fails)

exclude_filenames (list) : list of exact (relative to package root with forward slashes) filepathes to exclude from manifest. excludes (list) : list of file / dir patterns to exclude, whatever level they are in the file hierarchy

Returns:

dict: {filepath:shasum,}

build_package(excludes=[], target_directory=None, excludes_full=['.svn', '.git', '.gitignore', 'setup.pyc', '__pycache__'])[source]

Build the WAPT package, stores the result in target_directory Zip the content of self.sourcespath directory into a zipfile named with default package filename based on control attributes.

Update filename attribute. Update localpath attribute with result filepath.

Args:

excludes (list) : list of patterns for source files to exclude from built package. target_directory (str): target directory where to store built package.

If None, use parent directory of package sources dircetory.

excludes_full (list) : list of exact (relative to package root) filepathes to exclude from Zip.

Returns:

str: full filepath to built wapt package

call_setup_hook(hook_name='session_setup', wapt_context=None, params=None, force=None, user=None)[source]

Calls a hook in setuppy given a wapt_context

Set basedir, control, and run context within the function context.

Args:

hook_name (str): name of function to call in setuppy wapt_context (Wapt) : run context

Returns:

output of hook.

Changes:

1.6.2.1: the called hook is run with Disabled win6432 FileSystem redirection

change_depends_conflicts_prefix(new_prefix)[source]

Change prefix of package name to new_prefix in depends and conflicts csv lists and return True if it was really changed.

Args:

new_prefix (str): new prefix to put in package names

Returns:

bool

change_prefix(new_prefix)[source]

Change prefix of package name to new_prefix and return True if it was really changed.

check_control_signature(trusted_bundle, signers_bundle=None)[source]

Check in memory control signature against a list of public certificates

Args:

trusted_bundle (SSLCABundle): Trusted certificates. : packages certificates must be signed by one of this bundle. signers_bundle : Optional. List of potential packages signers certificates chains.

When checking Packages index, actual packages are not available, only certificates embedded in Packages index. Package signature are checked against these certificates looking here for potential intermediate CA too. and matching certificate is checked against trusted_bundle.

Returns:

SSLCertificate : matching trusted package’s signers SSLCertificate

>>> from waptpackage import *
>>> from common import SSLPrivateKey,SSLCertificate
>>> k = SSLPrivateKey('c:/private/test.pem')
>>> c = SSLCertificate('c:/private/test.crt')
>>> p = PackageEntry('test',version='1.0-0')
>>> p.depends = 'test'
>>> p._sign_control(k,c)
>>> p.check_control_signature(c)
>>> p.check_control_signature(SSLCABundle('c:/wapt/ssl'))
check_package_signature(trusted_bundle, ignore_missing_files=False)[source]

Check - hash of files in unzipped package_dir with list in package’s manifest file - try to decrypt manifest signature with package’s certificate - check that the package certificate is issued by a know CA or the same as one the authorized certitificates.

Args:

trusted_bundle (SSLCABundle) : Local certificates store. Certificates in trusted_bundle.trusted_certificates are trusted. ignore_missing_files (bool): whether to raise exception for missing files. Useful to check stripped down packages when remote resigning

Returns:

SSLCertificate : matching certificate

Raises:

Exception if no certificate match is found.

delete_localsources()[source]

Remove the unzipped local directory

get(name, default=None)[source]

Get PackageEntry property.

Args:

name (str): property to get. name is forced to lowercase. default (any) : value to return in case the property doesn’t not exist.

Returns:

any : property value

get_impacted_process_list()[source]

Return a list containing the impacted process

Returns:

List[str] impacted process list

get_localized_description(locale_code=None)[source]

locale_code is a 2 chars code like fr or en or de

get_software_version()[source]

Return the software version only (without the build number of the package)

Returns:

str: “software_version”

get_stripped_package()[source]

Build a package keeping only Wapt stuff… Keeps only WAPT directory and setup.py

Returns:

bytes: zipped data

has_file(fname)[source]

Return None if fname is not in package, else return file datetime

Args:

fname (unicode): file path like WAPT/signature

Returns:

datetime : last modification datetime of file in Wapt archive if zipped or local sources if unzipped

inc_build()[source]

Increment last number part of version in memory

invalidate_signature()[source]

Remove all signature informations from control and unzipped package directory Package must be in unzipped state.

list_corrupted_files(ignore_missing_files=False, remove_extra_files=False)[source]

Check hexdigest sha for the files in manifest. Package must be already unzipped.

Returns:

list: non matching files (corrupted files)

load_control_from_dict(adict)[source]

Fill in members of entry with keys from supplied dict

adict members which are not a registered control attribute are set too and attribute name is put in list of “calculated” attributes.

Args:

adict (dict): key,value to put in this entry

Returns:

PackageEntry: self

load_control_from_wapt(fname=None, calc_md5=True, keep_control_lines=False)[source]

Load package attributes from the control file (utf8 encoded) included in WAPT zipfile fname

Args:
fname (str or unicode): Package file/directory path

If None, try to load entry attributes from self.sourcespath or self.localpath If fname is a file path, it must be Wapt zipped file, and try to load control data from it If fname is a directory path, it must be root dir of unzipped package file and load control from <fname>/WAPT/control

calc_md5 (boolean): if True and fname is a zipped file, initialize md5sum attribute with md5 part of filename or calc from Zipped file

Returns:

PackageEntry: self

make_package_edit_directory()[source]

Return the standard package directory to edit the package based on current attributes

Returns:
str: standard package filename
  • {package}_{version}_{architecture}_{OS}_{Min-OS-Version}_{Max-OS-Version}_{maturity}_{locale}-wapt

make_package_filename(with_md5sum=False)[source]

Return the standard package filename based on current attributes parts of control which are either ‘all’ or empty are not included in filename

Returns:
str: standard package filename
  • packagename.wapt for host

  • packagename_arch_maturity_locale.wapt for group

  • packagename_version_arch_maturity_locale.wapt for others

match(match_expr)[source]

Return True if package entry match a package string like ‘tis-package (>=1.0.1-00)

Check if entry match search words

Args:

search (str): words to search for separated by spaces

Returns:

boolean: True if entry contains the words in search in correct order and at word boundaries

match_version(version_expr)[source]

Return True if package entry match a version string condition like ‘>=1.0.1-00’

merge_stripped_package(stripped_package_zip_data=None, stripped_package_filename=None)[source]

Use the files from stripped_package_zip_data and include it in current unzipped package, remove files not in manifest, and recheck file hashes.

package_certificates()[source]

Return certificates from package. If package is built, take it from Zip else take the certificates from unzipped directory

Returns:
list: list of embedded certificates when package was signed or None if not provided or signed.

First one of the list is the signer, the others are optional intermediate CA

package_ident()[source]

Version independent package key

Returns:

tuple

parse_version()[source]

Parse version to major, minor, patch, pre-release, build parts.

save_control_to_wapt(fname=None, force=True)[source]

Save package attributes to the control file (utf8 encoded)

Update self.locapath or self.sourcespath if not already set.

Args:
fname (str)base directoy of waptpackage or filepath of Zipped Packges.

If None, use self.sourcespath if exists, or self.localpath if exists

force (bool) : write control in wapt zip file even if it already exist

Returns:

PackageEntry : None if nothing written, or previous PackageEntry if new data written

Raises:

Exception: if fname is None and no sourcespath and no localpath Exception: if control exists and force is False

set_software_version(version, inc_build=False)[source]

Set the software version only inc_build will increment the buildnumber

sign_package(certificate, private_key, keep_signature_date=False, excludes_full=['.svn', '.git', '.gitignore', 'setup.pyc', '__pycache__'], excludes=[])[source]

Sign a package source directory or an already built (zipped) package. Should follow immediately the build_package step.

Append signed control, manifest.sha256 and signature to zip wapt package If these files are already in the package, they are first removed.

Use the self.localpath attribute to get location of waptfile build file.

Args:

certificate (SSLCertificate or list): signer certificate chain private_key (SSLPrivateKey): signer private key keep_signature_date (bool): If true, previous date fo signature is kept (useful when resigning is needed, but no meaningful change has been done) excludes_full (list) : list of exact (relative to package root) filepathes to exclude from manifest. excludes (list) : list of file / dir patterns to exclude, whatever level they are in the file hierarchy

Returns:

str: signature

sign_stripped_package(certificate, private_key, excludes_full=['.svn', '.git', '.gitignore', 'setup.pyc', '__pycache__'], excludes=[], sign_setuppy=False)[source]

Sign an unzipped source package assuming digests in manifest file are OK except for control and certificate.crt * remove signature files -> WAPT/certificate.crt, WAPT/signature.sha256, * resign WAPT/control * update control hash in WAPT/manifest.sha256 * resign WAPT/manifest.sha256 and put in WAPT/signature.sha256

Args:

certificate (list of SSLCertificate) : certificates chain of the signer. First certificate is the signer’s one. other are intermediate CA private_key (SSLPrivateKey) : key to sign the control and manifest files. excludes_full excludes

Returns:

str: signature of manifest.sha256

unzip_package(target_dir=None, cabundle=None, ignore_missing_files=False)[source]

Unzip package and optionnally check content

Args:

target_dir (str): where to unzip package content. If None, a temp dir is created cabundle (list) : list of Certificates to check content. If None, no check is done

Returns:

str : path to unzipped packages files

Raises:

EWaptNotAPackage, EWaptBadSignature,EWaptCorruptedFiles if check is not successful, unzipped files are deleted.

Version Class

class setuphelpers.Version(version, members_count=None)[source]

Version object of form 0.0.0 can compare with respect to natural numbering and not alphabetical

Args:

version (str) : version string member_count (int) : number of version memebers to take in account.

If actual members in version is less, add missing memeber with 0 value If actual members count is higher, removes last ones.

>>> Version('0.10.2') > Version('0.2.5')
True
>>> Version('0.1.2') < Version('0.2.5')
True
>>> Version('0.1.2') == Version('0.1.2')
True
>>> Version('7') < Version('7.1')
True

Changed in version 1.6.2.5: truncate version members list to members_count if provided.

sortable_str()[source]

Output a str suitable for direct ordering members are converted to a chars hex padded with zero on the left. If member is not a digit, it is padded to a 8 chars string padded with spaces on the right.