From 5bd1394a64b1c17479f3a4f9e8cd38e0ed410c3a Mon Sep 17 00:00:00 2001 From: Hristo Georgiev Date: Sat, 16 May 2020 10:43:49 +0100 Subject: [PATCH 1/2] Update deprecated Python syntax and func references (still Python v2 compatible). --- py/Boinc/db_base.py | 11 ++++++----- tests/old/testbase.py | 40 +++++++++++++++++++--------------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/py/Boinc/db_base.py b/py/Boinc/db_base.py index 24c247c324..7d3311d900 100644 --- a/py/Boinc/db_base.py +++ b/py/Boinc/db_base.py @@ -145,13 +145,13 @@ def _select_object(table, searchdict, extra_args="", extra_params=[], select_wha return cursor def _select_object_fetchall(*args, **kwargs): - cursor = apply(_select_object, args, kwargs) + cursor = _select_object(*args, **kwargs) results = cursor.fetchall() cursor.close() return results def _select_object_iterate(*args, **kwargs): - cursor = apply(_select_object, args, kwargs) + cursor = _select_object(*args, **kwargs) while True: result = cursor.fetchone() if not result: return @@ -159,7 +159,7 @@ def _select_object_iterate(*args, **kwargs): def _select_count_objects(*args, **kwargs): kwargs['select_what'] = 'count(*)' - cursor = apply(_select_object, args, kwargs) + cursor = _select_object(*args, **kwargs) result = cursor.fetchone().values()[0] cursor.close() return result @@ -338,7 +338,7 @@ class DatabaseTable: object.do_init(result) except KeyError: # create the object - looking up instructors, etc - object = apply(self.object_class, [], result) + object = self.object_class(**result) if object.id: self.objects[object.id] = object self._cache(object) @@ -347,7 +347,7 @@ class DatabaseTable: def find1(self, **kwargs): '''Return a single result. Raises a DatabaseInconsistency if not exactly 1 result returned.''' - objects = apply(self.find, [], kwargs) + objects = self.find(**kwargs) if len(objects) != 1: raise DatabaseInconsistency( descript="find1: expected 1 result but found %d"%len(objects), @@ -506,6 +506,7 @@ def close(): def get_dbconnection(): return dbconnection def set_dbconnection(d): + global dbconnection dbconnection = d def init_table_classes(database_classes_, more_id_lookups = {}): diff --git a/tests/old/testbase.py b/tests/old/testbase.py index 8d12e5b235..502d380823 100644 --- a/tests/old/testbase.py +++ b/tests/old/testbase.py @@ -27,7 +27,7 @@ def test_init(): if not os.path.exists('testbase.py'): # automake sets the srcdir env. variable if srcdir != builddir - os.chdir(os.path.join(os.getenv('srcdir'),'test')) + os.chdir(os.path.join(os.getenv('srcdir'), 'tests', 'old')) if not os.path.exists('testbase.py'): raise SystemExit('Could not find testbase.py anywhere') @@ -265,7 +265,7 @@ class TestProject(Project): kwargs['short_name'] = kwargs.get('short_name') or 'test_'+appname kwargs['long_name'] = kwargs.get('long_name') or 'Project ' + kwargs['short_name'].replace('_',' ').capitalize() - apply(Project.__init__, [self], kwargs) + super(TestProject, self).__init__(**kwargs) (num_wu, redundancy) = get_redundancy_args(num_wu, redundancy) self.resource_share = resource_share or 1 @@ -382,10 +382,10 @@ class TestProject(Project): def _disable(self, *path): '''Temporarily disable a file to test exponential backoff''' - path = apply(self.dir, path) + path = self.dir(*path) os.rename(path, path+'.disabled') def _reenable(self, *path): - path = apply(self.dir, path) + path = self.dir(*path) os.rename(path+'.disabled', path) def disable_masterindex(self): @@ -502,7 +502,7 @@ class Host: self.projects.append(project) def dir(self, *dirs): - return apply(os.path.join,(self.host_dir,)+dirs) + return os.path.join(self.host_dir, *dirs) def install(self): rmtree(self.dir()) @@ -513,14 +513,13 @@ class Host: filename = self.dir(account_file_name(project.config.config.master_url)) verbose_echo(2, "Setting up host '%s': writing %s" % (self.name, filename)) - f = open(filename, "w") - print >>f, "" - print >>f, map_xml(project.config.config, ['master_url']) - print >>f, map_xml(user, ['authenticator']) - if user.project_prefs: - print >>f, user.project_prefs - print >>f, "" - f.close() + with open(filename, "w") as f: + f.write("\n") + f.write("{}\n".format(map_xml(project.config.config, ['master_url']))) + f.write("{}\n".format(map_xml(user, ['authenticator']))) + if user.project_prefs: + f.write("{}\n".format(user.project_prefs)) + f.write("\n") # copy log flags and global prefs, if any if self.cc_config: @@ -654,7 +653,7 @@ class ResultMeter: database.connect() prev_s = None while True: - s = apply(func, args) + s = func(*args) if s != prev_s: verbose_echo(1, s) prev_s = s @@ -677,12 +676,12 @@ def run_check_all(): # (Is there a better way to do this?) while (1): time.sleep(1) - raise SystemExit, 'Stopped due to $TEST_STOP_BEFORE_HOST_RUN' + raise SystemExit('Stopped due to $TEST_STOP_BEFORE_HOST_RUN') # all_hosts.run(asynch=True) all_hosts.run() all_projects.run_finish_wait() all_projects.stop_progress_meter() - print + print() # all_hosts.stop() time.sleep(12) all_projects.stop() @@ -694,7 +693,7 @@ def delete_test(): verbose_echo(1, "Deleting testbed %s."%options.auto_setup_basedir) try: shutil.rmtree(options.auto_setup_basedir) - except e: + except Exception as e: verbose_echo(0, "Couldn't delete testbed %s: %s"%(options.auto_setup_basedir,e)) class Proxy: @@ -754,8 +753,7 @@ class AsynchCGIServer: self.pid = None def test_msg(msg): - print - print "-- Testing", msg, '-'*(66-len(msg)) + print("\n-- Testing", msg, '-'*(66-len(msg))) test_init() def test_done(): @@ -775,11 +773,11 @@ def test_done(): else: verbose_echo(1, "Passed test!") if options.echo_overwrite: - print + print() if options.delete_testbed == 'if-successful' or options.delete_testbed == 'always': delete_test() if options.echo_overwrite: - print + print() return 0 # Note: this bypasses other cleanup functions - if something goes wrong during From cda1559ed7231206321fb4777129815d9aa5c84c Mon Sep 17 00:00:00 2001 From: Hristo Georgiev Date: Sat, 16 May 2020 11:09:09 +0100 Subject: [PATCH 2/2] Add missing `self` argument to `read_cpu_time_file`. --- tests/old/testbase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/old/testbase.py b/tests/old/testbase.py index 502d380823..af5d802f19 100644 --- a/tests/old/testbase.py +++ b/tests/old/testbase.py @@ -550,7 +550,7 @@ class Host: verbose_echo(0, "Couldn't kill pid %d" % self.pid) self.pid = 0 - def read_cpu_time_file(filename): + def read_cpu_time_file(self, filename): try: return float(open(self.dir(filename)).readline()) except: