From 700055c194217aeebc7aae14746bf7619bdce5e2 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 14 Oct 2015 16:31:08 -0400 Subject: [PATCH] Apply modernize transform libmodernize.fixes.fix_file. This replaces use of file() with open() which is python 3 compatible, and also adds it to our python 3 support test suite. --- api/zulip/__init__.py | 2 +- bots/cron_file_helper.py | 2 +- bots/jabber_mirror_backend.py | 2 +- bots/process_ccache | 2 +- bots/zephyr_mirror_backend.py | 14 +++++++------- tools/lint-all | 2 +- tools/print-all/print-all | 2 +- tools/test_user_agent_parsing.py | 2 +- tools/travis/py3k | 1 + zerver/lib/cache.py | 2 +- zerver/lib/event_queue.py | 2 +- zilencer/management/commands/populate_db.py | 4 ++-- zilencer/management/commands/sync_api_key.py | 2 +- 13 files changed, 20 insertions(+), 19 deletions(-) diff --git a/api/zulip/__init__.py b/api/zulip/__init__.py index 3de8ba8406..a3a3317e27 100644 --- a/api/zulip/__init__.py +++ b/api/zulip/__init__.py @@ -164,7 +164,7 @@ class Client(object): config_file = get_default_config_filename() if os.path.exists(config_file): config = SafeConfigParser() - with file(config_file, 'r') as f: + with open(config_file, 'r') as f: config.readfp(f, config_file) if api_key is None: api_key = config.get("api", "key") diff --git a/bots/cron_file_helper.py b/bots/cron_file_helper.py index b61609cd03..96713b4f68 100644 --- a/bots/cron_file_helper.py +++ b/bots/cron_file_helper.py @@ -10,7 +10,7 @@ def nagios_from_file(results_file): This file is created by various nagios checking cron jobs such as check-rabbitmq-queues and check-rabbitmq-consumers""" - data = file(results_file).read().strip() + data = open(results_file).read().strip() pieces = data.split('|') if not len(pieces) == 4: diff --git a/bots/jabber_mirror_backend.py b/bots/jabber_mirror_backend.py index a418ac0042..09c07ec3ff 100755 --- a/bots/jabber_mirror_backend.py +++ b/bots/jabber_mirror_backend.py @@ -376,7 +376,7 @@ option does not affect login credentials.'''.replace("\n", " ")) config = SafeConfigParser() try: - with file(config_file, 'r') as f: + with open(config_file, 'r') as f: config.readfp(f, config_file) except IOError: pass diff --git a/bots/process_ccache b/bots/process_ccache index 89363b6653..04a7265af9 100755 --- a/bots/process_ccache +++ b/bots/process_ccache @@ -9,7 +9,7 @@ ccache_data_encoded = sys.argv[3] # Update the Kerberos ticket cache file program_name = "zmirror-%s" % (short_user,) -with file("/home/zulip/ccache/%s" % (program_name,), "w") as f: +with open("/home/zulip/ccache/%s" % (program_name,), "w") as f: f.write(base64.b64decode(ccache_data_encoded)) # Setup API key diff --git a/bots/zephyr_mirror_backend.py b/bots/zephyr_mirror_backend.py index 8c5bf3bb35..ee57c1bffb 100755 --- a/bots/zephyr_mirror_backend.py +++ b/bots/zephyr_mirror_backend.py @@ -191,7 +191,7 @@ def zephyr_bulk_subscribe(subs): def update_subscriptions(): try: - f = file(options.stream_file_path, "r") + f = open(options.stream_file_path, "r") public_streams = simplejson.loads(f.read()) f.close() except: @@ -287,7 +287,7 @@ def parse_zephyr_body(zephyr_data): def parse_crypt_table(zephyr_class, instance): try: - crypt_table = file(os.path.join(os.environ["HOME"], ".crypt-table")) + crypt_table = open(os.path.join(os.environ["HOME"], ".crypt-table")) except IOError: return None @@ -349,7 +349,7 @@ def process_notice(notice, log): if zephyr_class == options.nagios_class: # Mark that we got the message and proceed - with file(options.nagios_path, "w") as f: + with open(options.nagios_path, "w") as f: f.write("0\n") return @@ -468,7 +468,7 @@ def zephyr_load_session_autoretry(session_path): backoff = zulip.RandomExponentialBackoff() while backoff.keep_going(): try: - session = file(session_path, "r").read() + session = open(session_path, "r").read() zephyr._z.initialize() zephyr._z.load_session(session) zephyr.__inited = True @@ -510,7 +510,7 @@ def zephyr_to_zulip(options): if options.nagios_class: zephyr_subscribe_autoretry((options.nagios_class, "*", "*")) if options.use_sessions: - file(options.session_path, "w").write(zephyr._z.dump_session()) + open(options.session_path, "w").write(zephyr._z.dump_session()) if options.logs_to_resend is not None: with open(options.logs_to_resend, 'r') as log: @@ -857,7 +857,7 @@ def parse_zephyr_subs(verbose=False): logger.error("Couldn't find ~/.zephyr.subs!") return [] - for line in file(subs_file, "r").readlines(): + for line in open(subs_file, "r").readlines(): line = line.strip() if len(line) == 0: continue @@ -1050,7 +1050,7 @@ Could not find API key file. You need to either place your api key file at %s, or specify the --api-key-file option.""" % (options.api_key_file,)))) sys.exit(1) - api_key = file(options.api_key_file).read().strip() + api_key = open(options.api_key_file).read().strip() # Store the API key in the environment so that our children # don't need to read it in os.environ["HUMBUG_API_KEY"] = api_key diff --git a/tools/lint-all b/tools/lint-all index 003d2d528a..fd0ec9df23 100755 --- a/tools/lint-all +++ b/tools/lint-all @@ -70,7 +70,7 @@ for filepath in files: _, exn = path.splitext(filepath) if not exn: # No extension; look at the first line - with file(filepath) as f: + with open(filepath) as f: if re.match(r'^#!.*\bpython', f.readline()): exn = '.py' diff --git a/tools/print-all/print-all b/tools/print-all/print-all index d5c2496bb3..05688504a2 100755 --- a/tools/print-all/print-all +++ b/tools/print-all/print-all @@ -130,7 +130,7 @@ doc.write(r''' ''') doc.close() -with file('tools/print-all/tex/epilogue.tex', 'w') as f: +with open('tools/print-all/tex/epilogue.tex', 'w') as f: def print_list(title, xs): f.write('\\section*{%s}\n\n' % (title,)) for x in xs: diff --git a/tools/test_user_agent_parsing.py b/tools/test_user_agent_parsing.py index 724085b3bc..103ac86f2a 100755 --- a/tools/test_user_agent_parsing.py +++ b/tools/test_user_agent_parsing.py @@ -12,7 +12,7 @@ from zerver.lib.user_agent import parse_user_agent user_agents_parsed = defaultdict(int) user_agents_path = os.path.join(os.path.dirname(__file__), "user_agents_unique") parse_errors = 0 -for line in file(user_agents_path).readlines(): +for line in open(user_agents_path).readlines(): line = line.strip() match = re.match('^(?P[0-9]+) "(?P.*)"$', line) if match is None: diff --git a/tools/travis/py3k b/tools/travis/py3k index 96a5f99f05..188f262705 100755 --- a/tools/travis/py3k +++ b/tools/travis/py3k @@ -28,6 +28,7 @@ libfuturize.fixes.fix_next_call libfuturize.fixes.fix_print_with_import libfuturize.fixes.fix_raise libmodernize.fixes.fix_basestring +libmodernize.fixes.fix_file libmodernize.fixes.fix_filter libmodernize.fixes.fix_imports_six libmodernize.fixes.fix_input_six diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 798bcc2eaf..02f2a007a6 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -54,7 +54,7 @@ def get_or_create_key_prefix(): # The file already exists tries = 1 while tries < 10: - with file(filename, 'r') as f: + with open(filename, 'r') as f: prefix = f.readline()[:-1] if len(prefix) == 33: break diff --git a/zerver/lib/event_queue.py b/zerver/lib/event_queue.py index a5155329bc..f4d1ad0faa 100644 --- a/zerver/lib/event_queue.py +++ b/zerver/lib/event_queue.py @@ -370,7 +370,7 @@ def gc_event_queues(): def dump_event_queues(): start = time.time() - with file(settings.JSON_PERSISTENT_QUEUE_FILENAME, "w") as stored_queues: + with open(settings.JSON_PERSISTENT_QUEUE_FILENAME, "w") as stored_queues: ujson.dump([(qid, client.to_dict()) for (qid, client) in clients.iteritems()], stored_queues) diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index ccfd244c76..b1621733ac 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -369,7 +369,7 @@ def restore_saved_messages(): event_glob = os.path.join(settings.EVENT_LOG_DIR, 'events.*') for filename in sorted(glob.glob(event_glob)): - with file(filename, "r") as message_log: + with open(filename, "r") as message_log: for line in message_log.readlines(): process_line(line) @@ -699,7 +699,7 @@ def restore_saved_messages(): def send_messages(data): (tot_messages, personals_pairs, options, output) = data random.seed(os.getpid()) - texts = file("zilencer/management/commands/test_messages.txt", "r").readlines() + texts = open("zilencer/management/commands/test_messages.txt", "r").readlines() offset = random.randint(0, len(texts)) recipient_streams = [klass.id for klass in diff --git a/zilencer/management/commands/sync_api_key.py b/zilencer/management/commands/sync_api_key.py index 55e254bd32..e6487af9f1 100644 --- a/zilencer/management/commands/sync_api_key.py +++ b/zilencer/management/commands/sync_api_key.py @@ -14,7 +14,7 @@ class Command(BaseCommand): if not os.path.exists(config_file): raise RuntimeError("No ~/.zuliprc found") config = SafeConfigParser() - with file(config_file, 'r') as f: + with open(config_file, 'r') as f: config.readfp(f, config_file) api_key = config.get("api", "key") email = config.get("api", "email")