open->fdopen

Signed-off-by: shiminnan@huawei.com <shiminnan@huawei.com>
This commit is contained in:
swx1282997 2024-05-24 16:22:40 +08:00
parent 88612d84a2
commit 2165987eb7
3 changed files with 12 additions and 4 deletions

View File

@ -22,8 +22,10 @@ if __name__ == '__main__':
os.environ['DEVELOPER_DIR'] = args.developer_dir
rv = subprocess.check_call(['xcrun'] + unknown_args)
flags = os.O_RDWR | os.O_CREAT | os.O_EXCL
modes = stat.S_IWUSR | stat.S_IRUSR
if rv == 0 and args.stamp:
if os.path.exists(args.stamp):
os.unlink(args.stamp)
with open(args.stamp, 'w+') as fp:
with os.fdopen(os.open(args.stamp, flags, modes), 'w+') as fp:
sys.exit(rv)

View File

@ -94,12 +94,14 @@ def _copy_url(args, task_id, url, local_file, code_dir, unzip_dir, unzip_filenam
# download files
download_buffer_size = 32768
progress.console.log('Requesting {}'.format(url))
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
modes = 0o777
try:
response = urlopen(url)
except urllib.error.HTTPError as e:
progress.console.log("Failed to open {}, HTTPError: {}".format(url, e.code), style='red')
progress.update(task_id, total=int(response.info()["Content-length"]))
with open(local_file, "wb") as dest_file:
with os.fdopen(os.open(local_file, flags, modes), 'wb') as dest_file:
progress.start_task(task_id)
for data in iter(partial(response.read, download_buffer_size), b""):
dest_file.write(data)

View File

@ -58,6 +58,8 @@ def read_file(input_file):
# Write json file data
def write_json_file(output_file, content, check_changes=False):
file_dir = os.path.dirname(os.path.abspath(output_file))
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
modes = stat.S_IWUSR | stat.S_IRUSR
if not os.path.exists(file_dir):
os.makedirs(file_dir, exist_ok=True)
@ -66,7 +68,7 @@ def write_json_file(output_file, content, check_changes=False):
else:
changed = True
if changed is True:
with open(output_file, 'w') as output_f:
with os.fdopen(os.open(output_file, flags, modes), 'w') as output_f:
json.dump(content, output_f, sort_keys=True, indent=2)
@ -88,10 +90,12 @@ def __check_changes(output_file, content):
# Write file data
def write_file(output_file, content):
file_dir = os.path.dirname(os.path.abspath(output_file))
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
modes = stat.S_IWUSR | stat.S_IRUSR
if not os.path.exists(file_dir):
os.makedirs(file_dir, exist_ok=True)
with open(output_file, 'w') as output_f:
with os.fdopen(os.open(output_file, flags, modes), 'w') as output_f:
output_f.write(content)
if output_file.endswith('.gni') or output_file.endswith('.gn'):
# Call gn format to make the output gn file prettier.