本文整理汇总了Python中core.osutils.file.File类的典型用法代码示例。如果您正苦于以下问题:Python File类的具体用法?Python File怎么用?Python File使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了File类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: created
def created(app_name, output=None, full_check=True):
"""
Assert application is created properly.
:param app_name: App name
:param output: Output of `tns create command`
:param full_check: If true everything will be checked. If false only console output will be checked.
"""
# Assert console output is ok
if output is not None:
app = app_name.rsplit('/')[-1]
if USE_YARN != "True":
if Npm.version() < 5:
assert 'nativescript-theme-core' in output
assert 'Project {0} was successfully created'.format(app) in output, 'Failed to create {0}'.format(app)
if full_check:
# Assert files are ok
assert File.exists(app_name)
assert File.exists(app_name + '/node_modules/tns-core-modules/package.json')
assert File.exists(app_name + '/node_modules/tns-core-modules/LICENSE')
assert File.exists(app_name + '/node_modules/tns-core-modules/xml/xml.js')
assert File.exists(app_name + '/node_modules/nativescript-theme-core')
# Assert content of package.json
app_id = app_name.replace(' ', '').replace('_', '').replace('-', '').rsplit('/')[-1]
strings = ['org.nativescript.{0}'.format(app_id), 'tns-core-modules', 'nativescript-theme-core']
TnsAsserts.package_json_contains(app_name, string_list=strings)
示例2: start
def start(emulator_name=EMULATOR_NAME, port=EMULATOR_PORT, timeout=300, wipe_data=True):
"""
Start emulator.
:param wipe_data: If true it will wipe emulator date.
:param emulator_name: Name of android emulator image (avd).
:param port: Port for Android emulator.
:param timeout: Time to wait until emulator boot.
"""
print 'Starting emulator {0}'.format(emulator_name)
if CURRENT_OS == OSType.WINDOWS:
start_command = 'START /B ' + EMULATOR_PATH + ' -avd ' + emulator_name + ' -port ' + port
else:
start_command = EMULATOR_PATH + ' -avd ' + emulator_name + ' -port ' + port
if wipe_data:
start_command += ' -wipe-data'
log_file = run(start_command, timeout=timeout, wait=False, log_level=CommandLogLevel.COMMAND_ONLY)
# Check if emulator is running
device_name = 'emulator-' + port
if Emulator.wait(device_name, timeout):
print 'Emulator started successfully.'
else:
print 'Emulator failed to boot!'
print File.read(log_file)
raise Exception('Wait for emulator failed!')
示例3: get_screen
def get_screen(device_id, file_path):
"""
Save screen of mobile device.
:param device_id: Device identifier (example: `emulator-5554`).
:param file_path: Path to image that will be saved.
"""
File.remove(file_path)
base_path, file_name = os.path.split(file_path)
Folder.create(base_path)
device_type = Device.__get_device_type(device_id)
if (device_type == DeviceType.EMULATOR) or (device_type == DeviceType.ANDROID):
Adb.get_screen(device_id=device_id, file_path=file_path)
if device_type == DeviceType.SIMULATOR:
Simulator.get_screen(device_id=device_id, file_path=file_path)
if device_type == DeviceType.IOS:
IDevice.get_screen(device_id=device_id, file_path=file_path)
image_saved = False
if File.exists(file_path):
size = os.path.getsize(file_path)
if size > 10:
image_saved = True
return image_saved
示例4: platform_added
def platform_added(app_name, platform=Platform.NONE, output=None):
"""
Assert platform is added.
:param app_name: Application name (folder where app is located).
:param platform: Platforms that should be available.
:param output: output of `tns platform add` command
"""
# Verify console output is correct
if output is not None:
if platform is Platform.ANDROID:
assert 'Platform android successfully added' in output
if platform is Platform.IOS and CURRENT_OS == OSType.OSX:
assert 'Platform ios successfully added' in output
else:
assert 'Applications for platform ios can not be built on this OS'
assert 'Project successfully created.' not in output
# This is to handle test for app with space.
# In this case we put app name inside ''.
app_name = app_name.replace('\'', '')
app_name = app_name.replace('\"', '')
# Verify file and folder content
if platform is Platform.NONE:
assert not File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_IOS))
assert not File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID))
if platform is Platform.ANDROID or platform is Platform.BOTH:
assert File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID))
if platform is Platform.IOS or platform is Platform.BOTH:
if CURRENT_OS == OSType.OSX:
assert File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_IOS))
else:
assert not File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_IOS))
示例5: test_280_tns_run_android_console_time
def test_280_tns_run_android_console_time(self):
# Copy the app folder (app is modified in order to get some console logs on loaded)
source = os.path.join('data', 'apps', 'livesync-hello-world-ng', 'src')
target = os.path.join(self.app_name, 'src')
Folder.cleanup(target)
Folder.copy(src=source, dst=target)
# Replace app.component.ts to use console.time() and console.timeEnd()
source = os.path.join('data', 'issues', 'ios-runtime-843', 'app.component.ts')
target = os.path.join(self.app_name, 'src', 'app', 'app.component.ts')
File.copy(src=source, dest=target)
# `tns run android` and wait until app is deployed
log = Tns.run_android(attributes={'--path': self.app_name, '--device': EMULATOR_ID}, wait=False,
assert_success=False)
# Verify the app is running
strings = ['Successfully synced application',
'Application loaded!',
'Home page loaded!']
Tns.wait_for_log(log_file=log, string_list=strings, timeout=180, check_interval=10, clean_log=False)
# Verify initial state of the app
Device.screen_match(device_name=EMULATOR_NAME, device_id=EMULATOR_ID,
expected_image='ng-hello-world-home-white', tolerance=5.0)
# Verify console.time() works
console_time = ['JS: startup:']
Tns.wait_for_log(log_file=log, string_list=console_time)
示例6: setUpClass
def setUpClass(cls, class_name):
print ""
print "_________________________________CLASS START_______________________________________"
print "Class Name: {0}".format(class_name)
print "Start Time: {0}".format(time.strftime("%X"))
print ""
Tns.kill()
Gradle.kill()
Process.kill('node')
Process.kill('adb')
if CURRENT_OS == OSType.OSX:
Process.kill('NativeScript Inspector')
Process.kill('Safari')
Process.kill('Xcode')
if class_name is not None:
logfile = os.path.join('out', class_name + '.txt')
else:
logfile = os.path.join(OUTPUT_FOLDER, cls.__name__ + ".txt")
File.remove(logfile)
sys.stdout = sys.stderr = Logger.Logger(logfile)
Folder.cleanup(cls.app_name)
示例7: test_301_build_project_with_space_release
def test_301_build_project_with_space_release(self):
Tns.create_app(self.app_name_space)
Tns.platform_add_android(
attributes={"--path": "\"" + self.app_name_space + "\"", "--frameworkPath": ANDROID_PACKAGE})
# Ensure ANDROID_KEYSTORE_PATH contain spaces (verification for CLI issue 2650)
Folder.create("with space")
base_path, file_name = os.path.split(ANDROID_KEYSTORE_PATH)
cert_with_space_path = os.path.join("with space", file_name)
File.copy(src=ANDROID_KEYSTORE_PATH, dest=cert_with_space_path)
# Verify project builds
Tns.build_android(attributes={"--path": "\"" + self.app_name_space + "\"",
"--keyStorePath": "\"" + cert_with_space_path + "\"",
"--keyStorePassword": ANDROID_KEYSTORE_PASS,
"--keyStoreAlias": ANDROID_KEYSTORE_ALIAS,
"--keyStoreAliasPassword": ANDROID_KEYSTORE_ALIAS_PASS,
"--release": ""
})
output = File.read(self.app_name_space + os.sep + "package.json")
assert app_identifier in output.lower()
output = File.read(
self.app_name_space + "/" + TnsAsserts.PLATFORM_ANDROID_SRC_MAIN_PATH + "AndroidManifest.xml")
assert app_identifier in output.lower()
示例8: test_330_tns_run_ios_after_rebuild_of_native_project
def test_330_tns_run_ios_after_rebuild_of_native_project(self):
"""
`tns run ios` should work properly after rebuild of native project (test for issue #2860)
"""
# `tns run ios` and wait until app is deployed
log = Tns.run_ios(attributes={'--path': self.app_name, '--device': self.DEVICE_ID}, wait=False,
assert_success=False, log_trace=True)
strings = [self.DEVICE_ID, 'Successfully synced application']
Tns.wait_for_log(log_file=log, string_list=strings, timeout=120, check_interval=10)
# Verify app is running
assert Device.wait_for_text(device_id=self.DEVICE_ID, text="Tap the button"), "App failed to load!"
# Update native project
config_path = os.path.join(self.app_name, 'app', 'App_Resources', 'iOS', 'build.xcconfig')
File.replace(file_path=config_path, str1='More info', str2='If you need more info')
strings = ['ARCHIVE SUCCEEDED', 'Successfully synced application', self.DEVICE_ID, 'CONSOLE LOG']
not_existing_strings = ['Unable to sync files', 'Multiple errors were thrown']
Tns.wait_for_log(log_file=log, string_list=strings, not_existing_string_list=not_existing_strings, timeout=120)
# Verify app is running
assert Device.wait_for_text(device_id=self.DEVICE_ID, text="Tap the button"), "App failed to load!"
# Change JS and wait until app is synced
ReplaceHelper.replace(self.app_name, ReplaceHelper.CHANGE_JS, sleep=10)
strings = ['Successfully transferred', 'main-view-model.js', 'Successfully synced application', self.DEVICE_ID]
Tns.wait_for_log(log_file=log, string_list=strings)
assert Device.wait_for_text(device_id=self.DEVICE_ID, text="clicks"), "JS changes not synced on device!"
# Rollback all the changes and verify files are synced
ReplaceHelper.rollback(self.app_name, ReplaceHelper.CHANGE_JS, sleep=10)
strings = ['Successfully transferred', 'main-view-model.js', 'Restarting application']
Tns.wait_for_log(log_file=log, string_list=strings)
assert Device.wait_for_text(device_id=self.DEVICE_ID, text="taps left"), "JS changes not synced on device!"
示例9: test_210_tns_run_ios_add_remove_files_and_folders
def test_210_tns_run_ios_add_remove_files_and_folders(self):
"""
New files and folders should be synced properly.
"""
log = Tns.run_ios(attributes={'--path': self.app_name, '--device': self.DEVICE_ID}, wait=False,
assert_success=False)
strings = ['Successfully synced application', self.DEVICE_ID]
Tns.wait_for_log(log_file=log, string_list=strings, timeout=120, check_interval=10)
# Add new files
new_file_name = 'app2.css'
source_file = os.path.join(self.app_name, 'app', 'app.css')
destination_file = os.path.join(self.app_name, 'app', new_file_name)
File.copy(source_file, destination_file)
strings = ['Successfully transferred', new_file_name, 'Refreshing application']
Tns.wait_for_log(log_file=log, string_list=strings)
# Revert changes(rename file and delete file)
# File.copy(destination_file, source_file)
# File.remove(destination_file)
# strings = ['Successfully transferred', new_file_name]
# Tns.wait_for_log(log_file=log, string_list=strings)
# Add folder
new_folder_name = 'test2'
source_file = os.path.join(self.app_name, 'app', 'test')
destination_file = os.path.join(self.app_name, 'app', new_folder_name)
Folder.copy(source_file, destination_file)
strings = ['Successfully transferred test.txt', 'Successfully synced application']
Tns.wait_for_log(log_file=log, string_list=strings)
示例10: create_app_ng
def create_app_ng(app_name, attributes={}, log_trace=False, assert_success=True, update_modules=True,
template_version=None):
if template_version is not None:
template = "[email protected]" + template_version
attr = {"--template": template}
else:
if BRANCH is "master":
attr = {"--template": SUT_FOLDER + os.path.sep + "tns-template-hello-world-ng.tgz"}
else:
attr = {"--template": "tns-template-hello-world-ng"}
attributes.update(attr)
output = Tns.create_app(app_name=app_name, attributes=attributes, log_trace=log_trace,
assert_success=assert_success,
update_modules=update_modules)
if update_modules:
Tns.update_angular(path=app_name)
Tns.update_typescript(path=app_name)
if assert_success:
if USE_YARN != "True":
if Npm.version() < 5:
assert "nativescript-angular" in output
assert File.exists(os.path.join(app_name, 'node_modules', 'nativescript-theme-core'))
package_json = File.read(os.path.join(app_name, 'package.json'))
assert "tns-core-modules" in package_json
assert "nativescript-angular" in package_json
assert "nativescript-dev-typescript" in package_json
return output
示例11: test_181_tns_run_ios_console_dir
def test_181_tns_run_ios_console_dir(self):
"""
Test console.dir() of different objects.
"""
# Change main-page.js so it contains console logging
source_js = os.path.join('data', 'console-dir', 'main-page.js')
target_js = os.path.join(self.app_name, 'app', 'main-page.js')
File.copy(src=source_js, dest=target_js)
john_obj = "==== object dump start ====\n" \
"name: John\n" \
"age: 34\n" \
"==== object dump end ===="
log = Tns.run_ios(attributes={'--path': self.app_name, '--emulator': ''}, wait=False, assert_success=False)
strings = ['Project successfully built',
'Successfully installed on device with identifier', self.SIMULATOR_ID,
"true",
"false",
"null",
"undefined",
"-1",
"text",
self.max_long_string,
john_obj
]
Tns.wait_for_log(log_file=log, string_list=strings, timeout=150, check_interval=10)
assert self.very_long_string not in log
示例12: test_181_tns_run_android_console_dir
def test_181_tns_run_android_console_dir(self):
"""
Test console.dir() of different objects.
"""
# Change main-page.js so it contains console logging
source_js = os.path.join('data', 'console-dir', 'main-page.js')
target_js = os.path.join(self.app_name, 'app', 'main-page.js')
File.copy(src=source_js, dest=target_js)
log = Tns.run_android(attributes={'--path': self.app_name, '--device': EMULATOR_ID}, wait=False,
assert_success=False)
strings = ['Project successfully built',
'Successfully installed on device with identifier', EMULATOR_ID,
'Successfully synced application',
"true",
"false",
"null",
"undefined",
"-1",
"text",
"==== object dump start ====",
"name: \"John\"",
"age: \"34\"",
"==== object dump end ====",
self.max_long_string
]
Tns.wait_for_log(log_file=log, string_list=strings, timeout=180, check_interval=10)
assert self.very_long_string not in log
示例13: test_360_tns_run_android_with_jar_file_in_plugin
def test_360_tns_run_android_with_jar_file_in_plugin(self):
"""
App should not crash when reference .jar file in some plugin
https://github.com/NativeScript/android-runtime/pull/905
"""
# Add .jar file in plugin and modify the app to reference it
custom_jar_file = os.path.join('data', 'issues', 'android-runtime-pr-905', 'customLib.jar')
modules_widgets = os.path.join(self.app_name, 'node_modules', 'tns-core-modules-widgets', 'platforms',
'android')
File.copy(src=custom_jar_file, dest=modules_widgets)
source = os.path.join('data', 'issues', 'android-runtime-pr-905', 'app.js')
target = os.path.join(self.app_name, 'app', 'app.js')
File.copy(src=source, dest=target)
# `tns run android` and wait until app is deployed
log = Tns.run_android(attributes={'--path': self.app_name, '--device': EMULATOR_ID}, wait=False,
assert_success=False)
strings = ['Project successfully built',
'Successfully installed on device with identifier', EMULATOR_ID,
'Successfully synced application']
Tns.wait_for_log(log_file=log, string_list=strings, timeout=180, check_interval=10)
# Verify app looks correct inside emulator
Device.screen_match(device_name=EMULATOR_NAME, device_id=EMULATOR_ID,
expected_image='livesync-hello-world_home')
示例14: wait_for_log
def wait_for_log(log_file, string_list, not_existing_string_list=None, timeout=45, check_interval=3,
clean_log=True):
"""
Wait until log file contains list of string.
:param log_file: Path to log file.
:param string_list: List of strings.
:param not_existing_string_list: List of string that should not be in logs.
:param timeout: Timeout.
:param check_interval: Check interval.
:param clean_log: Specify if content of log file should be delete after check.
"""
t_end = time.time() + timeout
all_items_found = False
not_found_list = []
log = ""
while time.time() < t_end:
not_found_list = []
log = File.read(log_file)
log = str(log.decode('utf8').encode('utf8')).strip()
for item in string_list:
if item in log:
print "'{0}' found.".format(item)
else:
not_found_list.append(item)
if not_found_list == []:
all_items_found = True
print "Log contains: {0}".format(string_list)
break
else:
print "'{0}' NOT found. Wait...".format(not_found_list)
time.sleep(check_interval)
if 'BUILD FAILED' in log:
print 'BUILD FAILED. No need to wait more time!'
break
if 'Unable to sync files' in log:
print 'Sync process failed. No need to wait more time!'
break
if '????????????????????????????' in log:
print 'Log seems to be corrupted. No need to wait more time!'
break
if 'errors were thrown' in log:
print 'Multiple errors were thrown. No need to wait more time!'
break
if clean_log and (CURRENT_OS is not OSType.WINDOWS) and all_items_found:
File.write(file_path=log_file, text="")
if all_items_found:
if not_existing_string_list is None:
pass
else:
for item in not_existing_string_list:
assert item not in log, "{0} found! It should not be in logs.\nLog:\n{1}".format(item, log)
else:
print "##### OUTPUT BEGIN #####\n"
print log
print "##### OUTPUT END #####\n"
print ""
assert False, "Output does not contain {0}".format(not_found_list)
示例15: get_app_packages
def get_app_packages():
"""Copy Preview App packages from Shares to local folder"""
shutil.copy2(PREVIEW_APP_PATH_ANDROID.strip(), SUT_FOLDER)
shutil.copy2(PREVIEW_APP_PATH_IOS.strip(), SUT_FOLDER)
shutil.copy2(PLAYGROUND_APP_PATH_IOS.strip(), SUT_FOLDER)
"""Unpack the .tgz file to get the nsplaydev.app"""
File.unpack_tar(os.path.join(SUT_FOLDER, 'nsplaydev.tgz'), SUT_FOLDER)
File.unpack_tar(os.path.join(SUT_FOLDER, 'nsplay.tgz'), SUT_FOLDER)