Best Python code snippet using molecule_python
package_managers.py
Source:package_managers.py
1'''2Package management BuildTargets.3Copyright (c) 2015 - 2021 Rob "N3X15" Nelson <nexisentertainment@gmail.com>4Permission is hereby granted, free of charge, to any person obtaining a copy5of this software and associated documentation files (the "Software"), to deal6in the Software without restriction, including without limitation the rights7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell8copies of the Software, and to permit persons to whom the Software is9furnished to do so, subject to the following conditions:10The above copyright notice and this permission notice shall be included in all11copies or substantial portions of the Software.12THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,17OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE18SOFTWARE.19'''20import json21import os, shutil22from buildtools import os_utils, utils23from buildtools.maestro.base_target import SingleBuildTarget24class _NPMLikeBuildTarget(SingleBuildTarget):25 def __init__(self, invocation, base_command=None, working_dir='.', opts=[], files=[], target=None, dependencies=[], exe_path=None, specfile=None, lockfile=None, modules_dir=None):26 self.specfile = specfile27 self.lockfile = lockfile28 self.working_dir = working_dir29 self.opts = opts30 self.exe_path = exe_path31 self.base_command = base_command32 self.invocation = invocation33 self.modules_dir = modules_dir34 if self.exe_path is None:35 self.exe_path = os_utils.which(invocation)36 super().__init__(target=target, files=files, dependencies=dependencies)37 def get_lockfile(self):38 return self.lockfile39 def get_specfile(self):40 return self.specfile41 def clean(self):42 super().clean()43 if self.modules_dir is not None and os.path.isdir(self.modules_dir):44 for filename in os_utils.get_file_list(self.modules_dir):45 self.removeFile(filename)46 shutil.rmtree(self.modules_dir)47 def get_config(self):48 return {49 'working_dir': self.working_dir,50 'opts': self.opts,51 'exe_path': self.exe_path,52 'base_command': self.base_command,53 'invocation': self.invocation,54 'specfile': {55 'filename': self.get_specfile(),56 'md5': utils.md5sum(self.get_specfile()) if self.get_specfile() is not None else None,57 },58 'lockfile': {59 'filename': self.get_lockfile(),60 'md5': utils.md5sum(self.get_lockfile()) if self.get_lockfile() is not None else None,61 }62 }63 def buildOpts(self):64 return [self.exe_path] + self.opts65 def build(self):66 opts = self.buildOpts()67 with os_utils.Chdir(self.working_dir):68 os_utils.cmd([self.exe_path] + self.opts, show_output=True, echo=self.should_echo_commands(), critical=True)69 if not os.path.isfile(self.target):70 self.touch(self.target)71class YarnBuildTarget(_NPMLikeBuildTarget):72 BT_TYPE = 'Yarn'73 BT_LABEL = 'YARN'74 def __init__(self, base_command=None, working_dir='.', opts=[], modules_dir='node_modules', target=None, dependencies=[], yarn_path=None, lockfile=None):75 if target is None:76 target = os.path.join(working_dir, modules_dir, '.yarn-integrity')77 super().__init__('yarn', modules_dir=modules_dir, working_dir=working_dir, opts=opts, target=target, exe_path=yarn_path, files=[os.path.join(working_dir, 'package.json')], dependencies=[])78class NPMBuildTarget(_NPMLikeBuildTarget):79 BT_TYPE = 'NPM'80 BT_LABEL = 'NPM'81 def __init__(self, base_command=None, working_dir='.', opts=[], modules_dir='node_modules', target=None, dependencies=[], npm_path=None):82 if target is None:83 target = os.path.join(working_dir, modules_dir, '.npm.target')84 super().__init__('npm', working_dir=working_dir, modules_dir=modules_dir, opts=opts, target=target, exe_path=npm_path, files=[os.path.join(working_dir, 'package.json')], dependencies=[])85class BowerBuildTarget(_NPMLikeBuildTarget):86 BT_TYPE = 'Bower'87 BT_LABEL = 'BOWER'88 def __init__(self, base_command=None, working_dir='.', opts=[], modules_dir='bower_components', target=None, dependencies=[], bower_path=None):89 if target is None:90 target = os.path.join(working_dir, modules_dir, '.bower.target')91 super().__init__('bower', working_dir=working_dir, modules_dir=modules_dir, opts=opts, target=target, exe_path=bower_path, files=[os.path.join(working_dir, 'bower.json')], dependencies=[])92class GruntBuildTarget(_NPMLikeBuildTarget):93 BT_TYPE = 'Grunt'94 BT_LABEL = 'GRUNT'95 def __init__(self, base_command=None, working_dir='.', opts=[], target=None, dependencies=[], grunt_path=None):96 if target is None:97 target = os.path.join(working_dir, 'tmp', '.grunt.target')98 super().__init__('grunt', working_dir=working_dir, opts=opts, target=target, exe_path=grunt_path, files=[os.path.join(working_dir, 'Gruntfile.js')], dependencies=[])99class ComposerBuildTarget(_NPMLikeBuildTarget):100 BT_TYPE = 'Composer'101 BT_LABEL = 'COMPOSER'102 def __init__(self, base_command='install', working_dir='.', opts=[], modules_dir='vendor', target=None, dependencies=[], composer_path=None, composer_json=None, composer_lock=None, composer_bin_dir=None):103 if composer_json is None:104 composer_json = os.path.abspath(os.path.join(working_dir, 'composer.json'))105 if composer_lock is None:106 composer_lock = os.path.abspath(os.path.join(working_dir, 'composer.lock'))107 if target is None:108 target = os.path.abspath(os.path.join(working_dir, modules_dir, '.composer.target'))109 self.composer_bin_dir = composer_bin_dir110 super().__init__('composer', base_command=base_command, modules_dir=modules_dir, working_dir=working_dir, opts=opts, target=target, exe_path=composer_path, files=[], dependencies=[], specfile=composer_json, lockfile=composer_lock)111 self.detectAutoloadedFiles()112 def processOpts(self):113 o = [self.exe_path, self.base_command] + self.opts114 o += ['-d', self.working_dir]115 return o116 def detectAutoloadedFiles(self):117 packagedata = {}118 if os.path.isfile(self.specfile):119 with open(self.specfile, 'r') as f:120 packagedata = json.load(f)121 for autoload_type, autoload_namespaces in packagedata.get('autoload', {}).items():122 if autoload_type == 'psr-4':123 for nspath in autoload_namespaces.values():124 for filename in os_utils.get_file_list(nspath, '.'):125 phpfn = os.path.abspath(filename)126 self.files += [phpfn]127 self.files = list(set(self.files))128 def build(self):129 env = os_utils.ENV.clone()130 env.set('COMPOSER', self.specfile, noisy=self.should_echo_commands())131 if self.modules_dir is not None:132 env.set('COMPOSER_VENDOR_DIR', self.modules_dir, noisy=self.should_echo_commands())133 if self.composer_bin_dir is not None:134 env.set('COMPOSER_BIN_DIR', self.composer_bin_dir, noisy=self.should_echo_commands())135 cmdline = self.processOpts()136 os_utils.cmd(cmdline, show_output=True, echo=self.should_echo_commands(), critical=True, env=env)137 if os.path.isfile(self.target):138 self.touch(self.target)139class BrowserifyBuildTarget(_NPMLikeBuildTarget):140 BT_TYPE = 'Browserify'141 BT_LABEL = 'BROWSERIFY'142 def __init__(self, base_command=None, working_dir='.', opts=[], target=None, files=[], dependencies=[], browserify_path=None):...
bootstrap.py
Source:bootstrap.py
1#!/usr/bin/env python2# See: docs/bootstrap/readme.md3import os4import sys5import imp6import logging7# Required arguments:8# * Automatic argument - path to script as appeared in the command line.9start_path = sys.argv[0]10# * Action to run (i.e. `build`, `deploy`, etc.).11run_action = sys.argv[1]12# * Use case for action (i.e. `offline-minion-installer`).13run_use_case = sys.argv[2]14# * Target environment - path to configuration file as:15# ```16# path/to/conf/project_name/profile_name/host_id.py`17# ```18target_env_conf = sys.argv[3]19# * The last argument is optional (used for convenience of development) -20# path to `bootstrap.dir` with generated content (to keep this content21# outside of source directory).22# For "production" environment, `content_dir` is usually the same23# with `modules_dir` and contains everything:24# script sources, configuration, resources, etc.25content_dir = None26if len(sys.argv) > 4:27 content_dir = sys.argv[4]28sys.stderr.write("debug: initial content_dir = " + str(content_dir) + "\n") # before log level is set29# Path to script is _always_ derived from command line.30# NOTE: In other words, script is not accessible from PATH env var.31script_dir = os.path.dirname(start_path)32sys.stderr.write("debug: script_dir = " + str(script_dir) + "\n") # before log level is set33# Remember `run_dir`:34run_dir = os.getcwd()35sys.stderr.write("debug: run_dir = " + str(run_dir) + "\n") # before log level is set36# Redefine `content_dir` as absolute path.37if content_dir:38 if not os.path.isabs(content_dir):39 content_dir = os.path.join(40 run_dir,41 content_dir,42 )43else:44 if os.path.isabs(script_dir):45 content_dir = os.path.join(46 script_dir,47 )48 else:49 content_dir = os.path.join(50 run_dir,51 script_dir,52 )53sys.stderr.write("debug: finalized content_dir = " + str(content_dir) + "\n") # before log level is set54# Determine `modules_dir`.55# Variable `modules_dir`is _always_ derived from command line.56if os.path.isabs(script_dir):57 modules_dir = os.path.join(58 script_dir,59 'modules',60 )61else:62 modules_dir = os.path.join(63 run_dir,64 script_dir,65 'modules',66 )67sys.stderr.write("debug: modules_dir = " + str(modules_dir) + "\n") # before log level is set68# Add `modules` to import path.69sys.path.append(70 modules_dir,71)72# Import modules related to `bootstrap` after extending73# list of import directories.74import utils.set_log75# Set log level.76utils.set_log.setLoggingLevel('debug')77# Check if bootstrap supports this `run_use_case`.78if run_use_case not in [79 'initial-online-node',80 'offline-minion-installer',81]:82 raise Exception("Unknown use case: " + str(run_use_case))83# Compose path to configuration module.84# NOTE: Even though configuration is also a module, it is still content85# and looked up in the `content_dir`.86conf_module_path = os.path.join(87 content_dir,88 target_env_conf,89)90logging.info('conf_module_path = ' + conf_module_path)91# Load config module.92conf_m = imp.load_source('conf_m', conf_module_path)93# Compose path to platform implementation module.94impl_module_path = os.path.join(95 modules_dir,96 'platforms',97 conf_m.target_platform + '.py',98)99logging.info('impl_module_path = ' + impl_module_path)100# Load implementation module.101impl_m = imp.load_source('impl_m', impl_module_path)102logging.debug('impl_m = ' + str(impl_m))103# Create instance.104impl_i = impl_m.get_instance(105 run_dir,106 script_dir,107 content_dir,108 modules_dir,109 conf_m,110 run_action,111 run_use_case,112 target_env_conf,113)114# Run action....
test_module.py
Source:test_module.py
1import os2import unittest3import juce4modules_dir = os.path.join(os.path.dirname(__file__), 'resources', 'modules')5class TsetModuleClass(unittest.TestCase):6 def test_ismodule(self):7 self.assertFalse(juce.ismodule(os.path.join(modules_dir, 'test_invalid_id')))8 self.assertFalse(juce.ismodule(os.path.join(modules_dir, 'test_invalid_vendor')))9 self.assertFalse(juce.ismodule(os.path.join(modules_dir, 'test_missing_description')))10 self.assertFalse(juce.ismodule(os.path.join(modules_dir, 'test_missing_header')))11 self.assertFalse(juce.ismodule(os.path.join(modules_dir, 'test_missing_id')))12 self.assertFalse(juce.ismodule(os.path.join(modules_dir, 'test_missing_module')))13 self.assertFalse(juce.ismodule(os.path.join(modules_dir, 'test_missing_vendor')))14 self.assertFalse(juce.ismodule(os.path.join(modules_dir, 'test_missing_version')))15 self.assertTrue(juce.ismodule(os.path.join(modules_dir, 'test_valid_module')))16 def test_invalid_id(self):17 with self.assertRaises(ValueError):18 juce.Module(os.path.join(modules_dir, 'test_invalid_id'))19 def test_invalid_vendor(self):20 with self.assertRaises(ValueError):21 juce.Module(os.path.join(modules_dir, 'test_invalid_vendor'))22 def test_missing_description(self):23 with self.assertRaises(ValueError):24 juce.Module(os.path.join(modules_dir, 'test_missing_description'))25 def test_missing_header(self):26 with self.assertRaises(IOError):27 juce.Module(os.path.join(modules_dir, 'test_missing_header'))28 def test_missing_id(self):29 with self.assertRaises(ValueError):30 juce.Module(os.path.join(modules_dir, 'test_missing_id'))31 def test_missing_module(self):32 with self.assertRaises(IOError):33 juce.Module(os.path.join(modules_dir, 'test_missing_module'))34 def test_missing_vendor(self):35 with self.assertRaises(ValueError):36 juce.Module(os.path.join(modules_dir, 'test_missing_vendor'))37 def test_missing_version(self):38 with self.assertRaises(ValueError):39 juce.Module(os.path.join(modules_dir, 'test_missing_version'))40 def test_valid_module(self):41 try:42 module = juce.Module(os.path.join(modules_dir, 'test_valid_module'))43 except IOError:44 self.fail('Unexpected IOError while loading a valid module')45 except ValueError:46 self.fail('Unexpected ValueError while loading a valid module')47 except:48 self.fail('Unexpected error while loading a valid module')49 else:50 self.assertTrue(module.ID == 'test_valid_module')51 self.assertTrue(module.vendor == 'vendor')52 self.assertTrue(module.version == '1.0.0')53 self.assertTrue(module.name == 'name')54 self.assertTrue(module.description == 'description')55 try:56 module.version = '1.2.3'57 except:58 self.fail('Unexpected error setting the version number')59 else:60 self.assertTrue(module.version == '1.2.3')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!