#!/usr/bin/env python
"""
This module provide install of linux app.
Usage:
python deploy.py file_path
Authors:
Date:
"""
import os
import sys
import time
import json
def exec_script(content):
"""Runs the content as a device sh script.
Args:
content: the content of script.
Returns:
False when error, otherwise True.
Risks:
None
"""
if len(content) <= 0:
return False
with open("./device_exec.sh", 'w') as f:
f.write(content)
os.system('adb push ./device_exec.sh /tmp/')
os.system('adb shell chmod a+x /tmp/device_exec.sh')
os.system('adb shell /tmp/device_exec.sh')
os.system('adb shell rm /tmp/device_exec.sh')
os.remove("./device_exec.sh")
return True
def prepare_install_env():
"""Remove old install on device.
Args:
None
Returns:
None
Risks:
None
"""
prepare_script = """#!/bin/sh
PID=`ps -ef | grep "watch_dueros_se" | grep -v grep | awk '{print $1}'`
kill $PID
PID=`ps -ef | grep "duer_linux" | grep -v grep | awk '{print $1}'`
kill $PID
rm -rf /oem/duer/*
mkdir /oem/duer/appresources/
mkdir /oem/duer/lib32/
ln -s /oem/duer/lib32 /oem/duer/lib
mkdir /oem/duer/resources/
"""
exec_script(prepare_script)
def send_file(file_path, file_name, destony):
"""Push a file to device.
Args:
file_path: the parent directory on host of file pushed.
file_name: the name of the file pushed.
destony: the path of name on device.
Returns:
None
Risks:
None
"""
command = 'adb push ' + file_path + file_name + ' ' + '/tmp/'
print command
os.system(command)
command = 'adb shell mv /tmp/' + file_name + ' ' + destony
print command
os.system(command)
def create_link(file_name, target):
"""Create a soft link for a library.
Args:
file_name: library that wants to linked to.
target: soft link library name.
Returns:
the command of create link.
Risks:
None
"""
command = 'ln -s /oem/duer/lib32/' + file_name + ' ' + target
if file_name[0] == '/':
command = 'ln -s ' + file_name + ' ' + target
return command
def collect_link_files(manifest_file_path):
"""Collect all libraries needs to create soft links form manifest file.
Args:
None
Returns:
None
Risks:
None
"""
content = ''
with open(manifest_file_path, 'r') as f:
content = f.read()
print content
link_name_table = ["libbd_alsa_audio_client.so"]
config = json.loads(content)
links = config['links']
for link in links:
origin_file = link['name']
link_names = link['names']
link_name_table = link_name_table + link_names
return link_name_table
def create_lib_links(manifest_file_path):
"""Create soft links on device for some libraries.
Args:
None
Returns:
None
Risks:
None
"""
content = ''
with open(manifest_file_path, 'r') as f:
content = f.read()
config = json.loads(content)
links = config['links']
link_folder = config['link_folder']
script_context = '#!/bin/sh\n'
for link in links:
origin_file = link['name']
for link_name in link['names']:
script_context += create_link(origin_file, link_folder + "/" + link_name)
script_context += '\n'
time.sleep(5)
exec_script(script_context)
def main(argv):
"""Main routine of this script.
Args:
argv: argument list from first of input.
Returns:
None
Risks:
None
"""
prepare_install_env()
package_path = argv[0]
manifest_file_path = os.path.join(package_path, 'manifest.json')
link_file_list = collect_link_files(manifest_file_path)
print "copy main files ...."
files = os.listdir(package_path)
for file_name in files:
if os.path.isfile(os.path.join(package_path, file_name)):
send_file(package_path + "/", file_name, "/oem/duer/" + file_name)
print "copy appresources ...."
current_path = package_path + "/appresources/"
files = os.listdir(current_path)
for file_name in files:
if not os.path.isfile(os.path.join(current_path, file_name)):
continue
send_file(current_path, file_name, "/oem/duer/appresources/" + file_name)
print "copy resource files ...."
current_path = package_path + "/resources/"
files = os.listdir(current_path)
for file_name in files:
if not os.path.isfile(os.path.join(current_path, file_name)):
continue
send_file(current_path, file_name, "/oem/duer/resources/" + file_name)
print "copy lib files ...."
current_path = package_path + "/lib32/"
files = os.listdir(current_path)
for file_name in files:
if not os.path.isfile(os.path.join(current_path, file_name)):
continue
if file_name in link_file_list:
continue
send_file(current_path, file_name, "/oem/duer/lib32/" + file_name)
create_lib_links(manifest_file_path)
print "install finished, device is rebooting, please wait ...."
time.sleep(5)
os.system("adb shell reboot")
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Linux运行环境,兼容Mac