<commit_before>#!/usr/bin/env python
import argparse
import os
import tempfile
from androtoolbox.adb import adb
from androtoolbox.shared_pref import SharedPref, build_shared_pref_path


def read_shared_pref(package, pref_name):
    pref_device_path = build_shared_pref_path(package, pref_name)
    data = adb.shell("cat %s" % pref_device_path, use_su=True)
    return SharedPref.from_xml(data)


def write_shared_pref(package, pref_name, shared_pref):
    tmp_shared_file = tempfile.NamedTemporaryFile()
    shared_pref.to_file(tmp_shared_file)
    pref_device_path = build_shared_pref_path(package, pref_name)
    local_path = tmp_shared_file.name
    remote_tmp_path = "/data/local/tmp/%s" % os.path.basename(local_path)
    adb.push(tmp_shared_file.name, remote_tmp_path)
    adb.shell('mv %s %s' % (remote_tmp_path, pref_device_path), use_su=True)

    # We need to fix any permissions mix up
    adb.shell('chmod 0777 %s' % pref_device_path, use_su=True)


def update_shared_pref(package, pref_name, updates):<commit_msg>Close the temp shared file<commit_after>#!/usr/bin/env python
import argparse
import os
import tempfile
from androtoolbox.adb import adb
from androtoolbox.shared_pref import SharedPref, build_shared_pref_path


def read_shared_pref(package, pref_name):
    pref_device_path = build_shared_pref_path(package, pref_name)
    data = adb.shell("cat %s" % pref_device_path, use_su=True)
    return SharedPref.from_xml(data)


def write_shared_pref(package, pref_name, shared_pref):
    with tempfile.NamedTemporaryFile() as tmp_shared_file:
        shared_pref.to_file(tmp_shared_file)
        pref_device_path = build_shared_pref_path(package, pref_name)
        local_path = tmp_shared_file.name
        remote_tmp_path = "/data/local/tmp/%s" % os.path.basename(local_path)
        adb.push(tmp_shared_file.name, remote_tmp_path)
        adb.shell('mv %s %s' % (remote_tmp_path, pref_device_path), use_su=True)

        # We need to fix any permissions mix up
        adb.shell('chmod 0777 %s' % pref_device_path, use_su=True)


def update_shared_pref(package, pref_name, updates):
{'fraction_non_alphanumeric': 0.09125475285171103, 'fraction_numerical': 0.0038022813688212928, 'mean_word_length': 5.0488505747126435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7580779', 'n_tokens_mistral': 745, 'n_tokens_neox': 740, 'n_words': 145}