def download_files(context): root_path = ConfigAdapter.get_testdata_path() output_path = ConfigAdapter.get_output_path(product='linux') for row in context.table.rows: raw_download_path = row.get('path') raw_download_output = row.get('output') raw_download_ext = row.get('extensions') #add other parameters args = {} download_path = os.path.join(root_path, raw_download_path) args['path'] = download_path if raw_download_output: download_output = os.path.join(output_path, raw_download_output) FileHelper.create_directory(download_output) args['output'] = download_output if raw_download_ext: args['extensions'] = raw_download_ext #TODO: add overwrite as condition args['overwrite'] = None output = LinuxGUIClient.download_cmd.download(**args) LogHelper.info(output)
def download_and_install(cls, build, job): volume_name = cls.get_subdir() if volume_name is not None: CmdHelper.run("diskutil eject '" + volume_name + "'") LogHelper.info( "Prepare download Mozy Restore Manager from jenkins, build number is " + job) jh = JenkinsHelper(GLOBAL_CONFIG["JENKINS"]["URL"], GLOBAL_CONFIG["JENKINS"]["USERNAME"], GLOBAL_CONFIG["JENKINS"]["KEY"]) dest = ConfigAdapter.get_installer_path('MACFRYR') LogHelper.info('Clean up existing files') for file in FileHelper.find_file(dest, '*'): LogHelper.debug('Delete file %s' % file) FileHelper.delete_file(file) pattern = 'mozy-fryr' packages = jh.download_packages(jh.get_packages(job, build, pattern), dest=dest) TARGET_APP = '/Applications/Mozy Restore Manager.app' if os.path.exists(TARGET_APP): shutil.rmtree(TARGET_APP) image_path = packages[0] mount_cmd = 'sudo hdiutil attach {package}'.format(package=image_path) CmdHelper.run(mount_cmd) volume_name = cls.get_subdir() ORIGIN_APP = '/Volumes/' + volume_name + '/Mozy Restore Manager.app' shutil.copytree(ORIGIN_APP, TARGET_APP)
def check_md5(target1, target2): string1 = FileHelper.md5(target1) string2 = FileHelper.md5(target2) LogHelper.debug(target1 + " md5: " + string1) LogHelper.debug(target2 + " md5: " + string2) if string1 == string2: return True return False
def test_truncate_file(self): testfile = "%s%s%s" % (TestFileHelper.testdatadir, os.path.sep, "test_truncate.dat") size = 2000 size_truncate = 50 FileHelper.create_file(testfile, overwrite=True, size=size, content="aaa") FileHelper.truncate_file(testfile, size_truncate) new_size = FileHelper.get_file_size(testfile) self.assertEqual(new_size, size_truncate)
def restart(): cmd_output = "error: could not determine how to restart the daemon" if FileHelper.file_exist(LynxCtrl.service_path_upstart): cmd_output = LynxCtrl.restart_upstart() elif FileHelper.file_exist(LynxCtrl.service_path_initd): cmd_output = LynxCtrl.restart_initd() return cmd_output
def decrypt_files(context, source, dest): source_root = ConfigAdapter.get_installer_path('LINUX') source_full_path = os.path.join(source_root, source) LogHelper.info("Decrypt src {source}".format(source=source_full_path)) dest_root = ConfigAdapter.get_output_path('LINUX') dest_full_path = os.path.join(dest_root, dest) FileHelper.create_directory(dest_full_path, True) output = LinuxGUIClient.decrypt_cmd.decrypt(source_full_path, dest_full_path) LogHelper.info("Decrypt output {output}".format(output=output))
def launch_restore_manager(self): db_file = os.path.expanduser( '~/AppData/Local/Mozy Restore Manager/MozyRestoreManager.db') LogHelper.info("Deleting db file at " + db_file) FileHelper.delete_file(db_file) self.app = application.Application() self.app.start( "C:\Program Files\Mozy\Mozy Restore Manager\MozyRestoreManager.exe" ) time.sleep(2)
def stem_impl(context, output_dir): root_path = ConfigAdapter.get_installer_path(product='linux') output_path = os.path.join(ConfigAdapter.get_output_path(product='linux'), output_dir) for mzd_file in FileHelper.find_file(root_path, "*.mzd"): LogHelper.info('restore mzd file %s to %s' % (mzd_file, output_path)) FileHelper.create_directory(output_path) LinuxGUIClient.download_cmd.download(mzd=mzd_file, output=output_path, overwrite=None)
def parse_feature(features, pattern="*.feature"): testfeatures = [] if FileHelper.file_exist(features) and FileHelper.file_ext(features) == "feature": testfeatures.append(features) else: files = FileHelper.find_file(features, pattern) # TODO: refine with FileHelper for file in files: testfeatures.append("features" + file.split("features")[1]) return testfeatures
def upload_attachment(): file = request.files.get('file') if not file: name = request.args.get('name') file = FileStorage(request.stream, filename=name, name=name, headers=request.headers) if file: fh = FileHelper(file) fh.save() if fh.savepath: return f.succeed({'filepath':fh.savepath}) return f.failed(*const.UPLOAD_FAIL)
def step_impl(context, folder): testdata_root = os.path.expanduser("~/{folder}".format(folder=folder)) for row in context.table.rows: if "file_folder" in row.headings or "file_dir" in row.headings: testdata_dir = os.path.join( testdata_root, row.get('file_folder') or row.get('file_dir') or '') LogHelper.debug("Delete dirs " + testdata_dir) FileHelper.delete_directory(testdata_dir)
def test_create_file(self): """ test: create test file """ testfile = "%s%s%s" %(TestFileHelper.testdatadir, os.path.sep, "testfilename") size = 2000 FileHelper.create_file(testfile, size=size ) result = FileHelper.file_exist(testfile) self.assertTrue(result) stat = FileHelper.get_file_stat(testfile) self.assertEqual(stat.st_size,size)
def is_client_installed(self): """ usage: check whether windows client is installed :return: True if installed || False if not installed """ result = False if FileHelper.dir_exist(self.install_path) and FileHelper.file_exist(self.mozyutil) and FileHelper.file_exist(self.configapp) and FileHelper.file_exist(self.statusapp): result = True else: LogHelper.error("ERROR: %s is installed." % self.install_path) return result
def delete_json_file(filename=None, conf_dir=None): if not conf_dir: conf_dir = LynxCtrl.conf_dir if not filename: filename = "*.json" files = FileHelper.find_file(conf_dir, filename) for file_to_delete in files: FileHelper.delete_file(file_to_delete) return files
def clear_restore_folder(folder_name): directory = os.path.expanduser('~/Documents/' + folder_name) if platform.system() == "Windows": directory = "C:/" + folder_name for file in FileHelper.find_file(directory, '*'): LogHelper.debug('Delete file %s' % file) os.remove(file) for folder in FileHelper.find_dir(directory, '*'): LogHelper.debug('Delete folder %s' % folder) try: shutil.rmtree(folder) except: pass
def capture_worker_info(sender, instance, **kwargs): """ dump worker infomation whenever worker is connected """ info = PlatformHelper.get_platform_info() info['worker_name']='{0}'.format(sender) filename = os.path.join(os.path.dirname(__file__), "worker_info.yaml") if FileHelper.file_exist(filename): LogHelper.info("delete config file %s" % filename) FileHelper.delete_file(filename) data = dict((k.upper(), v) for k, v in info.items()) with open(filename, 'w') as outfile: yaml.dump(data, outfile, default_flow_style=False)
def step_impl(context, raw_path, raw_output): LogHelper.info("start to download %s to %s" % (raw_path, raw_output)) root_path = ConfigAdapter.get_testdata_path() path = os.path.join(root_path, raw_path) root_output = ConfigAdapter.get_output_path() output = os.path.join(root_output, raw_output) LogHelper.info("start to download %s to %s" % (path, output)) if FileHelper.dir_exist(output): FileHelper.delete_directory(output) FileHelper.create_directory(output) result = LinuxGUIClient.download_cmd.download(path=path, output=output, overwrite=None) LogHelper.info(result)
def clean_file(self): src = WIN_CONFIG["TESTDATA_PATH"] cli = Windows_Cli(self.oem) if os.path.isdir(src): if len(os.listdir(src)) == 0: cli.create_backupset(src) else: fi = FileHelper() fi.clean_dir(src) os.mkdir(src) cli.create_backupset(src) else: os.mkdir(src) cli.create_backupset(src)
def upload_attachment(): file = request.files.get('file') if not file: name = request.args.get('name') file = FileStorage(request.stream, filename=name, name=name, headers=request.headers) if file: fh = FileHelper(file) fh.save() if fh.savepath: return f.succeed({'filepath': fh.savepath}) return f.failed(*const.UPLOAD_FAIL)
def files_installed(cls, brand): path_app_support = "/Library/Application Support/{brand}".format( brand=brand) path_prefpane = "/Library/PreferencePanes/{brand}.prefPane".format( brand=brand) path_cache = "/Library/Caches/{brand}/cache.db".format(brand=brand) path_log = "/Library/Logs/{brand}.log".format(brand=brand) result = FileHelper.dir_exist( path_app_support) and FileHelper.dir_exist( path_prefpane) and FileHelper.file_exist( path_cache) and FileHelper.file_exist(path_log) return result
def clear_download_folder(): directory = ConfigAdapter.get_installer_path('MACFRYR') if platform.system() == "Windows": directory = "C:/" + ConfigAdapter.get_testdata_path( 'WINFRYR').split('\\\\')[1] for file in FileHelper.find_file(directory, '*'): LogHelper.debug('Delete file %s' % file) os.remove(file) for folder in FileHelper.find_dir(directory, '*'): LogHelper.debug('Delete folder %s' % folder) try: shutil.rmtree(folder) except: pass
def install_fb(cls): installer = cls.download_fb() LogHelper.info("start to install") if PlatformHelper.is_Linux() and PlatformHelper.get_arch() == "deb-64": install_cmd = "dpkg -i %s" % installer cls.__set_fb_path("/etc/filebeat/") if PlatformHelper.is_Linux() and PlatformHelper.get_arch() == "deb-32": install_cmd = "rpm -vi %s" % installer cls.__set_fb_path("/etc/filebeat/") if PlatformHelper.is_win(): import zipfile win_fb_dir = "C:\\filebeat" target = os.path.join(win_fb_dir, "filebeat-5.2.0-windows-x86_64") installcommnd = os.path.join(target, "install-service-filebeat.ps1") zip_ref = zipfile.ZipFile(installer, 'r') zip_ref.extractall(win_fb_dir) zip_ref.close() # FileHelper.rename() install_cmd = "powershell.exe %s" % (installcommnd) cls.__set_fb_path(target) cmd_output = CmdHelper.runas_admin(install_cmd) return cmd_output # # TODO: Unzip to start" service # install_cmd = "to be implemented" # win_fb_dir = "C:\\filebeat" # if not FileHelper.dir_exist(win_fb_dir): # FileHelper.create_directory(win_fb_dir) # cls.__set_fb_path(win_fb_dir) # install_cmd = "unzip %s -C %s" % (installer, win_fb_dir) if PlatformHelper.is_mac(): mac_fb_dir = "/filebeat" if not FileHelper.dir_exist(mac_fb_dir): FileHelper.create_directory(mac_fb_dir) cls.__set_fb_path(mac_fb_dir) install_cmd = "tar xzvf %s -C %s" % (installer, mac_fb_dir) cmd_output = CmdHelper.run(install_cmd) LogHelper.info(cmd_output)
def apply_conf(dict, conf_file="qa_automation.conf", keep_old_conf_file=True): if not conf_file.endswith(".conf"): conf_file += ".conf" conf_file_path = os.path.join(LynxCtrl.conf_dir, conf_file) if not keep_old_conf_file: FileHelper.delete_file(conf_file_path) for (key, value) in dict.items(): LynxCtrl.add_conf(key, value, conf_file, True) LynxCtrl.restart()
def add_conf(key, value, conf_file="qa_automation.conf", keep_old_conf_file=True): if not conf_file.endswith(".conf"): conf_file += ".conf" conf_file_path = os.path.join(LynxCtrl.conf_dir, conf_file) if not keep_old_conf_file: FileHelper.delete_file(conf_file_path) line = "%s = %s\n" % (key, value) with open(conf_file_path, 'aw') as f: f.write(line)
def get_archive_name(): download_path = ConfigAdapter.get_installer_path() for file in FileHelper.find_file(download_path, '*'): file = os.path.basename(file) pattern = r'restore_(.+?).zip' if re.match(pattern, file): return file
def before_scenario(context, scenario): testrun = RUNNER_CONFIG.get('TESTRUN') or 'testrun' context.log_starttime = strftime("%Y-%m-%dT%H:%M:%SZ", localtime()) start_time = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) hostname = PlatformHelper.get_hostname() ip = PlatformHelper.get_ip_address() product = RUNNER_CONFIG.get("PRODUCT") build = RUNNER_CONFIG.get('BUILD') context.senddb = RUNNER_CONFIG.get('DATABASE_ENABLED') context.env = RUNNER_CONFIG.get('ENVIRONMENT') logger_path = os.path.join(ConfigAdapter.get_log_path(), testrun) tc_prefix = ConfigAdapter.get_testlink_prefix(product) match = filter(lambda x: x.find(tc_prefix) >= 0, scenario.tags) if len(match)>0: # differentiate scenarios in scenario outline if hasattr(context, 'active_outline') and type(context.active_outline) == behave.model.Row: suffix = match.pop() for example_key in context.active_outline: suffix += ".%s" % (example_key) tc_id = testrun + "_" + suffix else: tc_id = testrun + "_" + match.pop() else: #no test link project id foud tc_id = hashlib.md5(testrun + "_" + scenario.name.encode()).hexdigest() if not FileHelper.dir_exist(logger_path): FileHelper.create_directory(logger_path) logger_filename = "%s.log" % (tc_id) logfile = os.path.join(logger_path, logger_filename) client_ip = PlatformHelper.get_ip_address() LogHelper.create_logger(logfile, fmt=client_ip+" %(asctime)s %(levelname)s " + product+" | %(message)s |") tc = TestCase(testrun=testrun, start_time=start_time, hostname=hostname, product=product, ip=ip, summary=scenario.name, feature=scenario.filename, tags=scenario.tags, logfile=logfile, _id=tc_id, build=build) context.tc = tc LogHelper.info("test start: " + scenario.name)
def start(self): """ usage: start :return: """ cmd_output = "error: could not determine how to start the service" if FileHelper.file_exist(self.mozy_daemon): cmd_output = self.start_upstart() return cmd_output
def step_impl(context, restore_dir, backup_dir): result = FileHelper.is_dir_same(restore_dir, backup_dir) if result: for diff in result: LogHelper.error("diff files found {path}".format(path=diff)) len(result).should.equal(0)
def download_package(cls, job, build, pattern): """ :return: """ jh = JenkinsHelper(GLOBAL_CONFIG["JENKINS"]["URL"], GLOBAL_CONFIG["JENKINS"]["USERNAME"], GLOBAL_CONFIG["JENKINS"]["KEY"]) dest = ConfigAdapter.get_installer_path(product='MAC') # Delete existing images before downloading import glob images = glob.glob(dest + '/*.dmg') for path in images: FileHelper.delete_file(path) package = jh.get_packages(job, build, pattern) return jh.download_packages(package, dest=dest)
def create_logger(cls, path, level="DEBUG", fmt="%(asctime)s %(levelname)s %(message)s", fmt_date='%Y-%m-%dT%H:%M:%S', gmtime=True, remove_newline=True): cls.remove_newline = remove_newline log_level = cls.__resolve_log_level(level) log_name = os.path.basename(path) cls.logger = logging.getLogger(log_name) cls.logger.setLevel(log_level) cls.logger.handlers = [ h for h in cls.logger.handlers if not isinstance(h, logging.StreamHandler) ] cls.logger.removeHandler(cls.logger.handlers) # create a file handler if not FileHelper.dir_exist(FileHelper.get_dir(path)): FileHelper.create_directory(FileHelper.get_dir(path)) handler = logging.FileHandler(path, mode='a', delay=False) handler.setLevel(log_level) # add the handlers to the logger cls.logger.addHandler(handler) cls.logger = logging.getLogger(log_name) cls.fmt_option["fmt"] = fmt cls.fmt_option['fmt_date'] = fmt_date cls.fmt_option['gmtime'] = gmtime cls.set_format( "%s %s" % (cls.fmt_option['fmt'], cls.__get_call_back()), cls.fmt_option['fmt_date'], cls.fmt_option['gmtime']) return cls.logger
def step_impl(context): LogHelper.info("start to overwrite files") testdata_root = ConfigAdapter.get_testdata_path() for row in context.table: testdata_dir = os.path.join(testdata_root, row.get('file_folder')) testdatas = FileHelper.find_file(testdata_dir, '*') max_size = int(row.get('maxsize')) min_size = int(row.get('minsize')) length = random.randrange(min_size, max_size) for testdata in testdatas: offset_pattern = row.get('offset_pattern') if offset_pattern == 'random': size = FileHelper.file_size(testdata) offset = random.randrange(1, size) elif offset_pattern.isdigit(): size = FileHelper.file_size(testdata) offset = min(int(offset_pattern), size) FileHelper.overwrite_file_random(testdata, offset, length)