From 3ffcd7155d7014cd9555fa2131852079c9cfc8b9 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 24 Jan 2015 11:03:06 -0800 Subject: [PATCH] swc-installation-test-2.py: Collect classes together Instead of interleaving class definitions with their instantiation. This will make it easier to build dependency chains that mix a few classes, because we don't have to worry about whether or not that class has been defined yet. --- swc-installation-test-2.py | 412 ++++++++++++++++++------------------- 1 file changed, 206 insertions(+), 206 deletions(-) diff --git a/swc-installation-test-2.py b/swc-installation-test-2.py index 9898d1b..5d794c7 100755 --- a/swc-installation-test-2.py +++ b/swc-installation-test-2.py @@ -378,21 +378,11 @@ class Dependency (object): return tuple(parsed_version) -class PythonDependency (Dependency): - def __init__(self, name='python', long_name='Python version', - minimum_version=(2, 6), **kwargs): - super(PythonDependency, self).__init__( - name=name, long_name=long_name, minimum_version=minimum_version, - **kwargs) - - def _get_version(self): - return _sys.version - - def _get_parsed_version(self): - return _sys.version_info - - -CHECKER['python'] = PythonDependency() +class VirtualDependency (Dependency): + def _check(self): + return '{0} {1}'.format( + self.or_pass['dependency'].full_name(), + self.or_pass['version']) class CommandDependency (Dependency): @@ -487,54 +477,84 @@ class CommandDependency (Dependency): return match.group(1) -def _program_files_paths(*args): - "Utility for generating MS Windows search paths" - pf = _os.environ.get('ProgramFiles', '/usr/bin') - pfx86 = _os.environ.get('ProgramFiles(x86)', pf) - paths = [_os.path.join(pf, *args)] - if pfx86 != pf: - paths.append(_os.path.join(pfx86, *args)) - return paths +class PathCommandDependency (CommandDependency): + """A command that doesn't support --version or equivalent options + On some operating systems (e.g. OS X), a command's executable may + be hard to find, or not exist in the PATH. Work around that by + just checking for the existence of a characteristic file or + directory. Since the characteristic path may depend on OS, + installed version, etc., take a list of paths, and succeed if any + of them exists. + """ + def _get_command_version_stream(self, *args, **kwargs): + raise NotImplementedError() -for command,long_name,minimum_version,paths in [ - ('sh', 'Bourne Shell', None, None), - ('ash', 'Almquist Shell', None, None), - ('bash', 'Bourne Again Shell', None, None), - ('csh', 'C Shell', None, None), - ('ksh', 'KornShell', None, None), - ('dash', 'Debian Almquist Shell', None, None), - ('tcsh', 'TENEX C Shell', None, None), - ('zsh', 'Z Shell', None, None), - ('git', 'Git', (1, 7, 0), None), - ('hg', 'Mercurial', (2, 0, 0), None), - ('EasyMercurial', None, (1, 3), None), - ('pip', None, None, None), - ('sqlite3', 'SQLite 3', None, None), - ('nosetests', 'Nose', (1, 0, 0), None), - ('ipython', 'IPython script', (1, 0), None), - ('emacs', 'Emacs', None, None), - ('xemacs', 'XEmacs', None, None), - ('vim', 'Vim', None, None), - ('vi', None, None, None), - ('nano', 'Nano', None, None), - ('gedit', None, None, None), - ('kate', 'Kate', None, None), - ('notepad++', 'Notepad++', None, - _program_files_paths('Notepad++', 'notepad++.exe')), - ('firefox', 'Firefox', None, - _program_files_paths('Mozilla Firefox', 'firefox.exe')), - ('google-chrome', 'Google Chrome', None, - _program_files_paths('Google', 'Chrome', 'Application', 'chrome.exe') - ), - ('chromium', 'Chromium', None, None), - ]: - if not long_name: - long_name = command - CHECKER[command] = CommandDependency( - command=command, paths=paths, long_name=long_name, - minimum_version=minimum_version) -del command, long_name, minimum_version, paths # cleanup namespace + def _get_version_stream(self, *args, **kwargs): + raise NotImplementedError() + + def _get_version(self): + for path in self.paths: + if _os.path.exists(path): + return None + raise DependencyError( + checker=self, + message=( + 'nothing exists at any of the expected paths for {0}:\n {1}' + ).format( + self.full_name(), + '\n '.join(p for p in self.paths))) + + +class UserTaskDependency (Dependency): + "Prompt the user to complete a task and check for success" + def __init__(self, prompt, **kwargs): + super(UserTaskDependency, self).__init__(**kwargs) + self.prompt = prompt + + def _check(self): + if _sys.version_info >= (3, ): + result = input(self.prompt) + else: # Python 2.x + result = raw_input(self.prompt) + return self._check_result(result) + + def _check_result(self, result): + raise NotImplementedError() + + +class EditorTaskDependency (UserTaskDependency): + def __init__(self, **kwargs): + self.path = _os.path.expanduser(_os.path.join( + '~', 'swc-installation-test.txt')) + self.contents = 'Hello, world!' + super(EditorTaskDependency, self).__init__( + prompt=( + 'Open your favorite text editor and create the file\n' + ' {0}\n' + 'containing the line:\n' + ' {1}\n' + 'Press enter here after you have done this.\n' + 'You may remove the file after you have finished testing.' + ).format(self.path, self.contents), + **kwargs) + + def _check_result(self, result): + message = None + try: + with open(self.path, 'r') as f: + contents = f.read() + except IOError as e: + raise DependencyError( + checker=self, + message='could not open {0!r}: {1}'.format(self.path, e) + )# from e + if contents.strip() != self.contents: + raise DependencyError( + checker=self, + message=( + 'file contents ({0!r}) did not match the expected {1!r}' + ).format(contents, self.contents)) class MakeDependency (CommandDependency): @@ -569,9 +589,6 @@ class MakeDependency (CommandDependency): self.version_options = version_options -CHECKER['make'] = MakeDependency(command='make', minimum_version=None) - - class EasyInstallDependency (CommandDependency): def _get_version(self): try: @@ -587,65 +604,18 @@ class EasyInstallDependency (CommandDependency): self.version_stream = version_stream -CHECKER['easy_install'] = EasyInstallDependency( - command='easy_install', long_name='Setuptools easy_install', - minimum_version=None) - - -CHECKER['py.test'] = CommandDependency( - command='py.test', version_stream='stderr', - minimum_version=None) - - -class PathCommandDependency (CommandDependency): - """A command that doesn't support --version or equivalent options - - On some operating systems (e.g. OS X), a command's executable may - be hard to find, or not exist in the PATH. Work around that by - just checking for the existence of a characteristic file or - directory. Since the characteristic path may depend on OS, - installed version, etc., take a list of paths, and succeed if any - of them exists. - """ - def _get_command_version_stream(self, *args, **kwargs): - raise NotImplementedError() - - def _get_version_stream(self, *args, **kwargs): - raise NotImplementedError() +class PythonDependency (Dependency): + def __init__(self, name='python', long_name='Python version', + minimum_version=(2, 6), **kwargs): + super(PythonDependency, self).__init__( + name=name, long_name=long_name, minimum_version=minimum_version, + **kwargs) def _get_version(self): - for path in self.paths: - if _os.path.exists(path): - return None - raise DependencyError( - checker=self, - message=( - 'nothing exists at any of the expected paths for {0}:\n {1}' - ).format( - self.full_name(), - '\n '.join(p for p in self.paths))) - + return _sys.version -for paths,name,long_name in [ - ([_os.path.join(_ROOT_PATH, 'Applications', 'Sublime Text 2.app')], - 'sublime-text', 'Sublime Text'), - ([_os.path.join(_ROOT_PATH, 'Applications', 'TextMate.app')], - 'textmate', 'TextMate'), - ([_os.path.join(_ROOT_PATH, 'Applications', 'TextWrangler.app')], - 'textwrangler', 'TextWrangler'), - ([_os.path.join(_ROOT_PATH, 'Applications', 'Safari.app')], - 'safari', 'Safari'), - ([_os.path.join(_ROOT_PATH, 'Applications', 'Xcode.app'), # OS X >=1.7 - _os.path.join(_ROOT_PATH, 'Developer', 'Applications', 'Xcode.app' - ) # OS X 1.6, - ], - 'xcode', 'Xcode'), - ]: - if not long_name: - long_name = name - CHECKER[name] = PathCommandDependency( - command=None, paths=paths, name=name, long_name=long_name) -del paths, name, long_name # cleanup namespace + def _get_parsed_version(self): + return _sys.version_info class PythonPackageDependency (Dependency): @@ -681,6 +651,122 @@ class PythonPackageDependency (Dependency): return version +class MercurialPythonPackage (PythonPackageDependency): + def _get_version(self): + try: # mercurial >= 1.2 + package = _importlib.import_module('mercurial.util') + except ImportError as e: # mercurial <= 1.1.2 + package = self._get_package('mercurial.version') + return package.get_version() + else: + return package.version() + + +class TornadoPythonPackage (PythonPackageDependency): + def _get_version_from_package(self, package): + return package.version + + def _get_parsed_version(self): + package = self._get_package(self.package) + return package.version_info + + +class SQLitePythonPackage (PythonPackageDependency): + def _get_version_from_package(self, package): + return _sys.version + + def _get_parsed_version(self): + return _sys.version_info + + +def _program_files_paths(*args): + "Utility for generating MS Windows search paths" + pf = _os.environ.get('ProgramFiles', '/usr/bin') + pfx86 = _os.environ.get('ProgramFiles(x86)', pf) + paths = [_os.path.join(pf, *args)] + if pfx86 != pf: + paths.append(_os.path.join(pfx86, *args)) + return paths + + +CHECKER['python'] = PythonDependency() + + +for command,long_name,minimum_version,paths in [ + ('sh', 'Bourne Shell', None, None), + ('ash', 'Almquist Shell', None, None), + ('bash', 'Bourne Again Shell', None, None), + ('csh', 'C Shell', None, None), + ('ksh', 'KornShell', None, None), + ('dash', 'Debian Almquist Shell', None, None), + ('tcsh', 'TENEX C Shell', None, None), + ('zsh', 'Z Shell', None, None), + ('git', 'Git', (1, 7, 0), None), + ('hg', 'Mercurial', (2, 0, 0), None), + ('EasyMercurial', None, (1, 3), None), + ('pip', None, None, None), + ('sqlite3', 'SQLite 3', None, None), + ('nosetests', 'Nose', (1, 0, 0), None), + ('ipython', 'IPython script', (1, 0), None), + ('emacs', 'Emacs', None, None), + ('xemacs', 'XEmacs', None, None), + ('vim', 'Vim', None, None), + ('vi', None, None, None), + ('nano', 'Nano', None, None), + ('gedit', None, None, None), + ('kate', 'Kate', None, None), + ('notepad++', 'Notepad++', None, + _program_files_paths('Notepad++', 'notepad++.exe')), + ('firefox', 'Firefox', None, + _program_files_paths('Mozilla Firefox', 'firefox.exe')), + ('google-chrome', 'Google Chrome', None, + _program_files_paths('Google', 'Chrome', 'Application', 'chrome.exe') + ), + ('chromium', 'Chromium', None, None), + ]: + if not long_name: + long_name = command + CHECKER[command] = CommandDependency( + command=command, paths=paths, long_name=long_name, + minimum_version=minimum_version) +del command, long_name, minimum_version, paths # cleanup namespace + + +CHECKER['make'] = MakeDependency(command='make', minimum_version=None) + + +CHECKER['easy_install'] = EasyInstallDependency( + command='easy_install', long_name='Setuptools easy_install', + minimum_version=None) + + +CHECKER['py.test'] = CommandDependency( + command='py.test', version_stream='stderr', + minimum_version=None) + + +for paths,name,long_name in [ + ([_os.path.join(_ROOT_PATH, 'Applications', 'Sublime Text 2.app')], + 'sublime-text', 'Sublime Text'), + ([_os.path.join(_ROOT_PATH, 'Applications', 'TextMate.app')], + 'textmate', 'TextMate'), + ([_os.path.join(_ROOT_PATH, 'Applications', 'TextWrangler.app')], + 'textwrangler', 'TextWrangler'), + ([_os.path.join(_ROOT_PATH, 'Applications', 'Safari.app')], + 'safari', 'Safari'), + ([_os.path.join(_ROOT_PATH, 'Applications', 'Xcode.app'), # OS X >=1.7 + _os.path.join(_ROOT_PATH, 'Developer', 'Applications', 'Xcode.app' + ) # OS X 1.6, + ], + 'xcode', 'Xcode'), + ]: + if not long_name: + long_name = name + CHECKER[name] = PathCommandDependency( + command=None, paths=paths, name=name, long_name=long_name) +del paths, name, long_name # cleanup namespace + + for package,name,long_name,minimum_version,and_dependencies in [ ('nose', None, 'Nose Python package', CHECKER['nosetests'].minimum_version, None), @@ -715,112 +801,26 @@ for package,name,long_name,minimum_version,and_dependencies in [ del package, name, long_name, minimum_version, and_dependencies, kwargs -class MercurialPythonPackage (PythonPackageDependency): - def _get_version(self): - try: # mercurial >= 1.2 - package = _importlib.import_module('mercurial.util') - except ImportError as e: # mercurial <= 1.1.2 - package = self._get_package('mercurial.version') - return package.get_version() - else: - return package.version() - - CHECKER['mercurial'] = MercurialPythonPackage( package='mercurial.util', name='mercurial', long_name='Mercurial Python package', minimum_version=CHECKER['hg'].minimum_version) -class TornadoPythonPackage (PythonPackageDependency): - def _get_version_from_package(self, package): - return package.version - - def _get_parsed_version(self): - package = self._get_package(self.package) - return package.version_info - - CHECKER['tornado'] = TornadoPythonPackage( package='tornado', name='tornado', long_name='Tornado', minimum_version=(2, 0)) -class SQLitePythonPackage (PythonPackageDependency): - def _get_version_from_package(self, package): - return _sys.version - - def _get_parsed_version(self): - return _sys.version_info - - CHECKER['sqlite3-python'] = SQLitePythonPackage( package='sqlite3', name='sqlite3-python', long_name='SQLite Python package', minimum_version=CHECKER['sqlite3'].minimum_version) -class UserTaskDependency (Dependency): - "Prompt the user to complete a task and check for success" - def __init__(self, prompt, **kwargs): - super(UserTaskDependency, self).__init__(**kwargs) - self.prompt = prompt - - def _check(self): - if _sys.version_info >= (3, ): - result = input(self.prompt) - else: # Python 2.x - result = raw_input(self.prompt) - return self._check_result(result) - - def _check_result(self, result): - raise NotImplementedError() - - -class EditorTaskDependency (UserTaskDependency): - def __init__(self, **kwargs): - self.path = _os.path.expanduser(_os.path.join( - '~', 'swc-installation-test.txt')) - self.contents = 'Hello, world!' - super(EditorTaskDependency, self).__init__( - prompt=( - 'Open your favorite text editor and create the file\n' - ' {0}\n' - 'containing the line:\n' - ' {1}\n' - 'Press enter here after you have done this.\n' - 'You may remove the file after you have finished testing.' - ).format(self.path, self.contents), - **kwargs) - - def _check_result(self, result): - message = None - try: - with open(self.path, 'r') as f: - contents = f.read() - except IOError as e: - raise DependencyError( - checker=self, - message='could not open {0!r}: {1}'.format(self.path, e) - )# from e - if contents.strip() != self.contents: - raise DependencyError( - checker=self, - message=( - 'file contents ({0!r}) did not match the expected {1!r}' - ).format(contents, self.contents)) - - CHECKER['other-editor'] = EditorTaskDependency( name='other-editor', long_name='') -class VirtualDependency (Dependency): - def _check(self): - return '{0} {1}'.format( - self.or_pass['dependency'].full_name(), - self.or_pass['version']) - - for name,long_name,dependencies in [ ('virtual-shell', 'command line shell', ( 'bash', -- 2.26.2