def __init__(self, source_file, destination_file, target_md5_data, logger, private_key = ''): self.source_file = source_file self.destination_file = destination_file self.destination_dir = rpartition(destination_file, os.path.sep)[0] self.logger = logger self.processors = [] self.start_dir = get_slash_cwd() options = self.determine_options(self.source_file) if not options: self.writer = NullWriter(self.logger) else: self.writer = FileWriter(self.destination_file, target_md5_data, self.logger) if JOIN in options: self.reader = JoinReader(self.source_file, target_md5_data, self.logger) else: self.reader = FileReader(self.source_file, target_md5_data, self.logger) if DECRYPT in options: if not private_key: raise NoPrivateKeyException() source_file_name = self.source_file.partition('.enc')[0] decryptor = Decryptor(source_file_name, private_key, target_md5_data, self.logger) self.processors.append(decryptor) if UNCOMPRESS in options: source_file_name = self.source_file.partition('.bz2')[0] uncompressor = UnCompressor(source_file_name, target_md5_data, self.logger) self.processors.append(uncompressor)
def start(): cwd = get_slash_cwd() INSTANCE_WORK_DIR = make_path(cwd, 'spkg', "TEST_INSTANCE") status_file = make_path(INSTANCE_WORK_DIR, "status.yml") spkg_work_dir = make_path(cwd, 'spkg') config_data = {"spkg_path": spkg_work_dir} os.system('bash -c "mkdir -p %s"' % INSTANCE_WORK_DIR) os.system('bash -c "mkdir %s/tmp"' % INSTANCE_WORK_DIR) #print "Copying repository files..." os.system('bash -c "cp -ax repos/ %s"' % spkg_work_dir) if not os.path.isdir(PKG_DIR): #print "Making %s..." % PKG_DIR os.system('bash -c "mkdir -p %s"' % PKG_DIR) #print "Copying package files..." os.system('bash -c "cd repos/type4 && tar -czmf TestPackage-7.spkg TestPackage-7"') os.system('bash -c "cp repos/type4/*.spkg %s"' % PKG_DIR) #print "Creating %s" % status_file open(status_file, 'w').write("{}") #print "Creating %s" % CONFIG_FILE current_config = get_current_config() if current_config: if "spkg_path" in current_config: current_config["old_spkg_path"] = current_config["spkg_path"] current_config["spkg_path"] = spkg_work_dir else: current_config = config_data config_fp = open(CONFIG_FILE, 'w') config_fp.write(yaml_dump(current_config)) config_fp.flush() config_fp.close()
def hunt_and_explode(self): ''' Used to find all type-5 package data in the repository and untar the files properly. ''' base_path = make_path(get_spkg_path(), "repos") output = "" if sys.platform == "cli": tfn = "c:/spkg/tmp/tarballs.txt" cmd = 'bash -c "find %s -name \"*.tar.gz\"" > %s' % (base_path, tfn) #Logger.info("CMD: (%s)" % cmd) os.system(cmd) output = open(tfn).read() else: _status, output = gso('find %s -name "*.tar.gz"' % base_path) start_dir = get_slash_cwd() for full_tar_file_name in output.split('\n'): tmp_list = full_tar_file_name.split('/') tar_file_name = tmp_list[-1] base_name = tar_file_name.split('.tar.gz')[0] tar_file_dir = make_path(tmp_list[:-1] + [base_name]) if not os.path.isdir(tar_file_dir): #Logger.info("Exploding %s..." % base_name) cmd = 'bash -c "mkdir -p %s"' % tar_file_dir #Logger.info("tar_file_dir: %s" % tar_file_dir) status = os.system(cmd) if status == OK: cmd = 'bash -c "cd %s && tar -mxzf ../%s"' cmd = cmd % (tar_file_dir, tar_file_name) #Logger.info("Cmd: %s" % cmd) if os.system(cmd) != OK: msg = "could not untar %s" % (tar_file_name) raise Exceptions.BadPackage(full_tar_file_name, msg) os.chdir(start_dir)
def test_uninstall_error_script(self): self.repository.pkg_data = {"pkg1": {"install": {"fullName": "TestBadUninstall"}, "package-version": 4}} base = make_path(get_slash_cwd(), "packages", "TestBadUninstall") package = PackageV4("pkg1", self.repository, self.config, INSTANCE) package.initialize() package.uninstall() assert package.status == FAIL, "Uninstallation of a package that returns an error succeeded"
def test_uninstall_ok_package(self): self.repository.pkg_data = {"pkg1": {"install": {"fullName": "TestPackage-7"}, "package-version": 4}} base = make_path(get_slash_cwd(), "packages", "TestPackage-7") package = PackageV4("pkg1", self.repository, self.config, INSTANCE) package.initialize() package.uninstall() assert package.status == OK, "Legitimate package uninstallation failed"
def execute_maint_script(self, script_name): ''' execute a user-defined function script_name -- name of the function to run ''' Package.execute_maint_script(self) script_path = "%s/%s.py" % (self.maint_dir, script_name) start_dir = get_slash_cwd() os.chdir(self.maint_dir) if not os.path.isfile(script_path): msg = "%s does not exist" % script_path raise BadPackage, (self.name, msg) sys.path.append(self.maint_dir ) import_string = 'import %s' % script_name cmd = 'status = %s.execute(self.config, Logger)' % script_name job = Job(import_string, cmd, self.config) status = job.execute() sys.path.remove(self.maint_dir) os.chdir(start_dir) if status == None: status = OK if type(status) != type(1): msg = "Invalid status type (%s: '%s')" Logger.warning(msg % (type(status), status)) status = FAIL return status
def test_verify_bad_package(self): self.repository.pkg_data = {"pkg1": {"install": {"fullName": "TestBadVerify"}, "package-version": 4}} base = make_path(get_slash_cwd(), "packages", "TestBadVerify") package = PackageV4("pkg1", self.repository, self.config, INSTANCE) package.initialize() package.action = VERIFY package.verify() assert package.status == FAIL, "Verification of a package that returns an error succeeded"
def _find_cmd(self, action, arguments=[], future_pkns=[], dry_run=False): ''' Perform the action on the system, importing modules from the package and running the appropriate method on the class within. action -- INSTALL, UNINSTALL, CONFIGURE, VERIFY future_pkns -- future package names. Some packages want to know about the packages that will come after them dry_run -- boolean flag to see if we're really going to do this ''' ret_val = None if type(action) == type(1): action = ACTION_REVERSE_LOOKUP[action] cwd = get_slash_cwd() obj, rand_string = self._get_object(future_pkns) try: if not hasattr(obj, action): msg = "Class %s does not have a %s method." raise BadPackage(self.name, msg % (self.class_name, action)) if not dry_run: if arguments: if ACTION_LOOKUP.get(action) == RESTORE: if len(arguments) != 1: Logger.error("Incorrect number of arguments passed to restore") return FAIL restore_path = make_path(get_spkg_path(), "archive", self.name, str(arguments[0])) if not os.path.isdir(restore_path): msg = "Cannot execute restore: archive data does not "\ "exist in %s" % (restore_path) Logger.error(msg) return FAIL self._prepare_restore(obj, restore_path) exec("ret_val = obj.%s('%s')" % (action, restore_path)) else: exec("ret_val = obj.%s()" % (action)) else: ret_val = OK self._cleanup(obj) del rand_string except SystemExit, err: if err.code: ret_val = err.code else: ret_val = OK del rand_string
def get_type_4(self, full_name): ''' Get a type-4 package from the filesystem, and process it full_name -- name of package (with version) ''' pkg_dir = get_package_path(self.instance_name) os.system('bash -c "mkdir -p %s"' % pkg_dir) pkg_path = make_path(pkg_dir, full_name) if not os.path.isfile(pkg_path + ".spkg"): erstr = "No package file in %s." % (pkg_path + ".spkg") Logger.error(erstr) raise Exceptions.BadPackage(full_name, erstr) if sys.platform != 'win32': cmd = 'bash -c "cd %s && tar -mxzf %s.spkg"' % (pkg_dir, full_name) Logger.info("Untarring with command: %s" %cmd) if not os.system(cmd) == OK: raise Exceptions.BadPackage(full_name, "Could not unpack") return OK if self.unzip_type_5(pkg_path, full_name) == FAIL: raise Exceptions.BadPackage(full_name, "could not unzip") # tar = tarfile.open(pkg_path + ".tar", "r") # tar.errorlevel = 2 cwd = get_slash_cwd() os.chdir(pkg_dir) # for tarinfo in tar: # try: # tar.extract(tarinfo) # except tarfile.ExtractError, err: # Logger.warning("Error with package %s,%s: "\ # "%s" % (full_name, tarinfo.name, err)) # tar.close() if not os.path.isdir(make_path(pkg_path, full_name)): erstr = "Package %s is malformed." % (full_name) os.chdir(cwd) raise Exceptions.BadPackage(full_name, erstr) os.chdir(cwd) # os.unlink(pkg_path + ".tar") return OK
def create_directory_structure(self): self.start_dir = get_slash_cwd() force_remove_dir(self.test_dir) log_command( 'bash -c "mkdir -p %s"' % self.test_dir ) for subdir in [ 'manifest', make_path('bar','bar2'), 'foo' ]: full_path = make_path(self.test_dir, subdir) log_command('bash -c "mkdir -p %s"' % full_path) fp = open( make_path(self.test_dir, 'bar','file.txt'), 'w' ) fp.write("This is text.") fp.close() del fp fp = open( make_path(self.test_dir, 'bar','bar2','file.aspx'), 'w' ) fp.write(u"Aspx file.") fp.close() del fp fp = open( make_path(self.test_dir, 'foo','file.exe'), 'w' ) fp.write("\x45\x90\x08\x21\x05") fp.close() del fp
import os from bombardier_core.static_data import CLIENT_CONFIG_FILE from bombardier_core.mini_utility import ensure_bombardier_config_dir from bombardier_core.mini_utility import make_path, get_slash_cwd from bombardier_core.mini_utility import yaml_load, yaml_dump cwd = get_slash_cwd() CONFIG_FILE = CLIENT_CONFIG_FILE.replace('\\', '/') INSTANCE_WORK_DIR = make_path(cwd, 'spkg', "TEST_INSTANCE") PKG_DIR = make_path(INSTANCE_WORK_DIR, "packages") STATUS_FILE = make_path(INSTANCE_WORK_DIR, "status.yml") SPKG_WORK_DIR = make_path(cwd, 'spkg') def get_current_config(): ensure_bombardier_config_dir() if os.path.isfile(CONFIG_FILE): current_config = yaml_load(open(CONFIG_FILE).read()) if type(current_config) == type({}): return current_config else: return None def start(): cwd = get_slash_cwd() INSTANCE_WORK_DIR = make_path(cwd, 'spkg', "TEST_INSTANCE") status_file = make_path(INSTANCE_WORK_DIR, "status.yml") spkg_work_dir = make_path(cwd, 'spkg') config_data = {"spkg_path": spkg_work_dir} os.system('bash -c "mkdir -p %s"' % INSTANCE_WORK_DIR) os.system('bash -c "mkdir %s/tmp"' % INSTANCE_WORK_DIR) #print "Copying repository files..."
def _find_cmd(self, action, argument='', future_pkns=[], dry_run=False): ''' Perform the action on the system, importing modules from the package and running the appropriate method on the class within. action -- INSTALL, UNINSTALL, CONFIGURE, VERIFY future_pkns -- future package names. Some packages want to know about the packages that will come after them dry_run -- boolean flag to see if we're really going to do this ''' cwd = get_slash_cwd() sys.path.insert(0, self.scripts_dir) os.chdir(self.scripts_dir) files = self._get_possible_module_files() status = FAIL file_found = False for file_name in files: try: obj = Spkg.SpkgV4(self.config, logger=Logger) os.chdir(self.working_dir) letters = [ chr( x ) for x in range(65, 91) ] random.shuffle(letters) rand_string = ''.join(letters) exec("import %s as %s" % (file_name, rand_string)) self.config["__FUTURE_PACKAGES__"] = future_pkns self.config["__INSTANCE__"] = self.instance_name cmd_str = "obj = %s.%s(self.config, Logger)" exec(cmd_str % (rand_string, file_name)) file_found = True if not dry_run: if action == INSTALL: status = obj.installer() elif action == VERIFY: status = obj.verify() elif action == UNINSTALL: status = obj.uninstaller() elif action == CONFIGURE: status = obj.configure() else: raise FeatureRemovedException(action) else: status = OK del rand_string if file_name in sys.modules: sys.modules.pop(file_name) while self.scripts_dir in sys.path: sys.path.remove(self.scripts_dir) break except ImportError: msg = "File %s is not runnable. Looking for others" % file_name Logger.debug(msg) continue except SystemExit, err: if err.code: status = err.code else: status = 0 file_found = True break except KeyboardInterrupt: Logger.error("Keyboard interrupt detected. Exiting...") sys.exit(10)