예제 #1
0
    def parse_args(self):
        """Parses arguments."""
        result = None
        _name = sys.argv[0]
        _err_msg = '{}: error: argument'.format(_name)

        if len(sys.argv) == 1:
            self.parser.print_help(sys.stderr)
            sys.exit(1)

        result = self.parser.parse_args()

        # Manual argument checks that can't be done by argparse.
        if result.apfs_dmg:
            _arg = '--APFS'

            if not result.build_dmg:
                self.parser.print_usage(sys.stderr)
                _msg = '{} {}: --APFS: not allowed with argument -b/--build-dmg'.format(
                    _err_msg, _arg)
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)

        # Check that at least on of the three apps is provided for download flag '-a/--apps'
        if result.apps == 'allpkgs':
            result.apps = config.ALL_LATEST_APPS
        elif result.apps:
            _arg = '-a/--apps'
            _apps = [_app for _app, _value in config.APPS.items()]
            _apps.extend(['allpkgs'])
            _apps.sort()
            _choices = ', '.join(["'{}'".format(_app) for _app in _apps])

            if not any([app in result.apps for app in _apps]):
                self.parser.print_usage(sys.stderr)
                _msg = '{} {}: expected one argument: (choose from {})'.format(
                    _err_msg, _arg, _choices)
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)

        # Handle plist argument exceptions
        if result.plists:
            _arg = '-p/--plists'
            # Make sure all items passed end with plist
            _supported = config.SUPPORTED_PLISTS.keys()
            _choices = ["'{}'".format(_plist) for _plist in _supported]
            _choices.sort()
            _choices = ', '.join(_choices)
            _choices = '\'allpkgs\', {}'.format(_choices)

            if result.plists == 'allpkgs':
                result.plists = [
                    config.SUPPORTED_PLISTS.get(_plist)
                    for _plist in config.ALL_LATEST_PLISTS
                ]
            elif not any([_plist in _supported for _plist in result.plists]):
                _msg = '{} {}: excpected one argument: (choose from {})'.format(
                    _err_msg, _arg, _choices)
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)
            else:
                result.plists = [
                    config.SUPPORTED_PLISTS.get(_plist)
                    for _plist in result.plists
                ]

            if not (result.mandatory or result.optional):
                self.parser.print_usage(sys.stderr)
                _msg = '{} {}: must provide at least -m/--mandatory or -o/--optional or both'.format(
                    _err_msg, _arg)
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)

            if result.deployment or result.force_deployment:
                self.parser.print_usage(sys.stderr)
                _msg = '{}: {}: not allowed with argument --deploy/--force-deploy'.format(
                    _err_msg, _arg)
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)

            if not (result.build_dmg or result.download
                    or result.force_download):
                self.parser.print_usage(sys.stderr)
                _msg = (
                    '{}: {}: not allowed without argument -b/--build-dmg or -d/--destination '
                    'or -f/--force-destination'.format(_err_msg, _arg))
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)

        # Test if user is root for deploy/force deploy modes
        if not result.dry_run:
            if result.deployment:
                _arg = '--deploy'
            elif result.force_deployment:
                _arg = '--force-deploy'

            if not misc.is_root() and (result.deployment
                                       or result.force_deployment):
                _msg = '{} {}: you must be root to install packages'.format(
                    _err_msg, _arg)
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)

        # Handle some checking for more specific circumstances.
        if (result.download or result.force_download or result.deployment
                or result.force_deployment):
            if result.download:
                _arg = '-d/--destination'

            if result.force_download:
                _arg = '-f/--force-destination'

            if result.deployment:
                _arg = '--deploy'

            if result.force_deployment:
                _arg = '--force-deploy'

            if not (result.mandatory or result.optional):
                _msg = '{} {}: must provide at least -m/--mandatory or -o/--optional or both'.format(
                    _err_msg, _arg)
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)

        if result.cache_server:
            _arg = '--cache-server'
            _cs = result.cache_server[0]
            _url = urlparse(_cs)

            if _url.scheme not in self._valid_url_schemes or not re.search(
                    r'(?::\d+)', _url.netloc):  # and _url.path is not None:
                _msg = '{} {}: cache server url format expected is https://example.org:1234'.format(
                    _err_msg, _arg)
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)

        if result.pkg_server:
            _arg = '--pkg-server'
            _ps = result.pkg_server[0]
            _url = urlparse(_ps)

            if _url.scheme not in self._valid_url_schemes or not _url.path:
                _msg = (
                    '{} {}: mirror server url format expected is https://example.org/<path> '
                    'or https://example.org/file.dmg'.format(_err_msg, _arg))
                print(_msg)
                LOG.info(_msg)
                sys.exit(1)

        if result.show_plists:
            supported.show_supported_plists()

        if result.compare:
            compare.differences(file_a=result.compare[0],
                                file_b=result.compare[1])

        # Set "globals" here rather than in '__main__.py'
        if not result.plists:
            config.APPS_TO_PROCESS = result.apps if result.apps else misc.find_installed_apps(
            )

        if not result.apps:
            config.PLISTS_TO_PROCESS = result.plists

        config.ALLOW_INSECURE_CURL = result.insecure
        config.ALLOW_UNSECURE_PKGS = result.unsecure
        config.APFS_DMG = result.apfs_dmg
        config.CACHING_SERVER = result.cache_server[0].rstrip(
            '/') if result.cache_server else None
        config.DEBUG = getattr(logging, result.log_level, None)
        config.DEPLOY_PKGS = result.deployment
        config.FORCED_DEPLOYMENT = result.force_deployment
        config.DMG_FILE = result.build_dmg[0] if result.build_dmg else None
        config.DRY_RUN = result.dry_run
        config.LOCAL_HTTP_SERVER = result.pkg_server[0].rstrip(
            '/') if result.pkg_server else None
        config.MANDATORY = result.mandatory
        config.OPTIONAL = result.optional
        config.QUIET = result.quiet
        config.SILENT = result.silent
        config.TARGET = result.install_target[
            0] if result.install_target else '/'

        # Handle result.download/result.force_download
        if result.download or result.force_download:
            if result.download:
                config.DESTINATION_PATH = result.download[0]
            elif result.force_download:
                config.FORCE_DOWNLOAD = True
                config.DESTINATION_PATH = result.force_download[0]
            else:
                config.DESTINATION_PATH = config.DEFAULT_DEST

        if config.DEPLOY_PKGS or config.FORCED_DEPLOYMENT:
            config.DESTINATION_PATH = config.DEFAULT_DEST

        # Handle HTTP based DMG
        if config.LOCAL_HTTP_SERVER:
            if config.LOCAL_HTTP_SERVER.endswith('.dmg'):
                config.HTTP_DMG = True
                config.HTTP_DMG_PATH = result.pkg_server[0]
                config.LOCAL_HTTP_SERVER = None

        # Handle building a DMG
        if result.build_dmg:
            _dmg_path = os.path.splitext(config.DMG_FILE)[0]
            config.DESTINATION_PATH = '{}.sparseimage'.format(_dmg_path)

            # Set a psuedo 'config.DMG_VOLUME_MOUNTPATH' for dry-run use.
            if config.DRY_RUN:
                config.DMG_VOLUME_MOUNTPATH = '/Volumes/appleloops'

        return result
예제 #2
0
DRY_RUN = False

# HTTP Status's that are OK
HTTP_OK_STATUS = [200, 301, 302, 303, 307, 308]

# When set via command line, should be in the form of 'https://example.org/path/appleloops'
# Best practice is to use the relevant argument to mirror the folder paths from Apple.
LOCAL_HTTP_SERVER = None

# Log level
LOGGER_NAME = 'appleloops'
LOG_FILE = 'appleloops.log'
LOG_LEVEL = 'INFO'

# If the user is root, change the log path so not to blat on user log folder.
if misc.is_root():
    LOG_PATH = '/var/log'
else:
    LOG_PATH = path.expanduser(path.expandvars('~/Library/Logs'))

LOG_FILE_PATH = path.join(LOG_PATH, LOG_FILE)

# Default 'path' is '2016'. Use '.replace()' when '2013' is required.
LP10_MS3_CONTENT = 'lp10_ms3_content_2016'

# Used to determine if processing mandatory packages
MANDATORY = False

# Used for overall name of app based on '__name__'
NAME = None